RetrievalQAWithSourcesChain Class — langchain Architecture
Architecture documentation for the RetrievalQAWithSourcesChain class in retrieval.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 76f853c5_b279_fd04_f253_b0c960712f80["RetrievalQAWithSourcesChain"] 5aa1201d_23fd_f122_acb5_563cb1cc3cb3["BaseQAWithSourcesChain"] 76f853c5_b279_fd04_f253_b0c960712f80 -->|extends| 5aa1201d_23fd_f122_acb5_563cb1cc3cb3 2d0480d1_fbf6_c0df_d443_92c8437d1fff["StuffDocumentsChain"] 76f853c5_b279_fd04_f253_b0c960712f80 -->|extends| 2d0480d1_fbf6_c0df_d443_92c8437d1fff 8d6c7cf5_8096_6885_9bdd_23e177d76376["retrieval.py"] 76f853c5_b279_fd04_f253_b0c960712f80 -->|defined in| 8d6c7cf5_8096_6885_9bdd_23e177d76376 0afcca8a_ef8b_63ff_50cb_7330bdb61fee["_reduce_tokens_below_limit()"] 76f853c5_b279_fd04_f253_b0c960712f80 -->|method| 0afcca8a_ef8b_63ff_50cb_7330bdb61fee 857136aa_95ce_637d_eb84_ed2df24dae5c["_get_docs()"] 76f853c5_b279_fd04_f253_b0c960712f80 -->|method| 857136aa_95ce_637d_eb84_ed2df24dae5c a9a5b4e9_e1f3_681e_a5ee_75ece394ee79["_aget_docs()"] 76f853c5_b279_fd04_f253_b0c960712f80 -->|method| a9a5b4e9_e1f3_681e_a5ee_75ece394ee79 be02c27f_613c_e8b1_6857_b7fcec0ea228["_chain_type()"] 76f853c5_b279_fd04_f253_b0c960712f80 -->|method| be02c27f_613c_e8b1_6857_b7fcec0ea228
Relationship Graph
Source Code
libs/langchain/langchain_classic/chains/qa_with_sources/retrieval.py lines 17–75
class RetrievalQAWithSourcesChain(BaseQAWithSourcesChain):
"""Question-answering with sources over an index."""
retriever: BaseRetriever = Field(exclude=True)
"""Index to connect to."""
reduce_k_below_max_tokens: bool = False
"""Reduce the number of results to return from store based on tokens limit"""
max_tokens_limit: int = 3375
"""Restrict the docs to return from store based on tokens,
enforced only for StuffDocumentChain and if reduce_k_below_max_tokens is to true"""
def _reduce_tokens_below_limit(self, docs: list[Document]) -> list[Document]:
num_docs = len(docs)
if self.reduce_k_below_max_tokens and isinstance(
self.combine_documents_chain,
StuffDocumentsChain,
):
tokens = [
self.combine_documents_chain.llm_chain._get_num_tokens(doc.page_content) # noqa: SLF001
for doc in docs
]
token_count = sum(tokens[:num_docs])
while token_count > self.max_tokens_limit:
num_docs -= 1
token_count -= tokens[num_docs]
return docs[:num_docs]
def _get_docs(
self,
inputs: dict[str, Any],
*,
run_manager: CallbackManagerForChainRun,
) -> list[Document]:
question = inputs[self.question_key]
docs = self.retriever.invoke(
question,
config={"callbacks": run_manager.get_child()},
)
return self._reduce_tokens_below_limit(docs)
async def _aget_docs(
self,
inputs: dict[str, Any],
*,
run_manager: AsyncCallbackManagerForChainRun,
) -> list[Document]:
question = inputs[self.question_key]
docs = await self.retriever.ainvoke(
question,
config={"callbacks": run_manager.get_child()},
)
return self._reduce_tokens_below_limit(docs)
@property
def _chain_type(self) -> str:
"""Return the chain type."""
return "retrieval_qa_with_sources_chain"
Source
Frequently Asked Questions
What is the RetrievalQAWithSourcesChain class?
RetrievalQAWithSourcesChain is a class in the langchain codebase, defined in libs/langchain/langchain_classic/chains/qa_with_sources/retrieval.py.
Where is RetrievalQAWithSourcesChain defined?
RetrievalQAWithSourcesChain is defined in libs/langchain/langchain_classic/chains/qa_with_sources/retrieval.py at line 17.
What does RetrievalQAWithSourcesChain extend?
RetrievalQAWithSourcesChain extends BaseQAWithSourcesChain, StuffDocumentsChain.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free