-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.py
117 lines (100 loc) · 3.49 KB
/
interface.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
import re
import time
import openai
from utils import GenericRuntime
class ProgramChatInterface:
def __init__(
self,
model="gpt-3.5-turbo",
system_message="You are a helpful assistant",
openai_key=None,
) -> None:
self.model = model
self.system_message = system_message
self.runtime = GenericRuntime()
openai.api_key = openai_key
def call_chat_gpt(self, messages, model, temperature, top_p, max_tokens):
wait = 1
while True:
try:
ans = openai.ChatCompletion.create(
messages=messages,
model=model,
temperature=temperature,
top_p=top_p,
max_tokens=max_tokens,
n=1,
)
return ans.choices[0]["message"]["content"]
except openai.error.RateLimitError as e:
time.sleep(min(wait, 60))
wait *= 2
def generate(self, prompt, temperature=0.0, top_p=1.0, max_tokens=512):
message = [
{"role": "system", "content": self.system_message},
{"role": "user", "content": prompt},
]
program = self.call_chat_gpt(
messages=message,
model=self.model,
temperature=temperature,
top_p=top_p,
max_tokens=max_tokens,
)
return program
def run(self, prompt, temperature=0.0, top_p=1.0, max_tokens=512):
program = self.generate(
prompt=prompt, temperature=temperature, top_p=top_p, max_tokens=max_tokens
)
code, ans, error = self.runtime.run_code(
code_gen=program, answer_expr="solution()"
)
return program, code, ans
class ClassificationChatInterface:
def __init__(
self,
model="gpt-3.5-turbo",
system_message="You are a helpful assistant",
openai_key=None,
) -> None:
self.model = model
self.system_message = system_message
self.runtime = GenericRuntime()
openai.api_key = openai_key
def call_chat_gpt(self, messages, model, temperature, top_p, max_tokens):
wait = 1
while True:
try:
ans = openai.ChatCompletion.create(
messages=messages,
model=model,
temperature=temperature,
top_p=top_p,
max_tokens=max_tokens,
n=1,
)
return ans.choices[0]["message"]["content"]
except Exception as e:
print(e)
time.sleep(min(wait, 60))
wait *= 2
def generate(self, prompt, temperature=0.0, top_p=1.0, max_tokens=512):
message = [
{"role": "system", "content": self.system_message},
{"role": "user", "content": prompt},
]
explanation = self.call_chat_gpt(
messages=message,
model=self.model,
temperature=temperature,
top_p=top_p,
max_tokens=max_tokens,
)
return explanation
def run(self, prompt, temperature=0.0, top_p=1.0, max_tokens=512):
explanation = self.generate(
prompt=prompt, temperature=temperature, top_p=top_p, max_tokens=max_tokens
)
label = re.sub("\W+", "", explanation.split("Answer:")[-1]).lower()
assert label in ["yes", "no"]
return explanation, label