Dynamic Agents
Dynamic agentsはランタイムコンテキスト(ユーザーIDやその他の重要なパラメータなど)を使用して、リアルタイムで設定を調整します。
これにより、使用するモデルを変更したり、指示を更新したり、必要に応じて異なるツールを選択したりできます。
このコンテキストを使用することで、エージェントは各ユーザーのニーズにより適切に対応できます。また、任意のAPIを呼び出してより多くの情報を収集することもでき、エージェントの機能向上に役立ちます。
設定例
以下は、ユーザーのサブスクリプション階層と言語設定に基づいて動作を調整するダイナミックサポートエージェントの例です:
const supportAgent = new Agent({
name: "Dynamic Support Agent",
instructions: async ({ runtimeContext }) => {
const userTier = runtimeContext.get("user-tier");
const language = runtimeContext.get("language");
return `You are a customer support agent for our SaaS platform.
The current user is on the ${userTier} tier and prefers ${language} language.
For ${userTier} tier users:
${userTier === "free" ? "- Provide basic support and documentation links" : ""}
${userTier === "pro" ? "- Offer detailed technical support and best practices" : ""}
${userTier === "enterprise" ? "- Provide priority support with custom solutions" : ""}
Always respond in ${language} language.`;
},
model: ({ runtimeContext }) => {
const userTier = runtimeContext.get("user-tier");
return userTier === "enterprise"
? openai("gpt-4")
: openai("gpt-3.5-turbo");
},
tools: ({ runtimeContext }) => {
const userTier = runtimeContext.get("user-tier");
const baseTools = [knowledgeBase, ticketSystem];
if (userTier === "pro" || userTier === "enterprise") {
baseTools.push(advancedAnalytics);
}
if (userTier === "enterprise") {
baseTools.push(customIntegration);
}
return baseTools;
},
});
この例では、エージェントは以下のことを行います:
- ユーザーのサブスクリプション階層(free、pro、またはenterprise)に基づいて指示を調整
- enterpriseユーザーに対してより強力なモデル(GPT-4)を使用
- ユーザーの階層に基づいて異なるツールセットを提供
- ユーザーの希望する言語で応答
これは、単一のエージェントがランタイムコンテキストを活用することで、異なるタイプのユーザーやシナリオを処理できることを示しており、各ユースケースに対して個別のエージェントを作成するよりも柔軟で保守しやすくなります。
APIルート、ミドルウェア設定、ランタイムコンテキスト処理を含む完全な実装例については、Dynamic Agents Exampleをご覧ください。