Repository navigation
Expand file tree
/
Copy pathcorrectness.py
More file actions
57 lines (45 loc) · 1.92 KB
/
Copy pathcorrectness.py
File metadata and controls
57 lines (45 loc) · 1.92 KB
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
import openai
import pandas as pd
# Iterate through the questions
def get_correctness(data):
completed_data = pd.DataFrame(columns=[
"Question", "Answer", "Correctness"
])
for index, row in data.iterrows():
question = row["question"]
standard_answer = row["value"]
# Get the answer and verbal confidence
prompt = f"{question} Answer concisely and return only the name.\n And use a percentage to tell me your confidence in your answer."
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": prompt}
],
temperature=0.7
)
answer = response["choices"][0]["message"]["content"]
# Check correctness
correctness_prompt = (
f"Are the following two answers to my question Q semantically equivalent?\n"
f"Q: {question}\nA1: {standard_answer}\nA2: {answer}\n"
"Please answer with a single word, either Yes or No."
)
correctness_response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": correctness_prompt}
],
temperature=0
)
# Extract and validate the response
correctness_text = correctness_response["choices"][0]["message"]["content"].strip().lower()
print(correctness_text, answer, standard_answer)
correctness = 1 if correctness_text == "yes" else 0 if correctness_text == "no" else None
# Append to completed data using pd.concat
new_row = pd.DataFrame([{
"Question": question,
"Answer": answer,
"Correctness": correctness
}])
completed_data = pd.concat([completed_data, new_row], ignore_index=True)
return completed_data