*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to do per-user retrieval

On this page

How to do per-user retrieval
============================

This guide demonstrates how to configure runtime properties of a retrieval chain. An example application is to limit the documents available to a retriever based on the user.

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. This generally involves two steps.

**Step 1: Make sure the retriever you are using supports multiple users**

At the moment, there is no unified flag or filter for this in LangChain. Rather, each vectorstore and retriever may have their own, and may be called different things (namespaces, multi-tenancy, etc). For vectorstores, this is generally exposed as a keyword argument that is passed in during `similarity_search`. By reading the documentation or source code, figure out whether the retriever you are using supports multiple users, and, if so, how to use it.

Note: adding documentation and/or support for multiple users for retrievers that do not support it (or document it) is a GREAT way to contribute to LangChain

**Step 2: Add that parameter as a configurable field for the chain**

This will let you easily call the chain and configure any relevant flags at runtime. See [this documentation](/v0.2/docs/how_to/configure/) for more information on configuration.

Now, at runtime you can call this chain with configurable field.

Code Example[​](#code-example "Direct link to Code Example")
------------------------------------------------------------

Let's see a concrete example of what this looks like in code. We will use Pinecone for this example.

To configure Pinecone, set the following environment variable:

*   `PINECONE_API_KEY`: Your Pinecone API key

    from langchain_openai import OpenAIEmbeddingsfrom langchain_pinecone import PineconeVectorStoreembeddings = OpenAIEmbeddings()vectorstore = PineconeVectorStore(index_name="test-example", embedding=embeddings)vectorstore.add_texts(["i worked at kensho"], namespace="harrison")vectorstore.add_texts(["i worked at facebook"], namespace="ankush")

**API Reference:**[OpenAIEmbeddings](https://api.python.langchain.com/en/latest/embeddings/langchain_openai.embeddings.base.OpenAIEmbeddings.html) | [PineconeVectorStore](https://api.python.langchain.com/en/latest/vectorstores/langchain_pinecone.vectorstores.PineconeVectorStore.html)

    ['ce15571e-4e2f-44c9-98df-7e83f6f63095']

The pinecone kwarg for `namespace` can be used to separate documents

    # This will only get documents for Ankushvectorstore.as_retriever(search_kwargs={"namespace": "ankush"}).get_relevant_documents(    "where did i work?")

    [Document(page_content='i worked at facebook')]

    # This will only get documents for Harrisonvectorstore.as_retriever(    search_kwargs={"namespace": "harrison"}).get_relevant_documents("where did i work?")

    [Document(page_content='i worked at kensho')]

We can now create the chain that we will use to do question-answering over.

Let's first select a LLM. import ChatModelTabs from "@theme/ChatModelTabs";

This is basic question-answering chain set up.

    from langchain_core.output_parsers import StrOutputParserfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.runnables import (    ConfigurableField,    RunnablePassthrough,)template = """Answer the question based only on the following context:{context}Question: {question}"""prompt = ChatPromptTemplate.from_template(template)retriever = vectorstore.as_retriever()

**API Reference:**[StrOutputParser](https://api.python.langchain.com/en/latest/output_parsers/langchain_core.output_parsers.string.StrOutputParser.html) | [ChatPromptTemplate](https://api.python.langchain.com/en/latest/prompts/langchain_core.prompts.chat.ChatPromptTemplate.html) | [ConfigurableField](https://api.python.langchain.com/en/latest/runnables/langchain_core.runnables.utils.ConfigurableField.html) | [RunnablePassthrough](https://api.python.langchain.com/en/latest/runnables/langchain_core.runnables.passthrough.RunnablePassthrough.html)

Here we mark the retriever as having a configurable field. All vectorstore retrievers have `search_kwargs` as a field. This is just a dictionary, with vectorstore specific fields.

This will let us pass in a value for `search_kwargs` when invoking the chain.

    configurable_retriever = retriever.configurable_fields(    search_kwargs=ConfigurableField(        id="search_kwargs",        name="Search Kwargs",        description="The search kwargs to use",    ))

We can now create the chain using our configurable retriever

    chain = (    {"context": configurable_retriever, "question": RunnablePassthrough()}    | prompt    | llm    | StrOutputParser())

We can now invoke the chain with configurable options. `search_kwargs` is the id of the configurable field. The value is the search kwargs to use for Pinecone

    chain.invoke(    "where did the user work?",    config={"configurable": {"search_kwargs": {"namespace": "harrison"}}},)

    'The user worked at Kensho.'

    chain.invoke(    "where did the user work?",    config={"configurable": {"search_kwargs": {"namespace": "ankush"}}},)

    'The user worked at Facebook.'

For more vectorstore implementations for multi-user, please refer to specific pages, such as [Milvus](/v0.2/docs/integrations/vectorstores/milvus/).

[Edit this page](https://github.com/langchain-ai/langchain/edit/master/docs/docs/how_to/qa_per_user.ipynb)

* * *

#### Was this page helpful?

  

#### You can also leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchain/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

How to get a RAG application to add citations

](/v0.2/docs/how_to/qa_citations/)[

Next

How to get your RAG application to return sources

](/v0.2/docs/how_to/qa_sources/)

*   [Code Example](#code-example)