-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprompt_evaluation.py
52 lines (39 loc) · 1.74 KB
/
prompt_evaluation.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
46
47
48
49
50
51
52
from .llm import ChatBotLLM
import json
import typing
from .utils import process_output
from tqdm import tqdm
from .evaluation import BaseEvaluation, Config
import numpy as np
class PromptEngineerEvaluation(BaseEvaluation):
prompt_template: str
def __init__(self, model: ChatBotLLM, data_path: str, config: Config, prompt_template: str, **kwargs) -> None:
super().__init__(model, data_path, config, **kwargs)
self.prompt_template = prompt_template
def eval(self) -> float:
raise NotImplementedError()
def submit(self) -> list[dict[str, str]]:
submission: list[dict[str, str]] = []
for task in tqdm(self.data):
id = task["id"]
answers = list(task["results"].keys())
question = task["question"]
formatted_answers = '\n'.join(list(map(lambda s: f" * {s}: ?" , answers)))
prompt = self.prompt_template.format(question, formatted_answers)
parsed_output = self.getParsedModelOutput(prompt, id)
if parsed_output is None:
task_submission = {}
for key in answers:
task_submission[key] = str(0.0)
submission.append(task_submission)
continue
if len(parsed_output) < len(answers):
excess = len(answers) - len(parsed_output)
parsed_output.extend([0 for i in range(excess)])
elif len(parsed_output) > len(answers):
parsed_output = parsed_output[:len(answers)]
task_submission = {}
for key, val in zip(answers, parsed_output):
task_submission[key] = str(val)
submission.append(task_submission)
return submission