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

Add option to RetrievalQA chains to use provided documents in call() and acall() methods #11887

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions libs/langchain/langchain/chains/retrieval_qa/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class BaseRetrievalQA(Chain):
"""Chain to use to combine the documents."""
input_key: str = "query" #: :meta private:
output_key: str = "result" #: :meta private:
documents_key: str = "from_documents" #: :meta private:
return_source_documents: bool = False
"""Return the source documents or not."""

Expand Down Expand Up @@ -121,6 +122,9 @@ def _call(
If chain has 'return_source_documents' as 'True', returns
the retrieved documents as well under the key 'source_documents'.

If 'from_documents' is passed in the inputs, chain will use those
instead of calling _get_docs().

Example:
.. code-block:: python

Expand All @@ -132,7 +136,13 @@ def _call(
accepts_run_manager = (
"run_manager" in inspect.signature(self._get_docs).parameters
)
if accepts_run_manager:
from_documents = inputs.get(self.documents_key)
if from_documents:
docs = from_documents
for doc in docs:
if not isinstance(doc, Document):
raise TypeError(f"{doc} is not a Document")
elif accepts_run_manager:
docs = self._get_docs(question, run_manager=_run_manager)
else:
docs = self._get_docs(question) # type: ignore[call-arg]
Expand Down Expand Up @@ -164,6 +174,9 @@ async def _acall(
If chain has 'return_source_documents' as 'True', returns
the retrieved documents as well under the key 'source_documents'.

If 'from_documents' is passed in the inputs, chain will use those
instead of calling _get_docs().

Example:
.. code-block:: python

Expand All @@ -175,7 +188,13 @@ async def _acall(
accepts_run_manager = (
"run_manager" in inspect.signature(self._aget_docs).parameters
)
if accepts_run_manager:
from_documents = inputs.get(self.documents_key)
if from_documents:
docs = from_documents
for doc in docs:
if not isinstance(doc, Document):
raise TypeError(f"{doc} is not a Document")
elif accepts_run_manager:
docs = await self._aget_docs(question, run_manager=_run_manager)
else:
docs = await self._aget_docs(question) # type: ignore[call-arg]
Expand Down
Loading