Chatbot With Multi Query Retriever
Question Answering with Langchain, OpenAI, and MultiQuery Retriever
This interactive workbook demonstrates example of Elasticsearch's MultiQuery Retriever to generate similar queries for a given user input and apply all queries to retrieve a larger set of relevant documents from a vectorstore.
Before we begin, we first split the fictional workplace documents into passages with langchain and uses OpenAI to transform these passages into embeddings and then store these into Elasticsearch.
We will then ask a question, generate similar questions using langchain and OpenAI, retrieve relevant passages from the vector store, and use langchain and OpenAI again to provide a summary for the questions.
Install packages and import modules
[notice] A new release of pip is available: 23.3.1 -> 23.3.2 [notice] To update, run: /Library/Developer/CommandLineTools/usr/bin/python3 -m pip install --upgrade pip
Connect to Elasticsearch
ℹ️ We're using an Elastic Cloud deployment of Elasticsearch for this notebook. If you don't have an Elastic Cloud deployment, sign up here for a free trial.
We'll use the Cloud ID to identify our deployment, because we are using Elastic Cloud deployment. To find the Cloud ID for your deployment, go to https://cloud.elastic.co/deployments and select your deployment.
We will use ElasticsearchStore to connect to our elastic cloud deployment, This would help create and index data easily. We would also send list of documents that we created in the previous step
Indexing Data into Elasticsearch
Let's download the sample dataset and deserialize the document.
Split Documents into Passages
We’ll chunk documents into passages in order to improve the retrieval specificity and to ensure that we can provide multiple passages within the context window of the final question answering prompt.
Here we are chunking documents into 800 token passages with an overlap of 400 tokens.
Here we are using a simple splitter but Langchain offers more advanced splitters to reduce the chance of context being lost.
Bulk Import Passages
Now that we have split each document into the chunk size of 800, we will now index data to elasticsearch using ElasticsearchStore.from_documents.
We will use Cloud ID, Password and Index name values set in the Create cloud deployment step.
Question Answering with MultiQuery Retriever
Now that we have the passages stored in Elasticsearch, we can now ask a question to get the relevant passages.
INFO:langchain.retrievers.multi_query:Generated queries: ['1. Can you provide information on the sales team at NASA?', '2. How does the sales team operate within NASA?', '3. What are the responsibilities of the NASA sales team?']
---- Answer ---- The NASA sales team is a part of the Americas region in the sales organization of the company. It is led by two Area Vice-Presidents, Laura Martinez for North America and Gary Johnson for South America. The team is responsible for promoting and selling the company's products and services in the North and South American markets. They work closely with other departments, such as marketing, product development, and customer support, to ensure the success of the company's sales objectives in the region.