Browse Source

Re-add ChatSession examples

tags/0.9.1
Philipp Bauer 2 years ago
parent
commit
422605d980
4 changed files with 121 additions and 92 deletions
  1. +61
    -44
      LLama.Examples/Examples/ChatSessionStripRoleName.cs
  2. +0
    -2
      LLama.Examples/Examples/ChatSessionWithHistory.cs
  3. +58
    -44
      LLama.Examples/Examples/ChatSessionWithRoleName.cs
  4. +2
    -2
      LLama.Examples/Examples/Runner.cs

+ 61
- 44
LLama.Examples/Examples/ChatSessionStripRoleName.cs View File

@@ -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<string> { "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<string> { "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;
}
}
}

+ 0
- 2
LLama.Examples/Examples/ChatSessionWithHistory.cs View File

@@ -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.");
}


+ 58
- 44
LLama.Examples/Examples/ChatSessionWithRoleName.cs View File

@@ -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<string> { "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<string> { "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;
}
}
}

+ 2
- 2
LLama.Examples/Examples/Runner.cs View File

@@ -7,8 +7,8 @@ public class Runner
private static readonly Dictionary<string, Func<Task>> 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 },


Loading…
Cancel
Save