forked from gpt-engineer-org/gpt-engineer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.py
39 lines (29 loc) · 1.01 KB
/
ai.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
import openai
class AI:
def __init__(self, **kwargs):
self.kwargs = kwargs
def start(self, system, user):
messages = [
{"role": "system", "content": system},
{"role": "user", "content": user},
]
return self.next(messages)
def fsystem(self, msg):
return {"role": "system", "content": msg}
def fuser(self, msg):
return {"role": "user", "content": msg}
def next(self, messages: list[dict[str, str]], prompt=None):
if prompt:
messages = messages + [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
messages=messages,
stream=True,
**self.kwargs
)
chat = []
for chunk in response:
delta = chunk['choices'][0]['delta']
msg = delta.get('content', '')
print(msg, end="")
chat.append(msg)
return messages + [{"role": "assistant", "content": "".join(chat)}]