AI Memory

  • Implemented the testing scripts
  • Save data from previous interactions
  • This could be very useful in scenarios where you need to continue the activity generation

Code

public async Task AIMemory()
        {
#pragma warning disable SKEXP0010
            var builder = Kernel.CreateBuilder();
            var endpoint = new Uri(_llmUrl);
            var modelId = "llama3.1";
 
            var kernelBuilder = Kernel.CreateBuilder().AddOpenAIChatCompletion(modelId: modelId, apiKey: null, endpoint: endpoint);
            var kernel = kernelBuilder.Build();
 
#pragma warning disable SKEXP0001, SKEXP0020
            var builder_memory = new MemoryBuilder();
            var embeddingEndpoint = _llmUrl;
            var cancellationTokenSource = new System.Threading.CancellationTokenSource();
            var cancellationToken = cancellationTokenSource.Token;
            builder_memory.WithHttpClient(new HttpClient());
            //builder_memory.WithOllamaTextEmbeddingGeneration("all-minilm", embeddingEndpoint);
            builder_memory.WithOllamaTextEmbeddingGeneration("nomic-embed-text", embeddingEndpoint);
            //builder.WithOllamaTextEmbeddingGeneration("mxbai-embed-large", embeddingEndpoint);
            var sqliteMemoryStore = await SqliteMemoryStore.ConnectAsync("memories.sqlite", cancellationToken);
            builder_memory.WithMemoryStore(sqliteMemoryStore);
            var memory = builder_memory.Build();
 
            const string MemoryCollectionName = "aboutMe";
            await memory.SaveInformationAsync(MemoryCollectionName, id: "info1", text: "My name is Stephan");
            await memory.SaveInformationAsync(MemoryCollectionName, id: "info2", text: "I currently work as a Software Solution Architect");
            await memory.SaveInformationAsync(MemoryCollectionName, id: "info3", text: "I live in South Africa and have been living there since 1979");
            await memory.SaveInformationAsync(MemoryCollectionName, id: "info4", text: "I travelled to the Cape twice since 2008");
            await memory.SaveInformationAsync(MemoryCollectionName, id: "info5", text: "My family is from Witbank");
 
            var history = "";
            var questions = new[]
            {
                "what is my name?",
                "where do I live?",
                "where is my family from?",
                "where have I travelled?",
                "what do I do for work?"
            };
 
            foreach (var q in questions)
            {
                var response = await memory.SearchAsync(MemoryCollectionName, q, limit: 1, minRelevanceScore: 0.3).FirstOrDefaultAsync();
                history += response?.Metadata.Text + "\n";
                Console.WriteLine(q + " " + response?.Metadata.Text);
            }
 
#pragma warning disable SKEXP0050, SKEXP0001
            // TextMemoryPlugin provides the "recall" function
            kernel.ImportPluginFromObject(new TextMemoryPlugin(memory));
 
            const string skPrompt = @"
ChatBot can have a conversation with you about any topic.
It can give explicit instructions or say 'I don't know' if it does not have an answer.
 
Information about me, from previous conversations:
- {{$fact1}} {{recall $fact1}}
- {{$fact2}} {{recall $fact2}}
- {{$fact3}} {{recall $fact3}}
- {{$fact4}} {{recall $fact4}}
- {{$fact5}} {{recall $fact5}}
 
Chat:
{{$history}}
User: {{$userInput}}
ChatBot: ";
 
            var chatFunction = kernel.CreateFunctionFromPrompt(skPrompt, new OpenAIPromptExecutionSettings { MaxTokens = 200, Temperature = 0.8 });
 
#pragma warning disable SKEXP0001, SKEXP0050    
            var arguments = new KernelArguments();
            arguments["fact1"] = "what is my name?";
            arguments["fact2"] = "where do I live?";
            arguments["fact3"] = "where is my family from?";
            arguments["fact4"] = "where have I travelled?";
            arguments["fact5"] = "what do I do for work?";
            arguments[TextMemoryPlugin.CollectionParam] = MemoryCollectionName;
            arguments[TextMemoryPlugin.LimitParam] = "2";
            arguments[TextMemoryPlugin.RelevanceParam] = "0.8";
 
            arguments["history"] = history;
            Func<string, Task> Chat = async (string input) =>
            {
                // Save new message in the kernel arguments
                arguments["userInput"] = input;
 
                // Process the user message and get an answer
                var answer = await chatFunction.InvokeAsync(kernel, arguments);
 
                // Append the new interaction to the chat history
                var result = $"\nUser: {input}\nChatBot: {answer}\n";
 
                history += result;
                arguments["history"] = history;
 
                // Show the bot response
                Console.WriteLine(result);
            };
 
            //await Chat("Hello, I think we've met before, remember? my name is...");
            await Chat("Tell me everything you remember from our previous conversations.");
        }

Output

what is my name? My name is Stephan
where do I live? I live in South Africa and have been living there since 1979
where is my family from? My family is from Witbank
where have I travelled? I travelled to the Cape twice since 2008
what do I do for work? I currently work as a Software Solution Architect

User: Tell me everything you remember from our previous conversations.
ChatBot: Based on our previous conversation, I remember the following:

1. **Your name: "Stephan"** - a pleasant name, my friend!
2. **Your residence:** You live in South Africa. Specifically, this occurred **(long story)_ in your long-stored past history_ , with a detailed time frame that started from 1979 until now as I interacted you.
3. **Family roots:** Your family hails from Witbank. Interestingly.
4. **Places travelled**: Although very briefly noted before, Stephan you indeed told me that Stephan You had an exciting travel history. Specifically since since travelling was mentioned for the year (20_)** in 2 instances and those two times specifically of your trips you were located as follows : I know 2 other location locations from this chat. I knew travelled to the cape Cape town once (no) not exactly  . but did visited
5. * Professional Profile:** You currently employ employ yourself as a proficient Software Solution Architect

Additional Metadata