教程 3:工具使用代理 欢迎来到工具的世界!本教程将教你如何创建能够使用自定义函数和内置工具来执行特定任务的代理。正是在这里,你的代理才真正变得强大,具备在现实世界中采取行动的能力。 你将学到的内容 函数工具:创建自定义 Python 函数作为代理工具 内置工具:使用 OpenAI 预建的功能 工具集成:有效地将工具添加到代理中 工具执行:了解代理如何决定何时使用工具 核心概念:OpenAI 代理 SDK 中的工具 工具是你的代理可以调用的函数,用于执行特定任务。
欢迎来到工具的世界!本教程将教你如何创建能够使用自定义函数和内置工具来执行特定任务的代理。正是在这里,你的代理才真正变得强大,具备在现实世界中采取行动的能力。
工具是你的代理可以调用的函数,用于执行特定任务。可以把它们想象成代理的“双手”——它们让代理能够:
┌─────────────────────────────────────────────────────────────┐ │ AGENT WITH TOOLS │ ├─────────────────────────────────────────────────────────────┤ │ │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │ │ INPUT │───▶│ AGENT │───▶│ OUTPUT │ │ │ │ "Calculate │ │ Reasoning │ │ "Using the │ │ │ │ compound │ │ + Tool Use │ │ calculator │ │ │ │ interest" │ │ │ │ tool: $..."│ │ │ └─────────────┘ └─────────────┘ └─────────────┘ │ │ │ │ │ ▼ │ │ ┌─────────────┐ │ │ │ TOOLS │ │ │ │ ┌─────────┐ │ │ │ │ │Calculator│ │ │ │ │ ├─────────┤ │ │ │ │ │Web Search│ │ │ │ │ ├─────────┤ │ │ │ │ │File I/O │ │ │ │ │ └─────────┘ │ │ │ └─────────────┘ │ └─────────────────────────────────────────────────────────────┘
你创建的自定义 Python 函数:
@function_tool def calculate_compound_interest(principal: float, rate: float, time: int) -> float: """Calculate compound interest""" return principal * (1 + rate) ** time
OpenAI 提供了强大的预建工具:
本教程包含三个聚焦的工具集成示例:
3_1_function_tools/)@function_tool decorator usage3_2_builtin_tools/)3_3_agents_as_tools/)3_tool_using_agent/ ├── README.md # This file - concept explanation ├── requirements.txt # Dependencies ├── 3_1_function_tools/ # Custom function tools │ ├── __init__.py │ ├── tools.py # Custom tool definitions │ └── agent.py # Agent with function tools (25 lines) ├── 3_2_builtin_tools/ # Built-in tools integration │ ├── __init__.py │ └── agent.py # Agent with built-in tools (30 lines) ├── 3_3_agents_as_tools/ # Agents as tools pattern │ ├── __init__.py │ ├── agent.py # Basic agent orchestration (40 lines) │ └── advanced_agent.py # Custom agent tools with Runner config ├── app.py # Streamlit web interface (optional) └── env.example # Environment variables template
完成本教程后,你将理解:
@function_tool 创建自定义函数工具安装 OpenAI 代理 SDK:
pip install openai-agents
安装依赖:
pip install -r requirements.txt
设置环境变量:
cp env.example .env # Edit .env and add your OpenAI API key
测试计算器代理:
python calculator_agent.py
测试研究代理:
python research_agent.py
测试数据分析代理:
python data_analysis_agent.py
运行交互式 Web 界面:
streamlit run app.py
尝试这些编排请求:
尝试这些信息请求:
尝试这些数据请求:
@function_tool def add_numbers(a: float, b: float) -> float: """Add two numbers together""" return a + b
@function_tool def get_weather(city: str, units: str = "metric") -> str: """Get current weather for a city""" if not city.strip(): return "Error: City name cannot be empty" # API call logic here return f"Weather data for {city}"
from agents import Agent # Define specialized agents translator = Agent(name="Translator", instructions="Translate text") # Use agent as a tool orchestrator = Agent( name="Orchestrator", instructions="Coordinate translation tasks", tools=[ translator.as_tool( tool_name="translate_text", tool_description="Translate user's message" ) ] )
完成本教程后,你将准备好:
function_tool 装饰器免责声明:
本文档采用基于机器的 AI 翻译服务进行翻译。尽管我们力求准确,但请注意,自动翻译可能存在错误或不准确之处。应以原文语言版本的文档作为权威依据。如需获取关键信息,建议使用专业的人工翻译。对于因使用本翻译而产生的任何误解或误读,我们概不负责。