Browse Source

Updated demos to use the new loading/multi context system

tags/v0.5.1
Martin Evans 2 years ago
parent
commit
02a46fc363
10 changed files with 59 additions and 74 deletions
  1. +10
    -7
      LLama.Examples/NewVersion/ChatSessionStripRoleName.cs
  2. +8
    -7
      LLama.Examples/NewVersion/ChatSessionWithRoleName.cs
  3. +1
    -6
      LLama.Examples/NewVersion/GetEmbeddings.cs
  4. +6
    -7
      LLama.Examples/NewVersion/InstructModeExecute.cs
  5. +7
    -8
      LLama.Examples/NewVersion/InteractiveModeExecute.cs
  6. +8
    -8
      LLama.Examples/NewVersion/LoadAndSaveSession.cs
  7. +8
    -9
      LLama.Examples/NewVersion/LoadAndSaveState.cs
  8. +5
    -9
      LLama.Examples/NewVersion/QuantizeModel.cs
  9. +5
    -6
      LLama.Examples/NewVersion/StatelessModeExecute.cs
  10. +1
    -7
      LLama.Examples/NewVersion/TestRunner.cs

+ 10
- 7
LLama.Examples/NewVersion/ChatSessionStripRoleName.cs View File

@@ -1,9 +1,5 @@
using LLama.Common; using LLama.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks;


namespace LLama.Examples.NewVersion namespace LLama.Examples.NewVersion
{ {
@@ -12,15 +8,22 @@ namespace LLama.Examples.NewVersion
public static void Run() public static void Run()
{ {
Console.Write("Please input your model path: "); Console.Write("Please input your model path: ");
string modelPath = Console.ReadLine();
var modelPath = Console.ReadLine();
var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim(); var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim();
InteractiveExecutor ex = new(new LLamaContext(new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5)));
ChatSession session = new ChatSession(ex).WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(new string[] { "User:", "Bob:" }, redundancyLength: 8));

var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5);
using var model = LLamaWeights.LoadFromFile(parameters);
using var context = model.CreateContext(parameters, Encoding.UTF8);
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.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("The chat session has started. The role names won't be printed."); Console.WriteLine("The chat session has started. The role names won't be printed.");
Console.ForegroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.White;


// show the prompt
Console.Write(prompt);
while (true) while (true)
{ {
foreach (var text in session.Chat(prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List<string> { "User:" } })) foreach (var text in session.Chat(prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List<string> { "User:" } }))


+ 8
- 7
LLama.Examples/NewVersion/ChatSessionWithRoleName.cs View File

@@ -1,9 +1,5 @@
using LLama.Common; using LLama.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks;


namespace LLama.Examples.NewVersion namespace LLama.Examples.NewVersion
{ {
@@ -12,10 +8,15 @@ namespace LLama.Examples.NewVersion
public static void Run() public static void Run()
{ {
Console.Write("Please input your model path: "); Console.Write("Please input your model path: ");
string modelPath = Console.ReadLine();
var modelPath = Console.ReadLine();
var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim(); var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim();
InteractiveExecutor ex = new(new LLamaContext(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.

var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5);
using var model = LLamaWeights.LoadFromFile(parameters);
using var context = model.CreateContext(parameters, Encoding.UTF8);
var executor = new InteractiveExecutor(context);

var session = new ChatSession(executor);


Console.ForegroundColor = ConsoleColor.Yellow; Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("The chat session has started. In this example, the prompt is printed for better visual result."); Console.WriteLine("The chat session has started. In this example, the prompt is printed for better visual result.");


+ 1
- 6
LLama.Examples/NewVersion/GetEmbeddings.cs View File

@@ -1,9 +1,4 @@
using LLama.Common; using LLama.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace LLama.Examples.NewVersion namespace LLama.Examples.NewVersion
{ {
@@ -12,7 +7,7 @@ namespace LLama.Examples.NewVersion
public static void Run() public static void Run()
{ {
Console.Write("Please input your model path: "); Console.Write("Please input your model path: ");
string modelPath = Console.ReadLine();
var modelPath = Console.ReadLine();
var embedder = new LLamaEmbedder(new ModelParams(modelPath)); var embedder = new LLamaEmbedder(new ModelParams(modelPath));


while (true) while (true)


+ 6
- 7
LLama.Examples/NewVersion/InstructModeExecute.cs View File

@@ -1,9 +1,5 @@
using LLama.Common; using LLama.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks;


namespace LLama.Examples.NewVersion namespace LLama.Examples.NewVersion
{ {
@@ -12,10 +8,13 @@ namespace LLama.Examples.NewVersion
public static void Run() public static void Run()
{ {
Console.Write("Please input your model path: "); Console.Write("Please input your model path: ");
string modelPath = Console.ReadLine();
var modelPath = Console.ReadLine();
var prompt = File.ReadAllText("Assets/dan.txt").Trim(); var prompt = File.ReadAllText("Assets/dan.txt").Trim();


InstructExecutor ex = new(new LLamaContext(new ModelParams(modelPath, contextSize: 1024)));
var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5);
using var model = LLamaWeights.LoadFromFile(parameters);
using var context = model.CreateContext(parameters, Encoding.UTF8);
var executor = new InstructExecutor(context);


Console.ForegroundColor = ConsoleColor.Yellow; 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 " + 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 " +
@@ -26,7 +25,7 @@ namespace LLama.Examples.NewVersion


while (true) while (true)
{ {
foreach (var text in ex.Infer(prompt, inferenceParams))
foreach (var text in executor.Infer(prompt, inferenceParams))
{ {
Console.Write(text); Console.Write(text);
} }


+ 7
- 8
LLama.Examples/NewVersion/InteractiveModeExecute.cs View File

@@ -1,21 +1,20 @@
using LLama.Common; using LLama.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks;


namespace LLama.Examples.NewVersion namespace LLama.Examples.NewVersion
{ {
public class InteractiveModeExecute public class InteractiveModeExecute
{ {
public async static Task Run()
public static async Task Run()
{ {
Console.Write("Please input your model path: "); Console.Write("Please input your model path: ");
string modelPath = Console.ReadLine();
var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim();
var modelPath = Console.ReadLine();
var prompt = (await File.ReadAllTextAsync("Assets/chat-with-bob.txt")).Trim();


InteractiveExecutor ex = new(new LLamaContext(new ModelParams(modelPath, contextSize: 256)));
var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5);
using var model = LLamaWeights.LoadFromFile(parameters);
using var context = model.CreateContext(parameters, Encoding.UTF8);
var ex = new InteractiveExecutor(context);


Console.ForegroundColor = ConsoleColor.Yellow; Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("The executor has been enabled. In this example, the prompt is printed, the maximum tokens is set to 128 and the context size is 256. (an example for small scale usage)"); Console.WriteLine("The executor has been enabled. In this example, the prompt is printed, the maximum tokens is set to 128 and the context size is 256. (an example for small scale usage)");


+ 8
- 8
LLama.Examples/NewVersion/LoadAndSaveSession.cs View File

@@ -1,10 +1,5 @@
using LLama.Common; using LLama.Common;
using LLama.OldVersion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks;


namespace LLama.Examples.NewVersion namespace LLama.Examples.NewVersion
{ {
@@ -13,10 +8,15 @@ namespace LLama.Examples.NewVersion
public static void Run() public static void Run()
{ {
Console.Write("Please input your model path: "); Console.Write("Please input your model path: ");
string modelPath = Console.ReadLine();
var modelPath = Console.ReadLine();
var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim(); var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim();
InteractiveExecutor ex = new(new LLamaContext(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.

var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5);
using var model = LLamaWeights.LoadFromFile(parameters);
using var context = model.CreateContext(parameters, Encoding.UTF8);
var ex = new InteractiveExecutor(context);

var session = new ChatSession(ex);


Console.ForegroundColor = ConsoleColor.Yellow; 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.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.");


+ 8
- 9
LLama.Examples/NewVersion/LoadAndSaveState.cs View File

@@ -1,9 +1,5 @@
using LLama.Common; using LLama.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks;


namespace LLama.Examples.NewVersion namespace LLama.Examples.NewVersion
{ {
@@ -12,10 +8,13 @@ namespace LLama.Examples.NewVersion
public static void Run() public static void Run()
{ {
Console.Write("Please input your model path: "); Console.Write("Please input your model path: ");
string modelPath = Console.ReadLine();
var modelPath = Console.ReadLine();
var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim(); var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim();


InteractiveExecutor ex = new(new LLamaContext(new ModelParams(modelPath, contextSize: 256)));
var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5);
using var model = LLamaWeights.LoadFromFile(parameters);
using var context = model.CreateContext(parameters, Encoding.UTF8);
var ex = new InteractiveExecutor(context);


Console.ForegroundColor = ConsoleColor.Yellow; 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.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)");
@@ -47,9 +46,9 @@ namespace LLama.Examples.NewVersion
Console.WriteLine("All states saved!"); Console.WriteLine("All states saved!");
Console.ForegroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.White;


var model = ex.Context;
model.LoadState(modelStatePath);
ex = new InteractiveExecutor(model);
var ctx = ex.Context;
ctx.LoadState(modelStatePath);
ex = new InteractiveExecutor(ctx);
ex.LoadState(executorStatePath); ex.LoadState(executorStatePath);
Console.ForegroundColor = ConsoleColor.Yellow; Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Loaded state!"); Console.WriteLine("Loaded state!");


+ 5
- 9
LLama.Examples/NewVersion/QuantizeModel.cs View File

@@ -1,11 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace LLama.Examples.NewVersion
namespace LLama.Examples.NewVersion
{ {
public class QuantizeModel public class QuantizeModel
{ {
@@ -13,13 +6,16 @@ namespace LLama.Examples.NewVersion
{ {
Console.Write("Please input your original model path: "); Console.Write("Please input your original model path: ");
var inputPath = Console.ReadLine(); var inputPath = Console.ReadLine();

Console.Write("Please input your output model path: "); Console.Write("Please input your output model path: ");
var outputPath = Console.ReadLine(); var outputPath = Console.ReadLine();

Console.Write("Please input the quantize type (one of q4_0, q4_1, q5_0, q5_1, q8_0): "); Console.Write("Please input the quantize type (one of q4_0, q4_1, q5_0, q5_1, q8_0): ");
var quantizeType = Console.ReadLine(); var quantizeType = Console.ReadLine();

if (LLamaQuantizer.Quantize(inputPath, outputPath, quantizeType)) if (LLamaQuantizer.Quantize(inputPath, outputPath, quantizeType))
{ {
Console.WriteLine("Quantization succeed!");
Console.WriteLine("Quantization succeeded!");
} }
else else
{ {


+ 5
- 6
LLama.Examples/NewVersion/StatelessModeExecute.cs View File

@@ -1,9 +1,5 @@
using LLama.Common; using LLama.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks;


namespace LLama.Examples.NewVersion namespace LLama.Examples.NewVersion
{ {
@@ -12,9 +8,12 @@ namespace LLama.Examples.NewVersion
public static void Run() public static void Run()
{ {
Console.Write("Please input your model path: "); Console.Write("Please input your model path: ");
string modelPath = Console.ReadLine();
var modelPath = Console.ReadLine();


StatelessExecutor ex = new(new LLamaContext(new ModelParams(modelPath, contextSize: 256)));
var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5);
using var model = LLamaWeights.LoadFromFile(parameters);
using var context = model.CreateContext(parameters, Encoding.UTF8);
var ex = new StatelessExecutor(context);


Console.ForegroundColor = ConsoleColor.Yellow; 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 " + 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 " +


+ 1
- 7
LLama.Examples/NewVersion/TestRunner.cs View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LLama.Examples.NewVersion
namespace LLama.Examples.NewVersion
{ {
public class NewVersionTestRunner public class NewVersionTestRunner
{ {


Loading…
Cancel
Save