Sarel-Esterhuizen / GroqSharp

GroqSharp is a C# client library that makes it easy to interact with GroqCloud. It's designed to provide a simple and flexible interface, allowing you to seamlessly integrate the Groq service into your C# applications.
MIT License
8 stars 1 forks source link

chat history/memory? #5

Open zix3r opened 3 weeks ago

zix3r commented 3 weeks ago

is it possible to implement some sort of memory/history for the chat so that the bot remembers what was said? cuz currently i have something like this: image

but when another prompt is sent, it doesn't remember anything from the last one

danijerez commented 2 weeks ago

You could create an in-memory cache that lasts for a while. Using an API, if you want it to remember, you have to resend previous messages so it has context. You could do something like this:

private readonly GroqClient groqClient;
private readonly IMemoryCache _memoryCache;

private async Task ProcesingIA(Message message)
{
    var cacheKey = $"messages_{message.Chat.Username}";
    if (!_memoryCache.TryGetValue(cacheKey, out ICollection<MessageGroq>? messages))
    {
        var timeMemory = 20;
        messages = [new MessageGroq {
                        Role = MessageRoleType.System,
                        Content = "your-prompt"
                    }];
        _memoryCache.Set(cacheKey, messages, TimeSpan.FromMinutes(timeMemory));
    }

    messages?.Add(new MessageGroq { Role = MessageRoleType.User, Content = message.Text! });
    var response = await groqClient.CreateChatCompletionAsync([.. messages!]);
}