快速开始
-
创建一个项目并初始化 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>')
以编程方式设置密钥,并使用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));
查看你的追踪
Section titled “查看你的追踪”Agents SDK 会自动为你生成追踪。这使你能够审查智能体的运行情况、调用了哪些工具或交接给了哪个智能体。
要查看智能体运行期间发生的情况,请前往 OpenAI Dashboard 的 Trace viewer。
了解如何构建更复杂的智能体流程: