의미적 유사성(Semantic Similarity)
의미적 유사성(Semantic Similarity)
의미적 유사성은 생성된 응답과 reference(ground truth) 답변 사이의 의미적 유사도를 평가하는 지표예요. 0에서 1 사이의 점수이며, 높을수록 생성 답변이 정답과 더 잘 정렬돼 있다는 뜻이에요. 두 답변을 벡터로 변환해 코사인 유사도를 계산하는 방식이라 생성 응답의 품질에 대한 유용한 통찰을 주죠.
출처: 문서
본문
의미적 유사성(Semantic Similarity)
Semantic Similarity 지표는 생성된 응답과 reference(ground truth) 답변 사이의 의미적 유사도를 평가해요. 0에서 1 사이이며, 점수가 높을수록 생성 답변과 정답 사이의 정렬이 더 좋다는 뜻이에요.
이 지표는 임베딩과 코사인 유사도를 사용해 두 답변이 의미적으로 얼마나 비슷한지 측정해요. 이는 생성 응답의 품질에 대한 소중한 통찰을 제공할 수 있어요.
예시(Example)
from openai import AsyncOpenAI
from ragas.embeddings import OpenAIEmbeddings
from ragas.metrics.collections import SemanticSimilarity
# Setup embeddings
client = AsyncOpenAI()
embeddings = OpenAIEmbeddings(model="text-embedding-3-small", client=client)
# Create metric
scorer = SemanticSimilarity(embeddings=embeddings)
# Evaluate
result = await scorer.ascore(
reference="The Eiffel Tower is located in Paris. It has a height of 1000ft.",
response="The Eiffel Tower is located in Paris."
)
print(f"Semantic Similarity Score: {result.value}")
출력(Output):
Semantic Similarity Score: 0.8151
동기 사용법(Synchronous Usage) 동기 코드를 선호한다면
.ascore()대신.score()메서드를 쓸 수 있어요:result = scorer.score( reference="The Eiffel Tower is located in Paris. It has a height of 1000ft.", response="The Eiffel Tower is located in Paris." )
계산 방법(How It's Calculated)
예시(Example) Reference: Albert Einstein's theory of relativity revolutionized our understanding of the universe. 높은 유사도 응답: Einstein's groundbreaking theory of relativity transformed our comprehension of the cosmos. 낮은 유사도 응답: Isaac Newton's laws of motion greatly influenced classical physics.
높은 유사도 응답의 의미적 유사성이 어떻게 계산되었는지 살펴볼게요:
- Step 1: 지정된 임베딩 모델로 reference 답변을 벡터화해요.
- Step 2: 같은 임베딩 모델로 생성된 응답을 벡터화해요.
- Step 3: 두 벡터 사이의 코사인 유사도를 계산해요.
- Step 4: 코사인 유사도 값(0-1)이 최종 점수가 돼요.
레거시 지표 API(Legacy Metrics API)
다음 예시는 레거시 지표 API 패턴을 사용해요. 새 프로젝트에는 위에서 보여준 컬렉션 기반 API를 권장해요.
폐지 일정(Deprecation Timeline) 이 API는 버전 0.4에서 폐지되고 버전 1.0에서 제거될 예정이에요. 위에 보여준 컬렉션 기반 API로 마이그레이션해주세요.
SingleTurnSample과 함께하는 예시(Example with SingleTurnSample)
from ragas.dataset_schema import SingleTurnSample
from ragas.metrics import SemanticSimilarity
from ragas.embeddings import LangchainEmbeddingsWrapper
sample = SingleTurnSample(
response="The Eiffel Tower is located in Paris.",
reference="The Eiffel Tower is located in Paris. It has a height of 1000ft."
)
scorer = SemanticSimilarity(embeddings=LangchainEmbeddingsWrapper(evaluator_embedding))
await scorer.single_turn_ascore(sample)
출력(Output):
0.8151371879226978