From 3586cd941f7d2bf83fb71bc46d94b1e6c2322cb1 Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Sat, 5 Aug 2023 21:44:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=B7=9F=E8=B8=AA?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E8=B0=83=E7=94=A8=E8=BF=87=E7=A8=8B=E5=B9=B6?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=90=AF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config-template.py | 5 +++++ pkg/openai/api/chat_completion.py | 2 +- pkg/openai/session.py | 30 ++++++++++++++++++++++++++++-- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/config-template.py b/config-template.py index 987e63da..4e6082ec 100644 --- a/config-template.py +++ b/config-template.py @@ -239,6 +239,11 @@ image_api_params = { "size": "256x256", # 图片尺寸,支持256x256, 512x512, 1024x1024 } +# 跟踪函数调用 +# 为True时,在每次GPT进行Function Calling时都会输出发送一条回复给用户 +# 同时,一次提问内所有的Function Calling和普通回复消息都会单独发送给用户 +trace_function_calls = True + # 群内回复消息时是否引用原消息 quote_origin = True diff --git a/pkg/openai/api/chat_completion.py b/pkg/openai/api/chat_completion.py index 1ea46ac6..032e14bc 100644 --- a/pkg/openai/api/chat_completion.py +++ b/pkg/openai/api/chat_completion.py @@ -98,7 +98,7 @@ class ChatCompletionRequest(RequestBase): self.append_message( role="assistant", - content=None, + content=choice0['message']['content'], function_call=choice0['message']['function_call'] ) diff --git a/pkg/openai/session.py b/pkg/openai/session.py index 1400a8c6..dd2b0d21 100644 --- a/pkg/openai/session.py +++ b/pkg/openai/session.py @@ -255,13 +255,33 @@ class Session: funcs = [] + trace_func_calls = config.trace_function_calls + botmgr = pkg.utils.context.get_qqbot_manager() + + session_name_spt: list[str] = self.name.split("_") + + pending_res_text = "" + + # TODO 对不起,我知道这样非常非常屎山,但我之后会重构的 for resp in pkg.utils.context.get_openai_manager().request_completion(prompts): + if pending_res_text != "": + botmgr.adapter.send_message( + session_name_spt[0], + session_name_spt[1], + pending_res_text + ) + pending_res_text = "" + finish_reason = resp['choices'][0]['finish_reason'] if resp['choices'][0]['message']['role'] == "assistant" and resp['choices'][0]['message']['content'] != None: # 包含纯文本响应 - res_text += resp['choices'][0]['message']['content'] + "\n" + if not trace_func_calls: + res_text += resp['choices'][0]['message']['content'] + "\n" + else: + res_text = resp['choices'][0]['message']['content'] + pending_res_text = resp['choices'][0]['message']['content'] total_tokens += resp['usage']['total_tokens'] @@ -275,13 +295,19 @@ class Session: pending_msgs.append(msg) - elif resp['choices'][0]['message']['type'] == 'function_call': + if resp['choices'][0]['message']['type'] == 'function_call': # self.prompt.append( # { # "role": "assistant", # "content": "function call: "+json.dumps(resp['choices'][0]['message']['function_call']) # } # ) + if trace_func_calls: + botmgr.adapter.send_message( + session_name_spt[0], + session_name_spt[1], + "调用函数 "+resp['choices'][0]['message']['function_call']['name'] + "..." + ) total_tokens += resp['usage']['total_tokens'] elif resp['choices'][0]['message']['type'] == 'function_return':