ストリーミング
Agents SDK は、モデルからの出力やその他の実行ステップを段階的に配信できます。ストリーミングを利用すると、UI が応答性を維持し、最終的な実行結果をすべて待たずにユーザーを更新できます。
ストリーミングの有効化
Section titled “ストリーミングの有効化”Runner.run()
に { stream: true }
オプションを渡すと、完全な実行結果ではなくストリーミングオブジェクトが返されます。
import { Agent, run } from '@openai/agents';
const agent = new Agent({ name: 'Storyteller', instructions: 'You are a storyteller. You will be given a topic and you will tell a story about it.',});
const result = await run(agent, 'Tell me a story about a cat.', { stream: true,});
ストリーミングが有効な場合、返される stream
は AsyncIterable
インターフェースを実装しています。各イベントは実行中に発生した内容を示すオブジェクトとして出力されます。ただし多くのアプリケーションはモデルのテキストのみを必要とするため、ストリームには補助メソッドが用意されています。
テキスト出力の取得
Section titled “テキスト出力の取得”stream.toTextStream()
を呼び出すと、生成されたテキストのストリームを取得できます。compatibleWithNodeStreams
が true
の場合、戻り値は通常の Node.js Readable
です。そのまま process.stdout
などへパイプできます。
import { Agent, run } from '@openai/agents';
const agent = new Agent({ name: 'Storyteller', instructions: 'You are a storyteller. You will be given a topic and you will tell a story about it.',});
const result = await run(agent, 'Tell me a story about a cat.', { stream: true,});
result .toTextStream({ compatibleWithNodeStreams: true, }) .pipe(process.stdout);
stream.completed
という Promise は、実行とすべての保留中コールバックが完了した時点で解決されます。出力が残っていないことを保証したい場合は必ず await してください。
すべてのイベントのリッスン
Section titled “すべてのイベントのリッスン”for await
ループを使用して、到着した各イベントを検査できます。低レベルのモデルイベント、エージェントの切り替え、SDK 固有の実行情報などが含まれます。
import { Agent, run } from '@openai/agents';
const agent = new Agent({ name: 'Storyteller', instructions: 'You are a storyteller. You will be given a topic and you will tell a story about it.',});
const result = await run(agent, 'Tell me a story about a cat.', { stream: true,});
for await (const event of result) { // these are the raw events from the model if (event.type === 'raw_model_stream_event') { console.log(`${event.type} %o`, event.data); } // agent updated events if (event.type === 'agent_updated_stream_event') { console.log(`${event.type} %s`, event.agent.name); } // Agent SDK specific events if (event.type === 'run_item_stream_event') { console.log(`${event.type} %o`, event.item); }}
完全なスクリプトは、ストリーミングのコード例 を参照してください。プレーンテキストストリームと元イベントストリームの両方を出力します。
ストリーミング中の Human in the loop
Section titled “ストリーミング中の Human in the loop”ストリーミングは、実行を一時停止するハンドオフ(たとえばツールの承認が必要な場合)と互換性があります。ストリームオブジェクトの interruption
フィールドで割り込みを取得し、それぞれに対して state.approve()
または state.reject()
を呼び出すことで実行を続行できます。再度 { stream: true }
を指定して実行すると、ストリーミング出力が再開されます。
import { Agent, run } from '@openai/agents';
const agent = new Agent({ name: 'Storyteller', instructions: 'You are a storyteller. You will be given a topic and you will tell a story about it.',});
let stream = await run( agent, 'What is the weather in San Francisco and Oakland?', { stream: true },);stream.toTextStream({ compatibleWithNodeStreams: true }).pipe(process.stdout);await stream.completed;
while (stream.interruptions?.length) { console.log( 'Human-in-the-loop: approval required for the following tool calls:', ); const state = stream.state; for (const interruption of stream.interruptions) { const approved = confirm( `Agent ${interruption.agent.name} would like to use the tool ${interruption.rawItem.name} with "${interruption.rawItem.arguments}". Do you approve?`, ); if (approved) { state.approve(interruption); } else { state.reject(interruption); } }
// Resume execution with streaming output stream = await run(agent, state, { stream: true }); const textStream = stream.toTextStream({ compatibleWithNodeStreams: true }); textStream.pipe(process.stdout); await stream.completed;}
ユーザーと実際に対話するより完全な例は human-in-the-loop-stream.ts
をご覧ください。
- すべての出力がフラッシュされていることを確認するため、終了前に
stream.completed
を待機してください。 - 最初の
{ stream: true }
オプションは、その呼び出しにのみ適用されます。RunState
で再実行する場合は再度オプションを指定してください。 - アプリケーションがテキスト結果だけを必要とする場合は、個々のイベントオブジェクトを扱わずに済む
toTextStream()
を利用してください。
ストリーミングとイベントシステムを活用すれば、チャットインターフェースやターミナルアプリケーションなど、インクリメンタルな更新が有益なあらゆる場所にエージェントを統合できます。