컨텍스트 엔티티 재현율(Context Entities Recall)

컨텍스트 엔티티 재현율(Context Entities Recall)

컨텍스트 엔티티 재현율은 검색된 컨텍스트의 재현율을 측정하는 지표예요. referenceretrieved_contexts 양쪽에 존재하는 엔티티 수를 reference에만 존재하는 엔티티 수와 비교해 계산해요. 쉽게 말해 reference에서 얼마나 많은 엔티티가 재현됐는지를 측정하는 거예요. 엔티티가 중요한 사실 기반 사용 사례(관광 헬프데스크, 역사 QA 등)에 유용해요.

출처: 문서

본문

컨텍스트 엔티티 재현율(Context Entities Recall)

ContextEntityRecall 지표는 referenceretrieved_contexts 양쪽에 존재하는 엔티티 수를 reference에만 존재하는 엔티티 수와 비교해 검색된 컨텍스트의 재현율을 측정해요. 간단히 말해 reference에서 엔티티의 몇 분율이 재현되는지 측정하는 거예요. 이 지표는 관광 헬프데스크, 역사 QA 같은 사실 기반 사용 사례에 유용해요. 엔티티가 중요한 경우에는 그들을 담은 retrieved_contexts가 필요하므로, reference에 있는 엔티티와 비교해 검색 메커니즘을 평가하는 데 도움이 돼요.

이 지표를 계산하기 위해 두 집합을 사용해요:

  • (RE): reference에 있는 엔티티 집합.
  • (RCE): retrieved contexts에 있는 엔티티 집합.

두 집합에 공통인 엔티티 수((RCE \cap RE))를 계산하고 reference의 전체 엔티티 수((RE))로 나눠요. 공식은:

[ \text{Context Entity Recall} = \frac{\text{Number of common entities between $RCE$ and $RE$}}{\text{Total number of entities in $RE$}} ]

예시(Example)

from openai import AsyncOpenAI
from ragas.llms import llm_factory
from ragas.metrics.collections import ContextEntityRecall

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

# Create metric
scorer = ContextEntityRecall(llm=llm)

# Evaluate
result = await scorer.ascore(
    reference="The Eiffel Tower is located in Paris.",
    retrieved_contexts=["The Eiffel Tower is located in Paris."]
)
print(f"Context Entity Recall Score: {result.value}")

출력(Output):

Context Entity Recall Score: 0.999999995

동기 사용법(Synchronous Usage) 동기 코드를 선호한다면 .ascore() 대신 .score() 메서드를 쓸 수 있어요: result = scorer.score( reference="The Eiffel Tower is located in Paris.", retrieved_contexts=["The Eiffel Tower is located in Paris."] )

계산 방법(How It's Calculated)

예시(Example) reference: The Taj Mahal is an ivory-white marble mausoleum on the right bank of the river Yamuna in the Indian city of Agra. It was commissioned in 1631 by the Mughal emperor Shah Jahan to house the tomb of his favorite wife, Mumtaz Mahal. 높은 엔티티 재현 컨텍스트: The Taj Mahal is a symbol of love and architectural marvel located in Agra, India. It was built by the Mughal emperor Shah Jahan in memory of his beloved wife, Mumtaz Mahal. The structure is renowned for its intricate marble work and beautiful gardens surrounding it. 낮은 엔티티 재현 컨텍스트: The Taj Mahal is an iconic monument in India. It is a UNESCO World Heritage Site and attracts millions of visitors annually. The intricate carvings and stunning architecture make it a must-visit destination.

위에 주어진 reference와 retrieved contexts를 고려해볼게요.

  • Step-1: reference에 존재하는 엔티티 찾기. Ground truth의 엔티티(RE) - ['Taj Mahal', 'Yamuna', 'Agra', '1631', 'Shah Jahan', 'Mumtaz Mahal']

  • Step-2: retrieved contexts에 존재하는 엔티티 찾기. Context의 엔티티(RCE1) - ['Taj Mahal', 'Agra', 'Shah Jahan', 'Mumtaz Mahal', 'India'] Context의 엔티티(RCE2) - ['Taj Mahal', 'UNESCO', 'India']

  • Step-3: 위에 주어진 공식을 사용해 엔티티 재현율을 계산해요.

[ \text{context entity recall 1} = \frac{| RCE1 \cap RE |}{| RE |} = 4/6 = 0.666 ]

[ \text{context entity recall 2} = \frac{| RCE2 \cap RE |}{| RE |} = 1/6 ]

첫 번째 컨텍스트가 reference를 기준으로 더 나은 엔티티 커버리지를 가지므로 높은 엔티티 재현율을 가진 걸 볼 수 있어요. 같은 문서 집합에 대해 두 검색 메커니즘이 이 두 컨텍스트를 검색했다면, 엔티티가 중요한 사용 사례에서 첫 번째 메커니즘이 다른 것보다 낫다고 말할 수 있어요.

레거시 지표 API(Legacy Metrics API)

다음 예시는 레거시 지표 API 패턴을 사용해요. 새 프로젝트에는 위에서 보여준 컬렉션 기반 API를 권장해요.

폐지 일정(Deprecation Timeline) 이 API는 버전 0.4에서 폐지되고 버전 1.0에서 제거될 예정이에요. 위에 보여준 컬렉션 기반 API로 마이그레이션해주세요.

SingleTurnSample과 함께하는 예시(Example with SingleTurnSample)

from ragas import SingleTurnSample
from ragas.metrics import ContextEntityRecall

sample = SingleTurnSample(
    reference="The Eiffel Tower is located in Paris.",
    retrieved_contexts=["The Eiffel Tower is located in Paris."],
)

scorer = ContextEntityRecall(llm=evaluator_llm)

await scorer.single_turn_ascore(sample)

출력(Output):

0.999999995