Skip to content
This repository has been archived by the owner on Feb 7, 2018. It is now read-only.

ibm-watson-data-lab/--deprecated--watson-recipe-bot-python-graph

Repository files navigation

Watson Recipe Bot + IBM Graph

🚫 This project is no longer maintained.

This project is based on the Watson Recipe Bot example. The Watson Recipe Bot is a Slack bot that recommends recipes based on ingredients or cuisines. This project is essentially a fork of the Watson Recipe Bot with some additional features, including:

  1. Multi-user support - the original application supported only a single user interacting with the bot at a time. This application supports multiple users interacting with the bot at the same time.
  2. IBM Graph integration - this application adds IBM Graph integration for caching 3rd party API calls and storing each user's chat history (the ingredients, cuisines, and recipes they have selected).
  3. Additional Watson Conversation intent - this application adds a "favorites" intent which allows a user to request their favorite recipes based on the history stored in Graph.

Getting Started

Before you get started read the original blog post to understand how the Watson Recipe Bot works, or the new blog post which talks about improving the bot by adding persistence with IBM Cloudant. You do not need to follow the instructions in these blog posts. All the instructions required to run the bot are below. After cloning this repo follow the steps below.

Quick Reference

The following environment variables are required to run the application:

SLACK_BOT_TOKEN=xxxx-xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxx
SLACK_BOT_ID=UXXXXXXXX
SPOONACULAR_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONVERSATION_USERNAME=xxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxxx
CONVERSATION_PASSWORD=xxxxxxxxxxxx
CONVERSATION_WORKSPACE_ID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
GRAPH_API_URL=https://ibmgraph-alpha.ng.bluemix.net/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/g
GRAPH_USERNAME=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
GRAPH_PASSWORD=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
GRAPH_ID=watson_recipe_bot

We will show you how to configure the necessary services and retrieve these values in the instructions below:

Prerequisites

The following prerequisites are required to run the application.

  1. A Bluemix account.
  2. A Watson Conversation service provisioned in your Bluemix account.
  3. A Graph service provisioned in your Bluemix account.
  4. A Spoonacular API key. A free tier is available, however a credit card is required.
  5. A Slack account and permission in your Slack team to register a Slack bot.

To run locally you will need Python 2 or 3, pip and virtualenv.

To push your application to Bluemix from your local development environment you will need the Bluemix CLI and Dev Tools.

Local Development Environment

We'll start by getting your local development environment set up. If you haven't already install Python, pip, and virtualenv.

You can install Python by following the instructions here.

You can install pip by following the instructions here.

You can install virtualenv by following the instructions here.

From the command-line cd into the watson-recipe-bot-python-graph directory:

git clone https://github.com/ibm-cds-labs/watson-recipe-bot-python-graph
cd watson-recipe-bot-python-graph

Create and activate a new virtual environment:

virtualenv venv
source ./venv/bin/activate

Install the application requirements:

pip install -r requirements.txt

This project requires py-graph which is an experimental Python library for IBM Graph. py-graph is not yet available in PyPI, so you will need to clone and install it manually:

cd ../
git clone https://github.com/ibm-cds-labs/py-graph
cd watson-recipe-bot-python-graph
pip install -e ../py-graph

Copy the .env.template file included in the project to .env. This file will contain the environment variable definitions:

cp .env.template .env

Slack

In this next step we'll create a new Slack bot in your Slack team.

Note: If you have previously configured Slack to work with the Watson Recipe Bot Cloudant example, or any other Watson Recipe Bot example, you can copy the SLACK_BOT_TOKEN and SLACK_BOT_ID values from your .env file for that example into the .env file for this example and move onto the next step.

In your web browser go to https://my.slack.com/services/new/bot. Make sure you sign into the appropriate Slack team. You can also change the Slack team from the pulldown in the top right.

  1. You'll start by choosing a username for your bot. In the field provided enter sous-chef.

    Slack

  2. Click the Add bot integration button.

  3. On the following screen you will find the API Token. Copy this value to your clipboard.

    Slack

  4. Open the .env file in a text editor.

  5. Paste the copied token from your clipboard as the SLACK_BOT_TOKEN value:

    SLACK_BOT_TOKEN=xoxb-xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxx
    
  6. Save the .env file

Next, we need to get the Slack ID of the bot.

  1. From the command-line run the following command:

    python scripts/get_bot_id.py
    
  2. The script should print out the bot ID. The output should be similar to the following:

    Bot ID for 'sous-chef' is U3XXXXXXX
    
  3. Copy and paste the bot ID into your .env file:

    SLACK_BOT_ID=U3XXXXXXX
    

Spoonacular

In this next step we'll set up your Spoonacular account. Spoonacular is a Food and Recipe API. The application uses Spoonacular to find recipes based on ingredient or cuisines requested by the user.

Note: If you have previously configured Spoonacular to work with the Watson Recipe Bot Cloudant example, or any other Watson Recipe Bot example, you can copy the SPOONACULAR_KEY value from your .env file for that example into the .env file for this example and move onto the next step.

  1. In your web browser go to https://spoonacular.com/food-api.

  2. Click the Get Access button.

    Spoonacular

  3. Click the appropriate button to gain access (i.e. Get Regular Access)

    Spoonacular

  4. Choose the appropriate Pricing plan (i.e. Basic) and click the Subscribe button.

  5. Follow the instructions to sign into or sign up for a Mashape account.

  6. After you have subscribed to Spoonacular in the Documentation tab find a curl example on the right. It should look similar to this:

    Spoonacular

  7. Copy the value of the X-Mashape-Key and paste it into your .env file:

    SPOONACULAR_KEY=vxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    

Bluemix

If you do not already have a Bluemix account click here to sign up.

Login to your Bluemix account.

Watson Conversation

First, we'll walk you through provisioning a Watson Conversation service in your Bluemix account:

Note: If you have previously configured Watson Conversation to work with the Watson Recipe Bot Cloudant example, or any other Watson Recipe Bot example, you can copy the CONVERSATION_USERNAME, CONVERSATION_PASSWORD, and CONVERSATION_WORKSPACE_ID values from your .env file for that example into the .env file for this example and move onto the next step.

  1. From your Bluemix Applications or Services Dashboard click the Create Service button.

    Bluemix

  2. In the IBM Bluemix Catalog search for Watson Conversation.

  3. Select the Conversation service.

    Watson Conversation

  4. Click the Create button on the Conversation detail page.

  5. On your newly created Conversation service page click the Service Credentials tab.

    Watson Conversation

  6. Find your newly created Credentials and click View Credentials

    Watson Conversation

  7. Copy the username and password into your .env file:

    CONVERSATION_USERNAME=xxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxxx
    CONVERSATION_PASSWORD=xxxxxxxxxxxx
    

Next, let's launch the Watson Conversation tool and import our conversation workspace.

  1. Go back to the Manage tab.

  2. Click the Launch tool button.

    Watson Conversation

  3. Log in to Watson Conversation with your Bluemix credentials if prompted to do so.

  4. On the Create workspace page click the Import button.

    Watson Conversation

  5. Choose the workspace.json file in the application directory (watson-recipe-bot-python-graph/workspace.json).

  6. Click the Import button.

    Watson Conversation

  7. Under Workspaces you should now see the Recipe Bot.

  8. Click the menu button (3 vertical dots) and click View Details

    Watson Conversation

  9. Copy the Workspace ID and paste it into your .env file:

    CONVERSATION_WORKSPACE_ID=40xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
    

IBM Graph

We're almost there! Next, we'll provision an instance of IBM Graph in our Bluemix account. After this step we will be able to run our bot locally.

  1. From your Bluemix Applications or Services Dashboard click the Create Service button.

  2. In the IBM Bluemix Catalog search for IBM Graph.

  3. Select the IBM Graph service.

    Watson Conversation

  4. Click the Create button on the Graph detail page.

  5. On your newly created Graph service page click the Service Credentials tab.

  6. Find your newly created Credentials and click View Credentials

  7. Copy the apiURL, username, and password into your .env file:

    GRAPH_API_URL=https://ibmgraph-alpha.ng.bluemix.net/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/g
    GRAPH_USER_NAME=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
    GRAPH_PASSWORD=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
    

Run Locally

We're now ready to test our bot. From the command-line run the following command:

python run.py

If all is well you should see output similar to the following:

Getting graphs...
Creating graph watson_recipe_bot...
Getting graph schema...
Creating graph schema...
sous-chef is connected and running!

To interact with the bot open Slack, go to the Slack team where you installed the bot, start a direct conversation with sous-chef, and say "hi".

sous-chef

IBM Graph Dashboard Quick Start

To learn more about IBM Graph and how the Watson Recipe Bot uses IBM Graph to store users, ingredients, cuisines, and recipes launch the IBM Graph dashboard from Bluemix by clicking the Open button on the IBM Graph Service Details page. Select the watson-recipe-bot graph:

IBM Graph

Once you have interacted with the Slack bot a Vertex for your Slack user should have been stored in Graph. You can find your Slack ID in python run.py output. Look for the following:

Creating person vertex where name=UXXXXXXXX

Run the following Gremlin query in the IBM Dashboard. Replace UXXXXXXXX with your Slack ID:

def g = graph.traversal();
g.V().hasLabel("person").has("name","UXXXXXXXX").outE().inV().outE().inV().path();

The screenshot below shows a sample of what you might see in the IBM Graph Dashboard. In this case the user has selected a single cuisine and a single recipe:

IBM Graph

You can find more information about IBM Graph and the Gremlin query language here.

Sample Conversations

Here are some sample conversations you can have with sous-chef:

sous-chef

sous-chef

sous-chef

Next Steps

For more information on how the sous-chef bot works read the original blog post and the new blog post which talks about improving the bot by adding persistence with IBM Cloudant.

License

Licensed under the Apache License, Version 2.0.

About

🚫 This project is no longer maintained. Watson Recipe Bot with IBM Graph integration

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published