From e3442512c413db99331811c8014eb56b673634e5 Mon Sep 17 00:00:00 2001 From: Rock Chin Date: Sat, 10 Dec 2022 17:39:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=85=81=E8=AE=B8=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=9C=A8=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E9=87=8C=E6=9B=B4?= =?UTF-8?q?=E6=94=B9=E6=AF=8F=E6=AC=A1=E5=90=91=E6=A8=A1=E5=9E=8B=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E7=9A=84=E4=B8=8A=E4=B8=8B=E6=96=87=E6=96=87=E5=AD=97?= =?UTF-8?q?=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config-template.py | 15 ++++++++++++--- pkg/openai/session.py | 14 +++++++++----- pkg/qqbot/manager.py | 6 ++++-- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/config-template.py b/config-template.py index af74cbae..22a92ea1 100644 --- a/config-template.py +++ b/config-template.py @@ -38,20 +38,29 @@ openai_config = { completion_api_params = { "model": "text-davinci-003", "temperature": 0.8, - "max_tokens": 200, + "max_tokens": 200, # 每次向OpenAI请求的最大字符数 "top_p": 1, "frequency_penalty": 0.2, "presence_penalty": 0.4, } +# 每次向OpenAI接口发送对话记录上下文的字符数 +# 最大不超过(4096 - max_tokens)个字符,max_tokens为上述completion_api_params中的max_tokens +# 注意:较大的prompt_submit_length会导致OpenAI账户额度消耗更快 +prompt_submit_length = 1024 -# 消息处理的超时时间 +# 每次向OpenAI接口发送对话记录上下文的聊天回合数 +# 不建议过大,向OpenAI接口发送对话上下文时保证内容不超过prompt_submit_length个字符, +# 不超过prompt_submit_round_amount个回合 +prompt_submit_round_amount = 8 + +# 消息处理的超时时间,单位为秒 process_message_timeout = 20 # 消息处理超时重试次数 retry_times = 3 -# 每个会话的过期时间 +# 每个会话的过期时间,单位为秒 # 默认值20分钟 session_expire_time = 60 * 20 diff --git a/pkg/openai/session.py b/pkg/openai/session.py index 975c0710..41dc29a4 100644 --- a/pkg/openai/session.py +++ b/pkg/openai/session.py @@ -94,10 +94,12 @@ class Session: def append(self, text: str) -> str: self.last_interact_timestamp = int(time.time()) + max_length = config.prompt_submit_length if config.prompt_submit_length is not None else 1024 + # 向API请求补全 response = pkg.openai.manager.get_inst().request_completion(self.cut_out(self.prompt + self.user_name + ':' + text + '\n' + self.bot_name + ':', - 7, 1024), self.user_name + ':') + 7, max_length), self.user_name + ':') self.prompt += self.user_name + ':' + text + '\n' + self.bot_name + ':' # print(response) @@ -119,7 +121,7 @@ class Session: return res_ans - # 截取prompt里不多于max_rounds个回合,长度为大于max_tokens的最小整数字符串 + # 从尾部截取prompt里不多于max_rounds个回合,长度不大于max_tokens的字符串 # 保证都是完整的对话 def cut_out(self, prompt: str, max_rounds: int, max_tokens: int) -> str: # 分隔出每个回合 @@ -130,15 +132,17 @@ class Session: checked_rounds = 0 # 从后往前遍历,加到result前面,检查result是否符合要求 for i in range(len(rounds_spt_by_user_name) - 1, 0, -1): - result = self.user_name + ':' + rounds_spt_by_user_name[i] + result + result_temp = self.user_name + ':' + rounds_spt_by_user_name[i] + result checked_rounds += 1 - if checked_rounds >= max_rounds: + if checked_rounds > max_rounds: break - if len(result) > max_tokens: + if len(result_temp) > max_tokens: break + result = result_temp + logging.debug('cut_out: {}'.format(result)) return result diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index f1948784..24846c44 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -142,12 +142,14 @@ class QQBotManager: reply = "[GPT]" + session.append(text_message) except openai.error.APIConnectionError as e: self.notify_admin("{}会话调用API失败:{}".format(session_name, e)) - # logging.exception(e) reply = "[bot]err:调用API失败,请联系作者,或等待修复" except openai.error.RateLimitError as e: - # logging.exception(e) self.notify_admin("API调用额度超限,请向OpenAI账户充值或在config.py中更换api_key") reply = "[bot]err:API调用额度超额,请联系作者,或等待修复" + except openai.error.InvalidRequestError as e: + self.notify_admin("{}API调用参数错误:{}\n\n这可能是由于config.py中的prompt_submit_length参数或" + "completion_api_params中的max_tokens参数数值过大导致的,请尝试将其降低".format(session_name, e)) + reply = "[bot]err:API调用参数错误,请联系作者,或等待修复" except Exception as e: logging.exception(e) reply = "[bot]err:{}".format(e)