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
+11 -7
View File
@@ -30,7 +30,7 @@ class ChatCompletionRequest(RequestBase):
) )
self.pending_msg = "" 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 = { msg = {
"role": role, "role": role,
"content": content "content": content
@@ -39,6 +39,9 @@ class ChatCompletionRequest(RequestBase):
if name is not None: if name is not None:
msg['name'] = name msg['name'] = name
if function_call is not None:
msg['function_call'] = function_call
self.messages.append(msg) self.messages.append(msg)
def __init__( def __init__(
@@ -87,16 +90,17 @@ class ChatCompletionRequest(RequestBase):
choice0 = resp["choices"][0] choice0 = resp["choices"][0]
# 如果不是函数调用,且finish_reason为stop,则停止迭代 # 如果不是函数调用,且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 self.stopped = True
if 'function_call' in choice0['message']: if 'function_call' in choice0['message']:
self.pending_func_call = choice0['message']['function_call'] self.pending_func_call = choice0['message']['function_call']
# self.append_message( self.append_message(
# role="assistant", role="assistant",
# content="function call: "+json.dumps(self.pending_func_call, ensure_ascii=False) content=None,
# ) function_call=choice0['message']['function_call']
)
return { return {
"id": resp["id"], "id": resp["id"],
@@ -106,7 +110,7 @@ class ChatCompletionRequest(RequestBase):
"message": { "message": {
"role": "assistant", "role": "assistant",
"type": "function_call", "type": "function_call",
"content": None, "content": choice0['message']['content'],
"function_call": choice0['message']['function_call'] "function_call": choice0['message']['function_call']
}, },
"finish_reason": "function_call" "finish_reason": "function_call"
+12 -8
View File
@@ -259,17 +259,21 @@ class Session:
finish_reason = resp['choices'][0]['finish_reason'] finish_reason = resp['choices'][0]['finish_reason']
if resp['choices'][0]['message']['type'] == 'text': # 普通回复 if resp['choices'][0]['message']['role'] == "assistant" and resp['choices'][0]['message']['content'] != None: # 包含纯文本响应
res_text += resp['choices'][0]['message']['content']
res_text += resp['choices'][0]['message']['content'] + "\n"
total_tokens += resp['usage']['total_tokens'] total_tokens += resp['usage']['total_tokens']
pending_msgs.append( msg = {
{ "role": "assistant",
"role": "assistant", "content": resp['choices'][0]['message']['content']
"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': elif resp['choices'][0]['message']['type'] == 'function_call':
# self.prompt.append( # self.prompt.append(