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

added hook for chain links #125

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
added hook for chain links
TesAnti committed Feb 1, 2024
commit c24435ac3a7d43bd7873fa1d53bf3d96af5843b2
Original file line number Diff line number Diff line change
@@ -96,7 +96,12 @@ public Task<IChainValues> CallAsync(IChainValues values, ICallbacks? callbacks =

try
{
return InternalCall(values);
var res= InternalCall(values);
if (_hook!=null)
{
_hook(values);
}
return res;
}
catch (StackableChainException)
{
@@ -217,4 +222,10 @@ public async Task<string> Run(

return res.Value[OutputKeys[0]].ToString() ?? string.Empty;
}

private Action<IChainValues>? _hook;
public void SetHook(Action<IChainValues> hook)
{
_hook = hook;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using LangChain.Abstractions.Schema;
using LangChain.Chains.HelperChains;

namespace LangChain.Chains.StackableChains.Extensions;

public static class HookExtension
{
public static T Hook<T>(this T chain, Action<IChainValues> hook) where T : BaseStackableChain
{
chain.SetHook(hook);

Check warning on line 10 in src/libs/LangChain.Core/Chains/StackableChains/Extensions/HookExtension.cs

GitHub Actions / Build and test / Build, test and publish

In externally visible method 'T HookExtension.Hook<T>(T chain, Action<IChainValues> hook)', validate parameter 'chain' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)
return chain;
}
}

Unchanged files with check annotations Beta

LoadMessages().Wait();
}
return _messages;

Check warning on line 25 in src/libs/LangChain.Core/Memory/FileChatMessageHistory.cs

GitHub Actions / Build and test / Build, test and publish

Possible null reference return.
}
}
/// </summary>
/// <param name="messagesFilePath">Path to local history file</param>
/// <exception cref="ArgumentNullException"></exception>
public FileChatMessageHistory(string messagesFilePath)

Check warning on line 34 in src/libs/LangChain.Core/Memory/FileChatMessageHistory.cs

GitHub Actions / Build and test / Build, test and publish

Non-nullable field '_messages' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
MessagesFilePath = messagesFilePath ?? throw new ArgumentNullException(nameof(messagesFilePath));
}
public override async Task AddMessage(Message message)
{
_messages.Add(message);
await SaveMessages();

Check warning on line 43 in src/libs/LangChain.Core/Memory/FileChatMessageHistory.cs

GitHub Actions / Build and test / Build, test and publish

Consider calling ConfigureAwait on the awaited task (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2007)
}
/// <inheritdoc/>
public override async Task Clear()
{
_messages.Clear();
await SaveMessages();

Check warning on line 50 in src/libs/LangChain.Core/Memory/FileChatMessageHistory.cs

GitHub Actions / Build and test / Build, test and publish

Consider calling ConfigureAwait on the awaited task (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2007)
}
private async Task SaveMessages()
{
string json = JsonSerializer.Serialize(_messages);
await Task.Run(() => File.WriteAllText(MessagesFilePath, json));

Check warning on line 56 in src/libs/LangChain.Core/Memory/FileChatMessageHistory.cs

GitHub Actions / Build and test / Build, test and publish

Consider calling ConfigureAwait on the awaited task (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2007)
}
private async Task LoadMessages()
{
if (File.Exists(MessagesFilePath))
{
string json = await Task.Run(() => File.ReadAllText(MessagesFilePath));

Check warning on line 63 in src/libs/LangChain.Core/Memory/FileChatMessageHistory.cs

GitHub Actions / Build and test / Build, test and publish

Consider calling ConfigureAwait on the awaited task (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2007)
_messages = JsonSerializer.Deserialize<List<Message>>(json);

Check warning on line 64 in src/libs/LangChain.Core/Memory/FileChatMessageHistory.cs

GitHub Actions / Build and test / Build, test and publish

Possible null reference assignment.
}
else
{
var inCode = false;
foreach (var line in lines)
{
if (line.StartsWith("```"))

Check warning on line 22 in src/libs/LangChain.Core/Chains/StackableChains/ExtractCodeChain.cs

GitHub Actions / Build and test / Build, test and publish

The behavior of 'string.StartsWith(string)' could vary based on the current user's locale settings. Replace this call in 'LangChain.Chains.StackableChains.ExtractCodeChain.ExtractCode(string)' with a call to 'string.StartsWith(string, System.StringComparison)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1310)
{
inCode = !inCode;
continue;
{
if (values.Value[InputKeys[0]] is string text)

Check warning on line 48 in src/libs/LangChain.Core/Chains/StackableChains/ExtractCodeChain.cs

GitHub Actions / Build and test / Build, test and publish

In externally visible method 'Task<IChainValues> ExtractCodeChain.InternalCall(IChainValues values)', validate parameter 'values' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)
{
values.Value[OutputKeys[0]] = ExtractCode(text);
}