From 61d0aeba00b6b0f38492e861cfa43b44ebc411c6 Mon Sep 17 00:00:00 2001 From: Samiul Monir Date: Wed, 27 Dec 2023 14:40:55 -0500 Subject: [PATCH 1/7] Adding multiquery retrieval tutorial --- .../langchain-multi-query-retriever.ipynb | 265 ++++++++++++++++++ 1 file changed, 265 insertions(+) create mode 100644 notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb diff --git a/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb b/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb new file mode 100644 index 00000000..10e500ab --- /dev/null +++ b/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb @@ -0,0 +1,265 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# MultiQueryRetriever with elasticsearch and langchain\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/elastic/elasticsearch-labs/blob/main/notebooks/langchain/notebooks/langchain/langchain-multi-query-retriever.ipynb)\n", + "\n", + "This workbook demonstrates example of Elasticsearch's [MultiQuery Retriever](https://api.python.langchain.com/en/latest/retrievers/langchain.retrievers.multi_query.MultiQueryRetriever.html) to generate multiple queries for a given user input query and apply all queries to retrieve a larger set of relevant documents from a vectorstore.\n", + "\n", + "Before we begin, we first split the documents into chunks with `langchain` and then using [`ElasticsearchStore.from_documents`](https://api.python.langchain.com/en/latest/vectorstores/langchain.vectorstores.elasticsearch.ElasticsearchStore.html#langchain.vectorstores.elasticsearch.ElasticsearchStore.from_documents), we create a `vectorstore` and index data to elasticsearch.\n", + "\n", + "\n", + "We will then see few examples query demonstrating full power of elasticsearch powered multiquery retriever." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Install packages and import modules" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.3.2\u001b[0m\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49m/Library/Developer/CommandLineTools/usr/bin/python3 -m pip install --upgrade pip\u001b[0m\n" + ] + } + ], + "source": [ + "!python3 -m pip install -qU lark elasticsearch langchain openai tiktoken\n", + "\n", + "from langchain.schema import Document\n", + "from langchain.embeddings.openai import OpenAIEmbeddings\n", + "from langchain.vectorstores import ElasticsearchStore\n", + "from langchain.chat_models import ChatOpenAI\n", + "from langchain.retrievers.multi_query import MultiQueryRetriever\n", + "from langchain.chains import LLMChain\n", + "from getpass import getpass" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Create documents\n", + "Next, we will create list of documents with summary of movies using [langchain Schema Document](https://api.python.langchain.com/en/latest/schema/langchain.schema.document.Document.html), containing each document's `page_content` and `metadata` ." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "docs = [\n", + " Document(\n", + " page_content=\"A bunch of scientists bring back dinosaurs and mayhem breaks loose\",\n", + " metadata={\n", + " \"year\": 1993, \n", + " \"rating\": 7.7, \n", + " \"genre\": \"science fiction\", \n", + " \"director\": \"Steven Spielberg\", \n", + " \"title\": \"Jurassic Park\"\n", + " },\n", + " ),\n", + " Document(\n", + " page_content=\"Leo DiCaprio gets lost in a dream within a dream within a dream within a ...\",\n", + " metadata={\n", + " \"year\": 2010,\n", + " \"director\": \"Christopher Nolan\",\n", + " \"rating\": 8.2,\n", + " \"title\": \"Inception\"\n", + " },\n", + " ),\n", + " Document(\n", + " page_content=\"A psychologist / detective gets lost in a series of dreams within dreams within dreams and Inception reused the idea\",\n", + " metadata={\n", + " \"year\": 2006, \n", + " \"director\": \"Satoshi Kon\", \n", + " \"rating\": 8.6, \n", + " \"title\": \"Paprika\"\n", + " },\n", + " ),\n", + " Document(\n", + " page_content=\"A bunch of normal-sized women are supremely wholesome and some men pine after them\",\n", + " metadata={\n", + " \"year\": 2019, \n", + " \"director\": \"Greta Gerwig\", \n", + " \"rating\": 8.3, \n", + " \"title\": \"Little Women\"\n", + " },\n", + " ),\n", + " Document(\n", + " page_content=\"Toys come alive and have a blast doing so\",\n", + " metadata={\n", + " \"year\": 1995,\n", + " \"genre\": \"animated\", \n", + " \"director\": \"John Lasseter\", \n", + " \"rating\": 8.3, \n", + " \"title\": \"Toy Story\"\n", + " },\n", + " ),\n", + " Document(\n", + " page_content=\"Three men walk into the Zone, three men walk out of the Zone\",\n", + " metadata={\n", + " \"year\": 1979,\n", + " \"rating\": 9.9,\n", + " \"director\": \"Andrei Tarkovsky\",\n", + " \"genre\": \"science fiction\",\n", + " \"rating\": 9.9,\n", + " \"title\": \"Stalker\",\n", + " },\n", + " ),\n", + "]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Connect to Elasticsearch\n", + "\n", + "ℹ️ We're using an Elastic Cloud deployment of Elasticsearch for this notebook. If you don't have an Elastic Cloud deployment, sign up [here](https://cloud.elastic.co/registration?utm_source=github&utm_content=elasticsearch-labs-notebook) for a free trial.\n", + "\n", + "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.\n", + "\n", + "\n", + "We will use [ElasticsearchStore](https://api.python.langchain.com/en/latest/vectorstores/langchain.vectorstores.elasticsearch.ElasticsearchStore.html) 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" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "# https://www.elastic.co/search-labs/tutorials/install-elasticsearch/elastic-cloud#finding-your-cloud-id\n", + "ELASTIC_CLOUD_ID = getpass(\"Elastic Cloud ID: \")\n", + "# ELASTIC_CLOUD_ID = \"Test_For_Multi_Query:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvOjQ0MyQzMWRmYjg0NDI2NTg0ZTc2ODU2NDgyMWRhNjY4NzYwYSRiZmY0YmZlYzE5MTM0ZDZhYTU2N2ZmNjZkMmIxNzE3Nw==\"\n", + "\n", + "# https://www.elastic.co/search-labs/tutorials/install-elasticsearch/elastic-cloud#creating-an-api-key\n", + "ELASTIC_API_KEY = getpass(\"Elastic Api Key: \")\n", + "# ELASTIC_API_KEY = \"WERUNmc0d0JSSVdlNEhqdndGdjk6bjdreWVndnlTZWkyMnVmQS1TdVFSQQ==\"\n", + "\n", + "# https://platform.openai.com/api-keys\n", + "OPENAI_API_KEY = getpass(\"OpenAI API key: \")\n", + "# OPENAI_API_KEY = \"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\"\n", + "\n", + "embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY)\n", + "\n", + "\n", + "vectorstore = ElasticsearchStore.from_documents(\n", + " docs,\n", + " embeddings,\n", + " index_name=\"elasticsearch-multi-query-demo\",\n", + " es_cloud_id=ELASTIC_CLOUD_ID,\n", + " es_api_key=ELASTIC_API_KEY\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Setup query retriever\n", + "\n", + "Next we will instantiate MultiQuery retriever by providing a bit information about our document attributes and a short description about the document.\n", + "\n", + "We will then instantiate retriever with [MultiQueryRetriever.from_llm](https://api.python.langchain.com/en/latest/retrievers/langchain.retrievers.multi_query.MultiQueryRetriever.html)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "# Set up openAI llm with sampling temperature 0\n", + "llm = ChatOpenAI(temperature=0, openai_api_key=OPENAI_API_KEY)\n", + "\n", + "# instantiate retriever\n", + "retriever = MultiQueryRetriever.from_llm(vectorstore.as_retriever(), llm)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test retriever with simple query\n", + "\n", + "We will test the retriever with a simple query: `What are some movies about dream`.\n", + "\n", + "The output shows all the relevant documents to the query." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO:langchain.retrievers.multi_query:Generated queries: ['1. Can you recommend any films that explore the theme of dreams?', '2. Are there any movies that delve into the realm of dreams?', '3. Could you suggest some films that revolve around the concept of dreaming?']\n" + ] + }, + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Set logging for the queries\n", + "import logging\n", + "\n", + "logging.basicConfig()\n", + "logging.getLogger(\"langchain.retrievers.multi_query\").setLevel(logging.INFO)\n", + "\n", + "# This example specifies a relevant\n", + "question = \"What are some movies about dream\"\n", + "unique_docs = retriever.get_relevant_documents(query=question)\n", + "len(unique_docs)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 90e51c39fe7886f040143d35d27de6ddb125b35b Mon Sep 17 00:00:00 2001 From: Samiul Monir Date: Fri, 5 Jan 2024 11:33:39 -0500 Subject: [PATCH 2/7] Adding chatbot example with multiquery --- .../chatbot-with-multi-query-retriever.ipynb | 289 ++++++++++++++++++ .../langchain-multi-query-retriever.ipynb | 2 +- 2 files changed, 290 insertions(+), 1 deletion(-) create mode 100644 notebooks/langchain/multi-query-retriever-examples/chatbot-with-multi-query-retriever.ipynb diff --git a/notebooks/langchain/multi-query-retriever-examples/chatbot-with-multi-query-retriever.ipynb b/notebooks/langchain/multi-query-retriever-examples/chatbot-with-multi-query-retriever.ipynb new file mode 100644 index 00000000..54dd5e31 --- /dev/null +++ b/notebooks/langchain/multi-query-retriever-examples/chatbot-with-multi-query-retriever.ipynb @@ -0,0 +1,289 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Question Answering with Langchain, OpenAI, and MultiQuery Retriever\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/elastic/elasticsearch-labs/blob/main/notebooks/langchain/notebooks/langchain/multi-query-retriever-examples/chatbot-with-multi-query-retriever.ipynb)\n", + "\n", + "This interactive workbook demonstrates example of Elasticsearch's [MultiQuery Retriever](https://api.python.langchain.com/en/latest/retrievers/langchain.retrievers.multi_query.MultiQueryRetriever.html) to generate similar queries for a given user input and apply all queries to retrieve a larger set of relevant documents from a vectorstore.\n", + "\n", + "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.\n", + "\n", + "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." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Install packages and import modules" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.3.2\u001b[0m\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49m/Library/Developer/CommandLineTools/usr/bin/python3 -m pip install --upgrade pip\u001b[0m\n" + ] + } + ], + "source": [ + "!python3 -m pip install -qU lark elasticsearch langchain openai\n", + "\n", + "from langchain.embeddings.openai import OpenAIEmbeddings\n", + "from langchain.vectorstores import ElasticsearchStore\n", + "from langchain.llms import OpenAI\n", + "from langchain.retrievers.multi_query import MultiQueryRetriever\n", + "from getpass import getpass" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Connect to Elasticsearch\n", + "\n", + "ℹ️ We're using an Elastic Cloud deployment of Elasticsearch for this notebook. If you don't have an Elastic Cloud deployment, sign up [here](https://cloud.elastic.co/registration?utm_source=github&utm_content=elasticsearch-labs-notebook) for a free trial. \n", + "\n", + "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.\n", + "\n", + "We will use [ElasticsearchStore](https://api.python.langchain.com/en/latest/vectorstores/langchain.vectorstores.elasticsearch.ElasticsearchStore.html) 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" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# https://www.elastic.co/search-labs/tutorials/install-elasticsearch/elastic-cloud#finding-your-cloud-id\n", + "ELASTIC_CLOUD_ID = getpass(\"Elastic Cloud ID: \")\n", + "\n", + "# https://www.elastic.co/search-labs/tutorials/install-elasticsearch/elastic-cloud#creating-an-api-key\n", + "ELASTIC_API_KEY = getpass(\"Elastic Api Key: \")\n", + "\n", + "# https://platform.openai.com/api-keys\n", + "OPENAI_API_KEY = getpass(\"OpenAI API key: \")\n", + "\n", + "embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY)\n", + "\n", + "vectorstore = ElasticsearchStore(\n", + " es_cloud_id=ELASTIC_CLOUD_ID, \n", + " es_api_key=ELASTIC_API_KEY,\n", + " index_name=\"elasticsearch-multi-query-demo\",\n", + " embedding= embeddings,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Indexing Data into Elasticsearch\n", + "Let's download the sample dataset and deserialize the document." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "from urllib.request import urlopen\n", + "import json\n", + "\n", + "url = \"https://raw.githubusercontent.com/elastic/elasticsearch-labs/main/example-apps/chatbot-rag-app/data/data.json\"\n", + "\n", + "response = urlopen(url)\n", + "data = json.load(response)\n", + "\n", + "with open('temp.json', 'w') as json_file:\n", + " json.dump(data, json_file)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Split Documents into Passages\n", + "\n", + "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.\n", + "\n", + "Here we are chunking documents into 800 token passages with an overlap of 400 tokens.\n", + "\n", + "Here we are using a simple splitter but Langchain offers more advanced splitters to reduce the chance of context being lost." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.document_loaders import JSONLoader \n", + "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", + "\n", + "def metadata_func(record: dict, metadata: dict) -> dict:\n", + " metadata[\"name\"] = record.get(\"name\")\n", + " metadata[\"summary\"] = record.get(\"summary\")\n", + " metadata[\"url\"] = record.get(\"url\")\n", + " metadata[\"category\"] = record.get(\"category\")\n", + " metadata[\"updated_at\"] = record.get(\"updated_at\")\n", + "\n", + " return metadata\n", + "\n", + "# For more loaders https://python.langchain.com/docs/modules/data_connection/document_loaders/\n", + "# And 3rd party loaders https://python.langchain.com/docs/modules/data_connection/document_loaders/#third-party-loaders\n", + "loader = JSONLoader(\n", + " file_path=\"temp.json\",\n", + " jq_schema=\".[]\",\n", + " content_key=\"content\",\n", + " metadata_func=metadata_func,\n", + ")\n", + "\n", + "text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(chunk_size=512, chunk_overlap=256)\n", + "docs = loader.load_and_split(text_splitter=text_splitter)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Bulk Import Passages\n", + "\n", + "Now that we have split each document into the chunk size of 800, we will now index data to elasticsearch using [ElasticsearchStore.from_documents](https://api.python.langchain.com/en/latest/vectorstores/langchain.vectorstores.elasticsearch.ElasticsearchStore.html#langchain.vectorstores.elasticsearch.ElasticsearchStore.from_documents).\n", + "\n", + "We will use Cloud ID, Password and Index name values set in the `Create cloud deployment` step." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "documents = vectorstore.from_documents(\n", + " docs, \n", + " embeddings, \n", + " index_name=\"elasticsearch-multi-query-demo\",\n", + " es_cloud_id=ELASTIC_CLOUD_ID,\n", + " es_api_key=ELASTIC_API_KEY\n", + ")\n", + "\n", + "llm = OpenAI(temperature=0, openai_api_key=OPENAI_API_KEY)\n", + "\n", + "retriever = MultiQueryRetriever.from_llm(vectorstore.as_retriever(), llm)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Question Answering with MultiQuery Retriever\n", + "\n", + "Now that we have the passages stored in Elasticsearch, we can now ask a question to get the relevant passages." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "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?']\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "---- Answer ----\n", + "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.\n" + ] + } + ], + "source": [ + "from langchain.schema.runnable import RunnableParallel, RunnablePassthrough\n", + "from langchain.prompts import ChatPromptTemplate, PromptTemplate\n", + "from langchain.schema import format_document\n", + "\n", + "import logging\n", + "\n", + "logging.basicConfig()\n", + "logging.getLogger(\"langchain.retrievers.multi_query\").setLevel(logging.INFO)\n", + "\n", + "LLM_CONTEXT_PROMPT = ChatPromptTemplate.from_template(\n", + " \"\"\"You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Be as verbose and educational in your response as possible. \n", + " \n", + " context: {context}\n", + " Question: \"{question}\"\n", + " Answer:\n", + " \"\"\"\n", + ")\n", + "\n", + "LLM_DOCUMENT_PROMPT = PromptTemplate.from_template(\"\"\"\n", + "---\n", + "SOURCE: {name}\n", + "{page_content}\n", + "---\n", + "\"\"\")\n", + "\n", + "def _combine_documents(\n", + " docs, document_prompt=LLM_DOCUMENT_PROMPT, document_separator=\"\\n\\n\"\n", + "):\n", + " doc_strings = [format_document(doc, document_prompt) for doc in docs]\n", + " return document_separator.join(doc_strings)\n", + "\n", + "_context = RunnableParallel(\n", + " context=retriever | _combine_documents,\n", + " question=RunnablePassthrough(),\n", + ")\n", + "\n", + "chain = (_context | LLM_CONTEXT_PROMPT | llm)\n", + "\n", + "ans = chain.invoke(\"what is the nasa sales team?\")\n", + "\n", + "print(\"---- Answer ----\")\n", + "print(ans)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb b/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb index 10e500ab..d3204736 100644 --- a/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb +++ b/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb @@ -5,7 +5,7 @@ "metadata": {}, "source": [ "# MultiQueryRetriever with elasticsearch and langchain\n", - "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/elastic/elasticsearch-labs/blob/main/notebooks/langchain/notebooks/langchain/langchain-multi-query-retriever.ipynb)\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/elastic/elasticsearch-labs/blob/main/notebooks/langchain/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb)\n", "\n", "This workbook demonstrates example of Elasticsearch's [MultiQuery Retriever](https://api.python.langchain.com/en/latest/retrievers/langchain.retrievers.multi_query.MultiQueryRetriever.html) to generate multiple queries for a given user input query and apply all queries to retrieve a larger set of relevant documents from a vectorstore.\n", "\n", From cafbfce237765cea9006a0e99f745db89c2942a2 Mon Sep 17 00:00:00 2001 From: Samiul Monir Date: Fri, 5 Jan 2024 11:42:57 -0500 Subject: [PATCH 3/7] removing misplaced key --- .../langchain-multi-query-retriever.ipynb | 2 -- 1 file changed, 2 deletions(-) diff --git a/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb b/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb index d3204736..79627748 100644 --- a/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb +++ b/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb @@ -155,11 +155,9 @@ "\n", "# https://platform.openai.com/api-keys\n", "OPENAI_API_KEY = getpass(\"OpenAI API key: \")\n", - "# OPENAI_API_KEY = \"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\"\n", "\n", "embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY)\n", "\n", - "\n", "vectorstore = ElasticsearchStore.from_documents(\n", " docs,\n", " embeddings,\n", From 200251d76b65bb3ace129be4c8902a5df698fd8b Mon Sep 17 00:00:00 2001 From: Samiul Monir Date: Fri, 5 Jan 2024 11:44:44 -0500 Subject: [PATCH 4/7] removing misplaced key --- .../langchain-multi-query-retriever.ipynb | 2 -- 1 file changed, 2 deletions(-) diff --git a/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb b/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb index 79627748..1baf5c79 100644 --- a/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb +++ b/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb @@ -147,11 +147,9 @@ "source": [ "# https://www.elastic.co/search-labs/tutorials/install-elasticsearch/elastic-cloud#finding-your-cloud-id\n", "ELASTIC_CLOUD_ID = getpass(\"Elastic Cloud ID: \")\n", - "# ELASTIC_CLOUD_ID = \"Test_For_Multi_Query:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvOjQ0MyQzMWRmYjg0NDI2NTg0ZTc2ODU2NDgyMWRhNjY4NzYwYSRiZmY0YmZlYzE5MTM0ZDZhYTU2N2ZmNjZkMmIxNzE3Nw==\"\n", "\n", "# https://www.elastic.co/search-labs/tutorials/install-elasticsearch/elastic-cloud#creating-an-api-key\n", "ELASTIC_API_KEY = getpass(\"Elastic Api Key: \")\n", - "# ELASTIC_API_KEY = \"WERUNmc0d0JSSVdlNEhqdndGdjk6bjdreWVndnlTZWkyMnVmQS1TdVFSQQ==\"\n", "\n", "# https://platform.openai.com/api-keys\n", "OPENAI_API_KEY = getpass(\"OpenAI API key: \")\n", From 7112c201dc222cf27c2462ae7049b7d55037e43d Mon Sep 17 00:00:00 2001 From: Samiul Monir Date: Thu, 11 Jan 2024 12:57:45 -0500 Subject: [PATCH 5/7] Updated package information for Multi Query Retrievals --- .../langchain-multi-query-retriever.ipynb | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb b/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb index 1baf5c79..a7ac4e0a 100644 --- a/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb +++ b/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb @@ -24,7 +24,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -38,14 +38,13 @@ } ], "source": [ - "!python3 -m pip install -qU lark elasticsearch langchain openai tiktoken\n", + "!python3 -m pip install -qU lark elasticsearch langchain_openai tiktoken\n", "\n", "from langchain.schema import Document\n", - "from langchain.embeddings.openai import OpenAIEmbeddings\n", + "from langchain_openai.embeddings import OpenAIEmbeddings\n", "from langchain.vectorstores import ElasticsearchStore\n", - "from langchain.chat_models import ChatOpenAI\n", + "from langchain_openai.chat_models import ChatOpenAI\n", "from langchain.retrievers.multi_query import MultiQueryRetriever\n", - "from langchain.chains import LLMChain\n", "from getpass import getpass" ] }, @@ -59,7 +58,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -141,7 +140,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -178,7 +177,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -202,7 +201,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -215,10 +214,10 @@ { "data": { "text/plain": [ - "4" + "1" ] }, - "execution_count": 16, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } From 2ce06791bb549974b7f7d781d2d87ea4a6fda7cc Mon Sep 17 00:00:00 2001 From: Samiul Monir Date: Thu, 11 Jan 2024 13:05:07 -0500 Subject: [PATCH 6/7] Fix document count size --- .../langchain-multi-query-retriever.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb b/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb index a7ac4e0a..13b13bc0 100644 --- a/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb +++ b/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb @@ -214,7 +214,7 @@ { "data": { "text/plain": [ - "1" + "4" ] }, "execution_count": 6, From 206f42d18fa30a6d5e99aad3db5d482c140c2fdb Mon Sep 17 00:00:00 2001 From: Samiul Monir Date: Thu, 11 Jan 2024 13:19:19 -0500 Subject: [PATCH 7/7] Fix import issues in Multi-Query Chatbot --- .../chatbot-with-multi-query-retriever.ipynb | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/notebooks/langchain/multi-query-retriever-examples/chatbot-with-multi-query-retriever.ipynb b/notebooks/langchain/multi-query-retriever-examples/chatbot-with-multi-query-retriever.ipynb index 54dd5e31..8f308d48 100644 --- a/notebooks/langchain/multi-query-retriever-examples/chatbot-with-multi-query-retriever.ipynb +++ b/notebooks/langchain/multi-query-retriever-examples/chatbot-with-multi-query-retriever.ipynb @@ -23,7 +23,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -37,11 +37,11 @@ } ], "source": [ - "!python3 -m pip install -qU lark elasticsearch langchain openai\n", + "!python3 -m pip install -qU jq lark elasticsearch langchain_openai tiktoken\n", "\n", - "from langchain.embeddings.openai import OpenAIEmbeddings\n", + "from langchain_openai.embeddings import OpenAIEmbeddings\n", "from langchain.vectorstores import ElasticsearchStore\n", - "from langchain.llms import OpenAI\n", + "from langchain_openai.llms import OpenAI\n", "from langchain.retrievers.multi_query import MultiQueryRetriever\n", "from getpass import getpass" ] @@ -61,7 +61,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -79,7 +79,7 @@ "vectorstore = ElasticsearchStore(\n", " es_cloud_id=ELASTIC_CLOUD_ID, \n", " es_api_key=ELASTIC_API_KEY,\n", - " index_name=\"elasticsearch-multi-query-demo\",\n", + " index_name=\"chatbot-multi-query-demo\",\n", " embedding= embeddings,\n", ")" ] @@ -94,7 +94,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -125,7 +125,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -167,14 +167,14 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "documents = vectorstore.from_documents(\n", " docs, \n", " embeddings, \n", - " index_name=\"elasticsearch-multi-query-demo\",\n", + " index_name=\"chatbot-multi-query-demo\",\n", " es_cloud_id=ELASTIC_CLOUD_ID,\n", " es_api_key=ELASTIC_API_KEY\n", ")\n", @@ -195,7 +195,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 9, "metadata": {}, "outputs": [ {