diff --git a/examples/pg_vectorstore.ipynb b/examples/pg_vectorstore.ipynb index 317a988..340abe1 100644 --- a/examples/pg_vectorstore.ipynb +++ b/examples/pg_vectorstore.ipynb @@ -64,7 +64,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "tags": [] }, @@ -108,7 +108,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "tags": [] }, @@ -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", @@ -178,7 +178,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -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", ")" ] @@ -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", @@ -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", @@ -315,7 +316,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "query = \"I'd like a fruit.\"\n", @@ -334,7 +337,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "query_vector = embedding.embed_query(query)\n", @@ -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" ] }, { @@ -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\"}})" ] }, { @@ -408,7 +443,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "from langchain_postgres.v2.indexes import IVFFlatIndex\n", @@ -429,7 +466,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "await vectorstore.areindex() # Re-index using default index name" @@ -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"