Elffuss BraidHub

kikocis/Based-Agent public

65e76436655c · 2.2 KiBRawPermalink
1from src.utils import get_completion
2from configs.prompts import EVAL_GROUNDTRUTH_PROMPT
3import json
4import re
5import ast
6from openai import OpenAI
7
8class EvalFunction:
9
10 def __init__(self, client, plan, task):
11 self.client = client
12 self.eval_function = getattr(self, task.eval_function, None)
13 self.task = task
14 self.groundtruth = task.groundtruth
15 self.plan = plan
16
17 def default(self):
18 response = get_completion(self.client, [{"role": "user", "content": EVAL_GROUNDTRUTH_PROMPT.format(self.plan, self.groundtruth)}])
19 if response.content.lower() == 'true':
20 return True
21 return False
22
23 def numeric(self):
24 number_pattern = r'\d+'
25 response = self.plan['step'][-1]
26 # Find all occurrences of numbers in the sentence
27 numbers = re.findall(number_pattern, response)
28 print(f"Number(s) to compare: {numbers}")
29 try:
30 ground_truth = ast.literal_eval(self.groundtruth)
31 except:
32 print(f"Ground truth is not numeric: {self.groundtruth}")
33 return False
34 try:
35 for n in numbers:
36 if int(ground_truth) == int(n) or float(ground_truth) == float(n):
37 return True
38 except:
39 print(f"Error in comparing numbers: {numbers}")
40 return False
41
42 def name(self):
43 extract_name_prompt = "You will be provided with a sentence. Your goal is to extract the full names you see in the sentence. Return the names as an array of strings."
44 response = self.plan['step'][-1]
45 completion_result = self.client.chat.completions.create(
46 model="gpt-4-turbo-preview",
47 max_tokens=100,
48 temperature=0,
49 messages=[
50 {"role": "system",
51 "content": extract_name_prompt
52 },
53 {"role": "user", "content": f"SENTENCE:\n{response}"}]
54 )
55 name_extract = completion_result.choices[0].message.content
56 print(f"Name extracted: {name_extract}")
57 try:
58 names = ast.literal_eval(name_extract)
59 ground_truth = self.groundtruth
60 for n in names:
61 if n.lower == ground_truth.lower():
62 return True
63 except:
64 print(f"Issue with extracted names: {name_extract}")
65 return False
66
67 def evaluate(self):
68 return self.eval_function()