From 0763f307ec06d85725d81c5f654c86218eb9068f Mon Sep 17 00:00:00 2001 From: eublefar Date: Sat, 2 Mar 2024 17:27:18 +0100 Subject: [PATCH] Example chat session with preprocessing of chat history and reset operation that resets chat to original point of history without extra processing --- LLama.Examples/ExampleRunner.cs | 1 + .../Examples/ChatSessionWithRestart.cs | 94 +++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 LLama.Examples/Examples/ChatSessionWithRestart.cs diff --git a/LLama.Examples/ExampleRunner.cs b/LLama.Examples/ExampleRunner.cs index 790a1f9c..b74170e3 100644 --- a/LLama.Examples/ExampleRunner.cs +++ b/LLama.Examples/ExampleRunner.cs @@ -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 }, diff --git a/LLama.Examples/Examples/ChatSessionWithRestart.cs b/LLama.Examples/Examples/ChatSessionWithRestart.cs new file mode 100644 index 00000000..3462c506 --- /dev/null +++ b/LLama.Examples/Examples/ChatSessionWithRestart.cs @@ -0,0 +1,94 @@ +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 { "User:" } + }; + + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine("The chat session has started."); + + // show the prompt + Console.ForegroundColor = ConsoleColor.Green; + string userInput = Console.ReadLine() ?? ""; + + while (userInput != "exit") + { + if(userInput == "reset") + { + session.LoadSession(resetState); + Console.WriteLine($"History: {session.HistoryTransform.HistoryToText(session.History)}"); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine("Session reset."); + } + else if (userInput == "save") + { + session.SaveSession("Assets/chat-with-bob"); + 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; + } + } +}