Building Chatbots with the OpenAI API and Pinecone
In this project, we aim to explore the fascinating world of AI chatbots. We will be using LangChain, OpenAI, and Pinecone vector DB, to build a chatbot capable of learning from the external world using Retrieval Augmented Generation (RAG).
We will be using a dataset sourced from the Llama 2 ArXiv paper and other related papers to help our chatbot answer questions about the latest and greatest in the world of GenAI.
This project is designed for learners who have a basic understanding of the OpenAI API and Pinecone, as covered in our previous projects. It's a great opportunity for those interested in AI, machine learning, and NLP to get hands-on experience with building a chatbot with RAG.
By the end of this project, you will have a functioning chatbot and RAG pipeline that can hold a conversation and provide informative responses based on a knowledge base. This project is a stepping stone towards understanding and building more complex AI systems in the future.
Before you begin
You'll need to get an OpenAI API key and Pinecone API key. You can refer to getting-started.ipynb for steps on how to store these API keys in Workspace.
Task 0: Setup
Before we start building our chatbot, we need to install some Python libraries. Here's a brief overview of what each library does:
- langchain: This is a library for GenAI. We'll use it to chain together different language models and components for our chatbot.
- openai: This is the official OpenAI Python client. We'll use it to interact with the OpenAI API and generate responses for our chatbot.
- datasets: This library provides a vast array of datasets for machine learning. We'll use it to load our knowledge base for the chatbot.
- pinecone-client: This is the official Pinecone Python client. We'll use it to interact with the Pinecone vector DB where we will store our chatbot's knowledge base.
- tiktoken: This is a library from OpenAI that allows you to count the number of tokens in a text string without making an API call.
You can install these libraries using pip like so:
!pip install -qU \
langchain==0.0.292 \
openai==0.28.0 \
datasets==2.10.1 \
pinecone-client==2.2.4 \
tiktoken==0.5.1
Task 1: Building a Chatbot
We will be relying heavily on the LangChain library to bring together the different components needed for our chatbot. To get more familiar with the library let's first create a chatbot without RAG.
Instructions
Initialize the chat model object.
- Make sure you have defined the
OPENAI_API_KEY
environment variable and connected it. See the 'Setting up Workspace Integrations' section of getting-started.ipynb. - From langchain's chat_models module, import
ChatOpenAI
. - Initialize a
ChatOpenAI
object with thegpt-3.5-turbo
model. Assign tochat
.
from langchain.chat_models import ChatOpenAI
chat = ChatOpenAI(model_name="gpt-3.5-turbo")
How are chats structured?
Chats with OpenAI's gpt-3.5-turbo
and gpt-4
chat models are typically structured (in plain text) like this:
System: You are a helpful assistant. User: Hi AI, how are you today? Assistant: I'm great thank you. How can I help you? User: I'd like to understand string theory. Assistant:
The final "Assistant:"
without a response is what would prompt the model to continue the conversation. In the official OpenAI ChatCompletion
endpoint these would be passed to the model in a format like:
[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hi AI, how are you today?"}, {"role": "assistant", "content": "I'm great thank you. How can I help you?"} {"role": "user", "content": "I'd like to understand string theory."} ]
In LangChain there is a slightly different format. We use three message objects like so:
messages = [ SystemMessage(content="You are a helpful assistant."), HumanMessage(content="Hi AI, how are you today?"), AIMessage(content="I'm great thank you. How can I help you?"), HumanMessage(content="I'd like to understand string theory.") ]
The format is very similar, we're just swapped the role of "user"
for HumanMessage
, and the role of "assistant"
for AIMessage
.
Instructions
Create a conversation.
- From langchain's schema module, import the three message types:
SystemMessage
,HumanMessage
, andAIMessage
. - Create a conversation as a list of messages. Assign to
messages
.- A system message with content
"You are a helpful assistant."
- A human message with content
"Hi AI, how are you today?"
- An AI message with content
"I'm great thank you. How can I help you?"
- A human message with content
"I'd like to understand string theory."
- A system message with content
from langchain.schema import (
SystemMessage,
HumanMessage,
AIMessage
)
messages = [
SystemMessage(content="You are a helpful assistant."),
HumanMessage(content="Hi AI, how are you today?"),
AIMessage(content="I'm great thank you. How can I help you?"),
HumanMessage(content="I'd like to understand string theory.")
]
We generate the next response from the AI by passing these messages to the ChatOpenAI
object. You can call chat
as though it is a function.