Table of Contents

Chat agent

AI Toolbox provides a simple single user ChatAgent for use cases where data persistence is not required. It can be used with or without memory retrieving.

Example

For a full working example please check the ChatAgentDemo project in the repository.

using var host = Host.CreateDefaultBuilder(args)
    .ConfigureServices((_, services) =>
    {
        services
            .AddAIToolbox()
            .AddConnectors()
            .IncludeOllamaConnector(options => options.Endpoint = "http://localhost:11434")
            .AddKernel(options =>
            {
                options.Ollama = new OllamaOptions
                {
                    ChatCompletion = new OllamaChatCompletionOptions { ModelId = "llama3" }
                };
            })
            .AddMemory(options =>
            {
                options.TextEmbeddingGeneration = new TextEmbeddingGenerationOptions
                {
                    Ollama = new OllamaTextEmbeddingGenerationMemoryOptions
                    {
                        ModelId = "llama3"
                    }
                };
            })
            .IncludeSimpleMemoryStore()
            .AddAgents()
            .IncludeChatCompletionAgent()
            .WithSemanticTextMemoryRetriever();
    })
    .Build();

var agent = host.Services.GetRequiredService<IChatAgent>();

Console.WriteLine();
Console.WriteLine("Chat agent");
Console.WriteLine("-----------------------");
Console.WriteLine("Press Ctrl+C to quit the conversation");
Console.WriteLine();

while (true)
{
    Console.Write("User > ");

    var message = Console.ReadLine()!;
    var first = true;
    
    await foreach (var response in agent.SendMessageAsStreamAsync(message))
    {
        if (first)
        {
            Console.Write("Assistant > ");
            first = false;
        }

        Console.Write(response.Content);
    }

    Console.WriteLine();
    Console.WriteLine();
}

await host.RunAsync();