Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Explodinggradients Ragas Collections ContextPrecision Metric

From Leeroopedia
Revision as of 14:53, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Explodinggradients_Ragas_Collections_ContextPrecision_Metric.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Field Value
source Repo
domains Metrics, Evaluation
last_updated 2026-02-10 00:00 GMT

Overview

The ContextPrecision module provides four v2 class-based metrics -- ContextPrecisionWithReference, ContextPrecisionWithoutReference, ContextPrecision, and ContextUtilization -- that evaluate whether retrieved contexts are useful for answering a question using LLM-based average precision scoring.

Description

All classes extend BaseMetric and require a modern InstructorBaseRagasLLM for evaluation.

ContextPrecisionWithReference evaluates each retrieved context against a reference answer. For each context, it uses a structured prompt (ContextPrecisionPrompt) to ask the LLM whether the context is useful for producing the reference answer, yielding a binary verdict (0 or 1). The final score is computed as average precision across all verdicts, rewarding retrieval systems that rank useful contexts higher.

ContextPrecisionWithoutReference uses the same approach but compares contexts against the generated response instead of a reference answer.

ContextPrecision is a convenience alias for ContextPrecisionWithReference with the default name "context_precision".

ContextUtilization is a convenience alias for ContextPrecisionWithoutReference with the default name "context_utilization".

The average precision algorithm accumulates a cumulative sum of verdicts and computes precision at each relevant position, then divides by the total number of relevant contexts.

Usage

Instantiate with a required llm parameter. Use the WithReference variant when you have ground truth answers, and the WithoutReference variant when only the generated response is available.

Code Reference

Property Value
Source Location src/ragas/metrics/collections/context_precision/metric.py L1--330
Signatures class ContextPrecisionWithReference(BaseMetric), class ContextPrecisionWithoutReference(BaseMetric), class ContextPrecision(ContextPrecisionWithReference), class ContextUtilization(ContextPrecisionWithoutReference)
Import from ragas.metrics.collections import ContextPrecision

I/O Contract

Inputs (ContextPrecisionWithReference / ContextPrecision)

Parameter Type Required Description
user_input str Yes The question being asked
reference str Yes The reference / ground truth answer
retrieved_contexts List[str] Yes Retrieved contexts to evaluate

Inputs (ContextPrecisionWithoutReference / ContextUtilization)

Parameter Type Required Description
user_input str Yes The question being asked
response str Yes The generated response
retrieved_contexts List[str] Yes Retrieved contexts to evaluate

Constructor Parameters

Parameter Type Default Description
llm InstructorBaseRagasLLM (required) Modern instructor-based LLM for context evaluation
name str varies by class Metric name

Outputs

Field Type Description
MetricResult.value float Average precision score in range 0.0--1.0 (higher is better)

Usage Examples

import openai
from ragas.llms.base import llm_factory
from ragas.metrics.collections import ContextPrecision

# Setup
client = openai.AsyncOpenAI()
llm = llm_factory("gpt-4o-mini", client=client)

# ContextPrecision (with reference)
metric = ContextPrecision(llm=llm)
result = await metric.ascore(
    user_input="What is the capital of France?",
    reference="Paris is the capital of France.",
    retrieved_contexts=[
        "Paris is the capital and largest city of France.",
        "Berlin is the capital of Germany.",
        "France is a country in Western Europe.",
    ]
)
print(f"Context Precision: {result.value}")

# ContextUtilization (without reference)
from ragas.metrics.collections.context_precision.metric import ContextUtilization

util_metric = ContextUtilization(llm=llm)
result = await util_metric.ascore(
    user_input="What is the capital of France?",
    response="Paris is the capital of France.",
    retrieved_contexts=["Paris is the capital of France.", "Unrelated context."]
)
print(f"Context Utilization: {result.value}")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment