Skip to content

Commit 37f4da0

Browse files
committed
Migrate wrapper prompts
1 parent 33e075c commit 37f4da0

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

src/ragas/metrics/collections/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
from ragas.metrics.collections._bleu_score import BleuScore
1616
from ragas.metrics.collections._context_entity_recall import ContextEntityRecall
1717
from ragas.metrics.collections._context_precision import (
18+
ContextPrecision,
1819
ContextPrecisionWithoutReference,
1920
ContextPrecisionWithReference,
21+
ContextUtilization,
2022
)
2123
from ragas.metrics.collections._faithfulness import Faithfulness
2224
from ragas.metrics.collections._noise_sensitivity import NoiseSensitivity
@@ -41,9 +43,11 @@
4143
"AspectCritic",
4244
"BleuScore",
4345
"ContextEntityRecall",
46+
"ContextPrecision",
4447
"ContextPrecisionWithReference",
4548
"ContextPrecisionWithoutReference",
4649
"ContextRelevance",
50+
"ContextUtilization",
4751
"DistanceMeasure",
4852
"ExactMatch",
4953
"Faithfulness",

src/ragas/metrics/collections/_context_precision.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,75 @@ def _calculate_average_precision(self, verdicts: List[int]) -> float:
256256
)
257257

258258
return score
259+
260+
261+
class ContextPrecision(ContextPrecisionWithReference):
262+
"""
263+
Modern v2 wrapper for ContextPrecisionWithReference with shorter name.
264+
265+
This is a simple wrapper that provides the legacy "context_precision" name
266+
while using the modern V2 implementation underneath.
267+
268+
Usage:
269+
>>> import openai
270+
>>> from ragas.llms.base import llm_factory
271+
>>> from ragas.metrics.collections import ContextPrecision
272+
>>>
273+
>>> # Setup dependencies
274+
>>> client = openai.AsyncOpenAI()
275+
>>> llm = llm_factory("gpt-4o-mini", client=client)
276+
>>>
277+
>>> # Create metric instance (same as ContextPrecisionWithReference)
278+
>>> metric = ContextPrecision(llm=llm)
279+
>>>
280+
>>> # Single evaluation
281+
>>> result = await metric.ascore(
282+
... user_input="What is the capital of France?",
283+
... reference="Paris is the capital of France.",
284+
... retrieved_contexts=["Paris is the capital and largest city of France."]
285+
... )
286+
"""
287+
288+
def __init__(
289+
self,
290+
llm: "InstructorBaseRagasLLM",
291+
**kwargs,
292+
):
293+
"""Initialize ContextPrecision with the legacy default name."""
294+
super().__init__(llm, name="context_precision", **kwargs)
295+
296+
297+
class ContextUtilization(ContextPrecisionWithoutReference):
298+
"""
299+
Modern v2 wrapper for ContextPrecisionWithoutReference with shorter name.
300+
301+
This is a simple wrapper that provides the legacy "context_utilization" name
302+
while using the modern V2 implementation underneath.
303+
304+
Usage:
305+
>>> import openai
306+
>>> from ragas.llms.base import llm_factory
307+
>>> from ragas.metrics.collections import ContextUtilization
308+
>>>
309+
>>> # Setup dependencies
310+
>>> client = openai.AsyncOpenAI()
311+
>>> llm = llm_factory("gpt-4o-mini", client=client)
312+
>>>
313+
>>> # Create metric instance (same as ContextPrecisionWithoutReference)
314+
>>> metric = ContextUtilization(llm=llm)
315+
>>>
316+
>>> # Single evaluation
317+
>>> result = await metric.ascore(
318+
... user_input="What is the capital of France?",
319+
... response="Paris is the capital of France.",
320+
... retrieved_contexts=["Paris is the capital and largest city of France."]
321+
... )
322+
"""
323+
324+
def __init__(
325+
self,
326+
llm: "InstructorBaseRagasLLM",
327+
**kwargs,
328+
):
329+
"""Initialize ContextUtilization with the legacy default name."""
330+
super().__init__(llm, name="context_utilization", **kwargs)

tests/e2e/metrics_migration/test_context_precision_migration.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
LLMContextPrecisionWithReference as LegacyContextPrecisionWithReference,
99
)
1010
from ragas.metrics.collections import (
11+
ContextPrecision,
1112
ContextPrecisionWithoutReference,
1213
ContextPrecisionWithReference,
14+
ContextUtilization,
1315
)
1416

1517

@@ -255,3 +257,65 @@ async def test_context_precision_edge_cases(self, test_modern_llm):
255257
retrieved_contexts=["In mathematics, 2+2 equals 4."],
256258
)
257259
assert 0.0 <= result.value <= 1.0
260+
261+
@pytest.mark.asyncio
262+
async def test_context_precision_wrappers(self, test_modern_llm):
263+
"""Test that the wrapper classes work identically to their base classes."""
264+
265+
if test_modern_llm is None:
266+
pytest.skip("Modern LLM required for wrapper testing")
267+
268+
test_data = {
269+
"user_input": "What is the capital of France?",
270+
"reference": "Paris is the capital of France.",
271+
"response": "Paris is the capital of France.",
272+
"retrieved_contexts": ["Paris is the capital and largest city of France."],
273+
}
274+
275+
# Test ContextPrecision wrapper vs ContextPrecisionWithReference
276+
wrapper = ContextPrecision(llm=test_modern_llm)
277+
base = ContextPrecisionWithReference(llm=test_modern_llm)
278+
279+
wrapper_result = await wrapper.ascore(
280+
user_input=test_data["user_input"],
281+
reference=test_data["reference"],
282+
retrieved_contexts=test_data["retrieved_contexts"],
283+
)
284+
285+
base_result = await base.ascore(
286+
user_input=test_data["user_input"],
287+
reference=test_data["reference"],
288+
retrieved_contexts=test_data["retrieved_contexts"],
289+
)
290+
291+
# Should have the correct names
292+
assert wrapper.name == "context_precision"
293+
assert base.name == "context_precision_with_reference"
294+
295+
# Should produce identical scores
296+
assert wrapper_result.value == base_result.value
297+
print(f"✅ ContextPrecision wrapper works correctly: {wrapper_result.value}")
298+
299+
# Test ContextUtilization wrapper vs ContextPrecisionWithoutReference
300+
wrapper2 = ContextUtilization(llm=test_modern_llm)
301+
base2 = ContextPrecisionWithoutReference(llm=test_modern_llm)
302+
303+
wrapper2_result = await wrapper2.ascore(
304+
user_input=test_data["user_input"],
305+
response=test_data["response"],
306+
retrieved_contexts=test_data["retrieved_contexts"],
307+
)
308+
309+
base2_result = await base2.ascore(
310+
user_input=test_data["user_input"],
311+
response=test_data["response"],
312+
retrieved_contexts=test_data["retrieved_contexts"],
313+
)
314+
315+
# Should have the correct names
316+
assert wrapper2.name == "context_utilization"
317+
assert base2.name == "context_precision_without_reference"
318+
319+
# Should produce identical scores
320+
assert wrapper2_result.value == base2_result.value
321+
print(f"✅ ContextUtilization wrapper works correctly: {wrapper2_result.value}")

0 commit comments

Comments
 (0)