perf: 优化openai模型消息截断机制

This commit is contained in:
Soulter
2023-09-30 15:11:06 +08:00
parent 33dcc4c152
commit f2c4ef126e
+10 -4
View File
@@ -119,6 +119,10 @@ class ProviderOpenAIOfficial(Provider):
retry = 0
response = None
err = ''
# 截断倍率
truncate_rate = 0.75
while retry < 15:
try:
response = openai.ChatCompletion.create(
@@ -139,19 +143,21 @@ class ProviderOpenAIOfficial(Provider):
else:
break
elif 'maximum context length' in str(e):
gu.log("token超限, 清空对应缓存,并进行消息0.75倍截断")
gu.log("token超限, 清空对应缓存,并进行消息截断")
self.session_dict[session_id] = []
prompt = prompt[:int(len(prompt)*0.75)]
prompt = prompt[:int(len(prompt)*truncate_rate)]
truncate_rate -= 0.05
cache_data_list, new_record, req = self.wrap(prompt, session_id)
elif 'Limit: 3 / min. Please try again in 20s.' in str(e) or "OpenAI response error" in str(e):
time.sleep(30)
continue
else:
time.sleep(5)
gu.log(str(e), level=gu.LEVEL_ERROR)
time.sleep(3)
err = str(e)
retry+=1
if retry >= 5:
if retry >= 15:
gu.log(r"如果报错, 且您的机器在中国大陆内, 请确保您的电脑已经设置好代理软件(梯子), 并在配置文件设置了系统代理地址。详见https://github.com/Soulter/QQChannelChatGPT/wiki/%E4%BA%8C%E3%80%81%E9%A1%B9%E7%9B%AE%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E9%85%8D%E7%BD%AE", max_len=999)
raise BaseException("连接出错: "+str(err))