-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.sync.py
136 lines (117 loc) · 3.7 KB
/
test.sync.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# %%
import json
import os
import sys
import openai
import pandas as pd
# %%
# load question
df = pd.read_csv("data/l1.csv", sep=";;", engine="python")
line = 238 # pg 104 q118
q = df.iloc[line - 2]
print(q.question)
# %%
# load model
model = "gpt-3.5-turbo"
temperature = 0.2
with open(os.path.expanduser("~/.cache/oai"), "r") as f:
openai.api_key = f.read().strip()
sys_prompt = f"""
You are a CFA (chartered financial analyst) taking a test to evaluate your knowledge of finance.
You will be given a question along with three possible answers.
Before answering, you should think through the question step-by-step.
Explain your reasoning at each step towards answering the question.
You must then choose the correct answer, indicating it at the end of your response with:
[[Answer: X]]
Where X is either A, B, or C. Be sure to answer in exactly this format.
Do not include any additional text in the answer line.
"""
messages = [
{
"role": "system",
"content": sys_prompt,
},
{
"role": "user",
"content": q.question,
},
]
# %%
res = openai.ChatCompletion.create(
model=model, messages=messages, temperature=temperature, stream=True
)
res_msg = ""
for completion_tokens, chunk in enumerate(res):
delta = chunk.choices[0].delta # type: ignore
if "content" in delta.__dict__["_previous"]:
res_msg += delta.content
sys.stdout.write(delta.content)
sys.stdout.flush()
print()
messages.append({"role": "assistant", "content": res_msg})
# %%
if res_msg.splitlines()[-1] != q.answer:
msg = f"Expected: {q.answer}\nGot: {res_msg.splitlines()[-1]}\nExplanation: {q.explanation}"
print("-" * 80)
print("CORRECTION")
print(msg)
print("-" * 80)
messages.append({"role": "user", "content": msg})
res = openai.ChatCompletion.create(
model=model, messages=messages, temperature=temperature, stream=True
)
res_msg = ""
for completion_tokens, chunk in enumerate(res):
delta = chunk.choices[0].delta # type: ignore
if "content" in delta.__dict__["_previous"]:
res_msg += delta.content
sys.stdout.write(delta.content)
sys.stdout.flush()
# %%
print(sys_prompt)
print(q.question)
# %%
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=[
{"role": "system", "content": sys_prompt},
{"role": "user", "content": q.question},
],
functions=[
{
"name": "answer",
"description": "Think through and answer a multiple choice question on finance",
"parameters": {
"type": "object",
"properties": {
"thinking": {
"type": "array",
"items": {
"type": "string",
"description": "Thought and/or calculation for a step in the process of answering the question",
},
"description": "Step by step thought process and calculations towards answering the question",
},
"answer": {
"type": "string",
"description": "The answer to the question",
"enum": ["A", "B", "C"],
},
},
"required": ["thinking", "answer"],
},
}
],
function_call={"name": "answer"},
)
reply = completion.choices[0].message # type: ignore
reply
# %%
ans = reply.to_dict()["function_call"]["arguments"]
ans = json.loads(ans)
for line in ans["thinking"]:
print("-", line)
print()
print("Answer:", ans["answer"])
# %%
pd.read_csv("chatgpt.csv").head()