|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +// This sample shows how to give an agent file-based memory using the FileMemoryProvider. |
| 4 | +// The FileMemoryProvider exposes a set of tools to the agent (write, read, delete, list, grep and replace) |
| 5 | +// that allow it to store memories as individual files in an AgentFileStore. |
| 6 | +// Because the files are stored outside of the conversation, the agent can recall them |
| 7 | +// in later conversations, even after the original chat history is gone. |
| 8 | +// |
| 9 | +// The sample also shows how to control the folder that memory files are written to, |
| 10 | +// by supplying a state initializer callback that sets the working folder for each session. |
| 11 | + |
| 12 | +#pragma warning disable MAAI001 // AgentFileStore and its implementations are experimental. |
| 13 | + |
| 14 | +using Azure.AI.Projects; |
| 15 | +using Azure.Identity; |
| 16 | +using Microsoft.Agents.AI; |
| 17 | + |
| 18 | +var endpoint = Environment.GetEnvironmentVariable("FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("FOUNDRY_PROJECT_ENDPOINT is not set."); |
| 19 | +var deploymentName = Environment.GetEnvironmentVariable("FOUNDRY_MODEL") ?? "gpt-5.4-mini"; |
| 20 | + |
| 21 | +// The id of the user that we are storing memories for. |
| 22 | +// It is used below to give each user their own memory folder. |
| 23 | +const string UserId = "UID1"; |
| 24 | + |
| 25 | +// Create the file store that the FileMemoryProvider will use to persist memory files. |
| 26 | +// Here we use a file system backed store rooted at a local folder called "agent-memory", |
| 27 | +// but any AgentFileStore implementation can be used, e.g. InMemoryAgentFileStore or a custom |
| 28 | +// implementation backed by blob storage. |
| 29 | +var memoryRoot = Path.Combine(AppContext.BaseDirectory, "agent-memory"); |
| 30 | +var fileStore = new FileSystemAgentFileStore(memoryRoot); |
| 31 | + |
| 32 | +// The working folder that memories for this user will be written to, relative to the store root. |
| 33 | +// The folder you choose determines the scope and lifetime of the memories: |
| 34 | +// - A stable folder, like the per-user one below, gives you durable memories that are shared by |
| 35 | +// every session for that user. That is what allows the second conversation further down to |
| 36 | +// recall what the user said in the first. |
| 37 | +// - A unique folder per session gives you memories that are isolated to a single session, e.g. |
| 38 | +// generate one in the state initializer callback below: |
| 39 | +// _ => new FileMemoryState { WorkingFolder = Guid.NewGuid().ToString() } |
| 40 | +var workingFolder = $"users/{UserId}"; |
| 41 | + |
| 42 | +Console.WriteLine($"Memory files will be written to: {Path.Combine(memoryRoot, workingFolder)}"); |
| 43 | +Console.WriteLine(); |
| 44 | + |
| 45 | +// Create the file memory provider. |
| 46 | +// The second parameter is a state initializer callback that is invoked whenever the provider |
| 47 | +// cannot find existing state in a session, i.e. typically the first time it is used with a new session. |
| 48 | +// It allows us to configure the folder that memory files for that session are written to. |
| 49 | +// If no callback is supplied, the working folder defaults to the root of the store, |
| 50 | +// which means all sessions share a single, flat set of memory files. |
| 51 | +using var fileMemoryProvider = new FileMemoryProvider( |
| 52 | + fileStore, |
| 53 | + _ => new FileMemoryState { WorkingFolder = workingFolder }); |
| 54 | + |
| 55 | +// Create the agent and attach the FileMemoryProvider so that the agent gets the file memory tools. |
| 56 | +AIAgent agent = new AIProjectClient( |
| 57 | + new Uri(endpoint), |
| 58 | + // WARNING: DefaultAzureCredential is convenient for development but requires careful consideration in production. |
| 59 | + // In production, consider using a specific credential (e.g., ManagedIdentityCredential) to avoid |
| 60 | + // latency issues, unintended credential probing, and potential security risks from fallback mechanisms. |
| 61 | + new DefaultAzureCredential()) |
| 62 | + .AsAIAgent(new ChatClientAgentOptions |
| 63 | + { |
| 64 | + ChatOptions = new() |
| 65 | + { |
| 66 | + ModelId = deploymentName, |
| 67 | + Instructions = "You are a helpful travel assistant. Remember what the user tells you about themselves so that you can give better recommendations later." |
| 68 | + }, |
| 69 | + Name = "TravelAssistant", |
| 70 | + AIContextProviders = [fileMemoryProvider], |
| 71 | + }); |
| 72 | + |
| 73 | +// First conversation: tell the agent something worth remembering. |
| 74 | +// The agent should use the file_memory_write tool to store it as a file in the working folder. |
| 75 | +AgentSession firstSession = await agent.CreateSessionAsync(); |
| 76 | +Console.WriteLine("=== First conversation ==="); |
| 77 | +Console.WriteLine(await agent.RunAsync( |
| 78 | + "I'm vegetarian and I always travel with my dog. Please remember this for future trips.", |
| 79 | + firstSession)); |
| 80 | +Console.WriteLine(); |
| 81 | + |
| 82 | +// Show the memory files that the agent created on disk. |
| 83 | +Console.WriteLine("=== Memory files on disk ==="); |
| 84 | +foreach (var file in Directory.EnumerateFiles(Path.Combine(memoryRoot, workingFolder))) |
| 85 | +{ |
| 86 | + Console.WriteLine(Path.GetFileName(file)); |
| 87 | +} |
| 88 | + |
| 89 | +Console.WriteLine(); |
| 90 | + |
| 91 | +// Second conversation: a brand new session with no chat history from the first conversation. |
| 92 | +// The provider surfaces the memory index to the agent, and the agent can read the memory files |
| 93 | +// using the file_memory_read tool, so it can still recall the user's preferences. |
| 94 | +AgentSession secondSession = await agent.CreateSessionAsync(); |
| 95 | +Console.WriteLine("=== Second conversation (new session) ==="); |
| 96 | +Console.WriteLine(await agent.RunAsync( |
| 97 | + "Suggest a hotel and a restaurant for my trip to Paris next week.", |
| 98 | + secondSession)); |
0 commit comments