Search code examples
pythonlangchainchromadbvectorstore

Is Per-User Retrieval supports open-source vectorstore chromadb?


From the langchain documentation - Per-User Retrieval

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. This means that you need to be able to configure your retrieval chain to only retrieve certain information.

The documentation has an example implementation using PineconeVectorStore. Does chromadb support multiple users? If yes, can anyone help with an example of how the per-user retrieval can be implemented using the open source ChromaDB?


Solution

  • We can use filter let Chromadb support multiple users.

    from langchain_openai import OpenAIEmbeddings
    from langchain.vectorstores import Chroma
    
    persist_directory = 'your_db'
    
    embeddings = OpenAIEmbeddings()
    vectordb = Chroma(embedding_function=embeddings,
                      persist_directory=persist_directory)
    
    vectordb.add_texts(["i worked at kensho"], metadatas=[{"user": "harrison"}])
    vectordb.add_texts(["i worked at facebook"], metadatas=[{"user": "ankush"}])
    
    # This will only get documents for Ankush
    vectordb.as_retriever(search_kwargs={'filter': {'user':'ankush'}}).get_relevant_documents(
        "where did i work?"
    )
    
    [Document(page_content='i worked at facebook', metadata={'user': 'ankush'})]