From 5c32307a7aaf750ea18fda438d180cbb36a13f0f Mon Sep 17 00:00:00 2001 From: RuofanChen03 <114096642+RuofanChen03@users.noreply.github.com> Date: Thu, 2 Jan 2025 16:08:25 -0500 Subject: [PATCH] docs: Add FAISS Filter with Advanced Query Operators Documentation & Demonstration (#28938) ## Description This pull request updates the documentation for FAISS regarding filter construction, following the changes made in commit `df5008f`. ## Issue None. This is a follow-up PR for documentation of [#28207](https://github.com/langchain-ai/langchain/pull/28207) ## Dependencies: None. --------- Co-authored-by: Chester Curme --- .../integrations/vectorstores/faiss.ipynb | 46 +++++++++++++++++++ .../vectorstores/faiss_async.ipynb | 43 +++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/docs/docs/integrations/vectorstores/faiss.ipynb b/docs/docs/integrations/vectorstores/faiss.ipynb index 0f957599065d0..26520baa6ac0d 100644 --- a/docs/docs/integrations/vectorstores/faiss.ipynb +++ b/docs/docs/integrations/vectorstores/faiss.ipynb @@ -286,6 +286,52 @@ " print(f\"* {res.page_content} [{res.metadata}]\")" ] }, + { + "cell_type": "markdown", + "id": "39cb1496", + "metadata": {}, + "source": [ + "Some [MongoDB query and projection operators](https://www.mongodb.com/docs/manual/reference/operator/query/) are supported for more advanced metadata filtering. The current list of supported operators are as follows:\n", + "- `$eq` (equals)\n", + "- `$neq` (not equals)\n", + "- `$gt` (greater than)\n", + "- `$lt` (less than)\n", + "- `$gte` (greater than or equal)\n", + "- `$lte` (less than or equal)\n", + "- `$in` (membership in list)\n", + "- `$nin` (not in list)\n", + "- `$and` (all conditions must match)\n", + "- `$or` (any condition must match)\n", + "- `$not` (negation of condition)\n", + "\n", + "Performing the same above similarity search with advanced metadata filtering can be done as follows:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1b3dd99d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "* Building an exciting new project with LangChain - come check it out! [{'source': 'tweet'}]\n", + "* LangGraph is the best framework for building stateful, agentic applications! [{'source': 'tweet'}]\n" + ] + } + ], + "source": [ + "results = vector_store.similarity_search(\n", + " \"LangChain provides abstractions to make working with LLMs easy\",\n", + " k=2,\n", + " filter={\"source\": {\"$eq\": \"tweet\"}},\n", + ")\n", + "for res in results:\n", + " print(f\"* {res.page_content} [{res.metadata}]\")" + ] + }, { "cell_type": "markdown", "id": "5ae35069", diff --git a/docs/docs/integrations/vectorstores/faiss_async.ipynb b/docs/docs/integrations/vectorstores/faiss_async.ipynb index e3bd1c90300ab..ec16f9e4d10ad 100644 --- a/docs/docs/integrations/vectorstores/faiss_async.ipynb +++ b/docs/docs/integrations/vectorstores/faiss_async.ipynb @@ -397,6 +397,49 @@ " print(f\"Content: {doc.page_content}, Metadata: {doc.metadata}\")" ] }, + { + "cell_type": "markdown", + "id": "8dead085", + "metadata": {}, + "source": [ + "Some [MongoDB query and projection operators](https://www.mongodb.com/docs/manual/reference/operator/query/) are supported for more advanced metadata filtering. The current list of supported operators are as follows:\n", + "- `$eq` (equals)\n", + "- `$neq` (not equals)\n", + "- `$gt` (greater than)\n", + "- `$lt` (less than)\n", + "- `$gte` (greater than or equal)\n", + "- `$lte` (less than or equal)\n", + "- `$in` (membership in list)\n", + "- `$nin` (not in list)\n", + "- `$and` (all conditions must match)\n", + "- `$or` (any condition must match)\n", + "- `$not` (negation of condition)\n", + "\n", + "Performing the same above similarity search with advanced metadata filtering can be done as follows:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "af47c6f9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Content: foo, Metadata: {'page': 1}\n" + ] + } + ], + "source": [ + "results = await db.asimilarity_search(\n", + " \"foo\", filter={\"page\": {\"$eq\": 1}}, k=1, fetch_k=4\n", + ")\n", + "for doc in results:\n", + " print(f\"Content: {doc.page_content}, Metadata: {doc.metadata}\")" + ] + }, { "cell_type": "markdown", "id": "1becca53",