From 695e7027e6bea69e1b90f7d7887e283cbf10380d Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Wed, 5 Jul 2023 20:51:25 -0400 Subject: [PATCH] Harrison/parameter (#7081) add parameter to use original question or not --------- Co-authored-by: Dev 2049 Co-authored-by: Bagatur --- .../chains/conversational_retrieval/base.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/langchain/chains/conversational_retrieval/base.py b/langchain/chains/conversational_retrieval/base.py index 2c866c4353..aa6101b010 100644 --- a/langchain/chains/conversational_retrieval/base.py +++ b/langchain/chains/conversational_retrieval/base.py @@ -63,6 +63,11 @@ class BaseConversationalRetrievalChain(Chain): a new standalone question to be used later on.""" output_key: str = "answer" """The output key to return the final answer of this chain in.""" + rephrase_question: bool = True + """Whether or not to pass the new generated question to the combine_docs_chain. + If True, will pass the new generated question along. + If False, will only use the new generated question for retrieval and pass the + original question along to the combine_docs_chain.""" return_source_documents: bool = False """Return the retrieved source documents as part of the final result.""" return_generated_question: bool = False @@ -131,7 +136,8 @@ class BaseConversationalRetrievalChain(Chain): else: docs = self._get_docs(new_question, inputs) # type: ignore[call-arg] new_inputs = inputs.copy() - new_inputs["question"] = new_question + if self.rephrase_question: + new_inputs["question"] = new_question new_inputs["chat_history"] = chat_history_str answer = self.combine_docs_chain.run( input_documents=docs, callbacks=_run_manager.get_child(), **new_inputs @@ -178,7 +184,8 @@ class BaseConversationalRetrievalChain(Chain): docs = await self._aget_docs(new_question, inputs) # type: ignore[call-arg] new_inputs = inputs.copy() - new_inputs["question"] = new_question + if self.rephrase_question: + new_inputs["question"] = new_question new_inputs["chat_history"] = chat_history_str answer = await self.combine_docs_chain.arun( input_documents=docs, callbacks=_run_manager.get_child(), **new_inputs @@ -212,8 +219,9 @@ class ConversationalRetrievalChain(BaseConversationalRetrievalChain): 2. This new question is passed to the retriever and relevant documents are returned. - 3. The retrieved documents are passed to an LLM along with the new question to - generate a final answer. + 3. The retrieved documents are passed to an LLM along with either the new question + (default behavior) or the original question and chat history to generate a final + response. Example: .. code-block:: python