Weave evaluation에서 모델을 scoring할 때는 절대값 메트릭(예: Model A는 9/10, Model B는 8/10)보다 상대적 메트릭(예: Model A가 Model B보다 더 잘 수행함)을 부여하는 편이 일반적으로 더 쉽습니다. 쌍대 평가를 사용하면 두 모델의 출력을 서로 비교해 상대적인 순위를 매길 수 있습니다. 이 방식은 텍스트 생성, 요약, 질의응답처럼 주관적인 작업에서 어떤 모델이 더 나은 성능을 보이는지 확인하려는 경우 특히 유용합니다. 쌍대 평가를 사용하면 특정 입력에 대해 어떤 모델이 가장 적합한지 보여주는 상대적 선호 순위를 획득할 수 있습니다.
이 방식은 임시 우회책이며 향후 릴리스에서 변경될 수 있습니다. 현재 쌍대 평가를 지원하는 더 안정적인 API를 적극적으로 개발하고 있습니다. 업데이트를 기다려 주세요!
다음 코드 예제는 PreferenceScorer라는 class-based scorer를 만들어 Weave에서 쌍대 평가를 구현하는 방법을 보여줍니다. PreferenceScorer는 ModelA와 ModelB 두 모델을 비교하고, 입력 텍스트의 명시적인 힌트를 바탕으로 모델 출력의 상대 점수를 반환합니다.
from weave import Model, Evaluation, Scorer, Dataset
from weave.flow.model import ApplyModelError, apply_model_async
class ModelA(Model):
@weave.op
def predict(self, input_text: str):
if "Prefer model A" in input_text:
return {"response": "This is a great answer from Model A"}
return {"response": "Meh, whatever"}
class ModelB(Model):
@weave.op
def predict(self, input_text: str):
if "Prefer model B" in input_text:
return {"response": "This is a thoughtful answer from Model B"}
return {"response": "I don't know"}
class PreferenceScorer(Scorer):
@weave.op
async def _get_other_model_output(self, example: dict) -> Any:
"""비교를 위해 다른 모델의 출력을 가져옵니다.
Args:
example: 다른 모델에 실행할 입력 예시 데이터
Returns:
다른 모델의 출력
"""
other_model_result = await apply_model_async(
self.other_model,
example,
None,
)
if isinstance(other_model_result, ApplyModelError):
return None
return other_model_result.model_output
@weave.op
async def score(self, output: dict, input_text: str) -> dict:
"""기본 모델의 출력을 다른 모델과 비교합니다.
Args:
output (dict): 기본 모델의 출력.
input_text (str): 출력 생성에 사용된 입력 텍스트.
Returns:
dict: 비교 결과와 이유를 담은 플랫 딕셔너리.
"""
other_output = await self._get_other_model_output(
{"input_text": input_text}
)
if other_output is None:
return {"primary_is_better": False, "reason": "다른 모델 실패"}
if "Prefer model A" in input_text:
primary_is_better = True
reason = "Model A가 훌륭한 답변을 제공했습니다"
else:
primary_is_better = False
reason = "이 유형의 질문에는 Model B가 선호됩니다"
return {"primary_is_better": primary_is_better, "reason": reason}
dataset = Dataset(
rows=[
{"input_text": "Prefer model A: Question 1"}, # Model A 승리
{"input_text": "Prefer model A: Question 2"}, # Model A 승리
{"input_text": "Prefer model B: Question 3"}, # Model B 승리
{"input_text": "Prefer model B: Question 4"}, # Model B 승리
]
)
model_a = ModelA()
model_b = ModelB()
pref_scorer = PreferenceScorer(other_model=model_b)
evaluation = Evaluation(dataset=dataset, scorers=[pref_scorer])
evaluation.evaluate(model_a)
평가