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

Chat session state management #560

Merged
Merged
Show file tree
Hide file tree
Changes from 11 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
1 change: 1 addition & 0 deletions LLama.Examples/ExampleRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class ExampleRunner
{ "Chat Session: History", ChatSessionWithHistory.Run },
{ "Chat Session: Role names", ChatSessionWithRoleName.Run },
{ "Chat Session: Role names stripped", ChatSessionStripRoleName.Run },
{ "Chat Session: Pre-processing and reset", ChatSessionWithRestart.Run },
{ "Chat Session: Coding Assistant", CodingAssistant.Run },
{ "Chat Session: Automatic conversation", TalkToYourself.Run },
{ "Chat Session: Chinese characters", ChatChineseGB2312.Run },
Expand Down
6 changes: 6 additions & 0 deletions LLama.Examples/Examples/ChatSessionWithHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public static async Task Run()
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Session saved.");
}
else if (userInput == "load")
eublefar marked this conversation as resolved.
Show resolved Hide resolved
{
session.LoadSession("Assets/chat-with-bob");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Session loaded.");
}
else if (userInput == "regenerate")
{
Console.ForegroundColor = ConsoleColor.Yellow;
Expand Down
95 changes: 95 additions & 0 deletions LLama.Examples/Examples/ChatSessionWithRestart.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using LLama.Common;

namespace LLama.Examples.Examples;

public class ChatSessionWithRestart
{
public static async Task Run()
{
string modelPath = UserSettings.GetModelPath();

var parameters = new ModelParams(modelPath)
{
ContextSize = 1024,
Seed = 1337,
GpuLayerCount = 5
};
using var model = LLamaWeights.LoadFromFile(parameters);
using var context = model.CreateContext(parameters);
var executor = new InteractiveExecutor(context);

var chatHistoryJson = File.ReadAllText("Assets/chat-with-bob.json");
ChatHistory chatHistory = ChatHistory.FromJson(chatHistoryJson) ?? new ChatHistory();
ChatSession prototypeSession =
await ChatSession.InitializeSessionFromHistoryAsync(executor, chatHistory);
prototypeSession.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(
new string[] { "User:", "Assistant:" },
redundancyLength: 8));
var resetState = prototypeSession.GetSessionState();

ChatSession session = new ChatSession(executor);
session.LoadSession(resetState);

InferenceParams inferenceParams = new InferenceParams()
{
Temperature = 0.9f,
AntiPrompts = new List<string> { "User:" }
};

Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("The chat session has started. Write `save` to save session in memory."
+ " Write `reset` to start from the last saved checkpoint");

// show the prompt
Console.ForegroundColor = ConsoleColor.Green;
string userInput = Console.ReadLine() ?? "";

while (userInput != "exit")
{
if(userInput == "reset")
eublefar marked this conversation as resolved.
Show resolved Hide resolved
{
session.LoadSession(resetState);
Console.WriteLine($"Reset to history:\n{session.HistoryTransform.HistoryToText(session.History)}");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Session reset.");
}
else if (userInput == "save")
{
resetState = session.GetSessionState();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Session saved.");
}
else if (userInput == "regenerate")
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Regenerating last response ...");

await foreach (
var text
in session.RegenerateAssistantMessageAsync(
inferenceParams))
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write(text);
}
}
else
{
await foreach (
var text
in session.ChatAsync(
new ChatHistory.Message(AuthorRole.User, userInput),
inferenceParams))
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write(text);
}
}

Console.ForegroundColor = ConsoleColor.Green;
userInput = Console.ReadLine() ?? "";

Console.ForegroundColor = ConsoleColor.White;
}
}
}
8 changes: 8 additions & 0 deletions LLama/Abstractions/IHistoryTransform.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using LLama.Common;
using System.Text.Json.Serialization;

namespace LLama.Abstractions
{
/// <summary>
/// Transform history to plain text and vice versa.
/// </summary>
[JsonConverter(typeof(PolymorphicJSONConverter<IHistoryTransform>))]
public interface IHistoryTransform
{
/// <summary>
Expand All @@ -21,5 +23,11 @@ public interface IHistoryTransform
/// <param name="text">The chat history as plain text.</param>
/// <returns>The updated history.</returns>
ChatHistory TextToHistory(AuthorRole role, string text);

/// <summary>
/// Copy the transform.
/// </summary>
/// <returns></returns>
IHistoryTransform Clone();
}
}
11 changes: 10 additions & 1 deletion LLama/Abstractions/ITextStreamTransform.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Collections.Generic;
using LLama.Common;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace LLama.Abstractions
{
/// <summary>
/// Takes a stream of tokens and transforms them.
/// </summary>
[JsonConverter(typeof(PolymorphicJSONConverter<ITextStreamTransform>))]
public interface ITextStreamTransform
{
/// <summary>
Expand All @@ -13,5 +16,11 @@ public interface ITextStreamTransform
/// <param name="tokens"></param>
/// <returns></returns>
IAsyncEnumerable<string> TransformAsync(IAsyncEnumerable<string> tokens);

/// <summary>
/// Copy the transform.
/// </summary>
/// <returns></returns>
ITextStreamTransform Clone();
}
}
12 changes: 11 additions & 1 deletion LLama/Abstractions/ITextTransform.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace LLama.Abstractions
using System.Text.Json.Serialization;
using LLama.Common;

namespace LLama.Abstractions
{
/// <summary>
/// An interface for text transformations.
Expand All @@ -9,6 +12,7 @@
/// - Trimming
/// - etc.
/// </summary>
[JsonConverter(typeof(PolymorphicJSONConverter<ITextTransform>))]
public interface ITextTransform
{
/// <summary>
Expand All @@ -17,5 +21,11 @@ public interface ITextTransform
/// <param name="text"></param>
/// <returns></returns>
string Transform(string text);

/// <summary>
/// Copy the transform.
/// </summary>
/// <returns></returns>
ITextTransform Clone();
}
}
Loading
Loading