microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Samples/Samples.AI/Handlers/MemoryManagementHandler.cs
104lines · modecode
| 1 | using Microsoft.Teams.AI; |
| 2 | using Microsoft.Teams.AI.Messages; |
| 3 | using Microsoft.Teams.AI.Models.OpenAI; |
| 4 | using Microsoft.Teams.AI.Prompts; |
| 5 | using Microsoft.Teams.AI.Templates; |
| 6 | using Microsoft.Teams.Api.Activities; |
| 7 | using Microsoft.Teams.Apps; |
| 8 | |
| 9 | namespace Samples.AI.Handlers; |
| 10 | |
| 11 | public static class MemoryManagementHandler |
| 12 | { |
| 13 | // Simple in-memory store for conversation histories |
| 14 | // In your application, it may be a good idea to use a more |
| 15 | // persistent store backed by a database or other storage solution |
| 16 | private static readonly Dictionary<string, List<IMessage>> ConversationStore = new(); |
| 17 | |
| 18 | /// <summary> |
| 19 | /// Get or create conversation memory for a specific conversation |
| 20 | /// </summary> |
| 21 | public static List<IMessage> GetOrCreateConversationMemory(string conversationId) |
| 22 | { |
| 23 | if (!ConversationStore.ContainsKey(conversationId)) |
| 24 | { |
| 25 | Console.WriteLine($"[MEMORY] Creating new conversation memory for conversation: {conversationId}"); |
| 26 | ConversationStore[conversationId] = new List<IMessage>(); |
| 27 | } |
| 28 | else |
| 29 | { |
| 30 | Console.WriteLine($"[MEMORY] Retrieved existing conversation memory for: {conversationId}"); |
| 31 | } |
| 32 | |
| 33 | return ConversationStore[conversationId]; |
| 34 | } |
| 35 | |
| 36 | /// <summary> |
| 37 | /// Example of stateful conversation handler that maintains conversation history |
| 38 | /// </summary> |
| 39 | public static async Task HandleStatefulConversation(OpenAIChatModel model, IContext<MessageActivity> context) |
| 40 | { |
| 41 | Console.WriteLine($"[HANDLER] Stateful conversation handler invoked"); |
| 42 | Console.WriteLine($"[HANDLER] User: {context.Activity.From.Name}, Message: '{context.Activity.Text}'"); |
| 43 | |
| 44 | // Retrieve existing conversation memory or initialize new one |
| 45 | var messages = GetOrCreateConversationMemory(context.Activity.Conversation.Id); |
| 46 | |
| 47 | Console.WriteLine($"[HANDLER] Current conversation history: {messages.Count} messages"); |
| 48 | |
| 49 | // Create prompt with conversation-specific memory |
| 50 | var prompt = new OpenAIChatPrompt(model, new ChatPromptOptions |
| 51 | { |
| 52 | Instructions = new StringTemplate("You are a helpful assistant that remembers our previous conversation.") |
| 53 | }); |
| 54 | |
| 55 | // Send with existing messages as context |
| 56 | Console.WriteLine("[HANDLER] Sending message to AI with conversation history..."); |
| 57 | var options = new IChatPrompt<OpenAI.Chat.ChatCompletionOptions>.RequestOptions |
| 58 | { |
| 59 | Messages = messages |
| 60 | }; |
| 61 | var result = await prompt.Send(context.Activity.Text, options); |
| 62 | |
| 63 | if (result.Content != null) |
| 64 | { |
| 65 | Console.WriteLine($"[HANDLER] AI response: {result.Content}"); |
| 66 | |
| 67 | var message = new MessageActivity |
| 68 | { |
| 69 | Text = result.Content, |
| 70 | }.AddAIGenerated(); |
| 71 | await context.Send(message); |
| 72 | |
| 73 | // Update conversation history |
| 74 | messages.Add(UserMessage.Text(context.Activity.Text)); |
| 75 | messages.Add(new ModelMessage<string>(result.Content)); |
| 76 | |
| 77 | Console.WriteLine($"[HANDLER] Updated conversation history, now has {messages.Count} messages"); |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | Console.WriteLine("[HANDLER] No content received from AI"); |
| 82 | await context.Reply("I did not generate a response."); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /// <summary> |
| 87 | /// Clear memory for a specific conversation |
| 88 | /// </summary> |
| 89 | public static Task ClearConversationMemory(string conversationId) |
| 90 | { |
| 91 | if (ConversationStore.TryGetValue(conversationId, out var messages)) |
| 92 | { |
| 93 | var messageCount = messages.Count; |
| 94 | messages.Clear(); |
| 95 | Console.WriteLine($"[MEMORY] Cleared {messageCount} messages from conversation: {conversationId}"); |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | Console.WriteLine($"[MEMORY] No conversation history found for: {conversationId}"); |
| 100 | } |
| 101 | |
| 102 | return Task.CompletedTask; |
| 103 | } |
| 104 | } |