コンテンツにスキップ

クイックスタート

  1. プロジェクトを作成し、npm を初期化します。これは一度だけ実行すれば大丈夫です

    Terminal window
    mkdir my_project
    cd my_project
    npm init -y
  2. Agents SDK をインストールします

    Terminal window
    npm install @openai/agents zod@3
  3. OpenAI API key を設定します。まだ持っていない場合は、OpenAI API key を作成するためにこれらの手順に従ってください

    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 メソッドを使ってエージェントを実行できます。開始するエージェントと、渡したい入力の両方を渡すことで実行をトリガーします

これにより、その実行中に行われた最終出力とアクションを含む実行結果が返ります

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);

情報の参照やアクションの実行のために、エージェントにツールを与えることができます

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 output
const 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 ファイルに配置して実行します

Quickstart
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 ダッシュボードのトレースビューアに移動してください

より複雑なエージェントフローの構築方法を学びましょう: