AI App: Tool Use Design Pattern 工具使用设计模式

1. 什么是工具使用设计模式

工具使用设计模式侧重于赋予大型语言模型与外部工具交互的能力,以实现特定目标。

工具是代理可以执行的代码,用于执行作。工具可以是简单的函数,比如计算器,也可以是调用第三方服务(如股票价格查询或天气预报)的 API。

在人工智能代理的背景下,工具设计为由代理响应模型生成的函数调用来执行。

2. 工具使用设计模式应用场景

人工智能代理可以利用工具完成复杂任务、获取信息或做出决策,包括:

  • 动态信息检索:通过调用外部API获取数据
  • 代码执行与解释: 代理可以执行代码或脚本来解决问题、生成报告等。
  • 工作流程自动化: 集成任务调度器、管道等工具,自动化重复或多步骤的工作流
  • 客户支持: 客服可以与客户关系管理系统、工单平台或知识库交互,解决用户查询。
  • 内容生成与编辑: 代理可以利用语法检查器、文本摘要器或内容安全评估器等工具,协助内容创作任务。

3. Coding

确保你使用的LLM支持函数调用,不确定的可以去豆包查一下,这里用的 qwen3-max。

https://github.com/Selene1225/ai_learning/blob/main/app/tool_bot/tool_chat_bot.py

核心代码:定义tool function

python 复制代码
# 定义 tool function
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_current_time",
            "description": "获取当前城市的时间",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "提供location的英文去获取时间, e.g. San Francisco"
                    }
                },
                "required": ["location"]
            }
        }
    }
]

# 用户输入
messages = [{
    "role": "user",
    "content": input("你想知道哪个城市的时间? ")
}]

# openai LLM
openai.api_key = api_key
openai.base_url = base_url
openai.api_type = "openai"
response = openai.chat.completions.create(
    model=model_name,
    messages=messages,
    tools=tools,
    tool_choice="auto"
)

response_message = response.choices[0].message
messages.append(response_message)

# LLM处理用户输入得到function参数
if response_message.tool_calls:
    for tool_call in response_message.tool_calls:
        if tool_call.function.name == "get_current_time":
            # 接收 LLM 返回的arg,调用工具
            tool_args = json.loads(tool_call.function.arguments)
            time_response = get_current_time(tool_args["location"])
            messages.append({
                "tool_call_id": tool_call.id,
                "role": "tool",
                "name": "get_current_time",
                "content": time_response
            })
        else:
            print(f"Unknown tool call: {tool_call.function.name}")

# LLM 完成「工具结果的 "二次处理"」,最终返回符合用户需求的自然语言响应
final_response = openai.chat.completions.create(
    model=model_name,
    messages=messages,
)

print(final_response.choices[0].message.content)

function关键属性:

bash 复制代码
Tool Function: 
name # 我们想要调用的函数名称。
description # 这是对该函数工作原理的描述。这里需要具体且清晰。
parameters # 你希望模型在响应中生成的数值和格式列表。参数数组由具有以下属性的项组成:
    type # 属性的数据类型将存储于 。
    properties # 模型响应时将使用的具体值列表
        name # 键是模型在其格式化响应中使用的属性名称,例如产品。
        type # 该属性的数据类型,例如字符串 。
        description # 具体物业的描述。

用户输入先丢给LLM处理,提取 tool 需要的入参,然后调用tool的function拿到response,最后再丢给LLM得到一个自然语言的输出反馈给用户。

对用户来说,提问和结果都是自然语言;对代码而言,入参和输出都是标准的代码结构,不需要额外处理参数格式等。

4. App

完整代码:https://github.com/Selene1225/ai_learning/tree/main/app/tool_bot

python 复制代码
app/tool_bot/
├── index.html          # 前端页面
├── app.py              # Flask 后端服务
├── requirements.txt    # 后端依赖
├── timezone_config.json # 时区配置文件
├── tool_chat_bot.py    # 原始的工具调用脚本
└── README.md           # 项目说明文档
相关推荐
GISer_Jing1 天前
智能体工具使用、规划模式
人工智能·设计模式·prompt·aigc
GISer_Jing1 天前
AI Agent:学习与适应、模型上下文协议
人工智能·学习·设计模式·aigc
小马爱打代码1 天前
MyBatis设计模式:构建者、工厂、代理模式
设计模式·mybatis·代理模式
月明长歌1 天前
Javasynchronized 原理拆解:锁升级链路 + JVM 优化 + CAS 与 ABA 问题(完整整合版)
java·开发语言·jvm·安全·设计模式
会员果汁1 天前
12.设计模式-状态模式
设计模式·状态模式
Yu_Lijing1 天前
基于C++的《Head First设计模式》笔记——抽象工厂模式
c++·笔记·设计模式
会员果汁1 天前
13.设计模式-适配器模式
设计模式·适配器模式
GISer_Jing2 天前
AI:多智能体协作与记忆管理
人工智能·设计模式·aigc
雨中飘荡的记忆2 天前
责任链模式实战应用:从理论到生产实践
设计模式
沛沛老爹2 天前
Web开发者进阶AI:Agent技能设计模式之迭代分析与上下文聚合实战
前端·人工智能·设计模式