feat: 内容函数全局开关支持

This commit is contained in:
RockChinQ
2023-07-29 16:28:18 +08:00
parent ae6994e241
commit 8c69b8a1d9
6 changed files with 48 additions and 163 deletions
+8 -3
View File
@@ -1,5 +1,6 @@
import openai
import json
import logging
from .model import RequestBase
@@ -86,7 +87,7 @@ class ChatCompletionRequest(RequestBase):
self.append_message(
role="assistant",
content="function call: "+json.dumps(self.pending_func_call)
content="function call: "+json.dumps(self.pending_func_call, ensure_ascii=False)
)
return {
@@ -147,12 +148,16 @@ class ChatCompletionRequest(RequestBase):
func_schema['parameters']['required'][0]: cp_pending_func_call['arguments']
}
logging.info("执行函数调用: name={}, arguments={}".format(func_name, arguments))
# 执行函数调用
ret = execute_function(func_name, arguments)
logging.info("函数执行完成。")
self.append_message(
role="function",
content=json.dumps(ret),
content=json.dumps(ret, ensure_ascii=False),
name=func_name
)
@@ -165,7 +170,7 @@ class ChatCompletionRequest(RequestBase):
"role": "function",
"type": "function_return",
"function_name": func_name,
"content": json.dumps(ret)
"content": json.dumps(ret, ensure_ascii=False)
},
"finish_reason": "function_return"
}
+10 -3
View File
@@ -16,12 +16,16 @@ class RequestBase:
"""处理代理问题"""
ret: dict = {}
exception: Exception = None
async def awrapper(**kwargs):
nonlocal ret
nonlocal ret, exception
ret = await self.req_func(**kwargs)
return ret
try:
ret = await self.req_func(**kwargs)
return ret
except Exception as e:
exception = e
loop = asyncio.new_event_loop()
@@ -33,6 +37,9 @@ class RequestBase:
thr.start()
thr.join()
if exception is not None:
raise exception
return ret
def __iter__(self):