欢迎来到 OpenAI Agents SDK 之旅的第一步!本教程将向你介绍使用 OpenAI 的 Agents SDK 创建一个简单 AI 代理的基本概念。
OpenAI 代理是一种可编程的 AI 助手,它可以:
把它想象成一个利用 AI 来处理复杂任务的智能函数。
OpenAI SDK 中创建 AI 代理的主要构建模块:
from agents import Agent
name: Unique identifier for your agentinstructions: How your agent should behavemodel: The AI model to use (defaults to "gpt-4o")This tutorial includes two focused agent examples:
personal_assistant_agent/)execution_demo_agent/)1_starter_agent/ ├── README.md # This file - concept explanation ├── requirements.txt # Dependencies ├── personal_assistant_agent/ # Basic agent creation │ ├── __init__.py │ └── agent.py # Simple agent definition (20 lines) ├── execution_demo_agent/ # Execution methods demonstration │ ├── __init__.py │ └── agent.py # Sync, async, streaming examples ├── app.py # Streamlit web interface (optional) └── env.example # Environment variables template
完成本教程后,你将掌握:
设置你的环境:
# Make sure you have your OpenAI API key # Get your API key from: https://platform.openai.com/api-keys
安装 OpenAI Agents SDK:
pip install openai-agents
安装依赖项:
# Install required packages pip install -r requirements.txt
设置环境变量:
# Copy the example environment file cp .env.example .env # Edit .env and add your OpenAI API key # OPENAI_API_KEY=sk-your_openai_key_here
测试代理:
# Run the agent directly python agent.py # Or run the Streamlit web interface streamlit run app.py
尝试不同的执行方式:
完成本教程后,你将准备好:
.env file contains a valid OPENAI_API_KEYpip install -r requirements.txt免责声明:
本文档采用基于机器的 AI 翻译服务进行翻译。尽管我们力求准确,但请注意,自动翻译可能存在错误或不准确之处。应以原文语言版本的文档作为权威依据。如需获取关键信息,建议使用专业的人工翻译。对于因使用本翻译而产生的任何误解或误读,我们概不负责。