BaseQAWithSourcesChain Class — langchain Architecture
Architecture documentation for the BaseQAWithSourcesChain class in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 916d1ca4_2df3_6007_0b8a_5943c3b4bd54["BaseQAWithSourcesChain"] 097a4781_5519_0b5d_6244_98c64eadc0d6["Chain"] 916d1ca4_2df3_6007_0b8a_5943c3b4bd54 -->|extends| 097a4781_5519_0b5d_6244_98c64eadc0d6 fdc86f9f_85df_84cf_83a4_a9e587fe40ec["base.py"] 916d1ca4_2df3_6007_0b8a_5943c3b4bd54 -->|defined in| fdc86f9f_85df_84cf_83a4_a9e587fe40ec 3557b460_a881_8b6e_04c6_e6375e302978["from_llm()"] 916d1ca4_2df3_6007_0b8a_5943c3b4bd54 -->|method| 3557b460_a881_8b6e_04c6_e6375e302978 4638149d_1d06_b222_a5b6_c92bab187030["from_chain_type()"] 916d1ca4_2df3_6007_0b8a_5943c3b4bd54 -->|method| 4638149d_1d06_b222_a5b6_c92bab187030 60253345_0524_01a8_20ad_82e3d60555c2["input_keys()"] 916d1ca4_2df3_6007_0b8a_5943c3b4bd54 -->|method| 60253345_0524_01a8_20ad_82e3d60555c2 41e380be_6c0e_496d_a372_cb481ff4a2ac["output_keys()"] 916d1ca4_2df3_6007_0b8a_5943c3b4bd54 -->|method| 41e380be_6c0e_496d_a372_cb481ff4a2ac 9c06df47_d1c2_a179_906f_6229e28720f3["validate_naming()"] 916d1ca4_2df3_6007_0b8a_5943c3b4bd54 -->|method| 9c06df47_d1c2_a179_906f_6229e28720f3 2aa33a84_9121_0a57_1fba_35beb1bb676b["_split_sources()"] 916d1ca4_2df3_6007_0b8a_5943c3b4bd54 -->|method| 2aa33a84_9121_0a57_1fba_35beb1bb676b 0a571725_c0af_8092_7e91_7f522a43efd1["_get_docs()"] 916d1ca4_2df3_6007_0b8a_5943c3b4bd54 -->|method| 0a571725_c0af_8092_7e91_7f522a43efd1 80dcc576_05d1_33a1_4182_ba9b68bcd9ce["_call()"] 916d1ca4_2df3_6007_0b8a_5943c3b4bd54 -->|method| 80dcc576_05d1_33a1_4182_ba9b68bcd9ce bcc00dbe_47c9_7eb1_2cd3_15702059ce34["_aget_docs()"] 916d1ca4_2df3_6007_0b8a_5943c3b4bd54 -->|method| bcc00dbe_47c9_7eb1_2cd3_15702059ce34 62e4e7f4_e493_f37a_5e6c_90b7c0cc140b["_acall()"] 916d1ca4_2df3_6007_0b8a_5943c3b4bd54 -->|method| 62e4e7f4_e493_f37a_5e6c_90b7c0cc140b
Relationship Graph
Source Code
libs/langchain/langchain_classic/chains/qa_with_sources/base.py lines 46–215
class BaseQAWithSourcesChain(Chain, ABC):
"""Question answering chain with sources over documents."""
combine_documents_chain: BaseCombineDocumentsChain
"""Chain to use to combine documents."""
question_key: str = "question"
input_docs_key: str = "docs"
answer_key: str = "answer"
sources_answer_key: str = "sources"
return_source_documents: bool = False
"""Return the source documents."""
@classmethod
def from_llm(
cls,
llm: BaseLanguageModel,
document_prompt: BasePromptTemplate = EXAMPLE_PROMPT,
question_prompt: BasePromptTemplate = QUESTION_PROMPT,
combine_prompt: BasePromptTemplate = COMBINE_PROMPT,
**kwargs: Any,
) -> BaseQAWithSourcesChain:
"""Construct the chain from an LLM."""
llm_question_chain = LLMChain(llm=llm, prompt=question_prompt)
llm_combine_chain = LLMChain(llm=llm, prompt=combine_prompt)
combine_results_chain = StuffDocumentsChain(
llm_chain=llm_combine_chain,
document_prompt=document_prompt,
document_variable_name="summaries",
)
reduce_documents_chain = ReduceDocumentsChain(
combine_documents_chain=combine_results_chain,
)
combine_documents_chain = MapReduceDocumentsChain(
llm_chain=llm_question_chain,
reduce_documents_chain=reduce_documents_chain,
document_variable_name="context",
)
return cls(
combine_documents_chain=combine_documents_chain,
**kwargs,
)
@classmethod
def from_chain_type(
cls,
llm: BaseLanguageModel,
chain_type: str = "stuff",
chain_type_kwargs: dict | None = None,
**kwargs: Any,
) -> BaseQAWithSourcesChain:
"""Load chain from chain type."""
_chain_kwargs = chain_type_kwargs or {}
combine_documents_chain = load_qa_with_sources_chain(
llm,
chain_type=chain_type,
**_chain_kwargs,
)
return cls(combine_documents_chain=combine_documents_chain, **kwargs)
model_config = ConfigDict(
arbitrary_types_allowed=True,
extra="forbid",
)
@property
def input_keys(self) -> list[str]:
"""Expect input key."""
return [self.question_key]
@property
def output_keys(self) -> list[str]:
"""Return output key."""
_output_keys = [self.answer_key, self.sources_answer_key]
if self.return_source_documents:
_output_keys = [*_output_keys, "source_documents"]
return _output_keys
@model_validator(mode="before")
@classmethod
def validate_naming(cls, values: dict) -> Any:
"""Fix backwards compatibility in naming."""
Extends
Source
Frequently Asked Questions
What is the BaseQAWithSourcesChain class?
BaseQAWithSourcesChain is a class in the langchain codebase, defined in libs/langchain/langchain_classic/chains/qa_with_sources/base.py.
Where is BaseQAWithSourcesChain defined?
BaseQAWithSourcesChain is defined in libs/langchain/langchain_classic/chains/qa_with_sources/base.py at line 46.
What does BaseQAWithSourcesChain extend?
BaseQAWithSourcesChain extends Chain.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free