Skip to content

docs: update tutorial #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 74 additions & 33 deletions examples/pg_vectorstore.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {
"tags": []
},
Expand Down Expand Up @@ -108,7 +108,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {
"tags": []
},
Expand Down Expand Up @@ -143,8 +143,8 @@
" await pg_engine.ainit_vectorstore_table(\n",
" table_name=TABLE_NAME,\n",
" vector_size=VECTOR_SIZE,\n",
" id_column=Column(name=\"id\", data_type=\"INTEGER\", nullable=False),\n",
" metadata_columns=[\n",
" Column(\"likes\", \"INTEGER\"),\n",
" Column(\"location\", \"TEXT\"),\n",
" Column(\"topic\", \"TEXT\"),\n",
" ],\n",
Expand Down Expand Up @@ -178,7 +178,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
Expand Down Expand Up @@ -226,7 +226,6 @@
" engine=pg_engine,\n",
" table_name=TABLE_NAME,\n",
" embedding_service=embedding,\n",
" id_column=\"id\",\n",
" metadata_columns=[\"location\", \"topic\"]\n",
")"
]
Expand Down Expand Up @@ -260,22 +259,22 @@
" Document(\n",
" id=uuid.uuid4(),\n",
" page_content=\"there are cats in the pond\",\n",
" metadata={\"id\": 1, \"location\": \"pond\", \"topic\": \"animals\"},\n",
" metadata={\"likes\": 1, \"location\": \"pond\", \"topic\": \"animals\"},\n",
" ),\n",
" Document(\n",
" id=uuid.uuid4(),\n",
" page_content=\"ducks are also found in the pond\",\n",
" metadata={\"id\": 2, \"location\": \"pond\", \"topic\": \"animals\"},\n",
" metadata={\"likes\": 30, \"location\": \"pond\", \"topic\": \"animals\"},\n",
" ),\n",
" Document(\n",
" id=uuid.uuid4(),\n",
" page_content=\"fresh apples are available at the market\",\n",
" metadata={\"id\": 3, \"location\": \"market\", \"topic\": \"food\"},\n",
" metadata={\"likes\": 20, \"location\": \"market\", \"topic\": \"food\"},\n",
" ),\n",
" Document(\n",
" id=uuid.uuid4(),\n",
" page_content=\"the market also sells fresh oranges\",\n",
" metadata={\"id\": 4, \"location\": \"market\", \"topic\": \"food\"},\n",
" metadata={\"likes\": 5, \"location\": \"market\", \"topic\": \"food\"},\n",
" ),\n",
"]\n",
"\n",
Expand All @@ -295,7 +294,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# We'll use the ID of the first doc to delete it\n",
Expand All @@ -315,7 +316,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"query = \"I'd like a fruit.\"\n",
Expand All @@ -334,7 +337,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"query_vector = embedding.embed_query(query)\n",
Expand All @@ -359,20 +364,20 @@
"\n",
"| Operator | Meaning/Category |\n",
"|-----------|-------------------------|\n",
"| $eq | Equality (==) |\n",
"| $ne | Inequality (!=) |\n",
"| $lt | Less than (<) |\n",
"| $lte | Less than or equal (<=) |\n",
"| $gt | Greater than (>) |\n",
"| $gte | Greater than or equal (>=) |\n",
"| $in | Special Cased (in) |\n",
"| $nin | Special Cased (not in) |\n",
"| $between | Special Cased (between) |\n",
"| $exists | Special Cased (is null) |\n",
"| $like | Text (like) |\n",
"| $ilike | Text (case-insensitive like) |\n",
"| $and | Logical (and) |\n",
"| $or | Logical (or) |"
"| \\$eq | Equality (==) |\n",
"| \\$ne | Inequality (!=) |\n",
"| \\$lt | Less than (<) |\n",
"| \\$lte | Less than or equal (<=) |\n",
"| \\$gt | Greater than (>) |\n",
"| \\$gte | Greater than or equal (>=) |\n",
"| \\$in | Special Cased (in) |\n",
"| \\$nin | Special Cased (not in) |\n",
"| \\$between | Special Cased (between) |\n",
"| \\$exists | Special Cased (is null) |\n",
"| \\$like | Text (like) |\n",
"| \\$ilike | Text (case-insensitive like) |\n",
"| \\$and | Logical (and) |\n",
"| \\$or | Logical (or) |\n"
]
},
{
Expand All @@ -383,10 +388,40 @@
},
"outputs": [],
"source": [
"query = \"meow\"\n",
"docs = await vectorstore.asimilarity_search(query, filter={\"topic\": \"food\"})\n",
"for doc in docs:\n",
" print(repr(doc))"
"await vectorstore.asimilarity_search(\"birds\", filter={\"$or\": [{\"topic\": \"animals\"}, {\"location\": \"market\"}]})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"await vectorstore.asimilarity_search(\"apple\", filter={\"topic\": \"food\"})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"await vectorstore.asimilarity_search(\"apple\", filter={\"topic\": {\"$in\": [\"food\", \"animals\"]}})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"await vectorstore.asimilarity_search(\"sales of fruit\", filter={\"topic\": {\"$ne\": \"animals\"}})"
]
},
{
Expand All @@ -408,7 +443,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langchain_postgres.v2.indexes import IVFFlatIndex\n",
Expand All @@ -429,7 +466,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"await vectorstore.areindex() # Re-index using default index name"
Expand All @@ -447,7 +486,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"await vectorstore.adrop_vector_index() # Drop index using default name"
Expand Down