Build a Movie Chatbot App
In this tutorial, you will build a demo app that allows a user to ask questions about Rotten Tomatoes Top Movie Dataset.
Prerequisites
This demo app example assumes you have some familiarity with OctoAI and how to use it's API endpoints. If you are not as familiar checkout the Quickstart section to ramp up or review.
All libraries and services used for this app
OctoAI account and API Token
How it all works
The Rotten Tomatoes data is loaded from the .csv
file. The docs are indexed using Instructor-Large model and GPTVectorStoreIndex
from the llama_index
package. All user inputs are then queried against this index using QueryEngine from LlamaIndex. The output text form the index is sent to the OctoAI endpoint using LangChain.
1: Create a new OctoAI endpoint
Visit the Llama 2 demo
Experiment with conversational outputs by using the prompt field
You should see the Python code sample on the demo page
2: Create a new virtual environment & install Streamlit
Install the appropriate version of Miniconda for your OS
From the terminal, create a conda environment using the
conda create --name moviebotapp
Activate the conda environment with
conda activate moviebotapp
3: Build the MovieBot app
We will use Streamlit to run our chat app, and you can find the code for this in the moviebot repo.
Clone the moviebot repo to your local machine using
git clone
Install all app dependencies with
pip install -r requirements.txt
Create an OctoAI API Token, if you do not have one
Be sure your
OCTOAI_API_TOKEN
is set in your'/.streamlit/secrets.toml'
file asOCTOAI_API_TOKEN="<paste token here>"
Add your ENDPOINT_URL to the
’/.streamlit/secrets.toml'
file asENDPOINT_URL="<paste url here>"
4: Deploy and run the MovieBot app
Now that your app is built, deploy it locally within your conda environment using the pre-built script that is provided below. See how:
From terminal,
cd
into/moviebotapp
folder wherestreamlit_app.py
is locatedRun the command:
streamlit run chatbot.py
Open a browser window and navigate to
localhost:8501
to see your application running.You can now chat with the bot and ask it questions about movies! For example:
Who starred in the movie Titanic?
What genre is The Matrix?
When was Jurassic Park released?
Responses from the OctoAI API are displayed in the Streamlit chat interface
Full code walkthrough
Let's do a full code walkthrough of the chatbot powered by OctoAI and hosted on Streamlit for the MovieBot.
Indexing and query flow
Setup environment variables
The setup_env_variables
function sets up environment variables OCTOAI_API_TOKEN
and ENDPOINT_URL
using the secrets from Streamlit.
Initializing session state
The handle_session_state
function sets up the initial session state variables.
Loading movie data
The load_data
function loads movie data from a CSV file using a PagedCSVReader
loader.
Initializing OctoAIEndpoint and LangChain LLMPredictor
The initialize_llm
function initializes the OctoAIEndpoint and LLMPredictor.
Creating LangchainEmbedding
The create_embeddings
function creates an instance of LangchainEmbedding using OctoAIEmbeddings wrapper for a hosted Instructor-Large model endpoint.
Creating ServiceContext
The create_service_context
function creates an instance of llama_index ServiceContext.
Creating GPTVectorStoreIndex
The create_index
function creates an instance of llama_index GPTVectorStoreIndex
. It first checks if the index.pkl
file exists and loads it if available. If not, it creates a new GPTVectorStoreIndex
from the given documents using the provided service_context
.
Creating query engine
The create_query_engine
function creates a llama_index query engine using the given index
and llm_predictor
.
Processing the query
The query
function processes a query and returns a response. It first gets a response from the llama_index query_engine
and then transforms the response into a string.
Main function
The main
function initializes the environment, sets up the endpoint URL, loads the data, initializes the LLM predictor, and creates the embeddings, service context, index, and query engine. It then displays the user interface and processes the user’s input.
The main
function continues by setting up the user interface and getting the user’s input. It then processes the user’s input and displays the generated response on the user interface.
In the exception handling part, if an error occurs during the execution, an error message “Something went wrong. Please try again.” is displayed. The main
function is called if this script is run as the main module.
Share what you build
We are excited to see what you build – feel free to share with us in our Discord, and see what other community members are creating.