文档

函数调用

更新时间:

Assistant API 支持函数调用(Function Calling),让智能体能够根据您的需求自动调用外部函数。例如,智能体可以调用函数查询天气或安排日程。本文介绍了一个简单的“天气查询智能体”示例,帮助您快速上手函数调用的基本方法。

快速开始

在这个示例中,我们将创建一个天气查询智能体,并创建一个函数get_weather作为智能体可以调用的工具。在这个示例中,我们将询问智能体“杭州今天的天气状况”。

在您开始前

首先,确保您已经安装了必要的依赖库,如 requests 和 dashscope。您可以通过以下命令安装它们:

pip install requests dashscope

其次,您需要参考高德天气API以获取高德天气查询的API-KEY。

第一步:创建“获取天气信息”函数

我们首先使用了高德地图的API来获取城市的天气信息。

import requests

def get_weather(city):
    # 请替换为你的高德API密钥
    amap_key = "YOUR_AMAP_API_KEY"
   
    # 获取城市编码
    city_url = f"https://restapi.amap.com/v3/config/district?keywords={city}&subdistrict=0&key={amap_key}"
    city_response = requests.get(city_url)
    city_data = city_response.json()
    
    if city_data['count'] == '0':
        return f"无法找到城市: {city}"
   
    adcode = city_data['districts'][0]['adcode']
    
    # 获取天气信息
    weather_url = f"https://restapi.amap.com/v3/weather/weatherInfo?city={adcode}&key={amap_key}"
    weather_response = requests.get(weather_url)
    weather_data = weather_response.json()
    
    if weather_data['status'] == '1' and weather_data['lives']:
        live_weather = weather_data['lives'][0]
        return f"{city}的天气:温度 {live_weather['temperature']}°C,天气 {live_weather['weather']},风向 {live_weather['winddirection']},风力 {live_weather['windpower']}级"
    else:
        return f"无法获取{city}的天气信息"

解释:

  • 获取城市编码:通过请求高德API来查询城市的行政编码(adcode),这是获取天气信息的必要参数。

  • 获取天气信息:使用城市的 adcode 来获取该城市的当前天气。

现在我们将通过 Assistant API 来创建一个智能体,该智能体将自动处理用户查询,并调用我们定义的 get_weather 函数来提供天气信息。

第二步:定义工具

我们首先定义一个工具,它描述了一个可以被智能体调用的函数(如 get_weather),用来获取天气信息。

from dashscope import Assistants, Messages, Runs, Threads
import json

# 定义天气查询工具
weather_tool = {
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "获取指定城市的当前天气信息",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {
                    "type": "string",
                    "description": "要查询天气的城市名称"
                }
            },
            "required": ["city"]
        }
    }
}

解释:

  • name: 函数的名称为 get_weather,将在智能体中被调用。

  • description: 提供了该工具的描述,帮助用户理解其用途。

  • parameters: 定义了函数的参数,这里是城市名称 city。

第三步:创建智能体

现在我们可以创建一个 Assistant 实例,它是一个智能体,并且将会使用我们定义的天气查询工具。

# 创建 Assistant
assistant = Assistants.create(
    model='qwen-plus',
    name='天气查询智能体',
    description='一个智能体,可以查询天气信息并解答用户的问题',
    instructions='你是一个智能体,你的任务是解答用户的问题,特别是关于天气的查询。当用户询问天气时,使用 get_weather 函数获取实时天气信息。',
    tools=[weather_tool]
)

解释:

  • model: 使用的模型类型,这里是 qwen-plus,它支持语言理解和任务处理。

  • name: 给智能体命名为“天气查询智能体”。

  • description: 描述了智能体的功能,它能够帮助用户查询天气。

  • tools: 注册了我们之前定义的 weather_tool,使得智能体可以调用该工具。

第四步:创建对话线程并与智能体互动

创建一个新的对话线程,并向其中添加用户消息,然后运行智能体,处理用户的问题。

# 创建一个新的线程
thread = Threads.create()

# 添加用户消息到线程
Messages.create(thread_id=thread.id, role="user", content="杭州今年中秋节的天气怎么样?")

# 运行 Assistant
run = Runs.create(thread_id=thread.id, assistant_id=assistant.id)

# 等待运行完成
run = Runs.wait(thread_id=thread.id, run_id=run.id)

解释:

  • Threads.create(): 创建一个新的对话线程,后续的消息将在此线程中进行。

  • Messages.create(): 向线程添加用户消息,这里用户询问了“杭州今年中秋节的天气怎么样?”

  • Runs.create(): 触发智能体开始处理用户消息。

  • Runs.wait(): 等待智能体处理完毕。

第五步:处理函数调用并返回结果

如果智能体在处理过程中需要调用工具,我们将调用 get_weather 函数获取天气信息,并将结果返回给用户。

# 检查是否需要调用函数
if run.required_action:
    for tool_call in run.required_action.submit_tool_outputs.tool_calls:
        if tool_call.function.name == "get_weather":
            city = json.loads(tool_call.function.arguments)["city"]
            weather_info = get_weather(city)
            
            # 提交工具输出
            Runs.submit_tool_outputs(
                thread_id=thread.id,
                run_id=run.id,
                tool_outputs=[{"tool_call_id": tool_call.id, "output": weather_info}]
            )
            
            # 等待新的运行完成
            run = Runs.wait(thread_id=thread.id, run_id=run.id)
            

解释:

  • 检查函数调用:如果智能体需要调用某个函数,我们检查它调用的是否是 get_weather,并通过之前定义的函数获取天气信息。

  • 提交结果:将结果通过 Runs.submit_tool_outputs 返回给智能体,并继续等待智能体的下一步回复。

第六步:获取智能体的回复

智能体处理完成后,我们可以从对话线程中获取智能体的回复并展示给用户。

# 获取 Assistant 的回复
messages = Messages.list(thread_id=thread.id)
for message in messages.data:
    if message.role == "assistant":
        print(f"Assistant: {message.content}")

总结

通过以上步骤,您已经成功创建了一个智能体,它可以处理用户的天气查询请求,并调用高德API来获取实时天气信息。Assistant API 使得构建复杂的任务驱动型智能体变得简单且高效。

您可以根据需求进一步扩展智能体的功能,例如增加更多工具或修改智能体的行为指令。