Dynamic Agents
Dynamic agentsはruntime context(ユーザーIDやその他の重要なパラメータなど)を使用して、リアルタイムで設定を調整します。
これにより、使用するモデルを変更したり、指示を更新したり、異なるツールを選択したり、必要に応じてメモリを設定したりできます。
このコンテキストを使用することで、agentsは各ユーザーのニーズにより適切に対応できます。また、任意のAPIを呼び出してより多くの情報を収集することもでき、agentsの機能向上に役立ちます。
設定例
以下は、ユーザーのサブスクリプション階層と言語設定に基づいて動作を調整するdynamic support agentの例です:
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;
},
memory: ({ runtimeContext }) => {
const userTier = runtimeContext.get("user-tier");
if (userTier === "enterprise") {
return new Memory({
storage: new LibSQLStore({ url: "file:enterprise.db" }),
options: {
semanticRecall: { topK: 15, messageRange: 8 },
workingMemory: { enabled: true },
},
});
} else if (userTier === "pro") {
return new Memory({
storage: new LibSQLStore({ url: "file:pro.db" }),
options: {
semanticRecall: { topK: 8, messageRange: 4 },
workingMemory: { enabled: true },
},
});
}
// Basic memory for free tier
return new Memory({
storage: new LibSQLStore({ url: "file:free.db" }),
options: {
semanticRecall: { topK: 3, messageRange: 2 },
workingMemory: { enabled: false },
},
});
},
});
この例では、agentは以下のことを行います:
- ユーザーのサブスクリプション階層(free、pro、またはenterprise)に基づいて指示を調整
- enterpriseユーザーにはより強力なモデル(GPT-4)を使用
- ユーザーの階層に基づいて異なるツールセットを提供
- ユーザーの階層に基づいて異なる機能でメモリを設定
- ユーザーの希望する言語で応答
これは、単一のagentがruntime contextを活用することで、異なるタイプのユーザーやシナリオを処理できることを示しており、各ユースケースに対して個別のagentsを作成するよりも柔軟で保守しやすくなります。
APIルート、ミドルウェア設定、runtime contextハンドリングを含む完全な実装例については、Dynamic Agents Exampleをご覧ください。