存储是 Microsoft 365 智能体 SDK 的关键组件。 它使智能体能够在不同会话之间持久化对话状态、用户数据和其他信息。 该 SDK 支持多种存储选项,包括:
- 内存存储
- Azure Cosmos DB
- Azure Blob 存储
- 自定义存储提供程序
主要存储选项
Agents SDK 提供了多种内置存储提供程序,每种都有其特定的应用场景和优势。 您可以选择最符合您智能体需求的存储提供程序。 您还可以实现自己的自定义存储提供程序。
内存存储
- 适用于测试和开发场景。
- 智能体重启时数据会被清空,因此不适合生产环境。
- 数据仅在 Web 应用实例上可用,因此不适用于集群环境。
Azure Cosmos DB
- 一种全球分布式、多模型数据库,非常适合生产环境中的智能体。
- 支持分区存储,以实现可扩展性和高性能。
Azure Blob 存储
- 针对文本或二进制文件等非结构化数据的存储进行了优化。
- 通常用于存储智能体状态和对话记录。
通过实现 IStorage 提供自定义存储选项
使用不同的存储提供商
内存存储
所有示例均使用 MemoryStorage。 此类存储具有易失性,仅适用于开发和测试。 对于生产环境,请使用更持久的存储选项,例如 Azure Cosmos DB 或 Azure Blob 存储。
在 Program.cs 中,注册 MemoryStorage:
builder.Services.AddSingleton<IStorage, MemoryStorage>();
import { MemoryStorage, AgentApplication, TurnState } from '@microsoft/agents-hosting'
const storage = new MemoryStorage()
const app = new AgentApplication<TurnState>({
storage
})
from microsoft_agents.hosting.core import AgentApplication, TurnState, MemoryStorage
storage = MemoryStorage()
app = AgentApplication[TurnState](
storage=storage,
adapter=adapter,
authorization=authorization,
)
Azure CosmosDb 存储
为 Microsoft.Agents.Storage.CosmosDb 添加包依赖项。
在 Program.cs 中,添加(或替换现有)IStorage 注册信息,内容如下:
builder.Services.AddSingleton<IStorage>(sp =>
{
var options = new CosmosDbPartitionedStorageOptions()
{
CosmosDbEndpoint = "your-cosmosdb-endpoint",
DatabaseId = "your-database-id",
ContainerId = "your-container-id",
// Get a TokenCredential from your defined Connections
TokenCredential = sp.GetService<IConnections>().GetConnection("ServiceConnection").GetTokenCredential()
};
return new CosmosDbPartitionedStorage(options);
});
若要了解详细信息,请访问 CosmosDbPartitionedStorageOptions。
为 @microsoft/agents-hosting-storage-cosmos 添加包依赖项。
配置并创建存储,然后将其传递给 AgentApplication:
import { AgentApplication, TurnState } from '@microsoft/agents-hosting'
import { CosmosDbPartitionedStorage } from '@microsoft/agents-hosting-storage-cosmos'
const storage = new CosmosDbPartitionedStorage({
databaseId: process.env.COSMOS_DATABASE_ID,
containerId: process.env.COSMOS_CONTAINER_ID,
cosmosClientOptions: {
endpoint: process.env.COSMOS_ENDPOINT,
key: process.env.COSMOS_KEY,
}
})
const app = new AgentApplication<TurnState>({
storage
})
若要了解详细信息,请访问 CosmosDbPartitionedStorageOptions。
为 microsoft-agents-storage-cosmos 添加包依赖项。
配置并创建存储,然后将其传递给 AgentApplication:
from microsoft_agents.hosting.core import AgentApplication, TurnState
from microsoft_agents.storage.cosmos import CosmosDBStorage, CosmosDBStorageConfig
config = CosmosDBStorageConfig(
cosmos_db_endpoint="your-cosmosdb-endpoint",
database_id="your-database-id",
container_id="your-container-id",
credential=your_token_credential,
)
storage = CosmosDBStorage(config)
app = AgentApplication[TurnState](
storage=storage,
adapter=adapter,
authorization=authorization,
)
若要了解详细信息,请访问 CosmosDBStorageConfig。
Azure Blob 存储
为 Microsoft.Agents.Storage.Blobs 添加包依赖项。
在 Program.cs 中,添加(或替换现有)IStorage 注册信息,内容如下:
builder.Services.AddSingleton<IStorage>(sp =>
{
// Get a TokenCredential from your defined Connections
var tokenCredential = sp.GetService<IConnections>().GetConnection("ServiceConnection").GetTokenCredential();
return new BlobsStorage(
new Uri("{{your-blobs-storage-endpoint}}/agent-state"),
tokenCredential);
});
为 @microsoft/agents-hosting-storage-blob 添加包依赖项。
配置并创建存储,然后将其传递给 AgentApplication:
import { AgentApplication, TurnState } from '@microsoft/agents-hosting'
import { BlobsStorage } from '@microsoft/agents-hosting-storage-blob'
const storage = new BlobsStorage(
process.env.BLOB_CONTAINER_ID,
process.env.BLOB_STORAGE_CONNECTION_STRING
)
const app = new AgentApplication<TurnState>({
storage
})
为 microsoft-agents-storage-blob 添加包依赖项。
配置并创建存储,然后将其传递给 AgentApplication:
from microsoft_agents.hosting.core import AgentApplication, TurnState
from microsoft_agents.storage.blob import BlobStorage, BlobStorageConfig
# Using a connection string
config = BlobStorageConfig(
container_name="agent-state",
connection_string="your-connection-string",
)
# Or using a TokenCredential
config = BlobStorageConfig(
container_name="agent-state",
url="https://your-storage-account.blob.core.windows.net",
credential=your_token_credential,
)
storage = BlobStorage(config)
app = AgentApplication[TurnState](
storage=storage,
adapter=adapter,
authorization=authorization,
)
若要了解详细信息,请访问 BlobStorageConfig。