forked from openai/evals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch.py
45 lines (39 loc) · 1.48 KB
/
match.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from typing import Any
import evals
import evals.metrics
from evals.prompt.base import is_chat_prompt
class Match(evals.Eval):
def __init__(
self,
model_specs: evals.ModelSpecs,
samples_jsonl: str,
*args,
max_tokens: int = 500,
num_few_shot: int = 0,
few_shot_jsonl: str = None,
**kwargs,
):
super().__init__(model_specs, *args, **kwargs)
self.max_tokens = max_tokens
self.samples_jsonl = samples_jsonl
self.num_few_shot = num_few_shot
if self.num_few_shot > 0:
assert few_shot_jsonl is not None, "few shot requires few shot sample dataset"
self.few_shot_jsonl = few_shot_jsonl
self.few_shot = evals.get_jsonl(self.few_shot_jsonl)
def eval_sample(self, sample: Any, *_):
prompt = sample["input"]
if self.num_few_shot > 0:
assert is_chat_prompt(sample["input"]), "few shot requires chat prompt"
prompt = sample["input"][:-1]
for s in self.few_shot[: self.num_few_shot]:
prompt += s["sample"]
prompt += sample["input"][-1:]
return evals.check_sampled_text(self.model_spec, prompt, expected=sample["ideal"])
def run(self, recorder):
samples = evals.get_jsonl(self.samples_jsonl)
self.eval_all_samples(recorder, samples)
events = recorder.get_events("match")
return {
"accuracy": evals.metrics.get_accuracy(events),
}