快速开始
-
创建项目并初始化 npm。此步骤只需执行一次。
Terminal window mkdir my_projectcd my_projectnpm init -y -
安装 Agents SDK。
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 配置。
创建第一个智能体
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);为智能体提供工具
Section titled “为智能体提供工具”您可以为智能体提供工具,以查询信息或执行操作。
import { Agent, tool } from '@openai/agents';
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.', // Adding the tool to the agent tools: [historyFunFact],});添加更多智能体
Section titled “添加更多智能体”可以以类似方式定义更多智能体,将问题拆分为更小的部分,使智能体更专注于当前任务。通过在智能体上定义模型,也可以针对不同问题使用不同的模型。
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。这将使其能够在对话过程中自动将会话交接给下一个智能体。
// Using the Agent.create method to ensures type safety for the final outputconst triageAgent = Agent.create({ name: 'Triage Agent', instructions: "You determine which agent to use based on the user's homework question", handoffs: [historyTutorAgent, mathTutorAgent],});运行结束后,您可以通过查看运行结果中的 finalAgent 属性,了解是哪一个智能体生成了最终响应。
运行智能体编排
Section titled “运行智能体编排”Runner 负责处理各个智能体的执行、可能的交接以及工具执行。
import { run } from '@openai/agents';
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 文件并运行。
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 = new Agent({ 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。
了解如何构建更复杂的智能体流程: