Skip to content

Commit

Permalink
Updated demos to use the new loading/multi context system
Browse files Browse the repository at this point in the history
  • Loading branch information
martindevans committed Aug 17, 2023
1 parent 6233185 commit 02a46fc
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 74 deletions.
17 changes: 10 additions & 7 deletions LLama.Examples/NewVersion/ChatSessionStripRoleName.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using LLama.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LLama.Examples.NewVersion
{
Expand All @@ -12,15 +8,22 @@ public class ChatSessionStripRoleName
public static void Run()
{
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();
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.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)
{
foreach (var text in session.Chat(prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List<string> { "User:" } }))
Expand Down
15 changes: 8 additions & 7 deletions LLama.Examples/NewVersion/ChatSessionWithRoleName.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using LLama.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LLama.Examples.NewVersion
{
Expand All @@ -12,10 +8,15 @@ public class ChatSessionWithRoleName
public static void Run()
{
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();
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.WriteLine("The chat session has started. In this example, the prompt is printed for better visual result.");
Expand Down
7 changes: 1 addition & 6 deletions LLama.Examples/NewVersion/GetEmbeddings.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using LLama.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

while (true)
Expand Down
13 changes: 6 additions & 7 deletions LLama.Examples/NewVersion/InstructModeExecute.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using LLama.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LLama.Examples.NewVersion
{
Expand All @@ -12,10 +8,13 @@ public class InstructModeExecute
public static void Run()
{
Console.Write("Please input your model path: ");
string modelPath = Console.ReadLine();
var modelPath = Console.ReadLine();
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.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 " +
Expand All @@ -26,7 +25,7 @@ public static void Run()

while (true)
{
foreach (var text in ex.Infer(prompt, inferenceParams))
foreach (var text in executor.Infer(prompt, inferenceParams))
{
Console.Write(text);
}
Expand Down
15 changes: 7 additions & 8 deletions LLama.Examples/NewVersion/InteractiveModeExecute.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using LLama.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LLama.Examples.NewVersion
{
public class InteractiveModeExecute
{
public async static Task Run()
public static async Task Run()
{
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.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)");
Expand Down
16 changes: 8 additions & 8 deletions LLama.Examples/NewVersion/LoadAndSaveSession.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using LLama.Common;
using LLama.OldVersion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LLama.Examples.NewVersion
{
Expand All @@ -13,10 +8,15 @@ public class SaveAndLoadSession
public static void Run()
{
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();
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.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.");
Expand Down
17 changes: 8 additions & 9 deletions LLama.Examples/NewVersion/LoadAndSaveState.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using LLama.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LLama.Examples.NewVersion
{
Expand All @@ -12,10 +8,13 @@ public class LoadAndSaveState
public static void Run()
{
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();

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.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)");
Expand Down Expand Up @@ -47,9 +46,9 @@ public static void Run()
Console.WriteLine("All states saved!");
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);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Loaded state!");
Expand Down
14 changes: 5 additions & 9 deletions LLama.Examples/NewVersion/QuantizeModel.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
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 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!");
Console.WriteLine("Quantization succeeded!");
}
else
{
Expand Down
11 changes: 5 additions & 6 deletions LLama.Examples/NewVersion/StatelessModeExecute.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using LLama.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LLama.Examples.NewVersion
{
Expand All @@ -12,9 +8,12 @@ public class StatelessModeExecute
public static void Run()
{
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.WriteLine("The executor has been enabled. In this example, the inference is an one-time job. That says, the previous input and response has " +
Expand Down
8 changes: 1 addition & 7 deletions LLama.Examples/NewVersion/TestRunner.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down

0 comments on commit 02a46fc

Please sign in to comment.