feat: 世界上最先进的调用流程

This commit is contained in:
RockChinQ 2023-08-04 18:41:04 +08:00
parent ec23bb5268
commit dd75f98d85
2 changed files with 23 additions and 15 deletions

View File

@ -30,7 +30,7 @@ class ChatCompletionRequest(RequestBase):
)
self.pending_msg = ""
def append_message(self, role: str, content: str, name: str=None):
def append_message(self, role: str, content: str, name: str=None, function_call: dict=None):
msg = {
"role": role,
"content": content
@ -39,6 +39,9 @@ class ChatCompletionRequest(RequestBase):
if name is not None:
msg['name'] = name
if function_call is not None:
msg['function_call'] = function_call
self.messages.append(msg)
def __init__(
@ -87,16 +90,17 @@ class ChatCompletionRequest(RequestBase):
choice0 = resp["choices"][0]
# 如果不是函数调用且finish_reason为stop则停止迭代
if 'function_call' not in choice0['message']: # and choice0["finish_reason"] == "stop"
if choice0['finish_reason'] == 'stop': # and choice0["finish_reason"] == "stop"
self.stopped = True
if 'function_call' in choice0['message']:
self.pending_func_call = choice0['message']['function_call']
# self.append_message(
# role="assistant",
# content="function call: "+json.dumps(self.pending_func_call, ensure_ascii=False)
# )
self.append_message(
role="assistant",
content=None,
function_call=choice0['message']['function_call']
)
return {
"id": resp["id"],
@ -106,7 +110,7 @@ class ChatCompletionRequest(RequestBase):
"message": {
"role": "assistant",
"type": "function_call",
"content": None,
"content": choice0['message']['content'],
"function_call": choice0['message']['function_call']
},
"finish_reason": "function_call"

View File

@ -259,17 +259,21 @@ class Session:
finish_reason = resp['choices'][0]['finish_reason']
if resp['choices'][0]['message']['type'] == 'text': # 普通回复
res_text += resp['choices'][0]['message']['content']
if resp['choices'][0]['message']['role'] == "assistant" and resp['choices'][0]['message']['content'] != None: # 包含纯文本响应
res_text += resp['choices'][0]['message']['content'] + "\n"
total_tokens += resp['usage']['total_tokens']
pending_msgs.append(
{
msg = {
"role": "assistant",
"content": resp['choices'][0]['message']['content']
}
)
if 'function_call' in resp['choices'][0]['message']:
msg['function_call'] = resp['choices'][0]['message']['function_call']
pending_msgs.append(msg)
elif resp['choices'][0]['message']['type'] == 'function_call':
# self.prompt.append(