You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Agentic AI自定义函数调用失败:找不到fetch_weather函数

修复Azure AI Agents自定义函数调用失败问题

问题根源

报错Function 'fetch_weather' not found是因为函数注册到工具集的方式不符合Azure AI Agents的API要求,你当前用Set包裹函数并传入FunctionTool的写法有误,导致工具无法正确关联到目标函数。

具体修复步骤

1. 修正函数注册逻辑

不要用集合存储函数,改用以下两种正确方式之一:

方式一:直接包装单个函数(简洁版)

from azure.ai.agents import FunctionTool, ToolSet
import json

def fetch_weather(location: str) -> str:
    """
    Fetches the weather information for the specified location.

    :param location: The location to fetch weather for.
    :return: Weather information as a JSON string.
    """
    mock_weather_data = {"New York": "Sunny, 25°C", "London": "Cloudy, 18°C", "Tokyo": "Rainy, 22°C"}
    weather = mock_weather_data.get(location, "Weather data not available for this location.")
    return json.dumps({"weather": weather})

# 直接用FunctionTool包装目标函数
weather_tool = FunctionTool(fetch_weather)
toolset = ToolSet()
toolset.add(weather_tool)

方式二:显式定义函数元数据(适合复杂场景)

如果需要更精准控制函数的描述和参数规则,用FunctionDefinition显式声明:

from azure.ai.agents import FunctionTool, ToolSet
from azure.ai.agents.models import FunctionDefinition
import json

def fetch_weather(location: str) -> str:
    mock_weather_data = {"New York": "Sunny, 25°C", "London": "Cloudy, 18°C", "Tokyo": "Rainy, 22°C"}
    weather = mock_weather_data.get(location, "Weather data not available for this location.")
    return json.dumps({"weather": weather})

# 定义函数元数据
weather_function_def = FunctionDefinition(
    name="fetch_weather",
    description="Fetches the weather information for the specified location.",
    parameters={
        "type": "object",
        "properties": {
            "location": {
                "type": "string",
                "description": "City name to get weather for, e.g., 'New York'"
            }
        },
        "required": ["location"]
    }
)

# 绑定元数据与实现函数
weather_tool = FunctionTool.from_definition(weather_function_def, fetch_weather)
toolset = ToolSet()
toolset.add(weather_tool)

2. 修复Docstring格式

原函数docstring中param的写法错误,去掉参数类型的括号包裹,改为:param location: 描述,否则会干扰工具对参数的解析逻辑。

3. 升级依赖包

确保使用最新稳定版的Azure AI Agents SDK,避免版本兼容问题:

pip install --upgrade azure-ai-agents azure-identity

验证注意事项

  • 代理初始化时必须正确传入toolset参数
  • 测试时使用明确的输入(如“查询纽约的天气”),避免模糊表述导致参数解析失败

内容的提问来源于stack exchange,提问作者Tech_fusion

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 03:56:13