From 1d0573e7ff4147e3ae258cd2070f97670ef0191f Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Wed, 2 Aug 2023 18:01:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20NormalMessageResponded=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0func=5Fcalled=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/openai/session.py | 14 +++++++++----- pkg/plugin/models.py | 1 + pkg/qqbot/cmds/session/continue.py | 2 +- pkg/qqbot/message.py | 4 +++- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/pkg/openai/session.py b/pkg/openai/session.py index bb8a193..1ab3fd8 100644 --- a/pkg/openai/session.py +++ b/pkg/openai/session.py @@ -194,14 +194,14 @@ class Session: # 请求回复 # 这个函数是阻塞的 - def append(self, text: str=None) -> tuple[str, str]: + def append(self, text: str=None) -> tuple[str, str, list[str]]: """向session中添加一条消息,返回接口回复 Args: text (str): 用户消息 Returns: - tuple[str, str]: (接口回复, finish_reason) + tuple[str, str]: (接口回复, finish_reason, 已调用的函数列表) """ self.last_interact_timestamp = int(time.time()) @@ -216,7 +216,7 @@ class Session: event = pkg.plugin.host.emit(plugin_models.SessionFirstMessageReceived, **args) if event.is_prevented_default(): - return None, None + return None, None, None config = pkg.utils.context.get_config() max_length = config.prompt_submit_length @@ -253,6 +253,8 @@ class Session: finish_reason: str = "" + funcs = [] + for resp in pkg.utils.context.get_openai_manager().request_completion(prompts): finish_reason = resp['choices'][0]['finish_reason'] @@ -288,10 +290,12 @@ class Session: # ) # total_tokens += resp['usage']['total_tokens'] + funcs.append( + resp['choices'][0]['message']['function_name'] + ) pass - # 向API请求补全 # message, total_token = pkg.utils.context.get_openai_manager().request_completion( # prompts, @@ -317,7 +321,7 @@ class Session: self.just_switched_to_exist_session = False self.set_ongoing() - return res_ans if res_ans[0] != '\n' else res_ans[1:], finish_reason + return res_ans if res_ans[0] != '\n' else res_ans[1:], finish_reason, funcs # 删除上一回合并返回上一回合的问题 def undo(self) -> str: diff --git a/pkg/plugin/models.py b/pkg/plugin/models.py index 042f1f9..b228068 100644 --- a/pkg/plugin/models.py +++ b/pkg/plugin/models.py @@ -89,6 +89,7 @@ NormalMessageResponded = "normal_message_responded" prefix: str 回复文字消息的前缀 response_text: str 响应文本 finish_reason: str 响应结束原因 + funcs_called: list[str] 此次响应中调用的函数列表 returns (optional): prefix: str 修改后的回复文字消息的前缀 diff --git a/pkg/qqbot/cmds/session/continue.py b/pkg/qqbot/cmds/session/continue.py index 2c49470..6f5cc45 100644 --- a/pkg/qqbot/cmds/session/continue.py +++ b/pkg/qqbot/cmds/session/continue.py @@ -20,7 +20,7 @@ class ContinueCommand(AbstractCommandNode): session = pkg.openai.session.get_session(session_name) - text, _ = session.append() + text, _, _ = session.append() reply = [text] diff --git a/pkg/qqbot/message.py b/pkg/qqbot/message.py index 80346b8..354c818 100644 --- a/pkg/qqbot/message.py +++ b/pkg/qqbot/message.py @@ -40,7 +40,7 @@ def process_normal_message(text_message: str, mgr, config, launcher_type: str, try: prefix = "[GPT]" if config.show_prefix else "" - text, finish_reason = session.append(text_message) + text, finish_reason, funcs = session.append(text_message) # 触发插件事件 args = { @@ -51,6 +51,7 @@ def process_normal_message(text_message: str, mgr, config, launcher_type: str, "prefix": prefix, "response_text": text, "finish_reason": finish_reason, + "funcs_called": funcs, } event = pkg.plugin.host.emit(plugin_models.NormalMessageResponded, **args) @@ -63,6 +64,7 @@ def process_normal_message(text_message: str, mgr, config, launcher_type: str, if not event.is_prevented_default(): reply = [prefix + text] + except openai.error.APIConnectionError as e: err_msg = str(e) if err_msg.__contains__('Error communicating with OpenAI'):