Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use async methods #15

Open
github-actions bot opened this issue Aug 6, 2024 · 0 comments
Open

use async methods #15

github-actions bot opened this issue Aug 6, 2024 · 0 comments
Labels

Comments

@github-actions
Copy link
Contributor

github-actions bot commented Aug 6, 2024

/ </summary>

/ Append the message to the record in Redis

/ </summary>

/ Clear session memory from Redis

/ </summary>

using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using LangChain.Memory;
using LangChain.Providers;
using StackExchange.Redis;

namespace LangChain.Databases;

/// <summary>
/// Chat message history stored in a Redis database.
/// </summary>
[RequiresDynamicCode("Requires dynamic code.")]
[RequiresUnreferencedCode("Requires unreferenced code.")]
public class RedisChatMessageHistory : BaseChatMessageHistory
{
    public TimeSpan? Ttl { get; set; }

    private readonly string _sessionId;
    private readonly string _keyPrefix;
    private readonly Lazy<ConnectionMultiplexer> _multiplexer;

    /// <inheritdoc />
    public RedisChatMessageHistory(
        string sessionId,
        string connectionString,
        string keyPrefix = "message_store:",
        TimeSpan? ttl = null)
    {
        _sessionId = sessionId;
        _keyPrefix = keyPrefix;
        Ttl = ttl;

        _multiplexer = new Lazy<ConnectionMultiplexer>(
            () =>
            {
                var multiplexer = ConnectionMultiplexer.Connect(connectionString);

                return multiplexer;
            },
            LazyThreadSafetyMode.ExecutionAndPublication);
    }

    /// <summary>
    /// Construct the record key to use
    /// </summary>
    private string Key => _keyPrefix + _sessionId;

    /// <summary>
    /// Retrieve the messages from Redis
    /// TODO: use async methods
    /// </summary>
    public override IReadOnlyList<Message> Messages
    {
        get
        {
            var database = _multiplexer.Value.GetDatabase();
            var values = database.ListRange(Key, start: 0, stop: -1);
            var messages = values.Select(v => JsonSerializer.Deserialize<Message>(v.ToString())).Reverse();

            return messages.ToList();
        }
    }

    /// <summary>
    /// Append the message to the record in Redis
    /// </summary>
    public override async Task AddMessage(Message message)
    {
        var database = _multiplexer.Value.GetDatabase();
        await database.ListLeftPushAsync(Key, JsonSerializer.Serialize(message)).ConfigureAwait(false);
        if (Ttl.HasValue)
        {
            await database.KeyExpireAsync(Key, Ttl).ConfigureAwait(false);
        }
    }

    /// <summary>
    /// Clear session memory from Redis
    /// </summary>
    public override async Task Clear()
    {
        var database = _multiplexer.Value.GetDatabase();
        await database.KeyDeleteAsync(Key).ConfigureAwait(false);
    }
}
@github-actions github-actions bot added the todo label Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants