-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Removed LangChain.Core usage over repo.
- Loading branch information
Showing
11 changed files
with
366 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/Abstractions/src/MessageHistory/BaseChatMessageHistory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using LangChain.Providers; | ||
|
||
namespace LangChain.Memory; | ||
|
||
/// <summary> | ||
/// Abstract base class for storing chat message history. | ||
/// | ||
/// Implementations should over-ride the AddMessages method to handle bulk addition | ||
/// of messages. | ||
/// | ||
/// The default implementation of AddMessages will correctly call AddMessage, so | ||
/// it is not necessary to implement both methods. | ||
/// | ||
/// When used for updating history, users should favor usage of `AddMessages` | ||
/// over `AddMessage` or other variants like `AddUserMessage` and `AddAiMessage` | ||
/// to avoid unnecessary round-trips to the underlying persistence layer. | ||
/// </summary> | ||
public abstract class BaseChatMessageHistory | ||
{ | ||
/// <summary> | ||
/// A list of messages stored in-memory. | ||
/// </summary> | ||
public abstract IReadOnlyList<Message> Messages { get; } | ||
|
||
/// <summary> | ||
/// Convenience method for adding a human message string to the store. | ||
/// | ||
/// Please note that this is a convenience method. Code should favor the | ||
/// bulk AddMessages interface instead to save on round-trips to the underlying | ||
/// persistence layer. | ||
/// | ||
/// This method may be deprecated in a future release. | ||
/// </summary> | ||
/// <param name="message">The human message to add</param> | ||
public async Task AddUserMessage(string message) | ||
{ | ||
await AddMessage(message.AsHumanMessage()).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Convenience method for adding an AI message string to the store. | ||
/// | ||
/// Please note that this is a convenience method. Code should favor the bulk | ||
/// AddMessages interface instead to save on round-trips to the underlying | ||
/// persistence layer. | ||
/// | ||
/// This method may be deprecated in a future release. | ||
/// </summary> | ||
/// <param name="message"></param> | ||
public async Task AddAiMessage(string message) | ||
{ | ||
await AddMessage(message.AsAiMessage()).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Add a message object to the store. | ||
/// </summary> | ||
/// <param name="message">A message object to store</param> | ||
public abstract Task AddMessage(Message message); | ||
|
||
/// <summary> | ||
/// Add a list of messages. | ||
/// | ||
/// Implementations should override this method to handle bulk addition of messages | ||
/// in an efficient manner to avoid unnecessary round-trips to the underlying store. | ||
/// </summary> | ||
/// <param name="messages">A list of message objects to store.</param> | ||
public virtual async Task AddMessages(IEnumerable<Message> messages) | ||
{ | ||
messages = messages ?? throw new ArgumentNullException(nameof(messages)); | ||
|
||
foreach (var message in messages) | ||
{ | ||
await AddMessage(message).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Replace the list of messages. | ||
/// | ||
/// Implementations should override this method to handle bulk addition of messages | ||
/// in an efficient manner to avoid unnecessary round-trips to the underlying store. | ||
/// </summary> | ||
/// <param name="messages">A list of message objects to store.</param> | ||
public virtual async Task SetMessages(IEnumerable<Message> messages) | ||
{ | ||
messages = messages ?? throw new ArgumentNullException(nameof(messages)); | ||
|
||
await Clear().ConfigureAwait(false); | ||
await AddMessages(messages).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Remove all messages from the store | ||
/// </summary> | ||
public abstract Task Clear(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using LangChain.Providers; | ||
|
||
namespace LangChain.Memory; | ||
|
||
/// <summary> | ||
/// In memory implementation of chat message history. | ||
/// | ||
/// Stores messages in an in memory list. | ||
/// </summary> | ||
public class ChatMessageHistory : BaseChatMessageHistory | ||
{ | ||
private readonly List<Message> _messages = new List<Message>(); | ||
|
||
/// <summary> | ||
/// Used to inspect and filter messages on their way to the history store | ||
/// NOTE: This is not a feature of python langchain | ||
/// </summary> | ||
public Predicate<Message> IsMessageAccepted { get; set; } = (x => true); | ||
|
||
/// <inheritdoc/> | ||
public override IReadOnlyList<Message> Messages => _messages; | ||
|
||
/// <inheritdoc/> | ||
public override Task AddMessage(Message message) | ||
{ | ||
if (IsMessageAccepted(message)) | ||
{ | ||
_messages.Add(message); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override Task Clear() | ||
{ | ||
_messages.Clear(); | ||
return Task.CompletedTask; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/Abstractions/src/MessageHistory/FileChatMessageHistory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using LangChain.Providers; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace LangChain.Memory; | ||
|
||
/// <summary> | ||
/// Chat message history that stores history in a local file. | ||
/// </summary> | ||
public class FileChatMessageHistory : BaseChatMessageHistory | ||
{ | ||
private string MessagesFilePath { get; } | ||
|
||
private List<Message> _messages = new List<Message>(); | ||
|
||
/// <inheritdoc/> | ||
public override IReadOnlyList<Message> Messages => _messages; | ||
|
||
/// <summary> | ||
/// Initializes new history instance with provided file path | ||
/// </summary> | ||
/// <param name="messagesFilePath">path of the local file to store the messages</param> | ||
/// <exception cref="ArgumentNullException"></exception> | ||
private FileChatMessageHistory(string messagesFilePath) | ||
{ | ||
MessagesFilePath = messagesFilePath ?? throw new ArgumentNullException(nameof(messagesFilePath)); | ||
} | ||
|
||
/// <summary> | ||
/// Create new history instance with provided file path | ||
/// </summary> | ||
/// <param name="path">path of the local file to store the messages</param> | ||
/// <param name="cancellationToken"></param> | ||
public static async Task<FileChatMessageHistory> CreateAsync(string path, CancellationToken cancellationToken = default) | ||
{ | ||
FileChatMessageHistory chatHistory = new FileChatMessageHistory(path); | ||
await chatHistory.LoadMessages().ConfigureAwait(false); | ||
|
||
return chatHistory; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override Task AddMessage(Message message) | ||
{ | ||
_messages.Add(message); | ||
SaveMessages(); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override Task Clear() | ||
{ | ||
_messages.Clear(); | ||
SaveMessages(); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private void SaveMessages() | ||
{ | ||
var json = JsonSerializer.Serialize(_messages, SourceGenerationContext.Default.ListMessage); | ||
|
||
File.WriteAllText(MessagesFilePath, json); | ||
} | ||
|
||
private async Task LoadMessages() | ||
{ | ||
if (File.Exists(MessagesFilePath)) | ||
{ | ||
var json = await File2.ReadAllTextAsync(MessagesFilePath).ConfigureAwait(false); | ||
if (!string.IsNullOrWhiteSpace(json)) | ||
{ | ||
_messages = JsonSerializer.Deserialize(json, SourceGenerationContext.Default.ListMessage) ?? new List<Message>(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
[JsonSerializable(typeof(List<Message>))] | ||
internal sealed partial class SourceGenerationContext : JsonSerializerContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.