Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: Passing Context to Conversational Chain #7936

Closed
rambabusure opened this issue Jul 19, 2023 · 4 comments
Closed

DOC: Passing Context to Conversational Chain #7936

rambabusure opened this issue Jul 19, 2023 · 4 comments
Labels
Ɑ: doc loader Related to document loader module (not documentation) 🤖:docs Changes to documentation and examples, like .md, .rst, .ipynb files. Changes to the docs/ folder

Comments

@rambabusure
Copy link

Issue with current documentation:

We don't have enough documentation Conversational chain and found only documentation relates conversational retrieval chain. We are looking for separating the retriever functionality. Please provide some examples on passing context (lang chain documents ) to conversational chain

Idea or request for content:

No response

@dosubot dosubot bot added Ɑ: doc loader Related to document loader module (not documentation) 🤖:docs Changes to documentation and examples, like .md, .rst, .ipynb files. Changes to the docs/ folder labels Jul 19, 2023
@dosubot
Copy link

dosubot bot commented Jul 19, 2023

Answer generated by a 🤖

Answer

I understand that you're seeking more detailed documentation on how to pass context (LangChain documents) to the ConversationalRetrievalChain in LangChain. You're also interested in separating the retriever functionality. Let me explain how you can achieve this.

In LangChain, the ConversationalRetrievalChain class is designed to manage conversations based on retrieved documents. It takes a chat history and a new question as inputs, and generates an answer. This process involves creating a standalone question from the chat history and the new question, retrieving relevant documents based on this standalone question, and then passing these documents to a language model to generate a final response.

To pass context to the ConversationalRetrievalChain, you can use the combine_docs_chain parameter when initializing the chain. This parameter should be an instance of a chain that combines documents, such as the StuffDocumentsChain.

Here's an example of how you can do this:

from langchain.chains import (
    StuffDocumentsChain, LLMChain, ConversationalRetrievalChain
)
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI

combine_docs_chain = StuffDocumentsChain(...)
vectorstore = ...
retriever = vectorstore.as_retriever()

template = (
    "Combine the chat history and follow up question into "
    "a standalone question. Chat History: {chat_history}"
    "Follow up question: {question}"
)
prompt = PromptTemplate.from_template(template)
llm = OpenAI()
question_generator_chain = LLMChain(llm=llm, prompt=prompt)

chain = ConversationalRetrievalChain(
    combine_docs_chain=combine_docs_chain,
    retriever=retriever,
    question_generator=question_generator_chain,
)

In this example, the combine_docs_chain is used to combine the chat history and the follow-up question into a standalone question. This standalone question is then passed to the retriever to fetch relevant documents. The retrieved documents are then passed to the question_generator_chain (an instance of LLMChain) to generate a final response.

Please note that this is one potential way to pass context to the ConversationalRetrievalChain and separate the retriever functionality. There might be other ways to achieve this depending on your specific use case.

If you find this solution helpful and believe it's not covered by the existing documentation, I encourage you to make a pull request to update the documentation. This will be beneficial for other users who might encounter the same issue.

Sources


This response is meant to be useful, save you time, and share context. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

@guillaumedelande
Copy link
Contributor

From the docs, I understand it's the opposite than what is being explained above in the text (code is correct though):

  • combine_docs_chain: "The chain used to combine any retrieved documents"
  • question_generator: "The chain used to generate a new question for the sake of retrieval. This chain will take in the current question (with variable question) and any chat history (with variable chat_history) and will produce a new standalone question to be used later on."

So the question_generator creates a standalone question, that is passed to the retriever, and then the combine_docs_chain is used to combine the returned docs/chunks using one of the methods (stuff, rerank, map_reduce,...)

Regarding the original question, it is indeed not always clear what's happening in the background. You can still find some details about the chain(s) yourself by passing qa.__dict__ and looking at how it was initialized (assuming qa is the name of the ConversationalRetrievalChain object).

source: https://api.python.langchain.com/en/latest/chains/langchain.chains.conversational_retrieval.base.BaseConversationalRetrievalChain.html?highlight=combine_docs_chain#langchain.chains.conversational_retrieval.base.BaseConversationalRetrievalChain.combine_docs_chain

Copy link

dosubot bot commented Nov 6, 2023

Hi, @rambabusure! I'm Dosu, and I'm helping the LangChain team manage our backlog. I wanted to let you know that we are marking this issue as stale.

From what I understand, you were requesting more documentation on passing context to the conversational chain in order to separate the retriever functionality. I provided a detailed explanation on how to pass context using the combine_docs_chain parameter when initializing the chain. However, guillaumedelande pointed out that the explanation provided by me is opposite to what is mentioned in the documentation. Guillaumedelande clarified that the question_generator creates a standalone question that is passed to the retriever, and then the combine_docs_chain is used to combine the returned documents.

Before we close this issue, we wanted to check if it is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on the issue. Otherwise, feel free to close the issue yourself or it will be automatically closed in 7 days.

Thank you for your contribution to the LangChain repository!

@dosubot dosubot bot added the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Nov 6, 2023
@dosubot dosubot bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 13, 2023
@dosubot dosubot bot removed the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Nov 13, 2023
@maximiliancw
Copy link

Hi all,

to me, this is still unclear. Mostly, because the example provided by @dosubot and the docstrings seems to be misleading about to things:

  1. The control flow should be different, as explained by @guillaumedelande
  2. The combine_docs_chain = StuffDocumentsChain(**...**) is very confusing. StuffDocumentsChain.__init__ expects the llm_chain to be set, but if I adjust the control flow and pass question_generator_chain to StuffDocumentsChain.__init__, the following error is being raised:
pydantic.v1.error_wrappers.ValidationError: 1 validation error for StuffDocumentsChain
__root__
  document_variable_name must be provided if there are multiple llm_chain_variables (type=value_error)

I feel like the docstrings should provide a plug-and-play example. That would be great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ɑ: doc loader Related to document loader module (not documentation) 🤖:docs Changes to documentation and examples, like .md, .rst, .ipynb files. Changes to the docs/ folder
Projects
None yet
Development

No branches or pull requests

3 participants