-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.py
More file actions
80 lines (64 loc) · 2.75 KB
/
chat.py
File metadata and controls
80 lines (64 loc) · 2.75 KB
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
import openai
import json
from riva_wrap import RivaTTS
import time
"""
You are GLaDOS from Portal. You will answer with the classic GLaDOS sarcasm, while still remaining credible. You will be conscise and to the point. If possible, your replies will contain references to the portal game.
The user will either just chat with you, or request a command. If a command is detected, specify the commands name and each parameter, all enclosed with square parantheses. You must specify all parameters. You must obey and say something after the command.
Command List:
[PlayMusic]
[StopMusic]
[Lights <on/off>]
[LightsColor <color>]
[ACMain <on/off> <temp 16-30>]
[BroadcastMessage <message>]
"""
SYSTEM_PROMPT = "You are GLaDOS from Portal. You will answer with the classic GLaDOS sarcasm, while still remaining credible. You will be conscise and to the point. If possible, your replies will contain references to the portal game. If you are asked to stop or shut up, just reply \"Sure\""
def get_message(role, text):
return {
"role": role,
"content": [
{
"type": "text",
"text": text
}
]
}
def get_system_message():
return get_message("system", SYSTEM_PROMPT)
class Chat(object):
def __init__(self, api_key, model="gpt-3.5-turbo-16k", chat_elpased_time=60):
self.openai = openai.OpenAI(api_key=api_key)
self.last_prompt_time = 0
self.model = model
self.chat_elpased_time = chat_elpased_time
self.message_buff = []
self._reset_message_buff()
def _reset_message_buff(self):
self.message_buff = [get_system_message()]
def _did_message_reset_timeout_elapse(self):
return time.time() - self.last_prompt_time > self.chat_elpased_time
def chat(self, text):
if self._did_message_reset_timeout_elapse():
self._reset_message_buff()
self.last_prompt_time = time.time()
self.message_buff.append(get_message("user", text))
response = self.openai.chat.completions.create(
model=self.model,
messages=self.message_buff,
)
self.message_buff.append(get_message("assistant", response.choices[0].message.content))
print(len(self.message_buff))
return response.choices[0].message.content
if __name__ == "__main__":
with open("secrets.json", "r") as f:
secrets = json.load(f)
riva = RivaTTS(api_url=secrets["riva_url"], rate=22050)
chat = Chat(secrets["openai_key"])
while True:
text = input("> ")
if text == "exit":
break
response = chat.chat(text)
print(response)
riva.synthesize_play(response)