-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchatbot.py
54 lines (46 loc) · 1.55 KB
/
chatbot.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
from pickle import TRUE
import re
import json
import time
import requests
import traceback
import aispeech
import wave
import transcribe
message_log = []
AI_RESPONSE_FILENAME = 'ai-response.txt'
logging_eventhandlers = []
PORT = 5000
history = []
def send_user_input(user_input):
global message_log
print(message_log)
log_message(f"User: {user_input}")
message_log.append({"role": "user", "content": user_input})
url = f'http://0.0.0.0:{PORT}/v1/chat/completions'
headers = {"Content-Type": "application/json"}
json = {
"messages": [
{"role": "system", "content": f"You are an ai assistant"},
{"role": "user", "content": f"{user_input}"}
],
"history": f"{history}",
"stop": ["### Instruction:"],
"temperature": 0.7,
"max_tokens": 800,
"stream": False
}
response = requests.post(url, headers=headers, json=json)
if response.status_code == 200:
result = response.json()['choices'][0]['message']['content']
history.append({"role": "system", "content": result } )
history.append({"role": "user", "content": user_input } )
text = result
log_message(f'{text}')
aispeech.initialize(text)
time.sleep(0.1)
def log_message(message_text):
print(message_text)
global logging_eventhandlers
for eventhandler in logging_eventhandlers:
eventhandler(message_text)