Skip to content

Commit c59ba02

Browse files
authored
fix: docs for discrete, numeric and ranking using instructor (#2397)
1 parent 09d22fc commit c59ba02

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

src/ragas/metrics/discrete.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,43 @@ class DiscreteMetric(SimpleLLMMetric, DiscreteValidator):
2222
2323
This class is used for metrics that output categorical values like
2424
"pass/fail", "good/bad/excellent", or custom discrete categories.
25+
Uses the instructor library for structured LLM outputs.
2526
2627
Attributes
2728
----------
2829
allowed_values : List[str]
2930
List of allowed categorical values the metric can output.
3031
Default is ["pass", "fail"].
32+
llm : Optional[BaseRagasLLM]
33+
The language model instance for evaluation. Can be created using llm_factory().
34+
prompt : Optional[Union[str, Prompt]]
35+
The prompt template for the metric. Should contain placeholders for
36+
evaluation inputs that will be formatted at runtime.
3137
3238
Examples
3339
--------
3440
>>> from ragas.metrics import DiscreteMetric
35-
>>> from ragas.llms import LangchainLLMWrapper
36-
>>> from langchain_openai import ChatOpenAI
41+
>>> from ragas.llms import llm_factory
42+
>>> from openai import OpenAI
43+
>>>
44+
>>> # Create an LLM instance
45+
>>> client = OpenAI(api_key="your-api-key")
46+
>>> llm = llm_factory("gpt-4o-mini", client=client)
3747
>>>
3848
>>> # Create a custom discrete metric
39-
>>> llm = LangchainLLMWrapper(ChatOpenAI())
4049
>>> metric = DiscreteMetric(
4150
... name="quality_check",
4251
... llm=llm,
52+
... prompt="Check the quality of the response: {response}. Return 'excellent', 'good', or 'poor'.",
4353
... allowed_values=["excellent", "good", "poor"]
4454
... )
55+
>>>
56+
>>> # Score with the metric
57+
>>> result = metric.score(
58+
... llm=llm,
59+
... response="This is a great response!"
60+
... )
61+
>>> print(result.value) # Output: "excellent" or similar
4562
"""
4663

4764
allowed_values: t.List[str] = field(default_factory=lambda: ["pass", "fail"])

src/ragas/metrics/numeric.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,43 @@ class NumericMetric(SimpleLLMMetric, NumericValidator):
2020
2121
This class is used for metrics that output numeric scores within a
2222
defined range, such as 0.0 to 1.0 for similarity scores or 1-10 ratings.
23+
Uses the instructor library for structured LLM outputs.
2324
2425
Attributes
2526
----------
2627
allowed_values : Union[Tuple[float, float], range]
2728
The valid range for metric outputs. Can be a tuple of (min, max) floats
2829
or a range object. Default is (0.0, 1.0).
30+
llm : Optional[BaseRagasLLM]
31+
The language model instance for evaluation. Can be created using llm_factory().
32+
prompt : Optional[Union[str, Prompt]]
33+
The prompt template for the metric. Should contain placeholders for
34+
evaluation inputs that will be formatted at runtime.
2935
3036
Examples
3137
--------
3238
>>> from ragas.metrics import NumericMetric
33-
>>> from ragas.llms import LangchainLLMWrapper
34-
>>> from langchain_openai import ChatOpenAI
39+
>>> from ragas.llms import llm_factory
40+
>>> from openai import OpenAI
41+
>>>
42+
>>> # Create an LLM instance
43+
>>> client = OpenAI(api_key="your-api-key")
44+
>>> llm = llm_factory("gpt-4o-mini", client=client)
3545
>>>
3646
>>> # Create a custom numeric metric with 0-10 range
37-
>>> llm = LangchainLLMWrapper(ChatOpenAI())
3847
>>> metric = NumericMetric(
3948
... name="quality_score",
4049
... llm=llm,
50+
... prompt="Rate the quality of this response on a scale of 0-10: {response}",
4151
... allowed_values=(0.0, 10.0)
4252
... )
53+
>>>
54+
>>> # Score with the metric
55+
>>> result = metric.score(
56+
... llm=llm,
57+
... response="This is a great response!"
58+
... )
59+
>>> print(result.value) # Output: a float between 0.0 and 10.0
4360
"""
4461

4562
allowed_values: t.Union[t.Tuple[float, float], range] = (0.0, 1.0)

src/ragas/metrics/ranking.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,42 @@ class RankingMetric(SimpleLLMMetric, RankingValidator):
2222
2323
This class is used for metrics that output ordered lists, such as
2424
ranking search results, prioritizing features, or ordering responses
25-
by relevance.
25+
by relevance. Uses the instructor library for structured LLM outputs.
2626
2727
Attributes
2828
----------
2929
allowed_values : int
3030
Expected number of items in the ranking list. Default is 2.
31+
llm : Optional[BaseRagasLLM]
32+
The language model instance for evaluation. Can be created using llm_factory().
33+
prompt : Optional[Union[str, Prompt]]
34+
The prompt template for the metric. Should contain placeholders for
35+
evaluation inputs that will be formatted at runtime.
3136
3237
Examples
3338
--------
3439
>>> from ragas.metrics import RankingMetric
35-
>>> from ragas.llms import LangchainLLMWrapper
36-
>>> from langchain_openai import ChatOpenAI
40+
>>> from ragas.llms import llm_factory
41+
>>> from openai import OpenAI
42+
>>>
43+
>>> # Create an LLM instance
44+
>>> client = OpenAI(api_key="your-api-key")
45+
>>> llm = llm_factory("gpt-4o-mini", client=client)
3746
>>>
3847
>>> # Create a ranking metric that returns top 3 items
39-
>>> llm = LangchainLLMWrapper(ChatOpenAI())
4048
>>> metric = RankingMetric(
4149
... name="relevance_ranking",
4250
... llm=llm,
51+
... prompt="Rank these results by relevance: {results}",
4352
... allowed_values=3
4453
... )
54+
>>>
55+
>>> # Score with the metric
56+
>>> result = metric.score(
57+
... llm=llm,
58+
... results="result1, result2, result3"
59+
... )
60+
>>> print(result.value) # Output: a list of 3 ranked items
4561
"""
4662

4763
allowed_values: int = 2

0 commit comments

Comments
 (0)