From a950fa0487728281a1b6a6f84beb33cb7ae55bfa Mon Sep 17 00:00:00 2001 From: ChengZi Date: Tue, 23 Jan 2024 06:25:26 +0800 Subject: [PATCH] docs: add milvus multitenancy doc (#16177) - **Description:** add milvus multitenancy doc, it is an example for this [pr](https://github.com/langchain-ai/langchain/pull/15740) . - **Issue:** No, - **Dependencies:** No, - **Twitter handle:** No Signed-off-by: ChengZi --- .../integrations/vectorstores/milvus.ipynb | 116 +++++++++++++++++- .../question_answering/per_user.ipynb | 14 ++- 2 files changed, 128 insertions(+), 2 deletions(-) diff --git a/docs/docs/integrations/vectorstores/milvus.ipynb b/docs/docs/integrations/vectorstores/milvus.ipynb index 1d3a25f684..79d0df5c3d 100644 --- a/docs/docs/integrations/vectorstores/milvus.ipynb +++ b/docs/docs/integrations/vectorstores/milvus.ipynb @@ -201,6 +201,120 @@ "source": [ "After retreival you can go on querying it as usual." ] + }, + { + "cell_type": "markdown", + "source": [ + "### Per-User Retrieval\n", + "\n", + "When building a retrieval app, you often have to build it with multiple users in mind. This means that you may be storing data not just for one user, but for many different users, and they should not be able to see eachother’s data.\n", + "\n", + "Milvus recommends using [partition_key](https://milvus.io/docs/multi_tenancy.md#Partition-key-based-multi-tenancy) to implement multi-tenancy, here is an example." + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%% md\n" + } + } + }, + { + "cell_type": "code", + "execution_count": 2, + "outputs": [], + "source": [ + "from langchain_core.documents import Document\n", + "\n", + "docs = [\n", + " Document(page_content=\"i worked at kensho\", metadata={\"namespace\": \"harrison\"}),\n", + " Document(page_content=\"i worked at facebook\", metadata={\"namespace\": \"ankush\"}),\n", + "]\n", + "vectorstore = Milvus.from_documents(\n", + " docs,\n", + " embeddings,\n", + " connection_args={\"host\": \"127.0.0.1\", \"port\": \"19530\"},\n", + " drop_old=True,\n", + " partition_key_field=\"namespace\", # Use the \"namespace\" field as the partition key\n", + ")" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "markdown", + "source": [ + "To conduct a search using the partition key, you should include either of the following in the boolean expression of the search request:\n", + "\n", + "`search_kwargs={\"expr\": ' == \"xxxx\"'}`\n", + "\n", + "`search_kwargs={\"expr\": ' == in [\"xxx\", \"xxx\"]'}`\n", + "\n", + "Do replace `` with the name of the field that is designated as the partition key.\n", + "\n", + "Milvus changes to a partition based on the specified partition key, filters entities according to the partition key, and searches among the filtered entities.\n" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%% md\n" + } + } + }, + { + "cell_type": "code", + "execution_count": 3, + "outputs": [ + { + "data": { + "text/plain": "[Document(page_content='i worked at facebook', metadata={'namespace': 'ankush'})]" + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# This will only get documents for Ankush\n", + "vectorstore.as_retriever(\n", + " search_kwargs={\"expr\": 'namespace == \"ankush\"'}\n", + ").get_relevant_documents(\"where did i work?\")" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": 4, + "outputs": [ + { + "data": { + "text/plain": "[Document(page_content='i worked at kensho', metadata={'namespace': 'harrison'})]" + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# This will only get documents for Harrison\n", + "vectorstore.as_retriever(\n", + " search_kwargs={\"expr\": 'namespace == \"harrison\"'}\n", + ").get_relevant_documents(\"where did i work?\")" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } } ], "metadata": { @@ -224,4 +338,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} +} \ No newline at end of file diff --git a/docs/docs/use_cases/question_answering/per_user.ipynb b/docs/docs/use_cases/question_answering/per_user.ipynb index 3724d00675..d1459a7391 100644 --- a/docs/docs/use_cases/question_answering/per_user.ipynb +++ b/docs/docs/use_cases/question_answering/per_user.ipynb @@ -298,6 +298,18 @@ " config={\"configurable\": {\"search_kwargs\": {\"namespace\": \"ankush\"}}},\n", ")" ] + }, + { + "cell_type": "markdown", + "source": [ + "For more vectorstore implementations for multi-user, please refer to specific pages, such as [Milvus](/docs/integrations/vectorstores/milvus)." + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%% md\n" + } + } } ], "metadata": { @@ -321,4 +333,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} +} \ No newline at end of file