跳转到内容

快速开始

  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 密钥。如果还没有密钥,请按照这些说明创建 OpenAI API 密钥。

    Terminal window
    export OPENAI_API_KEY=sk-...

    或者,你也可以调用 setDefaultOpenAIKey('<api 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 属性,了解哪个智能体生成了最终响应。

运行器负责执行各个智能体、所有交接以及所有工具调用。

运行智能体编排
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 viewer

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