From ab74be8e3381bf3df20b401ba7b967326df73149 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Mon, 2 Feb 2026 23:08:24 +0800 Subject: [PATCH] feat: add qwen models tool call --- agent/protocol/agent_stream.py | 55 +++- bridge/bridge.py | 3 + config-template.json | 1 + models/dashscope/dashscope_bot.py | 412 +++++++++++++++++++++++++++++- 4 files changed, 469 insertions(+), 2 deletions(-) diff --git a/agent/protocol/agent_stream.py b/agent/protocol/agent_stream.py index 9b3f1a6..530bc88 100644 --- a/agent/protocol/agent_stream.py +++ b/agent/protocol/agent_stream.py @@ -86,7 +86,7 @@ class AgentStreamExecutor: def _check_consecutive_failures(self, tool_name: str, args: dict) -> tuple[bool, str, bool]: """ - Check if tool has failed too many times consecutively + Check if tool has failed too many times consecutively or called repeatedly with same args Returns: (should_stop, reason, is_critical) @@ -96,6 +96,19 @@ class AgentStreamExecutor: """ args_hash = self._hash_args(args) + # Count consecutive calls (both success and failure) for same tool + args + # This catches infinite loops where tool succeeds but LLM keeps calling it + same_args_calls = 0 + for name, ahash, success in reversed(self.tool_failure_history): + if name == tool_name and ahash == args_hash: + same_args_calls += 1 + else: + break # Different tool or args, stop counting + + # Stop at 3 consecutive calls with same args (whether success or failure) + if same_args_calls >= 3: + return True, f"工具 '{tool_name}' 使用相同参数已被调用 {same_args_calls} 次,停止执行以防止无限循环。如果需要查看配置,结果已在之前的调用中返回。", False + # Count consecutive failures for same tool + args same_args_failures = 0 for name, ahash, success in reversed(self.tool_failure_history): @@ -269,6 +282,19 @@ class AgentStreamExecutor: result = self._execute_tool(tool_call) tool_results.append(result) + # Debug: Check if tool is being called repeatedly with same args + if turn > 2: + # Check last N tool calls for repeats + repeat_count = sum( + 1 for name, ahash, _ in self.tool_failure_history[-10:] + if name == tool_call["name"] and ahash == self._hash_args(tool_call["arguments"]) + ) + if repeat_count >= 3: + logger.warning( + f"⚠️ Tool '{tool_call['name']}' has been called {repeat_count} times " + f"with same arguments. This may indicate a loop." + ) + # Check if this is a file to send (from read tool) if result.get("status") == "success" and isinstance(result.get("result"), dict): result_data = result.get("result") @@ -331,6 +357,33 @@ class AgentStreamExecutor: "role": "user", "content": tool_result_blocks }) + + # Detect potential infinite loop: same tool called multiple times with success + # If detected, add a hint to LLM to stop calling tools and provide response + if turn >= 3 and len(tool_calls) > 0: + tool_name = tool_calls[0]["name"] + args_hash = self._hash_args(tool_calls[0]["arguments"]) + + # Count recent successful calls with same tool+args + recent_success_count = 0 + for name, ahash, success in reversed(self.tool_failure_history[-10:]): + if name == tool_name and ahash == args_hash and success: + recent_success_count += 1 + + # If tool was called successfully 2+ times, add hint to stop loop + if recent_success_count >= 2: + logger.warning( + f"⚠️ Detected potential loop: '{tool_name}' called {recent_success_count} times " + f"with same args. Adding hint to LLM to provide final response." + ) + # Add a gentle hint message to guide LLM to respond + self.messages.append({ + "role": "user", + "content": [{ + "type": "text", + "text": "工具已成功执行并返回结果。请基于这些信息向用户做出回复,不要重复调用相同的工具。" + }] + }) elif tool_calls: # If we have tool_calls but no tool_result_blocks (unexpected error), # create error results for all tool calls to maintain message integrity diff --git a/bridge/bridge.py b/bridge/bridge.py index 4c686f9..9978cbe 100644 --- a/bridge/bridge.py +++ b/bridge/bridge.py @@ -36,6 +36,9 @@ class Bridge(object): self.btype["chat"] = const.QWEN if model_type in [const.QWEN_TURBO, const.QWEN_PLUS, const.QWEN_MAX]: self.btype["chat"] = const.QWEN_DASHSCOPE + # Support Qwen3 and other DashScope models + if model_type and (model_type.startswith("qwen") or model_type.startswith("qwq") or model_type.startswith("qvq")): + self.btype["chat"] = const.QWEN_DASHSCOPE if model_type and model_type.startswith("gemini"): self.btype["chat"] = const.GEMINI if model_type and model_type.startswith("glm"): diff --git a/config-template.json b/config-template.json index e72a995..066f0bf 100644 --- a/config-template.json +++ b/config-template.json @@ -8,6 +8,7 @@ "gemini_api_key": "", "gemini_api_base": "https://generativelanguage.googleapis.com", "zhipu_ai_api_key": "", + "dashscope_api_key": "", "voice_to_text": "openai", "text_to_voice": "openai", "voice_reply_voice": false, diff --git a/models/dashscope/dashscope_bot.py b/models/dashscope/dashscope_bot.py index 75f4ea0..45b8ac5 100644 --- a/models/dashscope/dashscope_bot.py +++ b/models/dashscope/dashscope_bot.py @@ -1,5 +1,6 @@ # encoding:utf-8 +import json from models.bot import Bot from models.session_manager import SessionManager from bridge.context import ContextType @@ -17,7 +18,15 @@ dashscope_models = { "qwen-turbo": dashscope.Generation.Models.qwen_turbo, "qwen-plus": dashscope.Generation.Models.qwen_plus, "qwen-max": dashscope.Generation.Models.qwen_max, - "qwen-bailian-v1": dashscope.Generation.Models.bailian_v1 + "qwen-bailian-v1": dashscope.Generation.Models.bailian_v1, + # Qwen3 series models - use string directly as model name + "qwen3-max": "qwen3-max", + "qwen3-plus": "qwen3-plus", + "qwen3-turbo": "qwen3-turbo", + # Other new models + "qwen-long": "qwen-long", + "qwq-32b-preview": "qwq-32b-preview", + "qvq-72b-preview": "qvq-72b-preview" } # ZhipuAI对话模型API class DashscopeBot(Bot): @@ -115,3 +124,404 @@ class DashscopeBot(Bot): return self.reply_text(session, retry_count + 1) else: return result + + def call_with_tools(self, messages, tools=None, stream=False, **kwargs): + """ + Call DashScope API with tool support for agent integration + + This method handles: + 1. Format conversion (Claude format → DashScope format) + 2. System prompt injection + 3. API calling with DashScope SDK + 4. Thinking mode support (enable_thinking for Qwen3) + + Args: + messages: List of messages (may be in Claude format from agent) + tools: List of tool definitions (may be in Claude format from agent) + stream: Whether to use streaming + **kwargs: Additional parameters (max_tokens, temperature, system, etc.) + + Returns: + Formatted response or generator for streaming + """ + try: + # Convert messages from Claude format to DashScope format + messages = self._convert_messages_to_dashscope_format(messages) + + # Convert tools from Claude format to DashScope format + if tools: + tools = self._convert_tools_to_dashscope_format(tools) + + # Handle system prompt + system_prompt = kwargs.get('system') + if system_prompt: + # Add system message at the beginning if not already present + if not messages or messages[0].get('role') != 'system': + messages = [{"role": "system", "content": system_prompt}] + messages + else: + # Replace existing system message + messages[0] = {"role": "system", "content": system_prompt} + + # Build request parameters + model_name = kwargs.get("model", self.model_name) + + parameters = { + "result_format": "message", # Required for tool calling + "temperature": kwargs.get("temperature", conf().get("temperature", 0.85)), + "top_p": kwargs.get("top_p", conf().get("top_p", 0.8)), + } + + # Add max_tokens if specified + if kwargs.get("max_tokens"): + parameters["max_tokens"] = kwargs["max_tokens"] + + # Add tools if provided + if tools: + parameters["tools"] = tools + # Add tool_choice if specified + if kwargs.get("tool_choice"): + parameters["tool_choice"] = kwargs["tool_choice"] + + # Add thinking parameters for Qwen3 models (disabled by default for stability) + if "qwen3" in model_name.lower() or "qwq" in model_name.lower(): + # Only enable thinking mode if explicitly requested + enable_thinking = kwargs.get("enable_thinking", False) + if enable_thinking: + parameters["enable_thinking"] = True + + # Set thinking budget if specified + if kwargs.get("thinking_budget"): + parameters["thinking_budget"] = kwargs["thinking_budget"] + + # Qwen3 requires incremental_output=true in thinking mode + if stream: + parameters["incremental_output"] = True + + # Always use incremental_output for streaming (for better token-by-token streaming) + # This is especially important for tool calling to avoid incomplete responses + if stream: + parameters["incremental_output"] = True + + # Make API call with DashScope SDK + if stream: + return self._handle_stream_response(model_name, messages, parameters) + else: + return self._handle_sync_response(model_name, messages, parameters) + + except Exception as e: + error_msg = str(e) + logger.error(f"[DASHSCOPE] call_with_tools error: {error_msg}") + if stream: + def error_generator(): + yield { + "error": True, + "message": error_msg, + "status_code": 500 + } + return error_generator() + else: + return { + "error": True, + "message": error_msg, + "status_code": 500 + } + + def _handle_sync_response(self, model_name, messages, parameters): + """Handle synchronous DashScope API response""" + try: + # Set API key before calling + dashscope.api_key = self.api_key + + response = dashscope.Generation.call( + model=dashscope_models.get(model_name, model_name), + messages=messages, + **parameters + ) + + if response.status_code == HTTPStatus.OK: + # Convert DashScope response to OpenAI-compatible format + choice = response.output.choices[0] + return { + "id": response.request_id, + "object": "chat.completion", + "created": 0, + "model": model_name, + "choices": [{ + "index": 0, + "message": { + "role": choice.message.role, + "content": choice.message.content, + "tool_calls": self._convert_tool_calls_to_openai_format( + choice.message.get("tool_calls") + ) + }, + "finish_reason": choice.finish_reason + }], + "usage": { + "prompt_tokens": response.usage.input_tokens, + "completion_tokens": response.usage.output_tokens, + "total_tokens": response.usage.total_tokens + } + } + else: + logger.error(f"[DASHSCOPE] API error: {response.code} - {response.message}") + return { + "error": True, + "message": response.message, + "status_code": response.status_code + } + + except Exception as e: + logger.error(f"[DASHSCOPE] sync response error: {e}") + return { + "error": True, + "message": str(e), + "status_code": 500 + } + + def _handle_stream_response(self, model_name, messages, parameters): + """Handle streaming DashScope API response""" + try: + # Set API key before calling + dashscope.api_key = self.api_key + + responses = dashscope.Generation.call( + model=dashscope_models.get(model_name, model_name), + messages=messages, + stream=True, + **parameters + ) + + # Stream chunks to caller, converting to OpenAI format + for response in responses: + if response.status_code != HTTPStatus.OK: + logger.error(f"[DASHSCOPE] Stream error: {response.code} - {response.message}") + yield { + "error": True, + "message": response.message, + "status_code": response.status_code + } + continue + + # Get choice - use try-except because DashScope raises KeyError on hasattr() + try: + if isinstance(response.output, dict): + choice = response.output['choices'][0] + else: + choice = response.output.choices[0] + except (KeyError, AttributeError, IndexError) as e: + logger.warning(f"[DASHSCOPE] Cannot get choice: {e}") + continue + + # Get finish_reason safely + finish_reason = None + try: + if isinstance(choice, dict): + finish_reason = choice.get('finish_reason') + else: + finish_reason = choice.finish_reason + except (KeyError, AttributeError): + pass + + # Convert to OpenAI-compatible format + openai_chunk = { + "id": response.request_id, + "object": "chat.completion.chunk", + "created": 0, + "model": model_name, + "choices": [{ + "index": 0, + "delta": {}, + "finish_reason": finish_reason + }] + } + + # Get message safely - use try-except + message = {} + try: + if isinstance(choice, dict): + message = choice.get('message', {}) + else: + message = choice.message + except (KeyError, AttributeError): + pass + + # Add role if present + role = None + try: + if isinstance(message, dict): + role = message.get('role') + else: + role = message.role + except (KeyError, AttributeError): + pass + if role: + openai_chunk["choices"][0]["delta"]["role"] = role + + # Add content if present + content = None + try: + if isinstance(message, dict): + content = message.get('content') + else: + content = message.content + except (KeyError, AttributeError): + pass + if content: + openai_chunk["choices"][0]["delta"]["content"] = content + + # Add tool_calls if present + # DashScope's response object raises KeyError on hasattr() if attr doesn't exist + # So we use try-except instead + tool_calls = None + try: + if isinstance(message, dict): + tool_calls = message.get('tool_calls') + else: + tool_calls = message.tool_calls + except (KeyError, AttributeError): + pass + + if tool_calls: + openai_chunk["choices"][0]["delta"]["tool_calls"] = self._convert_tool_calls_to_openai_format(tool_calls) + + yield openai_chunk + + except Exception as e: + logger.error(f"[DASHSCOPE] stream response error: {e}") + yield { + "error": True, + "message": str(e), + "status_code": 500 + } + + def _convert_tools_to_dashscope_format(self, tools): + """ + Convert tools from Claude format to DashScope format + + Claude format: {name, description, input_schema} + DashScope format: {type: "function", function: {name, description, parameters}} + """ + if not tools: + return None + + dashscope_tools = [] + for tool in tools: + # Check if already in DashScope/OpenAI format + if 'type' in tool and tool['type'] == 'function': + dashscope_tools.append(tool) + else: + # Convert from Claude format + dashscope_tools.append({ + "type": "function", + "function": { + "name": tool.get("name"), + "description": tool.get("description"), + "parameters": tool.get("input_schema", {}) + } + }) + + return dashscope_tools + + def _convert_messages_to_dashscope_format(self, messages): + """ + Convert messages from Claude format to DashScope format + + Claude uses content blocks with types like 'tool_use', 'tool_result' + DashScope uses 'tool_calls' in assistant messages and 'tool' role for results + """ + if not messages: + return [] + + dashscope_messages = [] + + for msg in messages: + role = msg.get("role") + content = msg.get("content") + + # Handle string content (already in correct format) + if isinstance(content, str): + dashscope_messages.append(msg) + continue + + # Handle list content (Claude format with content blocks) + if isinstance(content, list): + # Check if this is a tool result message (user role with tool_result blocks) + if role == "user" and any(block.get("type") == "tool_result" for block in content): + # Convert each tool_result block to a separate tool message + for block in content: + if block.get("type") == "tool_result": + dashscope_messages.append({ + "role": "tool", + "content": block.get("content", ""), + "tool_call_id": block.get("tool_use_id") # DashScope uses 'tool_call_id' + }) + + # Check if this is an assistant message with tool_use blocks + elif role == "assistant": + # Separate text content and tool_use blocks + text_parts = [] + tool_calls = [] + + for block in content: + if block.get("type") == "text": + text_parts.append(block.get("text", "")) + elif block.get("type") == "tool_use": + tool_calls.append({ + "id": block.get("id"), + "type": "function", + "function": { + "name": block.get("name"), + "arguments": json.dumps(block.get("input", {})) + } + }) + + # Build DashScope format assistant message + dashscope_msg = { + "role": "assistant" + } + + # Add content only if there is actual text + # DashScope API: when tool_calls exist, content should be None or omitted if empty + if text_parts: + dashscope_msg["content"] = " ".join(text_parts) + elif not tool_calls: + # If no tool_calls and no text, set empty string (rare case) + dashscope_msg["content"] = "" + # If there are tool_calls but no text, don't set content field at all + + if tool_calls: + dashscope_msg["tool_calls"] = tool_calls + + dashscope_messages.append(dashscope_msg) + else: + # Other list content, keep as is + dashscope_messages.append(msg) + else: + # Other formats, keep as is + dashscope_messages.append(msg) + + return dashscope_messages + + def _convert_tool_calls_to_openai_format(self, tool_calls): + """Convert DashScope tool_calls to OpenAI format""" + if not tool_calls: + return None + + openai_tool_calls = [] + for tool_call in tool_calls: + # DashScope format is already similar to OpenAI + if isinstance(tool_call, dict): + openai_tool_calls.append(tool_call) + else: + # Handle object format + openai_tool_calls.append({ + "id": getattr(tool_call, 'id', None), + "type": "function", + "function": { + "name": tool_call.function.name, + "arguments": tool_call.function.arguments + } + }) + + return openai_tool_calls