From 2be4fbb8358b8301d7734bdc404bfc54e3c8ee88 Mon Sep 17 00:00:00 2001 From: Sun bin Date: Wed, 7 Jun 2023 13:10:36 +0800 Subject: [PATCH] add doc about reusing MongoDBAtlasVectorSearch (#5805) DOC: add doc about reusing MongoDBAtlasVectorSearch #### Who can review? Anyone authorized. --- .../mongodb_atlas_vector_search.ipynb | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/docs/modules/indexes/vectorstores/examples/mongodb_atlas_vector_search.ipynb b/docs/modules/indexes/vectorstores/examples/mongodb_atlas_vector_search.ipynb index 1cba5676..2e219561 100644 --- a/docs/modules/indexes/vectorstores/examples/mongodb_atlas_vector_search.ipynb +++ b/docs/modules/indexes/vectorstores/examples/mongodb_atlas_vector_search.ipynb @@ -43,7 +43,7 @@ }, { "cell_type": "markdown", - "id": "320af802-9271-46ee-948f-d2453933d44b", + "id": "457ace44-1d95-4001-9dd5-78811ab208ad", "metadata": {}, "source": [ "We want to use `OpenAIEmbeddings` so we have to get the OpenAI API Key. Make sure the environment variable `OPENAI_API_KEY` is set up before proceeding." @@ -143,6 +143,47 @@ "source": [ "print(docs[0].page_content)" ] + }, + { + "cell_type": "markdown", + "id": "851a2ec9-9390-49a4-8412-3e132c9f789d", + "metadata": {}, + "source": [ + "You can reuse vector index you created before, make sure environment variable `OPENAI_API_KEY` is set up, then create another file." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6336fe79-3e73-48be-b20a-0ff1bb6a4399", + "metadata": {}, + "outputs": [], + "source": [ + "from pymongo import MongoClient\n", + "from langchain.vectorstores import MongoDBAtlasVectorSearch\n", + "from langchain.embeddings.openai import OpenAIEmbeddings\n", + "import os\n", + "\n", + "MONGODB_ATLAS_URI = os.environ['MONGODB_ATLAS_URI']\n", + "\n", + "# initialize MongoDB python client\n", + "client = MongoClient(MONGODB_ATLAS_URI)\n", + "\n", + "db_name = \"langchain_db\"\n", + "collection_name = \"langchain_col\"\n", + "collection = client[db_name][collection_name]\n", + "index_name = \"langchain_index\"\n", + "\n", + "# initialize vector store\n", + "vectorStore = MongoDBAtlasVectorSearch(\n", + " collection, OpenAIEmbeddings(), index_name=index_name)\n", + "\n", + "# perform a similarity search between the embedding of the query and the embeddings of the documents\n", + "query = \"What did the president say about Ketanji Brown Jackson\"\n", + "docs = vectorStore.similarity_search(query)\n", + "\n", + "print(docs[0].page_content)" + ] } ], "metadata": {