Agents SDK 記憶體概觀

記憶體是 Microsoft Agents SDK 的重要元件,可讓代理程式跨會話保存交談狀態、用戶數據和其他資訊。 它支援各種記憶體選項,包括記憶體內部記憶體、Azure Cosmos DB、Azure Blob 記憶體,以及允許自定義記憶體提供者。

主要儲存選項

  1. 記憶體存儲

    • 適用於測試和開發目的。
    • 代理程式重新啟動時會清除數據,使其不適合生產環境。
    • 數據只能在 Webapp 實例上使用,使其不適合在叢集中執行。
  2. Azure Cosmos DB

    • 全域分佈式的多模型資料庫,適用於生產系統。
    • 支援分割的記憶體,以達到延展性和效能。
  3. Azure Blob 儲存體

    • 已針對儲存文字或二進位檔等非結構化數據進行優化。
    • 通常用於代理狀態和文字記錄儲存。
  4. 您可以實作 IStorage 來提供自訂儲存空間選項

使用不同的記憶體提供者

記憶體儲存

所有範例都使用 MemoryStorage

對於 .NET,在 Program.cs 中註冊MemoryStorage

builder.Services.AddSingleton<IStorage, MemoryStorage>();

Azure CosmosDb 儲存體

針對 .NET:

  1. Microsoft.Agents.Storage.CosmosDb新增套件相依性

  2. 在 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);
    });
    
  3. 如需詳細資訊,請參閱 CosmosDbPartitionedStorageOptions

Azure Blob 儲存服務

針對 .NET:

  1. Microsoft.Agents.Storage.Blobs新增套件相依性

  2. 在 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);
    });