diff --git a/LLama.Examples/Examples/ChatSessionStripRoleName.cs b/LLama.Examples/Examples/ChatSessionStripRoleName.cs index b39ac3ef..1246db59 100644 --- a/LLama.Examples/Examples/ChatSessionStripRoleName.cs +++ b/LLama.Examples/Examples/ChatSessionStripRoleName.cs @@ -1,44 +1,61 @@ -// using LLama.Common; - -// namespace LLama.Examples.Examples -// { -// public class ChatSessionStripRoleName -// { -// public static async Task Run() -// { -// Console.Write("Please input your model path: "); -// var modelPath = Console.ReadLine(); -// var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim(); - -// 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 session = new ChatSession(executor).WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(new string[] { "User:", "Bob:" }, redundancyLength: 8)); - -// Console.ForegroundColor = ConsoleColor.Yellow; -// Console.WriteLine("The chat session has started. The role names won't be printed."); -// Console.ForegroundColor = ConsoleColor.White; - -// // show the prompt -// Console.Write(prompt); -// while (true) -// { -// await foreach (var text in session.ChatAsync(prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List { "User:" } })) -// { -// Console.Write(text); -// } - -// Console.ForegroundColor = ConsoleColor.Green; -// prompt = Console.ReadLine(); -// Console.ForegroundColor = ConsoleColor.White; -// } -// } -// } -// } +using LLama.Common; + +namespace LLama.Examples.Examples; + +public class ChatSessionStripRoleName +{ + public static async Task Run() + { + Console.Write("Please input your model path: "); + var modelPath = Console.ReadLine(); + + 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 session = new(executor, chatHistory); + session.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform( + new string[] { "User:", "Assistant:" }, + redundancyLength: 8)); + + 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") + { + 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; + } + } +} diff --git a/LLama.Examples/Examples/ChatSessionWithHistory.cs b/LLama.Examples/Examples/ChatSessionWithHistory.cs index 27f4912b..98ba7d75 100644 --- a/LLama.Examples/Examples/ChatSessionWithHistory.cs +++ b/LLama.Examples/Examples/ChatSessionWithHistory.cs @@ -1,4 +1,3 @@ -using DocumentFormat.OpenXml.Bibliography; using LLama.Common; namespace LLama.Examples.Examples; @@ -60,7 +59,6 @@ public class ChatSessionWithHistory if (userInput == "save") { session.SaveSession("Assets/chat-with-bob"); - // await session.LoadSessionAsync("Assets/chat-with-bob"); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Session saved."); } diff --git a/LLama.Examples/Examples/ChatSessionWithRoleName.cs b/LLama.Examples/Examples/ChatSessionWithRoleName.cs index e5c180b7..d6b0d98e 100644 --- a/LLama.Examples/Examples/ChatSessionWithRoleName.cs +++ b/LLama.Examples/Examples/ChatSessionWithRoleName.cs @@ -1,44 +1,58 @@ -// using LLama.Common; - -// namespace LLama.Examples.Examples -// { -// public class ChatSessionWithRoleName -// { -// public static async Task Run() -// { -// Console.Write("Please input your model path: "); -// var modelPath = Console.ReadLine(); -// var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim(); - -// 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 session = new ChatSession(executor); - -// Console.ForegroundColor = ConsoleColor.Yellow; -// Console.WriteLine("The chat session has started. In this example, the prompt is printed for better visual result."); -// Console.ForegroundColor = ConsoleColor.White; - -// // show the prompt -// Console.Write(prompt); -// while (true) -// { -// await foreach (var text in session.ChatAsync(prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List { "User:" } })) -// { -// Console.Write(text); -// } - -// Console.ForegroundColor = ConsoleColor.Green; -// prompt = Console.ReadLine(); -// Console.ForegroundColor = ConsoleColor.White; -// } -// } -// } -// } +using LLama.Common; + +namespace LLama.Examples.Examples; + +public class ChatSessionWithRoleName +{ + public static async Task Run() + { + Console.Write("Please input your model path: "); + var modelPath = Console.ReadLine(); + + 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 session = new(executor, chatHistory); + + 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") + { + 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; + } + } +} diff --git a/LLama.Examples/Examples/Runner.cs b/LLama.Examples/Examples/Runner.cs index 43c12f87..d7653657 100644 --- a/LLama.Examples/Examples/Runner.cs +++ b/LLama.Examples/Examples/Runner.cs @@ -7,8 +7,8 @@ public class Runner private static readonly Dictionary> Examples = new() { { "Run a chat session with history.", ChatSessionWithHistory.Run }, - // { "Run a chat session without stripping the role names.", ChatSessionWithRoleName.Run }, - // { "Run a chat session with the role names stripped.", ChatSessionStripRoleName.Run }, + { "Run a chat session without stripping the role names.", ChatSessionWithRoleName.Run }, + { "Run a chat session with the role names stripped.", ChatSessionStripRoleName.Run }, { "Interactive mode chat by using executor.", InteractiveModeExecute.Run }, { "Instruct mode chat by using executor.", InstructModeExecute.Run }, { "Stateless mode chat by using executor.", StatelessModeExecute.Run },