快速开始
-
创建一个项目并初始化 npm。您只需要执行一次。
Terminal window mkdir my_projectcd my_projectnpm init -y -
安装 Agents SDK 和 Zod。SDK 使用 Zod v4 来定义工具 schema 和 structured outputs。
Terminal window npm install @openai/agents zod -
设置 OpenAI API 密钥。如果您还没有,请按照这些说明创建 OpenAI API 密钥。
Terminal window export OPENAI_API_KEY=sk-...或者,您也可以通过编程方式调用
setDefaultOpenAIKey('<api key>')来设置密钥,并使用setTracingExportApiKey('<api key>')进行追踪。更多详情请参阅SDK 配置。
创建您的第一个智能体
Section titled “创建您的第一个智能体”智能体通过 instructions 和名称来定义。
import { Agent } from '@openai/agents';
const agent = new Agent({ name: 'History Tutor', instructions: 'You provide assistance with historical queries. Explain important events and context clearly.',});运行您的第一个智能体
Section titled “运行您的第一个智能体”您可以使用 run 方法来运行智能体。通过同时传入要启动的智能体和要传递的输入,即可触发一次运行。
这将返回一个执行结果,其中包含最终输出以及该次运行期间执行的所有操作。
import { Agent, run } from '@openai/agents';
const agent = new Agent({ name: 'History Tutor', instructions: 'You provide assistance with historical queries. Explain important events and context clearly.',});
const result = await run(agent, 'When did sharks first appear?');
console.log(result.finalOutput);对于第二轮,您可以将 result.history 传回 run(),附加一个会话,或通过 conversationId / previousResponseId 复用 OpenAI 服务器管理的状态。运行智能体指南对这些方法进行了比较。
为智能体提供工具
Section titled “为智能体提供工具”您可以为智能体提供工具,以便其查找信息或执行操作。
import { Agent, tool } from '@openai/agents';import { z } from 'zod';
const historyFunFact = tool({ // The name of the tool will be used by the agent to tell what tool to use. name: 'history_fun_fact', // The description is used to describe when to use the tool by telling it what it does. description: 'Give a fun fact about a historical event', // This tool takes no parameters, so we provide an empty Zod object. parameters: z.object({}), execute: async () => { // The output will be returned back to the agent to use. return 'Sharks are older than trees.'; },});
const agent = new Agent({ name: 'History Tutor', instructions: 'You provide assistance with historical queries. Explain important events and context clearly.', // Add the tool to the agent. tools: [historyFunFact],});添加更多智能体
Section titled “添加更多智能体”您也可以用类似方式定义更多智能体,将问题拆分为更小的部分,使智能体更专注于当前任务。这样还可以通过在智能体上定义模型,为不同问题使用不同模型。
import { Agent } from '@openai/agents';
const historyTutorAgent = new Agent({ name: 'History Tutor', instructions: 'You provide assistance with historical queries. Explain important events and context clearly.',});
const mathTutorAgent = new Agent({ name: 'Math Tutor', instructions: 'You provide help with math problems. Explain your reasoning at each step and include examples',});为了在多个智能体之间进行编排,您可以为智能体定义 handoffs。这样智能体就能将对话传递给下一个智能体。这会在一次运行过程中自动发生。
import { Agent } from '@openai/agents';
const historyTutorAgent = new Agent({ name: 'History Tutor', instructions: 'You provide assistance with historical queries. Explain important events and context clearly.',});
const mathTutorAgent = new Agent({ name: 'Math Tutor', instructions: 'You provide help with math problems. Explain your reasoning at each step and include examples',});
// Use Agent.create() to keep handoff output types aligned.const triageAgent = Agent.create({ name: 'Triage Agent', instructions: "You determine which agent to use based on the user's homework question", handoffs: [historyTutorAgent, mathTutorAgent],});运行结束后,您可以通过查看执行结果上的 lastAgent 属性,了解是哪个智能体生成了最终响应。
运行智能体编排
Section titled “运行智能体编排”运行器负责执行各个智能体、所有交接以及所有工具调用。
import { Agent, run } from '@openai/agents';
const historyTutorAgent = new Agent({ name: 'History Tutor', instructions: 'You provide assistance with historical queries. Explain important events and context clearly.',});
const mathTutorAgent = new Agent({ name: 'Math Tutor', instructions: 'You provide help with math problems. Explain your reasoning at each step and include examples',});
const triageAgent = Agent.create({ name: 'Triage Agent', instructions: "You determine which agent to use based on the user's homework question", handoffs: [historyTutorAgent, mathTutorAgent],});
async function main() { const result = await run(triageAgent, 'What is the capital of France?'); console.log(result.finalOutput);}
main().catch((err) => console.error(err));让我们将以上内容整合成一个完整示例。将其放入您的 index.js 文件并运行。如果您的应用已经配置为 TypeScript,也可以改用 index.ts。
import { Agent, run } from '@openai/agents';
const historyTutorAgent = new Agent({ name: 'History Tutor', instructions: 'You provide assistance with historical queries. Explain important events and context clearly.',});
const mathTutorAgent = new Agent({ name: 'Math Tutor', instructions: 'You provide help with math problems. Explain your reasoning at each step and include examples',});
const triageAgent = Agent.create({ name: 'Triage Agent', instructions: "You determine which agent to use based on the user's homework question", handoffs: [historyTutorAgent, mathTutorAgent],});
async function main() { const result = await run(triageAgent, 'What is the capital of France?'); console.log(result.finalOutput);}
main().catch((err) => console.error(err));Agents SDK 会自动为您生成追踪。这使您可以查看智能体是如何运行的、调用了哪些工具,或交接给了哪个智能体。
要查看智能体运行期间发生的情况,请前往 OpenAI Dashboard 中的追踪查看器。
了解如何构建更复杂的智能体工作流: