Chatbot

openai-chatgptlangchain-pythonchatgptgenaielasticsearchelasticopenaiAIchatlogvectordatabasenotebooksPythonsearchgenaistackgenerative-aivectorelasticsearch-labslangchainapplications

Chatbot with LangChain conversational chain and OpenAI ๐Ÿค–๐Ÿ’ฌ

Open In Colab

In this notebook we'll build a chatbot that can respond to questions about custom data, such as policies of an employer.

The chatbot uses LangChain's ConversationalRetrievalChain and has the following capabilities:

  • Answer questions asked in natural language
  • Run hybrid search in Elasticsearch to find documents that answer the question
  • Extract and summarize the answer using OpenAI LLM
  • Maintain conversational memory for follow-up questions

Requirements ๐Ÿงฐ

For this example, you will need:

Use Elastic Cloud

If you don't have an Elastic Cloud deployment, follow these steps to create one.

  1. Go to Elastic cloud Registration and sign up for a free trial
  2. Select Create Deployment and follow the instructions

Locally install Elasticsearch

If you prefer to run Elasticsearch locally, the easiest way is to use Docker. See Install Elasticsearch with Docker for instructions.

Install packages ๐Ÿ“ฆ

First we pip install the packages we need for this example.

[1]
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
langserve 0.0.21 requires pydantic<2,>=1, but you have pydantic 2.3.0 which is incompatible.

[notice] A new release of pip is available: 23.2 -> 23.3.1
[notice] To update, run: pip install --upgrade pip
Note: you may need to restart the kernel to use updated packages.

Initialize clients ๐Ÿ”Œ

Next we input credentials with getpass. getpass is part of the Python standard library and is used to securely prompt for credentials.

[2]

Load and process documents ๐Ÿ“„

Time to load some data! We'll be using the workplace search example data, which is a list of employee documents and policies.

[3]
Successfully loaded 15 documents

Chunk documents into passages ๐Ÿช“

As we're chatting with our bot, it will run semantic searches on the index to find the relevant documents. In order for this to be accurate, we need to split the full documents into small chunks (also called passages). This way the semantic search will find the passage within a document that most likely answers our question.

We'll use LangChain's RecursiveCharacterTextSplitter and split the documents' text at 800 characters with some overlap between chunks.

[4]
Split 15 documents into 24 passages

Let's generate the embeddings and index the documents with them.

[5]

Chat with the chatbot ๐Ÿ’ฌ

Let's initialize our chatbot. We'll define Elasticsearch as a store for retrieving documents and for storing the chat session history, OpenAI as the LLM to interpret questions and summarize answers, then we'll pass these to the conversational chain.

[6]

Now we can ask questions from our chatbot!

See how the chat history is passed as context for each question.

[7]
[CHAT SESSION ID] f1ebce0a-edc1-46d6-a65b-f86f8ab15d2f
[QUESTION] What does NASA stand for?
[ANSWER]   NASA stands for North America South America region.
          [SUPPORTING DOCUMENTS] ['Sales Organization Overview', 'Intellectual Property Policy', 'April Work From Home Update', 'New Employee Onboarding Guide']
[QUESTION] Which countries are part of it?
[ANSWER]   The North America South America region includes the United States, Canada, Mexico, as well as Central and South America.
          [SUPPORTING DOCUMENTS] ['Sales Organization Overview', 'Wfh Policy Update May 2023', 'Fy2024 Company Sales Strategy', 'New Employee Onboarding Guide']
[QUESTION] Who are the team's leads?
[ANSWER]   Laura Martinez is the Area Vice-President of North America, and Gary Johnson is the Area Vice-President of South America.
          [SUPPORTING DOCUMENTS] ['Sales Organization Overview', 'Fy2024 Company Sales Strategy', 'Wfh Policy Update May 2023', 'Fy2024 Company Sales Strategy']

๐Ÿ’ก Try experimenting with other questions or after clearing the workplace data, and observe how the responses change.

(Optional) Clean up ๐Ÿงน

Once we're done, we can clean up the chat history for this session...

[8]

... or delete the indices.

[9]
ObjectApiResponse({'acknowledged': True})