LLMEvaluator
LLMEvaluator
LLM을 사용해 사용자가 정의한 지시문과 예시가 담긴 프롬프트를 바탕으로 입력을 평가하는 Evaluator예요. 단독으로 쓰거나 평가 파이프라인에서, Evaluator의 입력을 생성한 별도 파이프라인 뒤에 쓰면 돼요.
출처: LLMEvaluator
본문
개요
LLMEvaluator 컴포넌트는 사용자가 정의한 기준(aspect)에 따라 답변, 문서, 또는 Haystack 파이프라인의 다른 어떤 출력이든 평가할 수 있어요. 컴포넌트는 지시문, 예시, 기대 출력 이름을 하나의 프롬프트로 결합해요. 사용자 정의 모델 기반 평가 지표를 계산하기 위한 것이에요. 바로 사용 가능한 미리 정의된 모델 기반 평가기를 찾고 있다면, Haystack의 FaithfulnessEvaluator와 ContextRelevanceEvaluator 컴포넌트를 대신 살펴보세요.
파라미터
이 Evaluator의 기본 모델은 gpt-5-mini예요. 초기화 때 chat_generator 파라미터로 모델을 오버라이드할 수 있어요. JSON 객체를 반환하도록 설정된 Chat Generator 인스턴스여야 해요. 예를 들어 OpenAIChatGenerator를 쓸 때는 generation_kwargs에 {"response_format": {"type": "json_object"}}를 전달해야 해요.
OpenAI이 아닌 자체 Chat Generator로 Evaluator를 초기화하지 않는다면, 유효한 OpenAI API 키를 OPENAI_API_KEY 환경 변수로 설정해야 해요. 자세한 내용은 secret management 문서를 참고하세요.
LLMEvaluator는 초기화에 다음 파라미터를 받아요. 처음 네 개는 필수, 마지막 두 개는 기본값이 있어요.
instructions: 평가에 사용할 프롬프트 지시문. 예를 들어 LLM이 yes, no, 또는 점수로 답할 수 있는 입력에 대한 질문.inputs:LLMEvaluator가 기대하고 평가하는 입력. 이 입력들이 컴포넌트의 수신 연결을 결정해요. 각 입력은 입력 이름과 입력 타입의 튜플이에요. 입력 타입은 리스트여야 해요. 예:[("responses", List[str])].outputs: 출력 사전의 키에 해당하는 평가 결과의 출력 이름. 예:["score"].examples: 기대 입력·출력 형식에 맞는 few-shot 예시를 전달하는 파라미터. 이 예시들은 LLM에 보내는 프롬프트에 포함돼요. 예시는 프롬프트의 토큰 수를 늘려 각 요청 비용을 높여요. 한두 개 이상의 예시를 추가하면 더 많은 토큰을 쓰는 대신 평가 품질을 높이는 데 도움이 될 수 있어요.raise_on_failure: True(기본값)면 실패한 API 호출에 예외를 발생시켜요.progress_bar: 평가 중 진행 막대를 표시할지. 기본값은 True예요.
각 예시는 inputs와 outputs 키를 가진 사전이어야 해요.
inputs는 inputs 파라미터에서 선언한 입력 이름을 키로 가진 사전이에요.
outputs는 outputs 파라미터에서 선언한 이름을 키로 가진 사전이에요.
inputs=[("responses", list[str])]와 outputs=["score"]로 선언된 Evaluator의 기대 형식이에요.
[
{
"inputs": {"responses": "Damn, this is straight outta hell!!!"},
"outputs": {"score": 1},
},
{
"inputs": {"responses": "Football is the most popular sport."},
"outputs": {"score": 0},
},
]
사용법
단독 사용 예시예요. 생성된 응답을 LLMEvaluator 컴포넌트로 평가해요. 평가 기준은 지시문에 정의된 대로 응답이 아이들에게 문제가 되는지예요. LLMEvaluator는 각 입력 응답에 대해 이진 점수 하나를 반환하며, 두 응답 모두 문제가 없다는 결과가 나와요.
from typing import List
from haystack.components.evaluators import LLMEvaluator
llm_evaluator = LLMEvaluator(
instructions="Is this answer problematic for children?",
inputs=[("responses", List[str])],
outputs=["score"],
examples=[
{
"inputs": {"responses": "Damn, this is straight outta hell!!!"},
"outputs": {"score": 1},
},
{
"inputs": {"responses": "Football is the most popular sport."},
"outputs": {"score": 0},
},
],
)
responses = [
"Football is the most popular sport with around 4 billion followers worldwide",
"Python language was created by Guido van Rossum.",
]
results = llm_evaluator.run(responses=responses)
print(results)
# {'results': [{'score': 0}, {'score': 0}],
# 'meta': [{'model': 'gpt-5-mini-2025-08-07', 'index': 0, 'finish_reason': 'stop', 'usage': {...}},
# {'model': 'gpt-5-mini-2025-08-07', 'index': 0, 'finish_reason': 'stop', 'usage': {...}}]}
파이프라인 안에서:
아래는 파이프라인에서 LLMEvaluator로 응답을 평가하는 예시예요.
from typing import List
from haystack import Pipeline
from haystack.components.evaluators import LLMEvaluator
pipeline = Pipeline()
llm_evaluator = LLMEvaluator(
instructions="Is this answer problematic for children?",
inputs=[("responses", List[str])],
outputs=["score"],
examples=[
{
"inputs": {"responses": "Damn, this is straight outta hell!!!"},
"outputs": {"score": 1},
},
{
"inputs": {"responses": "Football is the most popular sport."},
"outputs": {"score": 0},
},
],
)
pipeline.add_component("llm_evaluator", llm_evaluator)
responses = [
"Football is the most popular sport with around 4 billion followers worldwide",
"Python language was created by Guido van Rossum.",
]
result = pipeline.run({"llm_evaluator": {"responses": responses}})
for evaluator in result:
print(result[evaluator]["results"])
# [{'score': 0}, {'score': 0}]