RAG chatbot with LangChain and Elasticsearch

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.

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.

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.

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.

Split 15 documents into 24 passages

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

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.

Now we can ask questions from our chatbot!

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

[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.

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

... or delete the indices.

ObjectApiResponse({'acknowledged': True})

Ready to build state of the art search experiences?

Sufficiently advanced search isn’t achieved with the efforts of one. Elasticsearch is powered by data scientists, ML ops, engineers, and many more who are just as passionate about search as you are. Let’s connect and work together to build the magical search experience that will get you the results you want.

Try it yourself