|
11 | 11 | "cell_type": "markdown",
|
12 | 12 | "metadata": {},
|
13 | 13 | "source": [
|
14 |
| - "This notebook is a companion to the [LangChain Get Started](https://www.mongodb.com/docs/atlas/atlas-vector-search/ai-integrations/langchain/get-started/) page. Refer to the page for set-up instructions and detailed explanations.\n", |
| 14 | + "This notebook is a companion to the [LangChain Get Started](https://www.mongodb.com/docs/atlas/atlas-vector-search/ai-integrations/langchain/get-started/) tutorial. Refer to the page for set-up instructions and detailed explanations.\n", |
15 | 15 | "\n",
|
16 | 16 | "<a target=\"_blank\" href=\"https://colab.research.google.com/github/mongodb/docs-notebooks/blob/main/ai-integrations/langchain.ipynb\">\n",
|
17 | 17 | " <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n",
|
|
37 | 37 | "metadata": {},
|
38 | 38 | "outputs": [],
|
39 | 39 | "source": [
|
40 |
| - "import os, pprint\n", |
41 |
| - "from langchain_community.document_loaders import PyPDFLoader\n", |
42 |
| - "from langchain_core.output_parsers import StrOutputParser\n", |
43 |
| - "from langchain_core.runnables import RunnablePassthrough\n", |
44 |
| - "from langchain_mongodb import MongoDBAtlasVectorSearch\n", |
45 |
| - "from langchain_openai import ChatOpenAI, OpenAIEmbeddings\n", |
46 |
| - "from langchain.prompts import PromptTemplate\n", |
47 |
| - "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", |
48 |
| - "from pymongo import MongoClient" |
49 |
| - ] |
50 |
| - }, |
51 |
| - { |
52 |
| - "cell_type": "code", |
53 |
| - "execution_count": null, |
54 |
| - "metadata": {}, |
55 |
| - "outputs": [], |
56 |
| - "source": [ |
| 40 | + "import os\n", |
| 41 | + "\n", |
57 | 42 | "os.environ[\"OPENAI_API_KEY\"] = \"<api-key>\"\n",
|
58 | 43 | "ATLAS_CONNECTION_STRING = \"<connection-string>\""
|
59 | 44 | ]
|
|
64 | 49 | "metadata": {},
|
65 | 50 | "outputs": [],
|
66 | 51 | "source": [
|
67 |
| - "# Connect to your Atlas cluster\n", |
68 |
| - "client = MongoClient(ATLAS_CONNECTION_STRING)\n", |
69 |
| - "\n", |
70 |
| - "# Define collection and index name\n", |
71 |
| - "db_name = \"langchain_db\"\n", |
72 |
| - "collection_name = \"test\"\n", |
73 |
| - "atlas_collection = client[db_name][collection_name]\n", |
74 |
| - "vector_search_index = \"vector_index\"" |
75 |
| - ] |
76 |
| - }, |
77 |
| - { |
78 |
| - "cell_type": "code", |
79 |
| - "execution_count": null, |
80 |
| - "metadata": {}, |
81 |
| - "outputs": [], |
82 |
| - "source": [ |
| 52 | + "from langchain_community.document_loaders import PyPDFLoader\n", |
| 53 | + "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", |
| 54 | + "\n", |
83 | 55 | "# Load the PDF\n",
|
84 |
| - "loader = PyPDFLoader(\"https://query.prod.cms.rt.microsoft.com/cms/api/am/binary/RE4HkJP\")\n", |
| 56 | + "loader = PyPDFLoader(\"https://investors.mongodb.com/node/13176/pdf\")\n", |
85 | 57 | "data = loader.load()\n",
|
86 | 58 | "\n",
|
87 | 59 | "# Split PDF into documents\n",
|
|
98 | 70 | "metadata": {},
|
99 | 71 | "outputs": [],
|
100 | 72 | "source": [
|
101 |
| - "# Create the vector store\n", |
102 |
| - "vector_store = MongoDBAtlasVectorSearch.from_documents(\n", |
103 |
| - " documents = docs,\n", |
104 |
| - " embedding = OpenAIEmbeddings(disallowed_special=()),\n", |
105 |
| - " collection = atlas_collection,\n", |
106 |
| - " index_name = vector_search_index\n", |
107 |
| - ")" |
| 73 | + "from langchain_mongodb import MongoDBAtlasVectorSearch\n", |
| 74 | + "from langchain_openai import OpenAIEmbeddings\n", |
| 75 | + "\n", |
| 76 | + "# Instantiate the vector store using your MongoDB connection string\n", |
| 77 | + "vector_store = MongoDBAtlasVectorSearch.from_connection_string(\n", |
| 78 | + " connection_string = ATLAS_CONNECTION_STRING,\n", |
| 79 | + " namespace = \"langchain_db.test\",\n", |
| 80 | + " embedding = OpenAIEmbeddings(model=\"text-embedding-3-large\"),\n", |
| 81 | + " index_name = \"vector_index\"\n", |
| 82 | + ")\n", |
| 83 | + "\n", |
| 84 | + "# Add documents to the vector store\n", |
| 85 | + "vector_store.add_documents(documents=docs)" |
108 | 86 | ]
|
109 | 87 | },
|
110 | 88 | {
|
|
113 | 91 | "metadata": {},
|
114 | 92 | "outputs": [],
|
115 | 93 | "source": [
|
| 94 | + "import time\n", |
| 95 | + "\n", |
116 | 96 | "# Use helper method to create the vector search index\n",
|
117 | 97 | "vector_store.create_vector_search_index(\n",
|
118 |
| - " dimensions = 1536, # The dimensions of the vector embeddings to be indexed\n", |
119 |
| - " filters = [ \"page\" ]\n", |
120 |
| - ")" |
| 98 | + " dimensions = 3072, # The dimensions of the vector embeddings to be indexed\n", |
| 99 | + " filters = [ \"page_label\" ]\n", |
| 100 | + ")\n", |
| 101 | + "\n", |
| 102 | + "# Wait for the index to build (this can take around a minute)\n", |
| 103 | + "time.sleep(60)" |
121 | 104 | ]
|
122 | 105 | },
|
123 | 106 | {
|
|
133 | 116 | "metadata": {},
|
134 | 117 | "outputs": [],
|
135 | 118 | "source": [
|
136 |
| - "query = \"MongoDB Atlas security\"\n", |
| 119 | + "import pprint\n", |
| 120 | + "\n", |
| 121 | + "query = \"MongoDB acquisition\"\n", |
137 | 122 | "results = vector_store.similarity_search(query)\n",
|
138 | 123 | "\n",
|
139 | 124 | "pprint.pprint(results)"
|
|
152 | 137 | "metadata": {},
|
153 | 138 | "outputs": [],
|
154 | 139 | "source": [
|
155 |
| - "query = \"MongoDB Atlas security\"\n", |
| 140 | + "query = \"MongoDB acquisition\"\n", |
156 | 141 | "results = vector_store.similarity_search_with_score(\n",
|
157 | 142 | " query = query, k = 3\n",
|
158 | 143 | ")\n",
|
|
173 | 158 | "metadata": {},
|
174 | 159 | "outputs": [],
|
175 | 160 | "source": [
|
176 |
| - "query = \"MongoDB Atlas security\"\n", |
| 161 | + "query = \"MongoDB acquisition\"\n", |
177 | 162 | "\n",
|
178 | 163 | "results = vector_store.similarity_search_with_score(\n",
|
179 | 164 | " query = query,\n",
|
180 | 165 | " k = 3,\n",
|
181 |
| - " pre_filter = { \"page\": { \"$eq\": 17 } }\n", |
| 166 | + " pre_filter = { \"page_label\": { \"$eq\": 2 } }\n", |
182 | 167 | ")\n",
|
183 | 168 | "\n",
|
184 | 169 | "pprint.pprint(results)"
|
|
197 | 182 | "metadata": {},
|
198 | 183 | "outputs": [],
|
199 | 184 | "source": [
|
| 185 | + "from langchain_core.output_parsers import StrOutputParser\n", |
| 186 | + "from langchain_core.runnables import RunnablePassthrough\n", |
| 187 | + "from langchain_openai import ChatOpenAI\n", |
| 188 | + "from langchain.prompts import PromptTemplate\n", |
| 189 | + "\n", |
200 | 190 | "# Instantiate Atlas Vector Search as a retriever\n",
|
201 | 191 | "retriever = vector_store.as_retriever(\n",
|
202 | 192 | " search_type = \"similarity\",\n",
|
|
215 | 205 | "\"\"\"\n",
|
216 | 206 | "custom_rag_prompt = PromptTemplate.from_template(template)\n",
|
217 | 207 | "\n",
|
218 |
| - "llm = ChatOpenAI()\n", |
| 208 | + "llm = ChatOpenAI(model=\"gpt-4o\")\n", |
219 | 209 | "\n",
|
220 | 210 | "def format_docs(docs):\n",
|
221 | 211 | " return \"\\n\\n\".join(doc.page_content for doc in docs)\n",
|
|
229 | 219 | ")\n",
|
230 | 220 | "\n",
|
231 | 221 | "# Prompt the chain\n",
|
232 |
| - "question = \"How can I secure my MongoDB Atlas cluster?\"\n", |
| 222 | + "question = \"What was MongoDB's latest acquisition?\"\n", |
233 | 223 | "answer = rag_chain.invoke(question)\n",
|
234 | 224 | "\n",
|
235 | 225 | "print(\"Question: \" + question)\n",
|
|
260 | 250 | " search_kwargs = {\n",
|
261 | 251 | " \"k\": 10,\n",
|
262 | 252 | " \"score_threshold\": 0.75,\n",
|
263 |
| - " \"pre_filter\": { \"page\": { \"$eq\": 17 } }\n", |
| 253 | + " \"pre_filter\": { \"page_label\": { \"$eq\": 2 } }\n", |
264 | 254 | " }\n",
|
265 | 255 | ")\n",
|
266 | 256 | "\n",
|
|
276 | 266 | "\"\"\"\n",
|
277 | 267 | "custom_rag_prompt = PromptTemplate.from_template(template)\n",
|
278 | 268 | "\n",
|
279 |
| - "llm = ChatOpenAI()\n", |
| 269 | + "llm = ChatOpenAI(model=\"gpt-4o\")\n", |
280 | 270 | "\n",
|
281 | 271 | "def format_docs(docs):\n",
|
282 | 272 | " return \"\\n\\n\".join(doc.page_content for doc in docs)\n",
|
|
290 | 280 | ")\n",
|
291 | 281 | "\n",
|
292 | 282 | "# Prompt the chain\n",
|
293 |
| - "question = \"How can I secure my MongoDB Atlas cluster?\"\n", |
| 283 | + "question = \"What was MongoDB's latest acquisition?\"\n", |
294 | 284 | "answer = rag_chain.invoke(question)\n",
|
295 | 285 | "\n",
|
296 | 286 | "print(\"Question: \" + question)\n",
|
|
0 commit comments