跳转到内容

快速开始

  1. 创建一个项目并初始化 npm。你只需要做一次。

    Terminal window
    mkdir my_project
    cd my_project
    npm init -y
  2. 安装 Agents SDK 和 Zod。SDK 使用 Zod v4 来定义工具 schema 和 structured outputs。

    Terminal window
    npm install @openai/agents zod
  3. 设置 OpenAI API key。如果你还没有,请按照这些说明创建一个 OpenAI API key。

    Terminal window
    export OPENAI_API_KEY=sk-...

    或者,你也可以调用 setDefaultOpenAIKey('<api key>') 以编程方式设置 key,并使用 setTracingExportApiKey('<api key>') 进行追踪。 更多细节请参阅SDK 配置指南

智能体通过 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.',
});

你可以使用 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 服务器管理的状态。运行智能体 指南对比了这些方法。

你可以为智能体提供工具,用于查询信息或执行操作。

添加工具
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],
});

你也可以类似地定义更多智能体,将问题拆分为更小的部分,让智能体更专注于当前任务。 此外,通过在智能体上定义模型,你还可以针对不同问题使用不同模型。

创建专家智能体
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 属性,确认是哪个智能体生成了最终响应。

runner 会处理单个智能体执行、交接以及工具调用。

运行智能体编排
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 中的 Trace 查看器

了解如何构建更复杂的智能体流程: