# 1. Weave 모델 구성
class FruitExtract(BaseModel):
fruit: str
color: str
flavor: str
class ExtractFruitsModel(weave.Model):
model_name: str
prompt_template: str
@weave.op()
def predict(self, sentence: str) -> dict:
client = OpenAI()
response = client.beta.chat.completions.parse(
model=self.model_name,
messages=[
{
"role": "user",
"content": self.prompt_template.format(sentence=sentence),
}
],
response_format=FruitExtract,
)
result = response.choices[0].message.parsed
return result
model = ExtractFruitsModel(
name="gpt4o",
model_name="gpt-4o",
prompt_template='Extract fields ("fruit": <str>, "color": <str>, "flavor": <str>) as json, from the following text : {sentence}',
)
# 2. 샘플 수집
sentences = [
"There are many fruits that were found on the recently discovered planet Goocrux. There are neoskizzles that grow there, which are purple and taste like candy.",
"Pounits are a bright green color and are more savory than sweet.",
"Finally, there are fruits called glowls, which have a very sour and bitter taste which is acidic and caustic, and a pale orange tinge to them.",
]
labels = [
{"fruit": "neoskizzles", "color": "purple", "flavor": "candy"},
{"fruit": "pounits", "color": "green", "flavor": "savory"},
{"fruit": "glowls", "color": "orange", "flavor": "sour, bitter"},
]
examples = [
{"id": "0", "sentence": sentences[0], "target": labels[0]},
{"id": "1", "sentence": sentences[1], "target": labels[1]},
{"id": "2", "sentence": sentences[2], "target": labels[2]},
]
# 3. 평가를 위한 스코어링 함수 정의
@weave.op()
def fruit_name_score(target: dict, output: FruitExtract) -> dict:
target_flavors = [f.strip().lower() for f in target["flavor"].split(",")]
output_flavors = [f.strip().lower() for f in output.flavor.split(",")]
# 타겟 풍미가 출력 풍미에 포함되어 있는지 확인
matches = any(tf in of for tf in target_flavors for of in output_flavors)
return {"correct": matches}
# 4. 평가 실행
evaluation = weave.Evaluation(
name="fruit_eval",
dataset=examples,
scorers=[fruit_name_score],
)
await evaluation.evaluate(model)