feat: 支持跟踪函数调用过程并默认启用

This commit is contained in:
RockChinQ 2023-08-05 21:44:11 +08:00
parent 054d0839ac
commit 3586cd941f
3 changed files with 34 additions and 3 deletions

View File

@ -239,6 +239,11 @@ image_api_params = {
"size": "256x256", # 图片尺寸支持256x256, 512x512, 1024x1024 "size": "256x256", # 图片尺寸支持256x256, 512x512, 1024x1024
} }
# 跟踪函数调用
# 为True时在每次GPT进行Function Calling时都会输出发送一条回复给用户
# 同时一次提问内所有的Function Calling和普通回复消息都会单独发送给用户
trace_function_calls = True
# 群内回复消息时是否引用原消息 # 群内回复消息时是否引用原消息
quote_origin = True quote_origin = True

View File

@ -98,7 +98,7 @@ class ChatCompletionRequest(RequestBase):
self.append_message( self.append_message(
role="assistant", role="assistant",
content=None, content=choice0['message']['content'],
function_call=choice0['message']['function_call'] function_call=choice0['message']['function_call']
) )

View File

@ -255,13 +255,33 @@ class Session:
funcs = [] 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): 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'] finish_reason = resp['choices'][0]['finish_reason']
if resp['choices'][0]['message']['role'] == "assistant" and resp['choices'][0]['message']['content'] != None: # 包含纯文本响应 if resp['choices'][0]['message']['role'] == "assistant" and resp['choices'][0]['message']['content'] != None: # 包含纯文本响应
if not trace_func_calls:
res_text += resp['choices'][0]['message']['content'] + "\n" 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'] total_tokens += resp['usage']['total_tokens']
@ -275,13 +295,19 @@ class Session:
pending_msgs.append(msg) pending_msgs.append(msg)
elif resp['choices'][0]['message']['type'] == 'function_call': if resp['choices'][0]['message']['type'] == 'function_call':
# self.prompt.append( # self.prompt.append(
# { # {
# "role": "assistant", # "role": "assistant",
# "content": "function call: "+json.dumps(resp['choices'][0]['message']['function_call']) # "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'] total_tokens += resp['usage']['total_tokens']
elif resp['choices'][0]['message']['type'] == 'function_return': elif resp['choices'][0]['message']['type'] == 'function_return':