Skip to content

Commit 02b1fc6

Browse files
authored
docs: update tutorial (#199)
current example w/ ID field wasn't working perhaps due to a collision in the metadata fields and the ID column
1 parent fc5a903 commit 02b1fc6

File tree

1 file changed

+74
-33
lines changed

1 file changed

+74
-33
lines changed

examples/pg_vectorstore.ipynb

+74-33
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
},
6565
{
6666
"cell_type": "code",
67-
"execution_count": 1,
67+
"execution_count": null,
6868
"metadata": {
6969
"tags": []
7070
},
@@ -108,7 +108,7 @@
108108
},
109109
{
110110
"cell_type": "code",
111-
"execution_count": 2,
111+
"execution_count": null,
112112
"metadata": {
113113
"tags": []
114114
},
@@ -143,8 +143,8 @@
143143
" await pg_engine.ainit_vectorstore_table(\n",
144144
" table_name=TABLE_NAME,\n",
145145
" vector_size=VECTOR_SIZE,\n",
146-
" id_column=Column(name=\"id\", data_type=\"INTEGER\", nullable=False),\n",
147146
" metadata_columns=[\n",
147+
" Column(\"likes\", \"INTEGER\"),\n",
148148
" Column(\"location\", \"TEXT\"),\n",
149149
" Column(\"topic\", \"TEXT\"),\n",
150150
" ],\n",
@@ -178,7 +178,7 @@
178178
},
179179
{
180180
"cell_type": "code",
181-
"execution_count": 5,
181+
"execution_count": null,
182182
"metadata": {
183183
"colab": {
184184
"base_uri": "https://localhost:8080/"
@@ -226,7 +226,6 @@
226226
" engine=pg_engine,\n",
227227
" table_name=TABLE_NAME,\n",
228228
" embedding_service=embedding,\n",
229-
" id_column=\"id\",\n",
230229
" metadata_columns=[\"location\", \"topic\"]\n",
231230
")"
232231
]
@@ -260,22 +259,22 @@
260259
" Document(\n",
261260
" id=uuid.uuid4(),\n",
262261
" page_content=\"there are cats in the pond\",\n",
263-
" metadata={\"id\": 1, \"location\": \"pond\", \"topic\": \"animals\"},\n",
262+
" metadata={\"likes\": 1, \"location\": \"pond\", \"topic\": \"animals\"},\n",
264263
" ),\n",
265264
" Document(\n",
266265
" id=uuid.uuid4(),\n",
267266
" page_content=\"ducks are also found in the pond\",\n",
268-
" metadata={\"id\": 2, \"location\": \"pond\", \"topic\": \"animals\"},\n",
267+
" metadata={\"likes\": 30, \"location\": \"pond\", \"topic\": \"animals\"},\n",
269268
" ),\n",
270269
" Document(\n",
271270
" id=uuid.uuid4(),\n",
272271
" page_content=\"fresh apples are available at the market\",\n",
273-
" metadata={\"id\": 3, \"location\": \"market\", \"topic\": \"food\"},\n",
272+
" metadata={\"likes\": 20, \"location\": \"market\", \"topic\": \"food\"},\n",
274273
" ),\n",
275274
" Document(\n",
276275
" id=uuid.uuid4(),\n",
277276
" page_content=\"the market also sells fresh oranges\",\n",
278-
" metadata={\"id\": 4, \"location\": \"market\", \"topic\": \"food\"},\n",
277+
" metadata={\"likes\": 5, \"location\": \"market\", \"topic\": \"food\"},\n",
279278
" ),\n",
280279
"]\n",
281280
"\n",
@@ -295,7 +294,9 @@
295294
{
296295
"cell_type": "code",
297296
"execution_count": null,
298-
"metadata": {},
297+
"metadata": {
298+
"tags": []
299+
},
299300
"outputs": [],
300301
"source": [
301302
"# We'll use the ID of the first doc to delete it\n",
@@ -315,7 +316,9 @@
315316
{
316317
"cell_type": "code",
317318
"execution_count": null,
318-
"metadata": {},
319+
"metadata": {
320+
"tags": []
321+
},
319322
"outputs": [],
320323
"source": [
321324
"query = \"I'd like a fruit.\"\n",
@@ -334,7 +337,9 @@
334337
{
335338
"cell_type": "code",
336339
"execution_count": null,
337-
"metadata": {},
340+
"metadata": {
341+
"tags": []
342+
},
338343
"outputs": [],
339344
"source": [
340345
"query_vector = embedding.embed_query(query)\n",
@@ -359,20 +364,20 @@
359364
"\n",
360365
"| Operator | Meaning/Category |\n",
361366
"|-----------|-------------------------|\n",
362-
"| $eq | Equality (==) |\n",
363-
"| $ne | Inequality (!=) |\n",
364-
"| $lt | Less than (<) |\n",
365-
"| $lte | Less than or equal (<=) |\n",
366-
"| $gt | Greater than (>) |\n",
367-
"| $gte | Greater than or equal (>=) |\n",
368-
"| $in | Special Cased (in) |\n",
369-
"| $nin | Special Cased (not in) |\n",
370-
"| $between | Special Cased (between) |\n",
371-
"| $exists | Special Cased (is null) |\n",
372-
"| $like | Text (like) |\n",
373-
"| $ilike | Text (case-insensitive like) |\n",
374-
"| $and | Logical (and) |\n",
375-
"| $or | Logical (or) |"
367+
"| \\$eq | Equality (==) |\n",
368+
"| \\$ne | Inequality (!=) |\n",
369+
"| \\$lt | Less than (<) |\n",
370+
"| \\$lte | Less than or equal (<=) |\n",
371+
"| \\$gt | Greater than (>) |\n",
372+
"| \\$gte | Greater than or equal (>=) |\n",
373+
"| \\$in | Special Cased (in) |\n",
374+
"| \\$nin | Special Cased (not in) |\n",
375+
"| \\$between | Special Cased (between) |\n",
376+
"| \\$exists | Special Cased (is null) |\n",
377+
"| \\$like | Text (like) |\n",
378+
"| \\$ilike | Text (case-insensitive like) |\n",
379+
"| \\$and | Logical (and) |\n",
380+
"| \\$or | Logical (or) |\n"
376381
]
377382
},
378383
{
@@ -383,10 +388,40 @@
383388
},
384389
"outputs": [],
385390
"source": [
386-
"query = \"meow\"\n",
387-
"docs = await vectorstore.asimilarity_search(query, filter={\"topic\": \"food\"})\n",
388-
"for doc in docs:\n",
389-
" print(repr(doc))"
391+
"await vectorstore.asimilarity_search(\"birds\", filter={\"$or\": [{\"topic\": \"animals\"}, {\"location\": \"market\"}]})"
392+
]
393+
},
394+
{
395+
"cell_type": "code",
396+
"execution_count": null,
397+
"metadata": {
398+
"tags": []
399+
},
400+
"outputs": [],
401+
"source": [
402+
"await vectorstore.asimilarity_search(\"apple\", filter={\"topic\": \"food\"})"
403+
]
404+
},
405+
{
406+
"cell_type": "code",
407+
"execution_count": null,
408+
"metadata": {
409+
"tags": []
410+
},
411+
"outputs": [],
412+
"source": [
413+
"await vectorstore.asimilarity_search(\"apple\", filter={\"topic\": {\"$in\": [\"food\", \"animals\"]}})"
414+
]
415+
},
416+
{
417+
"cell_type": "code",
418+
"execution_count": null,
419+
"metadata": {
420+
"tags": []
421+
},
422+
"outputs": [],
423+
"source": [
424+
"await vectorstore.asimilarity_search(\"sales of fruit\", filter={\"topic\": {\"$ne\": \"animals\"}})"
390425
]
391426
},
392427
{
@@ -408,7 +443,9 @@
408443
{
409444
"cell_type": "code",
410445
"execution_count": null,
411-
"metadata": {},
446+
"metadata": {
447+
"tags": []
448+
},
412449
"outputs": [],
413450
"source": [
414451
"from langchain_postgres.v2.indexes import IVFFlatIndex\n",
@@ -429,7 +466,9 @@
429466
{
430467
"cell_type": "code",
431468
"execution_count": null,
432-
"metadata": {},
469+
"metadata": {
470+
"tags": []
471+
},
433472
"outputs": [],
434473
"source": [
435474
"await vectorstore.areindex() # Re-index using default index name"
@@ -447,7 +486,9 @@
447486
{
448487
"cell_type": "code",
449488
"execution_count": null,
450-
"metadata": {},
489+
"metadata": {
490+
"tags": []
491+
},
451492
"outputs": [],
452493
"source": [
453494
"await vectorstore.adrop_vector_index() # Drop index using default name"

0 commit comments

Comments
 (0)