-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt.py
executable file
·30 lines (20 loc) · 872 Bytes
/
gpt.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
import openai
class gpt:
def __init__(self) -> None:
with open("key.txt", "r") as key_file:
openai.api_key = key_file.read()
with open("ai_instructions.txt", "r") as instructions_file:
ai_instructions = instructions_file.read()
self.messages = [ {
"role": "system",
"content": ai_instructions
} ]
def ask_msg(self, message) -> str:
if not message:
print("Invalid message!")
return self.ask_msg()
self.messages.append( {"role": "user", "content": message} )
chat = openai.ChatCompletion.create(model="gpt-4o", messages=self.messages)
reply = chat.choices[0].message.content
self.messages.append( {"role": "assistant", "content": reply} )
return reply