אחסון הוא רכיב חיוני ב-Microsoft 365 Agents SDK. הוא מאפשר לסוכנים לשמר את מצב השיחה, נתוני המשתמש ומידע נוסף בין sessions. ה-SDK תומך במגוון אפשרויות storage, כולל:
- אחסון בזיכרון
- Azure Cosmos DB
- Azure Blob Storage
- ספקי אחסון מותאמים אישית
אפשרויות storage עיקריות
Agents SDK מספק מספר ספקי שירותי אחסון מובנים, שלכל אחד מהם מקרי שימוש ויתרונות משלו. באפשרותך לבחור את האפשרות המתאימה ביותר לצורכי הסוכן שלך. באפשרותך גם לממש custom storage provider משלך.
אחסון זיכרון
- מתאים למטרות בדיקה ופיתוח.
- הנתונים נמחקים כאשר הסוכן מופעל מחדש, ולכן אינו מתאים ל-production.
- הנתונים זמינים רק ב-web app instance, ולכן אינו מתאים להפעלה ב-cluster.
Azure Cosmos DB
- מסד נתונים multimodel מבוזר גלובלית, אידיאלי עבור סוכני production.
- תומך ב-partitioned storage לצורך scalability וביצועים.
Azure Blob Storage
- מותאם לאחסון נתונים לא מובנים כגון טקסט או קבצים בינארים.
- משמש בדרך כלל לאחסון מצב הסוכן וטרנסקריפטים.
אפשרויות custom storage באמצעות מימוש IStorage
שימוש ב-storage providers שונים
אחסון זיכרון
כל הדוגמאות משתמשות ב- MemoryStorage. storage זה הוא volatile ומתאים לפיתוח ולבדיקות בלבד. עבור תרחישי ייצור, השתמש באפשרות אחסון עמידה יותר, כגון Azure Cosmos DB או Azure Blob Storage.
ב- 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 Cosmos DB
הוסף תלות בחבילה עבור 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.
הגדר וצור את ה-storage, ולאחר מכן העבר אותו אל 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.
הגדר וצור את ה-storage, ולאחר מכן העבר אותו אל 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.
הגדר וצור את ה-storage, ולאחר מכן העבר אותו אל 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.
הגדר וצור את ה-storage, ולאחר מכן העבר אותו אל 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.