クイックスタート
プロジェクトのセットアップ
Section titled “プロジェクトのセットアップ”-
プロジェクトを作成して npm を初期化します。この作業は最初の一度だけで大丈夫です
Terminal window mkdir my_projectcd my_projectnpm init -y -
Agents SDK をインストールします
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 の設定をご覧ください
はじめてのエージェントの作成
Section titled “はじめてのエージェントの作成”エージェントは instructions と name で定義します
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
メソッドでエージェントを実行できます。開始したいエージェントと、渡したい入力の両方を渡して実行します
これにより、その実行中に行われた最終出力とあらゆるアクションを含む execution result が返されます
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 “いくつかのエージェントの追加”追加のエージェントを同様に定義することで、問題を小さな部分に分解し、エージェントを目の前のタスクにより集中させることができます。また、エージェントごとに model を定義することで、異なる問題に異なるモデルを使うこともできます
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',});
ハンドオフの定義
Section titled “ハンドオフの定義”複数のエージェント間をオーケストレーションするために、エージェントに 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],});
実行後、result の 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));
すべての統合
Section titled “すべての統合”すべてを 1 つの完全な例にまとめましょう。これを 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 ダッシュボードの Trace viewer に移動します
次のステップ
Section titled “次のステップ”より複雑なエージェントフローの構築方法を学びましょう: