Skip to content

Commit e8f8897

Browse files
authored
fix: add missing output sockes to LLM evaluators (#12049)
1 parent e6f6b39 commit e8f8897

7 files changed

Lines changed: 28 additions & 6 deletions

File tree

haystack/components/evaluators/context_relevance.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ def __init__(
162162
progress_bar=progress_bar,
163163
)
164164

165-
@component.output_types(score=float, results=list[dict[str, Any]])
165+
@component.output_types(
166+
score=float, individual_scores=list[float], results=list[dict[str, Any]], meta=list[dict[str, Any]] | None
167+
)
166168
def run(self, **inputs: Any) -> dict[str, Any]:
167169
"""
168170
Run the LLM evaluator.
@@ -174,14 +176,17 @@ def run(self, **inputs: Any) -> dict[str, Any]:
174176
:returns:
175177
A dictionary with the following outputs:
176178
- `score`: Mean context relevance score over all the provided input questions.
179+
- `individual_scores`: A list of context relevance scores for each input question.
177180
- `results`: A list of dictionaries with `relevant_statements`, `score`, and `status` for each input
178181
context. `status` is `evaluated` for valid results and `error` for failed evaluations.
179182
"""
180183
result = super(ContextRelevanceEvaluator, self).run(**inputs) # noqa: UP008
181184
# Post-process the raw results to calculate relevance metrics and scores
182185
return self._postprocess_results(result)
183186

184-
@component.output_types(score=float, results=list[dict[str, Any]])
187+
@component.output_types(
188+
score=float, individual_scores=list[float], results=list[dict[str, Any]], meta=list[dict[str, Any]] | None
189+
)
185190
async def run_async(self, **inputs: Any) -> dict[str, Any]:
186191
"""
187192
Run the LLM evaluator asynchronously.
@@ -193,6 +198,7 @@ async def run_async(self, **inputs: Any) -> dict[str, Any]:
193198
:returns:
194199
A dictionary with the following outputs:
195200
- `score`: Mean context relevance score over all the provided input questions.
201+
- `individual_scores`: A list of context relevance scores for each input question.
196202
- `results`: A list of dictionaries with `relevant_statements`, `score`, and `status` for each input
197203
context. `status` is `evaluated` for valid results and `error` for failed evaluations.
198204
"""

haystack/components/evaluators/faithfulness.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ def __init__(
150150
progress_bar=progress_bar,
151151
)
152152

153-
@component.output_types(individual_scores=list[float], score=float, results=list[dict[str, Any]])
153+
@component.output_types(
154+
individual_scores=list[float], score=float, results=list[dict[str, Any]], meta=list[dict[str, Any]] | None
155+
)
154156
def run(self, **inputs: Any) -> dict[str, Any]:
155157
"""
156158
Run the LLM evaluator.
@@ -172,7 +174,9 @@ def run(self, **inputs: Any) -> dict[str, Any]:
172174
# Post-process the raw results to calculate relevance metrics and scores
173175
return self._postprocess_results(result)
174176

175-
@component.output_types(individual_scores=list[float], score=float, results=list[dict[str, Any]])
177+
@component.output_types(
178+
individual_scores=list[float], score=float, results=list[dict[str, Any]], meta=list[dict[str, Any]] | None
179+
)
176180
async def run_async(self, **inputs: Any) -> dict[str, Any]:
177181
"""
178182
Run the LLM evaluator asynchronously.

haystack/components/evaluators/llm_evaluator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def validate_init_parameters(
198198
)
199199
raise ValueError(msg)
200200

201-
@component.output_types(results=list[dict[str, Any]])
201+
@component.output_types(results=list[dict[str, Any]], meta=list[dict[str, Any]] | None)
202202
def run(self, **inputs: Any) -> dict[str, Any]:
203203
"""
204204
Run the LLM evaluator.
@@ -262,7 +262,7 @@ def run(self, **inputs: Any) -> dict[str, Any]:
262262

263263
return {"results": results, "meta": metadata or None}
264264

265-
@component.output_types(results=list[dict[str, Any]])
265+
@component.output_types(results=list[dict[str, Any]], meta=list[dict[str, Any]] | None)
266266
async def run_async(self, **inputs: Any) -> dict[str, Any]:
267267
"""
268268
Run the LLM evaluator asynchronously
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
Declare the ``meta`` output of ``LLMEvaluator``, ``ContextRelevanceEvaluator``, and ``FaithfulnessEvaluator``, and
5+
the ``individual_scores`` output of ``ContextRelevanceEvaluator``. These outputs were already returned but not
6+
declared, so they could not be connected to other components in a Pipeline.

test/components/evaluators/test_context_relevance_evaluator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def test_init_default(self, monkeypatch):
5555
assert component._chat_generator.api_key.resolve_value() == "test-api-key"
5656
assert component._chat_generator.generation_kwargs == {"response_format": {"type": "json_object"}, "seed": 42}
5757

58+
assert set(component.__haystack_output__._sockets_dict) == {"score", "individual_scores", "results", "meta"}
59+
5860
def test_key_resolved_at_warm_up_not_init(self, monkeypatch):
5961
monkeypatch.delenv("OPENAI_API_KEY", raising=False)
6062
component = ContextRelevanceEvaluator()

test/components/evaluators/test_faithfulness_evaluator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ def test_init_default(self, monkeypatch):
7070
assert component._chat_generator.api_key.resolve_value() == "test-api-key"
7171
assert component._chat_generator.generation_kwargs == {"response_format": {"type": "json_object"}, "seed": 42}
7272

73+
assert set(component.__haystack_output__._sockets_dict) == {"score", "individual_scores", "results", "meta"}
74+
7375
def test_key_resolved_at_warm_up_not_init(self, monkeypatch):
7476
monkeypatch.delenv("OPENAI_API_KEY", raising=False)
7577
component = FaithfulnessEvaluator()

test/components/evaluators/test_llm_evaluator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def test_init_default(self, monkeypatch):
3333
assert isinstance(component._chat_generator, OpenAIChatGenerator)
3434
assert component._chat_generator.generation_kwargs == {"response_format": {"type": "json_object"}, "seed": 42}
3535

36+
assert set(component.__haystack_output__._sockets_dict) == {"results", "meta"}
37+
3638
def test_key_resolved_at_warm_up_not_init(self, monkeypatch):
3739
monkeypatch.delenv("OPENAI_API_KEY", raising=False)
3840
component = LLMEvaluator(

0 commit comments

Comments
 (0)