-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatGptWrapper.py
66 lines (52 loc) · 2.19 KB
/
chatGptWrapper.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
from time import sleep
import openai
import openai.error
retryTime = 5
class ChatGPT:
def __init__(self, systemMessage="", temperature=0.8, model="gpt-3.5-turbo") -> None:
self.context = []
self.systemMessage = systemMessage
self.temperature = temperature
self.model = model
with open("openAiApiKey.txt", "r") as file:
apiKey = file.read()
openai.api_key = apiKey
def _runQuery(self, messages):
attempts = 0
completion = ""
while completion == "" and attempts <= 10:
try:
completion = openai.ChatCompletion.create(
model=self.model,
temperature=self.temperature, # 0 to 2
max_tokens = 2048,
messages=messages)
except (openai.error.APIError, openai.error.ServiceUnavailableError) as e:
attempts += 1
print(e)
print(f"OpenAI APIError: trying again in {retryTime} seconds...")
sleep(retryTime)
if attempts > 10:
return "OpenAI APIError"
return completion.choices[0].message.content # type: ignore
def askWithoutContext(self, userMessage):
messages = []
if self.systemMessage != "":
messages.append({"role": "system", "content": self.systemMessage})
messages.append({"role": "user", "content": userMessage})
response = self._runQuery(messages)
return str(response)
def askWithContext(self, userMessage):
messages = []
if self.systemMessage != "":
messages.append({"role": "system", "content": self.systemMessage})
for message in self.context:
messages.append(message)
messages.append({"role": "user", "content": userMessage})
response = self._runQuery(messages)
self.context.append({"role": "assistant", "content": response})
return str(response)
def listModels(self):
models = openai.Model.list()
if isinstance(models, dict):
return [i["id"] for i in models["data"]]