快速开始
-
创建项目并初始化 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));Agents SDK 会自动为您生成追踪。您可以据此审查智能体的运行情况、调用了哪些工具或将会话交接给了哪个智能体。
要查看智能体运行期间发生的情况,请前往 OpenAI Dashboard 中的 Trace viewer。
了解如何构建更复杂的智能体流程: