Spaces:
Runtime error
Runtime error
Commit
·
ba55863
1
Parent(s):
c35feec
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,12 @@ from app_utils import (
|
|
| 12 |
get_chat_history, initialize_knowledge_base,
|
| 13 |
text_to_speech_gen, logging, buzz_user)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
global FUNC_CALL #全局变量 用于判断角色动作
|
| 16 |
FUNC_CALL = 0
|
| 17 |
|
|
@@ -27,6 +33,17 @@ AUDIO_HTML = ''
|
|
| 27 |
nltk.download('averaged_perceptron_tagger') #下载语料库
|
| 28 |
conv_model, voice_model = initialize_knowledge_base() #初始化知识库
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def idle_timer():
|
| 32 |
global BUZZ_TIMEOUT
|
|
@@ -54,6 +71,7 @@ def get_response(history, audio_input):
|
|
| 54 |
|
| 55 |
query_type = 'text'
|
| 56 |
question =history[-1][0]
|
|
|
|
| 57 |
|
| 58 |
global BUZZ_TIMEOUT
|
| 59 |
BUZZ_TIMEOUT = 80
|
|
@@ -80,12 +98,18 @@ def get_response(history, audio_input):
|
|
| 80 |
LOGGER.info("\ndocument_response: %s", answer)
|
| 81 |
print('\ndocument_response:', answer)
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
for trigger in GENERAL_RSPONSE_TRIGGERS:
|
| 84 |
if trigger in answer:
|
| 85 |
MESSAGES.append({"role": "user", "content": question})
|
| 86 |
chat = openai.ChatCompletion.create(
|
| 87 |
-
model="gpt-3.5-turbo",
|
| 88 |
messages=MESSAGES,
|
|
|
|
| 89 |
temperature=0.7,
|
| 90 |
n=128,
|
| 91 |
stop="\n"
|
|
|
|
| 12 |
get_chat_history, initialize_knowledge_base,
|
| 13 |
text_to_speech_gen, logging, buzz_user)
|
| 14 |
|
| 15 |
+
global max_response_tokens
|
| 16 |
+
global token_limit
|
| 17 |
+
max_response_tokens = 500
|
| 18 |
+
token_limit= 15000
|
| 19 |
+
|
| 20 |
+
|
| 21 |
global FUNC_CALL #全局变量 用于判断角色动作
|
| 22 |
FUNC_CALL = 0
|
| 23 |
|
|
|
|
| 33 |
nltk.download('averaged_perceptron_tagger') #下载语料库
|
| 34 |
conv_model, voice_model = initialize_knowledge_base() #初始化知识库
|
| 35 |
|
| 36 |
+
def num_tokens_from_messages(messages, model="gpt-3.5-turbo-16k"):
|
| 37 |
+
encoding = tiktoken.encoding_for_model(model)
|
| 38 |
+
num_tokens = 0
|
| 39 |
+
for message in messages:
|
| 40 |
+
num_tokens += 4 # every message follows <im_start>{role/name}\n{content}<im_end>\n
|
| 41 |
+
for key, value in message.items():
|
| 42 |
+
num_tokens += len(encoding.encode(value))
|
| 43 |
+
if key == "name": # if there's a name, the role is omitted
|
| 44 |
+
num_tokens += -1 # role is always required and always 1 token
|
| 45 |
+
num_tokens += 2 # every reply is primed with <im_start>assistant
|
| 46 |
+
return num_tokens
|
| 47 |
|
| 48 |
def idle_timer():
|
| 49 |
global BUZZ_TIMEOUT
|
|
|
|
| 71 |
|
| 72 |
query_type = 'text'
|
| 73 |
question =history[-1][0]
|
| 74 |
+
conv_history_tokens = 0
|
| 75 |
|
| 76 |
global BUZZ_TIMEOUT
|
| 77 |
BUZZ_TIMEOUT = 80
|
|
|
|
| 98 |
LOGGER.info("\ndocument_response: %s", answer)
|
| 99 |
print('\ndocument_response:', answer)
|
| 100 |
|
| 101 |
+
conv_history_tokens = num_tokens_from_messages(conversation)
|
| 102 |
+
while (conv_history_tokens + max_response_tokens >= token_limit):
|
| 103 |
+
del MESSAGES[1]
|
| 104 |
+
conv_history_tokens = num_tokens_from_messages(conversation)
|
| 105 |
+
|
| 106 |
for trigger in GENERAL_RSPONSE_TRIGGERS:
|
| 107 |
if trigger in answer:
|
| 108 |
MESSAGES.append({"role": "user", "content": question})
|
| 109 |
chat = openai.ChatCompletion.create(
|
| 110 |
+
model="gpt-3.5-turbo-16k",
|
| 111 |
messages=MESSAGES,
|
| 112 |
+
max_tokens=500,
|
| 113 |
temperature=0.7,
|
| 114 |
n=128,
|
| 115 |
stop="\n"
|