tryAGI / tryAGI/LangChain.Databases

use async methods

Open
#15 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

todo
Dominant language
C#
Stars
12
Forks
8
PR merge metrics
No merged PRs in 30d

Description

/ </summary>

/ Append the message to the record in Redis

/ </summary>

/ Clear session memory from Redis

/ </summary>

https://github.com/tryAGI/LangChain.Databases/blob/f753e0c87454ab9978786b3302ce986d2166c133/src/Redis/src/RedisChatMessageHistory.cs#L50


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);
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked RedisChatMessageHistory.cs around line 50 and inspect the BaseChatMessageHistory contract. Determine how message retrieval can use StackExchange.Redis async methods while preserving ordering and serialization; done when the Redis history read path is asynchronous without changing append or clear behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, redis
Domain
databases
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.