クイックスタート
プロジェクトセットアップ
Section titled “プロジェクトセットアップ”-
プロジェクトを作成して npm を初期化します。これは一度だけで構いません。
Terminal window mkdir my_projectcd my_projectnpm init -y -
Agents SDK をインストールします。
Terminal window npm install @openai/agents -
OpenAI API キーを設定します。まだお持ちでない場合は、こちらの手順に従って OpenAI API キーを作成してください。
Terminal window export OPENAI_API_KEY=sk-...代わりに
setDefaultOpenAIKey('<api key>')
を呼び出して API キーを設定することもできます。トレーシング用にはsetTracingExportApiKey('<api key>')
を使用してください。詳しくは 設定ガイド をご覧ください。
最初のエージェントの作成
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', 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',});
ハンドオフの定義
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],});
実行後、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));
すべてをまとめて 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 “次のステップ”より複雑なエージェントフローの構築方法を学びましょう: