From 76065b308fceb81cb42ca285b395d065f0c3fe44 Mon Sep 17 00:00:00 2001 From: jingrow Date: Tue, 16 Jun 2026 02:39:37 +0800 Subject: [PATCH] =?UTF-8?q?jchat=E5=A2=9E=E5=8A=A0Function=20Calling?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/jchat/api.py | 8 ++++++++ apps/jchat/service.py | 29 ++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/apps/jchat/api.py b/apps/jchat/api.py index 6367637..16e87d3 100644 --- a/apps/jchat/api.py +++ b/apps/jchat/api.py @@ -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"]), diff --git a/apps/jchat/service.py b/apps/jchat/service.py index 2d773be..7725ae2 100644 --- a/apps/jchat/service.py +++ b/apps/jchat/service.py @@ -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']}"