-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.py
48 lines (41 loc) · 1.42 KB
/
chat.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
from typing import Optional
import openai
class Chat:
def __init__(self, system: Optional[str] = None):
self.system = system
self.messages = []
if system is not None:
self.messages.append({
"role": "system",
"content": system
})
def prompt_for_json(self, content: str) -> str:
self.messages.append({
"role": "user",
"content": content
})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=self.messages
)
response_content = response["choices"][0]["message"]["content"]
self.messages.append({
"role": "assistant",
"content": response_content
})
return response_content
def prompt(self, content: str) -> str:
self.messages.append({
"role": "user",
"content": "if user place some order say 'write some soft professional word to say order will be place in your cart' in professional AND friendly way:\n" + content
})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=self.messages
)
response_content = response["choices"][0]["message"]["content"]
self.messages.append({
"role": "assistant",
"content": response_content
})
return response_content