jchat增加Function Calling支持
This commit is contained in:
parent
0d6baa6195
commit
76065b308f
@ -53,6 +53,10 @@ async def chat_api(data: dict, request: Request):
|
||||
service.top_p = data["top_p"]
|
||||
if "max_tokens" in data:
|
||||
service.max_tokens = data["max_tokens"]
|
||||
if "tools" in data:
|
||||
service.tools = data["tools"]
|
||||
if "tool_choice" in data:
|
||||
service.tool_choice = data["tool_choice"]
|
||||
|
||||
result = await service.chat(data["messages"])
|
||||
return result
|
||||
@ -88,6 +92,10 @@ async def chat_stream_api(data: dict, request: Request):
|
||||
service.top_p = data["top_p"]
|
||||
if "max_tokens" in data:
|
||||
service.max_tokens = data["max_tokens"]
|
||||
if "tools" in data:
|
||||
service.tools = data["tools"]
|
||||
if "tool_choice" in data:
|
||||
service.tool_choice = data["tool_choice"]
|
||||
|
||||
return StreamingResponse(
|
||||
service.chat_stream(data["messages"]),
|
||||
|
||||
@ -50,6 +50,8 @@ class ChatService:
|
||||
self.temperature = temperature
|
||||
self.top_p = top_p
|
||||
self.max_tokens = max_tokens
|
||||
self.tools = None # function calling tools
|
||||
self.tool_choice = None # tool_choice 参数
|
||||
|
||||
def _get_model_config(self, model: str) -> Dict:
|
||||
"""获取模型配置
|
||||
@ -122,6 +124,11 @@ class ChatService:
|
||||
"max_tokens": self.max_tokens
|
||||
}
|
||||
|
||||
if self.tools:
|
||||
payload["tools"] = self.tools
|
||||
if self.tool_choice:
|
||||
payload["tool_choice"] = self.tool_choice
|
||||
|
||||
return payload
|
||||
|
||||
def _send_request(self, messages: List[Dict], model_type: str, model_name: str) -> Optional[Dict]:
|
||||
@ -186,17 +193,24 @@ class ChatService:
|
||||
"message": "AI响应无效"
|
||||
}
|
||||
|
||||
message = choices[0].get("message", {}).get("content", "")
|
||||
if not message:
|
||||
message_obj = choices[0].get("message", {})
|
||||
content = message_obj.get("content", "")
|
||||
tool_calls = message_obj.get("tool_calls")
|
||||
|
||||
if not content and not tool_calls:
|
||||
return {
|
||||
"status": "error",
|
||||
"message": "AI响应内容为空"
|
||||
"message": "AI响应无效"
|
||||
}
|
||||
|
||||
return {
|
||||
result = {
|
||||
"status": "success",
|
||||
"data": message
|
||||
"data": content
|
||||
}
|
||||
if tool_calls:
|
||||
result["tool_calls"] = tool_calls
|
||||
result["finish_reason"] = choices[0].get("finish_reason")
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
return {
|
||||
@ -239,6 +253,11 @@ class ChatService:
|
||||
"stream": True
|
||||
}
|
||||
|
||||
if self.tools:
|
||||
payload["tools"] = self.tools
|
||||
if self.tool_choice:
|
||||
payload["tool_choice"] = self.tool_choice
|
||||
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {api_config['key']}"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user