diff --git a/docs/Examples/ChatSessionStripRoleName.md b/docs/Examples/ChatSessionStripRoleName.md new file mode 100644 index 00000000..785db83a --- /dev/null +++ b/docs/Examples/ChatSessionStripRoleName.md @@ -0,0 +1,38 @@ +# Use chat session and strip role names + +```cs +using LLama.Common; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +public class ChatSessionStripRoleName +{ + public static void Run() + { + Console.Write("Please input your model path: "); + string modelPath = Console.ReadLine(); + var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim(); + InteractiveExecutor ex = new(new LLamaModel(new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5))); + ChatSession session = new ChatSession(ex).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; + + while (true) + { + foreach (var text in session.Chat(prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List { "User:" } })) + { + Console.Write(text); + } + + Console.ForegroundColor = ConsoleColor.Green; + prompt = Console.ReadLine(); + Console.ForegroundColor = ConsoleColor.White; + } + } +} +``` \ No newline at end of file diff --git a/docs/Examples/ChatSessionWithRoleName.md b/docs/Examples/ChatSessionWithRoleName.md new file mode 100644 index 00000000..55332755 --- /dev/null +++ b/docs/Examples/ChatSessionWithRoleName.md @@ -0,0 +1,40 @@ +# Use chat session without removing role names + +```cs +using LLama.Common; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +public class ChatSessionWithRoleName +{ + public static void Run() + { + Console.Write("Please input your model path: "); + string modelPath = Console.ReadLine(); + var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim(); + InteractiveExecutor ex = new(new LLamaModel(new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5))); + ChatSession session = new ChatSession(ex); // The only change is to remove the transform for the output text stream. + + 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) + { + foreach (var text in session.Chat(prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List { "User:" } })) + { + Console.Write(text); + } + + Console.ForegroundColor = ConsoleColor.Green; + prompt = Console.ReadLine(); + Console.ForegroundColor = ConsoleColor.White; + } + } +} +``` \ No newline at end of file diff --git a/docs/Examples/GetEmbeddings.md b/docs/Examples/GetEmbeddings.md new file mode 100644 index 00000000..e827271f --- /dev/null +++ b/docs/Examples/GetEmbeddings.md @@ -0,0 +1,31 @@ +# Get embeddings + +```cs +using LLama.Common; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +public class GetEmbeddings +{ + public static void Run() + { + Console.Write("Please input your model path: "); + string modelPath = Console.ReadLine(); + var embedder = new LLamaEmbedder(new ModelParams(modelPath)); + + while (true) + { + Console.Write("Please input your text: "); + Console.ForegroundColor = ConsoleColor.Green; + var text = Console.ReadLine(); + Console.ForegroundColor = ConsoleColor.White; + + Console.WriteLine(string.Join(", ", embedder.GetEmbeddings(text))); + Console.WriteLine(); + } + } +} +``` \ No newline at end of file diff --git a/docs/Examples/InstructModeExecute.md b/docs/Examples/InstructModeExecute.md new file mode 100644 index 00000000..8daba174 --- /dev/null +++ b/docs/Examples/InstructModeExecute.md @@ -0,0 +1,40 @@ +# Use instruct executor + +```cs +using LLama.Common; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +public class InstructModeExecute +{ + public static void Run() + { + Console.Write("Please input your model path: "); + string modelPath = Console.ReadLine(); + var prompt = File.ReadAllText("Assets/dan.txt").Trim(); + + InstructExecutor ex = new(new LLamaModel(new ModelParams(modelPath, contextSize: 1024))); + + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine("The executor has been enabled. In this example, the LLM will follow your instructions. For example, you can input \"Write a story about a fox who want to " + + "make friend with human, no less than 200 words.\""); + Console.ForegroundColor = ConsoleColor.White; + + var inferenceParams = new InferenceParams() { Temperature = 0.8f, MaxTokens = 300 }; + + while (true) + { + foreach (var text in ex.Infer(prompt, inferenceParams)) + { + Console.Write(text); + } + Console.ForegroundColor = ConsoleColor.Green; + prompt = Console.ReadLine(); + Console.ForegroundColor = ConsoleColor.White; + } + } +} +``` \ No newline at end of file diff --git a/docs/Examples/InteractiveModeExecute.md b/docs/Examples/InteractiveModeExecute.md new file mode 100644 index 00000000..9b15667b --- /dev/null +++ b/docs/Examples/InteractiveModeExecute.md @@ -0,0 +1,41 @@ +# Use interactive executor + +```cs +using LLama.Common; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +public class InteractiveModeExecute +{ + public async static Task Run() + { + Console.Write("Please input your model path: "); + string modelPath = Console.ReadLine(); + var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim(); + + InteractiveExecutor ex = new(new LLamaModel(new ModelParams(modelPath, contextSize: 256))); + + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine("The executor has been enabled. In this example, the prompt is printed, the maximum tokens is set to 64 and the context size is 256. (an example for small scale usage)"); + Console.ForegroundColor = ConsoleColor.White; + + Console.Write(prompt); + + var inferenceParams = new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List { "User:" }, MaxTokens = 64 }; + + while (true) + { + await foreach (var text in ex.InferAsync(prompt, inferenceParams)) + { + Console.Write(text); + } + Console.ForegroundColor = ConsoleColor.Green; + prompt = Console.ReadLine(); + Console.ForegroundColor = ConsoleColor.White; + } + } +} +``` \ No newline at end of file diff --git a/docs/Examples/LoadAndSaveSession.md b/docs/Examples/LoadAndSaveSession.md new file mode 100644 index 00000000..01a9957d --- /dev/null +++ b/docs/Examples/LoadAndSaveSession.md @@ -0,0 +1,66 @@ +# Load and save chat session + +```cs +using LLama.Common; +using LLama.OldVersion; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +public class SaveAndLoadSession +{ + public static void Run() + { + Console.Write("Please input your model path: "); + string modelPath = Console.ReadLine(); + var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim(); + InteractiveExecutor ex = new(new LLamaModel(new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5))); + ChatSession session = new ChatSession(ex); // The only change is to remove the transform for the output text stream. + + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine("The chat session has started. In this example, the prompt is printed for better visual result. Input \"save\" to save and reload the session."); + Console.ForegroundColor = ConsoleColor.White; + + // show the prompt + Console.Write(prompt); + while (true) + { + foreach (var text in session.Chat(prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List { "User:" } })) + { + Console.Write(text); + } + + Console.ForegroundColor = ConsoleColor.Green; + prompt = Console.ReadLine(); + Console.ForegroundColor = ConsoleColor.White; + if (prompt == "save") + { + Console.Write("Preparing to save the state, please input the path you want to save it: "); + Console.ForegroundColor = ConsoleColor.Green; + var statePath = Console.ReadLine(); + session.SaveSession(statePath); + Console.ForegroundColor = ConsoleColor.White; + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine("Saved session!"); + Console.ForegroundColor = ConsoleColor.White; + + ex.Model.Dispose(); + ex = new(new LLamaModel(new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5))); + session = new ChatSession(ex).WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(new string[] { "User:", "Bob:" }, redundancyLength: 8)); + session.LoadSession(statePath); + + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine("Loaded session!"); + Console.ForegroundColor = ConsoleColor.White; + + Console.Write("Now you can continue your session: "); + Console.ForegroundColor = ConsoleColor.Green; + prompt = Console.ReadLine(); + Console.ForegroundColor = ConsoleColor.White; + } + } + } +} +``` \ No newline at end of file diff --git a/docs/Examples/LoadAndSaveState.md b/docs/Examples/LoadAndSaveState.md new file mode 100644 index 00000000..80319b5c --- /dev/null +++ b/docs/Examples/LoadAndSaveState.md @@ -0,0 +1,67 @@ +# Load and save model/exeutor state + +```cs +using LLama.Common; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +public class LoadAndSaveState +{ + public static void Run() + { + Console.Write("Please input your model path: "); + string modelPath = Console.ReadLine(); + var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim(); + + InteractiveExecutor ex = new(new LLamaModel(new ModelParams(modelPath, contextSize: 256))); + + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine("The executor has been enabled. In this example, the prompt is printed, the maximum tokens is set to 64 and the context size is 256. (an example for small scale usage)"); + Console.ForegroundColor = ConsoleColor.White; + + Console.Write(prompt); + + var inferenceParams = new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List { "User:" } }; + + while (true) + { + foreach (var text in ex.Infer(prompt, inferenceParams)) + { + Console.Write(text); + } + + prompt = Console.ReadLine(); + if (prompt == "save") + { + Console.Write("Your path to save model state: "); + string modelStatePath = Console.ReadLine(); + ex.Model.SaveState(modelStatePath); + + Console.Write("Your path to save executor state: "); + string executorStatePath = Console.ReadLine(); + ex.SaveState(executorStatePath); + + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine("All states saved!"); + Console.ForegroundColor = ConsoleColor.White; + + var model = ex.Model; + model.LoadState(modelStatePath); + ex = new InteractiveExecutor(model); + ex.LoadState(executorStatePath); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine("Loaded state!"); + Console.ForegroundColor = ConsoleColor.White; + + Console.Write("Now you can continue your session: "); + Console.ForegroundColor = ConsoleColor.Green; + prompt = Console.ReadLine(); + Console.ForegroundColor = ConsoleColor.White; + } + } + } +} +``` \ No newline at end of file diff --git a/docs/Examples/QuantizeModel.md b/docs/Examples/QuantizeModel.md new file mode 100644 index 00000000..22596657 --- /dev/null +++ b/docs/Examples/QuantizeModel.md @@ -0,0 +1,31 @@ +# Quantize model + +```cs +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +public class QuantizeModel +{ + public static void Run() + { + Console.Write("Please input your original model path: "); + var inputPath = Console.ReadLine(); + Console.Write("Please input your output model path: "); + var outputPath = Console.ReadLine(); + Console.Write("Please input the quantize type (one of q4_0, q4_1, q5_0, q5_1, q8_0): "); + var quantizeType = Console.ReadLine(); + if (LLamaQuantizer.Quantize(inputPath, outputPath, quantizeType)) + { + Console.WriteLine("Quantization succeed!"); + } + else + { + Console.WriteLine("Quantization failed!"); + } + } +} +``` \ No newline at end of file diff --git a/docs/Examples/StatelessModeExecute.md b/docs/Examples/StatelessModeExecute.md new file mode 100644 index 00000000..3f493a2e --- /dev/null +++ b/docs/Examples/StatelessModeExecute.md @@ -0,0 +1,44 @@ +# Use stateless exeutor + +```cs +using LLama.Common; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +public class StatelessModeExecute +{ + public static void Run() + { + Console.Write("Please input your model path: "); + string modelPath = Console.ReadLine(); + + StatelessExecutor ex = new(new LLamaModel(new ModelParams(modelPath, contextSize: 256))); + + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine("The executor has been enabled. In this example, the inference is an one-time job. That says, the previous input and response has " + + "no impact on the current response. Now you can ask it questions. Note that in this example, no prompt was set for LLM and the maximum response tokens is 50. " + + "It may not perform well because of lack of prompt. This is also an example that could indicate the improtance of prompt in LLM. To improve it, you can add " + + "a prompt for it yourself!"); + Console.ForegroundColor = ConsoleColor.White; + + var inferenceParams = new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List { "Question:", "#", "Question: ", ".\n" }, MaxTokens = 50 }; + + while (true) + { + Console.Write("\nQuestion: "); + Console.ForegroundColor = ConsoleColor.Green; + string prompt = Console.ReadLine(); + Console.ForegroundColor = ConsoleColor.White; + Console.Write("Answer: "); + prompt = $"Question: {prompt.Trim()} Answer: "; + foreach (var text in ex.Infer(prompt, inferenceParams)) + { + Console.Write(text); + } + } + } +} +``` \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index fb5aa2c4..8667b1ab 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -26,6 +26,16 @@ nav: - BotSharp: HighLevelApps/bot-sharp.md - More: - Logger: More/log.md + - Examples: + - Chat session 1: Examples/ChatSessionStripRoleName.md + - Chat session 2: Examples/ChatSessionWithRoleName.md + - Get embeddings: Examples/GetEmbeddings.md + - Instruct executor: Examples/InstructModeExecute.md + - Interactive executor: Examples/InteractiveModeExecute.md + - Stateless exeutor: Examples/StatelessModeExecute.md + - Load/Save session: Examples/LoadAndSaveSession.md + - Load/Save state: Examples/LoadAndSaveState.md + - Quantize model: Examples/QuantizeModel.md - API Reference: - index: ./xmldocs/index.md - llama.abstractions.ihistorytransform: ./xmldocs/llama.abstractions.ihistorytransform.md diff --git a/site/404.html b/site/404.html index 1b5587ac..a1fcefe8 100644 --- a/site/404.html +++ b/site/404.html @@ -682,12 +682,173 @@ + + + + + + + + + + + + +
  • + + + + + + + + + + +
  • + + + + + + + + + + + +
  • + + + + + + + + + + +
  • + + + + + + + + + + + +
  • + + + + + + + + + + +
  • + + + + + + + + + + + +
  • + + + + + + + + + + +
  • + + + + + + + + + + + +
  • + + + + + + + + + + +
  • + + + + + + + + + + + +
  • + + + + + + + + + + +
  • + + + + + + + + + + + +
  • + + + + + + + + + + +
  • + + + + + + + + + + + +
  • + + + + + + + + + + +
  • + + + + + + + + + + + +
  • + + + + + + + + + + +
  • + + + + + + + + + + + +
  • + + + + + + + + + + +
  • + + + + + + + + + + + +
  • + + + + + + + + + + +
  • + + + + + + + + + + + +
  • + + + + + + + + + + +
  • + + + + + + + + + + + +
  • + + + + + + + + + + +
  • + + + + + + + + + + + +
  • + + + + + + + + + + +
  • + + + + + + + + + + + +
  • + + + + + + + + + + +
  • + + + + + + + + + + + +
  • + + + + + + + + + + +
  • + + + + + + + + + + + +
  • + + + + + + + + + + +
  • + + + + + + + + + + + +
  • + + + + + + + + + + +
  • + + + + + + + + + + + +
  • + + + + + + + + + + +
  • + + + + + + + + + + + +
  • + + + + + + + + + + +