ContextQAEvalChain Class — langchain Architecture
Architecture documentation for the ContextQAEvalChain class in eval_chain.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 98ba7e83_a00c_fa91_104f_0171d31f0bae["ContextQAEvalChain"] 8d3a235d_a08f_2979_f52a_1772067dd1d3["LLMChain"] 98ba7e83_a00c_fa91_104f_0171d31f0bae -->|extends| 8d3a235d_a08f_2979_f52a_1772067dd1d3 42f35457_68a1_961e_1ac4_cbaa4a2b48b3["StringEvaluator"] 98ba7e83_a00c_fa91_104f_0171d31f0bae -->|extends| 42f35457_68a1_961e_1ac4_cbaa4a2b48b3 649622c5_b1b0_2ee7_22ee_c9c12162f9c3["LLMEvalChain"] 98ba7e83_a00c_fa91_104f_0171d31f0bae -->|extends| 649622c5_b1b0_2ee7_22ee_c9c12162f9c3 2b85c9f2_3b3e_8497_58e2_8cb5e7dceb45["eval_chain.py"] 98ba7e83_a00c_fa91_104f_0171d31f0bae -->|defined in| 2b85c9f2_3b3e_8497_58e2_8cb5e7dceb45 70264e7a_1d06_a9ca_c6d9_5b9d0ce4e2eb["is_lc_serializable()"] 98ba7e83_a00c_fa91_104f_0171d31f0bae -->|method| 70264e7a_1d06_a9ca_c6d9_5b9d0ce4e2eb 613a37ba_d29c_5138_8352_bc30eff16a35["requires_reference()"] 98ba7e83_a00c_fa91_104f_0171d31f0bae -->|method| 613a37ba_d29c_5138_8352_bc30eff16a35 0a4060e6_6be9_2cd4_6081_54e017078c76["requires_input()"] 98ba7e83_a00c_fa91_104f_0171d31f0bae -->|method| 0a4060e6_6be9_2cd4_6081_54e017078c76 436b0a31_65cb_16e6_1d44_514a65431cb1["_validate_input_vars()"] 98ba7e83_a00c_fa91_104f_0171d31f0bae -->|method| 436b0a31_65cb_16e6_1d44_514a65431cb1 2cdc6c6d_be57_c147_a1f9_d119200423c5["evaluation_name()"] 98ba7e83_a00c_fa91_104f_0171d31f0bae -->|method| 2cdc6c6d_be57_c147_a1f9_d119200423c5 aa5e25d3_e485_237f_6c90_25560ea49b04["from_llm()"] 98ba7e83_a00c_fa91_104f_0171d31f0bae -->|method| aa5e25d3_e485_237f_6c90_25560ea49b04 95a6349b_4530_577f_fb65_d05a4ff9af09["evaluate()"] 98ba7e83_a00c_fa91_104f_0171d31f0bae -->|method| 95a6349b_4530_577f_fb65_d05a4ff9af09 5644b8b0_b346_23c8_7838_3feb215668e1["_prepare_output()"] 98ba7e83_a00c_fa91_104f_0171d31f0bae -->|method| 5644b8b0_b346_23c8_7838_3feb215668e1 9893db4a_3d28_82ac_4cca_a5fce4bc54af["_evaluate_strings()"] 98ba7e83_a00c_fa91_104f_0171d31f0bae -->|method| 9893db4a_3d28_82ac_4cca_a5fce4bc54af 55d3d55b_b89f_ad0b_a30c_62590fb347e1["_aevaluate_strings()"] 98ba7e83_a00c_fa91_104f_0171d31f0bae -->|method| 55d3d55b_b89f_ad0b_a30c_62590fb347e1
Relationship Graph
Source Code
libs/langchain/langchain_classic/evaluation/qa/eval_chain.py lines 219–347
class ContextQAEvalChain(LLMChain, StringEvaluator, LLMEvalChain):
"""LLM Chain for evaluating QA w/o GT based on context."""
@classmethod
@override
def is_lc_serializable(cls) -> bool:
return False
@property
def requires_reference(self) -> bool:
"""Whether the chain requires a reference string."""
return True
@property
def requires_input(self) -> bool:
"""Whether the chain requires an input string."""
return True
model_config = ConfigDict(
extra="ignore",
)
@classmethod
def _validate_input_vars(cls, prompt: PromptTemplate) -> None:
expected_input_vars = {"query", "context", "result"}
if expected_input_vars != set(prompt.input_variables):
msg = (
f"Input variables should be {expected_input_vars}, "
f"but got {prompt.input_variables}"
)
raise ValueError(msg)
@property
@override
def evaluation_name(self) -> str:
return "Contextual Accuracy"
@classmethod
def from_llm(
cls,
llm: BaseLanguageModel,
prompt: PromptTemplate | None = None,
**kwargs: Any,
) -> ContextQAEvalChain:
"""Load QA Eval Chain from LLM.
Args:
llm: The base language model to use.
prompt: A prompt template containing the `input_variables`:
`'query'`, `'context'` and `'result'` that will be used as the prompt
for evaluation.
Defaults to `PROMPT`.
**kwargs: Additional keyword arguments.
Returns:
The loaded QA eval chain.
"""
prompt = prompt or CONTEXT_PROMPT
cls._validate_input_vars(prompt)
return cls(llm=llm, prompt=prompt, **kwargs)
def evaluate(
self,
examples: list[dict],
predictions: list[dict],
question_key: str = "query",
context_key: str = "context",
prediction_key: str = "result",
*,
callbacks: Callbacks = None,
) -> list[dict]:
"""Evaluate question answering examples and predictions."""
inputs = [
{
"query": example[question_key],
"context": example[context_key],
"result": predictions[i][prediction_key],
}
for i, example in enumerate(examples)
]
Source
Frequently Asked Questions
What is the ContextQAEvalChain class?
ContextQAEvalChain is a class in the langchain codebase, defined in libs/langchain/langchain_classic/evaluation/qa/eval_chain.py.
Where is ContextQAEvalChain defined?
ContextQAEvalChain is defined in libs/langchain/langchain_classic/evaluation/qa/eval_chain.py at line 219.
What does ContextQAEvalChain extend?
ContextQAEvalChain extends LLMChain, StringEvaluator, LLMEvalChain.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free