generated from BaoLocPham/ML-Project-Experiment-Tracking-Template
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathinference_utils.py
62 lines (54 loc) · 1.99 KB
/
inference_utils.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
53
54
55
56
57
58
59
60
61
62
import re
def get_user_prompt(example, relevant_examples=None):
question = example["question"]
choices = example["choices"]
text_choices = "["
for idx, choice in enumerate(choices):
text_choices += f"'{choice}'"
if idx != len(choices) - 1:
text_choices += ","
text_choices += "]"
user_prompt = None
if not relevant_examples:
user_prompt = (
"Below is a math exercise. Provide a solution to that problem, if given multiple choices to answer; please give a final choice for solving that problem.\n"
f"### Question: {question}\n"
"### Choices: "
f"{text_choices}\n"
"### Explanation: "
)
else:
user_prompt = (
"Below is a math exercise. Provide a solution to that problem, if given multiple choices to answer; please give a final choice for solving that problem.\n"
f"Similar Question and their explanation: {relevant_examples}\n"
f"### Question: {question}\n"
"### Choices: "
f"{text_choices}\n"
"### Explanation: "
)
return user_prompt
def post_processing_answer(answer_text, choices):
answer = None
for choice in choices:
full_answer = choice
if full_answer in answer_text:
print("full answer process", full_answer)
answer = choice
break
if answer is None:
for choice in choices:
value_only = re.sub("[ABCD]. ", "", choice)
if value_only in answer_text:
print("value only process", value_only)
answer = choice
break
if answer is None:
for choice in choices:
tag_only = choice.split(".")[0].strip()
print("*" * 10)
print("check tag", tag_only)
if tag_only in answer_text:
print("tag only process", tag_only)
answer = choice
break
return answer