|
72 | 72 | "from langchain_core.documents import Document\n",
|
73 | 73 | "\n",
|
74 | 74 | "# See docker command above to launch a postgres instance with pgvector enabled.\n",
|
75 |
| - "connection = \"postgresql+psycopg://langchain:langchain@localhost:6024/langchain\" \n", |
| 75 | + "connection = \"postgresql+psycopg://langchain:langchain@localhost:6024/langchain\"\n", |
76 | 76 | "collection_name = \"my_docs\"\n",
|
77 | 77 | "embeddings = CohereEmbeddings()\n",
|
78 | 78 | "\n",
|
|
126 | 126 | "outputs": [],
|
127 | 127 | "source": [
|
128 | 128 | "docs = [\n",
|
129 |
| - " Document(page_content='there are cats in the pond', metadata={\"id\": 1, \"location\": \"pond\", \"topic\": \"animals\"}),\n", |
130 |
| - " Document(page_content='ducks are also found in the pond', metadata={\"id\": 2, \"location\": \"pond\", \"topic\": \"animals\"}),\n", |
131 |
| - " Document(page_content='fresh apples are available at the market', metadata={\"id\": 3, \"location\": \"market\", \"topic\": \"food\"}),\n", |
132 |
| - " Document(page_content='the market also sells fresh oranges', metadata={\"id\": 4, \"location\": \"market\", \"topic\": \"food\"}),\n", |
133 |
| - " Document(page_content='the new art exhibit is fascinating', metadata={\"id\": 5, \"location\": \"museum\", \"topic\": \"art\"}),\n", |
134 |
| - " Document(page_content='a sculpture exhibit is also at the museum', metadata={\"id\": 6, \"location\": \"museum\", \"topic\": \"art\"}),\n", |
135 |
| - " Document(page_content='a new coffee shop opened on Main Street', metadata={\"id\": 7, \"location\": \"Main Street\", \"topic\": \"food\"}),\n", |
136 |
| - " Document(page_content='the book club meets at the library', metadata={\"id\": 8, \"location\": \"library\", \"topic\": \"reading\"}),\n", |
137 |
| - " Document(page_content='the library hosts a weekly story time for kids', metadata={\"id\": 9, \"location\": \"library\", \"topic\": \"reading\"}),\n", |
138 |
| - " Document(page_content='a cooking class for beginners is offered at the community center', metadata={\"id\": 10, \"location\": \"community center\", \"topic\": \"classes\"})\n", |
139 |
| - "]\n" |
| 129 | + " Document(\n", |
| 130 | + " page_content=\"there are cats in the pond\",\n", |
| 131 | + " metadata={\"id\": 1, \"location\": \"pond\", \"topic\": \"animals\"},\n", |
| 132 | + " ),\n", |
| 133 | + " Document(\n", |
| 134 | + " page_content=\"ducks are also found in the pond\",\n", |
| 135 | + " metadata={\"id\": 2, \"location\": \"pond\", \"topic\": \"animals\"},\n", |
| 136 | + " ),\n", |
| 137 | + " Document(\n", |
| 138 | + " page_content=\"fresh apples are available at the market\",\n", |
| 139 | + " metadata={\"id\": 3, \"location\": \"market\", \"topic\": \"food\"},\n", |
| 140 | + " ),\n", |
| 141 | + " Document(\n", |
| 142 | + " page_content=\"the market also sells fresh oranges\",\n", |
| 143 | + " metadata={\"id\": 4, \"location\": \"market\", \"topic\": \"food\"},\n", |
| 144 | + " ),\n", |
| 145 | + " Document(\n", |
| 146 | + " page_content=\"the new art exhibit is fascinating\",\n", |
| 147 | + " metadata={\"id\": 5, \"location\": \"museum\", \"topic\": \"art\"},\n", |
| 148 | + " ),\n", |
| 149 | + " Document(\n", |
| 150 | + " page_content=\"a sculpture exhibit is also at the museum\",\n", |
| 151 | + " metadata={\"id\": 6, \"location\": \"museum\", \"topic\": \"art\"},\n", |
| 152 | + " ),\n", |
| 153 | + " Document(\n", |
| 154 | + " page_content=\"a new coffee shop opened on Main Street\",\n", |
| 155 | + " metadata={\"id\": 7, \"location\": \"Main Street\", \"topic\": \"food\"},\n", |
| 156 | + " ),\n", |
| 157 | + " Document(\n", |
| 158 | + " page_content=\"the book club meets at the library\",\n", |
| 159 | + " metadata={\"id\": 8, \"location\": \"library\", \"topic\": \"reading\"},\n", |
| 160 | + " ),\n", |
| 161 | + " Document(\n", |
| 162 | + " page_content=\"the library hosts a weekly story time for kids\",\n", |
| 163 | + " metadata={\"id\": 9, \"location\": \"library\", \"topic\": \"reading\"},\n", |
| 164 | + " ),\n", |
| 165 | + " Document(\n", |
| 166 | + " page_content=\"a cooking class for beginners is offered at the community center\",\n", |
| 167 | + " metadata={\"id\": 10, \"location\": \"community center\", \"topic\": \"classes\"},\n", |
| 168 | + " ),\n", |
| 169 | + "]" |
140 | 170 | ]
|
141 | 171 | },
|
142 | 172 | {
|
|
159 | 189 | }
|
160 | 190 | ],
|
161 | 191 | "source": [
|
162 |
| - "vectorstore.add_documents(docs, ids=[doc.metadata['id'] for doc in docs])" |
| 192 | + "vectorstore.add_documents(docs, ids=[doc.metadata[\"id\"] for doc in docs])" |
163 | 193 | ]
|
164 | 194 | },
|
165 | 195 | {
|
|
191 | 221 | }
|
192 | 222 | ],
|
193 | 223 | "source": [
|
194 |
| - "vectorstore.similarity_search('kitty', k=10)" |
| 224 | + "vectorstore.similarity_search(\"kitty\", k=10)" |
195 | 225 | ]
|
196 | 226 | },
|
197 | 227 | {
|
|
212 | 242 | "outputs": [],
|
213 | 243 | "source": [
|
214 | 244 | "docs = [\n",
|
215 |
| - " Document(page_content='there are cats in the pond', metadata={\"id\": 1, \"location\": \"pond\", \"topic\": \"animals\"}),\n", |
216 |
| - " Document(page_content='ducks are also found in the pond', metadata={\"id\": 2, \"location\": \"pond\", \"topic\": \"animals\"}),\n", |
217 |
| - " Document(page_content='fresh apples are available at the market', metadata={\"id\": 3, \"location\": \"market\", \"topic\": \"food\"}),\n", |
218 |
| - " Document(page_content='the market also sells fresh oranges', metadata={\"id\": 4, \"location\": \"market\", \"topic\": \"food\"}),\n", |
219 |
| - " Document(page_content='the new art exhibit is fascinating', metadata={\"id\": 5, \"location\": \"museum\", \"topic\": \"art\"}),\n", |
220 |
| - " Document(page_content='a sculpture exhibit is also at the museum', metadata={\"id\": 6, \"location\": \"museum\", \"topic\": \"art\"}),\n", |
221 |
| - " Document(page_content='a new coffee shop opened on Main Street', metadata={\"id\": 7, \"location\": \"Main Street\", \"topic\": \"food\"}),\n", |
222 |
| - " Document(page_content='the book club meets at the library', metadata={\"id\": 8, \"location\": \"library\", \"topic\": \"reading\"}),\n", |
223 |
| - " Document(page_content='the library hosts a weekly story time for kids', metadata={\"id\": 9, \"location\": \"library\", \"topic\": \"reading\"}),\n", |
224 |
| - " Document(page_content='a cooking class for beginners is offered at the community center', metadata={\"id\": 10, \"location\": \"community center\", \"topic\": \"classes\"})\n", |
225 |
| - "]\n" |
| 245 | + " Document(\n", |
| 246 | + " page_content=\"there are cats in the pond\",\n", |
| 247 | + " metadata={\"id\": 1, \"location\": \"pond\", \"topic\": \"animals\"},\n", |
| 248 | + " ),\n", |
| 249 | + " Document(\n", |
| 250 | + " page_content=\"ducks are also found in the pond\",\n", |
| 251 | + " metadata={\"id\": 2, \"location\": \"pond\", \"topic\": \"animals\"},\n", |
| 252 | + " ),\n", |
| 253 | + " Document(\n", |
| 254 | + " page_content=\"fresh apples are available at the market\",\n", |
| 255 | + " metadata={\"id\": 3, \"location\": \"market\", \"topic\": \"food\"},\n", |
| 256 | + " ),\n", |
| 257 | + " Document(\n", |
| 258 | + " page_content=\"the market also sells fresh oranges\",\n", |
| 259 | + " metadata={\"id\": 4, \"location\": \"market\", \"topic\": \"food\"},\n", |
| 260 | + " ),\n", |
| 261 | + " Document(\n", |
| 262 | + " page_content=\"the new art exhibit is fascinating\",\n", |
| 263 | + " metadata={\"id\": 5, \"location\": \"museum\", \"topic\": \"art\"},\n", |
| 264 | + " ),\n", |
| 265 | + " Document(\n", |
| 266 | + " page_content=\"a sculpture exhibit is also at the museum\",\n", |
| 267 | + " metadata={\"id\": 6, \"location\": \"museum\", \"topic\": \"art\"},\n", |
| 268 | + " ),\n", |
| 269 | + " Document(\n", |
| 270 | + " page_content=\"a new coffee shop opened on Main Street\",\n", |
| 271 | + " metadata={\"id\": 7, \"location\": \"Main Street\", \"topic\": \"food\"},\n", |
| 272 | + " ),\n", |
| 273 | + " Document(\n", |
| 274 | + " page_content=\"the book club meets at the library\",\n", |
| 275 | + " metadata={\"id\": 8, \"location\": \"library\", \"topic\": \"reading\"},\n", |
| 276 | + " ),\n", |
| 277 | + " Document(\n", |
| 278 | + " page_content=\"the library hosts a weekly story time for kids\",\n", |
| 279 | + " metadata={\"id\": 9, \"location\": \"library\", \"topic\": \"reading\"},\n", |
| 280 | + " ),\n", |
| 281 | + " Document(\n", |
| 282 | + " page_content=\"a cooking class for beginners is offered at the community center\",\n", |
| 283 | + " metadata={\"id\": 10, \"location\": \"community center\", \"topic\": \"classes\"},\n", |
| 284 | + " ),\n", |
| 285 | + "]" |
226 | 286 | ]
|
227 | 287 | },
|
228 | 288 | {
|
|
275 | 335 | }
|
276 | 336 | ],
|
277 | 337 | "source": [
|
278 |
| - "vectorstore.similarity_search('kitty', k=10, filter={\n", |
279 |
| - " 'id': {'$in': [1, 5, 2, 9]}\n", |
280 |
| - "})" |
| 338 | + "vectorstore.similarity_search(\"kitty\", k=10, filter={\"id\": {\"$in\": [1, 5, 2, 9]}})" |
281 | 339 | ]
|
282 | 340 | },
|
283 | 341 | {
|
|
309 | 367 | }
|
310 | 368 | ],
|
311 | 369 | "source": [
|
312 |
| - "vectorstore.similarity_search('ducks', k=10, filter={\n", |
313 |
| - " 'id': {'$in': [1, 5, 2, 9]},\n", |
314 |
| - " 'location': {'$in': [\"pond\", \"market\"]}\n", |
315 |
| - "})" |
| 370 | + "vectorstore.similarity_search(\n", |
| 371 | + " \"ducks\",\n", |
| 372 | + " k=10,\n", |
| 373 | + " filter={\"id\": {\"$in\": [1, 5, 2, 9]}, \"location\": {\"$in\": [\"pond\", \"market\"]}},\n", |
| 374 | + ")" |
316 | 375 | ]
|
317 | 376 | },
|
318 | 377 | {
|
|
336 | 395 | }
|
337 | 396 | ],
|
338 | 397 | "source": [
|
339 |
| - "vectorstore.similarity_search('ducks', k=10, filter={\n", |
340 |
| - " '$and': [\n", |
341 |
| - " {'id': {'$in': [1, 5, 2, 9]}},\n", |
342 |
| - " {'location': {'$in': [\"pond\", \"market\"]}},\n", |
343 |
| - " ]\n", |
344 |
| - "}\n", |
| 398 | + "vectorstore.similarity_search(\n", |
| 399 | + " \"ducks\",\n", |
| 400 | + " k=10,\n", |
| 401 | + " filter={\n", |
| 402 | + " \"$and\": [\n", |
| 403 | + " {\"id\": {\"$in\": [1, 5, 2, 9]}},\n", |
| 404 | + " {\"location\": {\"$in\": [\"pond\", \"market\"]}},\n", |
| 405 | + " ]\n", |
| 406 | + " },\n", |
345 | 407 | ")"
|
346 | 408 | ]
|
347 | 409 | },
|
|
372 | 434 | }
|
373 | 435 | ],
|
374 | 436 | "source": [
|
375 |
| - "vectorstore.similarity_search('bird', k=10, filter={\n", |
376 |
| - " 'location': { \"$ne\": 'pond'}\n", |
377 |
| - "})" |
| 437 | + "vectorstore.similarity_search(\"bird\", k=10, filter={\"location\": {\"$ne\": \"pond\"}})" |
378 | 438 | ]
|
379 | 439 | }
|
380 | 440 | ],
|
|
0 commit comments