Skip to content

Commit

Permalink
Add class attribute "return_generated_question" to class "BaseConvers…
Browse files Browse the repository at this point in the history
…ationalRetrievalChain" (langchain-ai#5749)

<!--
Thank you for contributing to LangChain! Your PR will appear in our
release under the title you set. Please make sure it highlights your
valuable contribution.

Replace this with a description of the change, the issue it fixes (if
applicable), and relevant context. List any dependencies required for
this change.

After you're done, someone will review your PR. They may suggest
improvements. If no one reviews your PR within a few days, feel free to
@-mention the same people again, as notifications can get lost.

Finally, we'd love to show appreciation for your contribution - if you'd
like us to shout you out on Twitter, please also include your handle!
-->

Adding a class attribute "return_generated_question" to class
"BaseConversationalRetrievalChain". If set to `True`, the chain's output
has a key "generated_question" with the question generated by the
sub-chain `question_generator` as the value. This way the generated
question can be logged.

<!-- If you're adding a new integration, please include:

1. a test for the integration - favor unit tests that does not rely on
network access.
2. an example notebook showing its use


See contribution guidelines for more information on how to write tests,
lint
etc:


https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md
-->

#### Who can review?

<!-- For a quicker response, figure out the right person to tag with @

  @hwchase17 - project lead

  Tracing / Callbacks
  - @agola11

  Async
  - @agola11

  DataLoaders
  - @eyurtsev

  Models
  - @hwchase17
  - @agola11

  Agents / Tools / Toolkits
  - @vowelparrot

  VectorStores / Retrievers / Memory
  - @dev2049

 -->
@dev2049 @vowelparrot
  • Loading branch information
felpigeon authored and Undertone0809 committed Jun 19, 2023
1 parent 51dcfaf commit e6a7e26
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions langchain/chains/conversational_retrieval/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class BaseConversationalRetrievalChain(Chain):
question_generator: LLMChain
output_key: str = "answer"
return_source_documents: bool = False
return_generated_question: bool = False
get_chat_history: Optional[Callable[[CHAT_TURN_TYPE], str]] = None
"""Return the source documents."""

Expand All @@ -80,6 +81,8 @@ def output_keys(self) -> List[str]:
_output_keys = [self.output_key]
if self.return_source_documents:
_output_keys = _output_keys + ["source_documents"]
if self.return_generated_question:
_output_keys = _output_keys + ["generated_question"]
return _output_keys

@abstractmethod
Expand Down Expand Up @@ -110,10 +113,12 @@ def _call(
answer = self.combine_docs_chain.run(
input_documents=docs, callbacks=_run_manager.get_child(), **new_inputs
)
output: Dict[str, Any] = {self.output_key: answer}
if self.return_source_documents:
return {self.output_key: answer, "source_documents": docs}
else:
return {self.output_key: answer}
output["source_documents"] = docs
if self.return_generated_question:
output["generated_question"] = new_question
return output

@abstractmethod
async def _aget_docs(self, question: str, inputs: Dict[str, Any]) -> List[Document]:
Expand Down Expand Up @@ -142,10 +147,12 @@ async def _acall(
answer = await self.combine_docs_chain.arun(
input_documents=docs, callbacks=_run_manager.get_child(), **new_inputs
)
output: Dict[str, Any] = {self.output_key: answer}
if self.return_source_documents:
return {self.output_key: answer, "source_documents": docs}
else:
return {self.output_key: answer}
output["source_documents"] = docs
if self.return_generated_question:
output["generated_question"] = new_question
return output

def save(self, file_path: Union[Path, str]) -> None:
if self.get_chat_history:
Expand Down

0 comments on commit e6a7e26

Please sign in to comment.