-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Сreated various Memory classes from LangChain documentation.
- Loading branch information
Showing
16 changed files
with
156 additions
and
44 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,20 @@ | ||
using LangChain.Base; | ||
using LangChain.Callback; | ||
using LangChain.Memory; | ||
using LangChain.Prompts.Base; | ||
using LangChain.Providers; | ||
|
||
namespace LangChain.Chains.LLM; | ||
|
||
public class LlmChainInput : ILlmChainInput | ||
public class LlmChainInput( | ||
IChatModel llm, | ||
BasePromptTemplate prompt, | ||
BaseMemory? memory = null) | ||
: ILlmChainInput | ||
{ | ||
public LlmChainInput(IChatModel llm, BasePromptTemplate prompt) | ||
{ | ||
this.Llm = llm; | ||
this.Prompt = prompt; | ||
} | ||
|
||
public BasePromptTemplate Prompt { get; set; } | ||
public IChatModel Llm { get; set; } | ||
public BasePromptTemplate Prompt { get; set; } = prompt; | ||
public IChatModel Llm { get; set; } = llm; | ||
public string OutputKey { get; set; } | ||
public bool? Verbose { get; set; } | ||
public CallbackManager CallbackManager { get; set; } | ||
public BaseMemory? Memory { get; set; } = memory; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using LangChain.Schema; | ||
|
||
namespace LangChain.Memory; | ||
|
||
public abstract class BaseChatMemory : BaseMemory | ||
{ | ||
protected BaseChatMessageHistory ChatHistory { get; set; } | ||
public bool ReturnMessages { get; set; } | ||
|
||
public BaseChatMemory(BaseChatMemoryInput input) | ||
{ | ||
if (input.ChatHistory is null) ChatHistory = new ChatMessageHistory(); | ||
else ChatHistory = input.ChatHistory; | ||
ReturnMessages = input.ReturnMessages; | ||
} | ||
|
||
public abstract override OutputValues LoadMemoryVariables(InputValues inputValues); | ||
|
||
public override void SaveContext(InputValues inputValues, OutputValues outputValues) | ||
{ | ||
ChatHistory.AddUserMessage(inputValues.Value[inputValues.Value.Keys.FirstOrDefault().ToString()].ToString()); | ||
ChatHistory.AddAiMessage(outputValues.Value[outputValues.Value.Keys.FirstOrDefault().ToString()].ToString()); | ||
} | ||
|
||
public void Clear() | ||
{ | ||
ChatHistory.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,9 @@ | ||
namespace LangChain.Memory; | ||
|
||
public class BaseChatMemoryInput | ||
{ | ||
public BaseChatMessageHistory ChatHistory { get; set; } | ||
public string InputKey { get; set; } | ||
public string MemoryKey { get; set; } | ||
public bool ReturnMessages { get; set; } | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using LangChain.Schema; | ||
|
||
namespace LangChain.Memory; | ||
|
||
public abstract class BaseMemory | ||
{ | ||
public abstract OutputValues LoadMemoryVariables(InputValues inputValues); | ||
public abstract void SaveContext(InputValues inputValues, OutputValues outputValues); | ||
} |
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,15 @@ | ||
using LangChain.Schema; | ||
|
||
namespace LangChain.Memory; | ||
|
||
public class BufferMemory : BaseChatMemory | ||
{ | ||
public BufferMemory(BufferMemoryInput input) : base(input) | ||
{ | ||
} | ||
|
||
public override OutputValues LoadMemoryVariables(InputValues? inputValues) | ||
{ | ||
return new OutputValues(new Dictionary<string, object> { { "history", ChatHistory } }); | ||
} | ||
} |
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,7 @@ | ||
namespace LangChain.Memory; | ||
|
||
public sealed class BufferMemoryInput : BaseChatMemoryInput | ||
{ | ||
public string AiPrefix { get; set; } | ||
public string HumanPrefix { get; set; } | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace LangChain.Memory; | ||
|
||
public class ConversationBufferMemory : BufferMemory | ||
{ | ||
public ConversationBufferMemory(BufferMemoryInput input) : base(input) | ||
{ | ||
} | ||
} |
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,26 @@ | ||
using LangChain.Providers; | ||
|
||
namespace LangChain.Memory; | ||
|
||
public static class MemoryExtensions | ||
{ | ||
public static IReadOnlyCollection<Message> WithHistory(this IReadOnlyCollection<Message> messages, BaseMemory? memory) | ||
{ | ||
var history = "These are our previous conversations:\n"; | ||
var previousMessages = memory.LoadMemoryVariables(null); | ||
if (previousMessages.Value is { } messageDict && | ||
messageDict["history"] is ChatMessageHistory msg) | ||
{ | ||
foreach (var chatMessage in msg.Messages) | ||
{ | ||
history += chatMessage.Content + "\n"; | ||
} | ||
} | ||
|
||
return new[] | ||
{ | ||
history.AsHumanMessage(), | ||
}.Concat(messages).ToArray(); | ||
} | ||
|
||
} |
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,11 @@ | ||
namespace LangChain.Schema; | ||
|
||
public class OutputValues | ||
{ | ||
public OutputValues(Dictionary<string, object> value) | ||
{ | ||
this.Value = value; | ||
} | ||
|
||
public Dictionary<string, object> Value { get; set; } = new(); | ||
} |
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