jchat增加Function Calling支持

This commit is contained in:
jingrow 2026-06-16 02:39:37 +08:00
parent 0d6baa6195
commit 76065b308f
2 changed files with 32 additions and 5 deletions

View File

@ -53,6 +53,10 @@ async def chat_api(data: dict, request: Request):
service.top_p = data["top_p"] service.top_p = data["top_p"]
if "max_tokens" in data: if "max_tokens" in data:
service.max_tokens = data["max_tokens"] 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"]) result = await service.chat(data["messages"])
return result return result
@ -88,6 +92,10 @@ async def chat_stream_api(data: dict, request: Request):
service.top_p = data["top_p"] service.top_p = data["top_p"]
if "max_tokens" in data: if "max_tokens" in data:
service.max_tokens = data["max_tokens"] 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( return StreamingResponse(
service.chat_stream(data["messages"]), service.chat_stream(data["messages"]),

View File

@ -50,6 +50,8 @@ class ChatService:
self.temperature = temperature self.temperature = temperature
self.top_p = top_p self.top_p = top_p
self.max_tokens = max_tokens 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: def _get_model_config(self, model: str) -> Dict:
"""获取模型配置 """获取模型配置
@ -122,6 +124,11 @@ class ChatService:
"max_tokens": self.max_tokens "max_tokens": self.max_tokens
} }
if self.tools:
payload["tools"] = self.tools
if self.tool_choice:
payload["tool_choice"] = self.tool_choice
return payload return payload
def _send_request(self, messages: List[Dict], model_type: str, model_name: str) -> Optional[Dict]: def _send_request(self, messages: List[Dict], model_type: str, model_name: str) -> Optional[Dict]:
@ -186,17 +193,24 @@ class ChatService:
"message": "AI响应无效" "message": "AI响应无效"
} }
message = choices[0].get("message", {}).get("content", "") message_obj = choices[0].get("message", {})
if not message: content = message_obj.get("content", "")
tool_calls = message_obj.get("tool_calls")
if not content and not tool_calls:
return { return {
"status": "error", "status": "error",
"message": "AI响应内容为空" "message": "AI响应无效"
} }
return { result = {
"status": "success", "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: except Exception as e:
return { return {
@ -239,6 +253,11 @@ class ChatService:
"stream": True "stream": True
} }
if self.tools:
payload["tools"] = self.tools
if self.tool_choice:
payload["tool_choice"] = self.tool_choice
headers = { headers = {
"Content-Type": "application/json", "Content-Type": "application/json",
"Authorization": f"Bearer {api_config['key']}" "Authorization": f"Bearer {api_config['key']}"