RagasEvaluator

RagasEvaluator

LLM 기반 메트릭으로 Haystack 파이프라인을 평가하는 컴포넌트예요. 컨텍스트 관련성, 사실 정확성, 응답 관련성 등 여러 메트릭을 지원해요.

출처: 문서

본문

항목 내용
파이프라인에서 가장 흔한 위치 단독으로, 또는 평가 파이프라인에서 사용. 별도의 파이프라인이 Evaluator의 입력을 생성한 뒤에 사용해요
필수 init 변수 ragas_metrics: ragas.metrics.collections의 현대적인 Ragas 메트릭 목록. 각 메트릭은 생성 시점에 완전히 구성되어야 해요(LLM 포함)
필수 run 변수 평가하는 메트릭에 따라 입력이 달라지는데, query, response, documents, reference_contexts, multi_responses, reference, rubrics를 포함할 수 있어요
출력 변수 result: 메트릭 이름을 해당 MetricResult에 매핑하는 딕셔너리
API reference Ragas
GitHub 링크 ragas 통합
패키지 이름 ragas-haystack

Ragas는 다양한 LLM 기반 평가 메트릭을 제공하는 평가 프레임워크예요. RagasEvaluator 컴포넌트를 사용하면 RAG 생성 파이프라인 같은 Haystack 파이프라인을 Ragas가 제공하는 메트릭 중 하나로 평가할 수 있어요.

지원 메트릭

RagasEvaluator는 현대적인 Ragas 메트릭 API를 지원해요. ragas.metrics.collections(예: Faithfulness, AnswerRelevancy, ContextPrecision 등)의 어떤 메트릭이든 SimpleBaseMetric 인스턴스라면 전달할 수 있어요. 각 메트릭은 생성 시점에 완전히 구성되어야 해요(LLM과 임베딩 포함).

이 메트릭들에 대한 완전한 가이드는 Ragas 문서를 방문하세요.

파라미터 개요

RagasEvaluator를 초기화하려면 다음 파라미터를 제공해야 해요:

  • ragas_metrics: ragas.metrics.collections의 현대적인 Ragas 메트릭 목록. 각 메트릭은 생성 시점에 완전히 구성되어야 해요(LLM 포함).

사용법

RagasEvaluator를 사용하려면 먼저 통합 패키지를 설치해야 해요:

pip install ragas-haystack

RagasEvaluator를 사용하려면 다음 단계를 따라야 해요:

  • 사용할 메트릭을 완전히 구성해 RagasEvaluator를 초기화하세요.
  • RagasEvaluator를 단독으로 또는 파이프라인에서 실행하세요. 사용 중인 메트릭에 필요한 입력(예: query, documents, response 등)을 제공하면 돼요.

예제

답변 관련성(Answer Relevancy) 평가

답변 관련성 평가 파이프라인을 만들려면 이렇게 해요(이 예제가 동작하려면 OPENAI_API_KEY 환경 변수가 설정되어 있어야 합니다):

from haystack import Pipeline
from haystack_integrations.components.evaluators.ragas import RagasEvaluator
from openai import AsyncOpenAI
from ragas.llms import llm_factory
from ragas.embeddings import embedding_factory
from ragas.metrics.collections import AnswerRelevancy

client = AsyncOpenAI()
llm = llm_factory("gpt-4o-mini", client=client)
embeddings = embedding_factory("openai", model="text-embedding-3-small", client=client)

pipeline = Pipeline()
evaluator = RagasEvaluator(
    ragas_metrics=[AnswerRelevancy(llm=llm, embeddings=embeddings)],
)
pipeline.add_component("evaluator", evaluator)

평가 파이프라인을 실행하려면 메트릭에 필요한 expected inputs을 준비해 두어야 해요. 이 메트릭은 query와 response를 기대하며, 이 값들은 평가하려는 파이프라인의 결과에서 와야 해요.

results = pipeline.run(
    {
        "evaluator": {
            "query": "Where is the Pyramid of Giza?",
            "response": "The Pyramid of Giza is located in Egypt.",
        },
    },
)

컨텍스트 정밀도(Context Precision)와 충실도(Faithfulness) 평가

여러 메트릭을 한 번에 평가하는 파이프라인을 만들려면:

from haystack import Pipeline
from haystack_integrations.components.evaluators.ragas import RagasEvaluator
from openai import AsyncOpenAI
from ragas.llms import llm_factory
from ragas.metrics.collections import ContextPrecision, Faithfulness

client = AsyncOpenAI()
llm = llm_factory("gpt-4o-mini", client=client)

pipeline = Pipeline()
evaluator = RagasEvaluator(
    ragas_metrics=[ContextPrecision(llm=llm), Faithfulness(llm=llm)],
)
pipeline.add_component("evaluator", evaluator)

평가 파이프라인을 실행하려면 모든 메트릭이 요구하는 입력을 결합해 제공해야 해요.

results = pipeline.run(
    {
        "evaluator": {
            "query": "Which is the most popular global sport?",
            "documents": [
                "The popularity of sports can be measured in various ways, including TV viewership, social media presence, number of participants, and economic impact. Football is undoubtedly the world's most popular sport with major events like the FIFA World Cup and sports personalities like Ronaldo and Messi, drawing a followership of more than 4 billion people."
            ],
            "response": "Football is the most popular sport with around 4 billion followers worldwide",
            "reference": "Football is the most popular sport",
        },
    },
)

추가 참고 자료

더 알아보기 (Learn more)