Browse Source

test: add 9 examples of the new version.

tags/v0.4.0
Yaohui Liu 3 years ago
parent
commit
2eb2d6df83
No known key found for this signature in database GPG Key ID: E86D01E1809BD23E
15 changed files with 531 additions and 119 deletions
  1. +37
    -0
      LLama.Examples/NewVersion/ChatSessionStripRoleName.cs
  2. +39
    -0
      LLama.Examples/NewVersion/ChatSessionWithRoleName.cs
  3. +30
    -0
      LLama.Examples/NewVersion/GetEmbeddings.cs
  4. +39
    -0
      LLama.Examples/NewVersion/InstructModeExecute.cs
  5. +40
    -0
      LLama.Examples/NewVersion/InteractiveModeExecute.cs
  6. +65
    -0
      LLama.Examples/NewVersion/LoadAndSaveSession.cs
  7. +47
    -36
      LLama.Examples/NewVersion/LoadAndSaveState.cs
  8. +30
    -0
      LLama.Examples/NewVersion/QuantizeModel.cs
  9. +43
    -0
      LLama.Examples/NewVersion/StatelessModeExecute.cs
  10. +78
    -0
      LLama.Examples/NewVersion/TestRunner.cs
  11. +4
    -59
      LLama.Examples/Program.cs
  12. +2
    -0
      LLama/LLamaExecutorBase.cs
  13. +29
    -12
      LLama/LLamaInstructExecutor.cs
  14. +23
    -8
      LLama/LLamaInteractExecutor.cs
  15. +25
    -4
      LLama/LLamaTransforms.cs

+ 37
- 0
LLama.Examples/NewVersion/ChatSessionStripRoleName.cs View File

@@ -0,0 +1,37 @@
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 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<string> { "User:" } }))
{
Console.Write(text);
}

Console.ForegroundColor = ConsoleColor.Green;
prompt = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
}
}
}
}

+ 39
- 0
LLama.Examples/NewVersion/ChatSessionWithRoleName.cs View File

@@ -0,0 +1,39 @@
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 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<string> { "User:" } }))
{
Console.Write(text);
}

Console.ForegroundColor = ConsoleColor.Green;
prompt = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
}
}
}
}

+ 30
- 0
LLama.Examples/NewVersion/GetEmbeddings.cs View File

@@ -0,0 +1,30 @@
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 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();
}
}
}
}

+ 39
- 0
LLama.Examples/NewVersion/InstructModeExecute.cs View File

@@ -0,0 +1,39 @@
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 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;
}
}
}
}

+ 40
- 0
LLama.Examples/NewVersion/InteractiveModeExecute.cs View File

@@ -0,0 +1,40 @@
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()
{
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<string> { "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;
}
}
}
}

+ 65
- 0
LLama.Examples/NewVersion/LoadAndSaveSession.cs View File

@@ -0,0 +1,65 @@
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
{
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<string> { "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;
}
}
}
}
}

+ 47
- 36
LLama.Examples/NewVersion/LoadAndSaveState.cs View File

@@ -1,5 +1,4 @@
using LLama.Common;
using LLama.OldVersion;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -8,48 +7,60 @@ using System.Threading.Tasks;

namespace LLama.Examples.NewVersion
{
public class SaveAndLoadState : IDisposable
public class LoadAndSaveState
{
InteractiveExecutor _executor;
string _prompt;
public SaveAndLoadState(string modelPath, string prompt)
public static void Run()
{
_prompt = prompt;
_executor = new InteractiveExecutor(new LLamaModel(new ModelParams(modelPath: modelPath)));
foreach (var text in _executor.Infer(_prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List<string> { "user:" } }))
{
Console.Write(text);
}
}
Console.Write("Please input your model path: ");
string modelPath = Console.ReadLine();
var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim();

public void Run(string prompt)
{
InferenceParams sessionParams = new InferenceParams() { Temperature = 0.2f, AntiPrompts = new List<string> { "user:" } };
foreach (var text in _executor.Infer(prompt, sessionParams))
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<string> { "User:" } };

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

public void SaveState(string executorStateFile, string modelStateFile)
{
_executor.Model.SaveState(modelStateFile);
_executor.SaveState(executorStateFile);
Console.WriteLine("Saved state!");
}
prompt = Console.ReadLine();
if (prompt == "save")
{
Console.Write("Your path to save model state: ");
string modelStatePath = Console.ReadLine();
ex.Model.SaveState(modelStatePath);

public void LoadState(string executorStateFile, string modelStateFile)
{
var model = _executor.Model;
model.LoadState(modelStateFile);
_executor = new InteractiveExecutor(model);
_executor.LoadState(executorStateFile);
Console.WriteLine("Loaded state!");
}
Console.Write("Your path to save executor state: ");
string executorStatePath = Console.ReadLine();
ex.SaveState(executorStatePath);

public void Dispose()
{
_executor.Model.Dispose();
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;
}
}
}
}
}

+ 30
- 0
LLama.Examples/NewVersion/QuantizeModel.cs View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

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!");
}
else
{
Console.WriteLine("Quantization failed!");
}
}
}
}

+ 43
- 0
LLama.Examples/NewVersion/StatelessModeExecute.cs View File

@@ -0,0 +1,43 @@
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 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<string> { "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);
}
}
}
}
}

+ 78
- 0
LLama.Examples/NewVersion/TestRunner.cs View File

@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LLama.Examples.NewVersion
{
public class NewVersionTestRunner
{
public static async Task Run()
{
Console.WriteLine("================LLamaSharp Examples (New Version)==================\n");

Console.WriteLine("Please input a number to choose an example to run:");
Console.WriteLine("0: Run a chat session without stripping the role names.");
Console.WriteLine("1: Run a chat session with the role names strippped.");
Console.WriteLine("2: Interactive mode chat by using executor.");
Console.WriteLine("3: Instruct mode chat by using executor.");
Console.WriteLine("4: Stateless mode chat by using executor.");
Console.WriteLine("5: Load and save chat session.");
Console.WriteLine("6: Load and save state of model and executor.");
Console.WriteLine("7: Get embeddings from LLama model.");
Console.WriteLine("8: Quantize the model.");

while (true)
{
Console.Write("\nYour choice: ");
int choice = int.Parse(Console.ReadLine());

if (choice == 0)
{
ChatSessionStripRoleName.Run();
}
else if (choice == 1)
{
ChatSessionWithRoleName.Run();
}
else if(choice == 2)
{
await InteractiveModeExecute.Run();
}
else if(choice == 3)
{
InstructModeExecute.Run();
}
else if(choice == 4)
{
StatelessModeExecute.Run();
}
else if(choice == 5)
{
SaveAndLoadSession.Run();
}
else if(choice == 6)
{
LoadAndSaveState.Run();
}
else if(choice == 7)
{
GetEmbeddings.Run();
}
else if(choice == 8)
{
QuantizeModel.Run();
}
else
{
Console.WriteLine("Cannot parse your choice. Please select again.");
continue;
}
break;
}
}
}

}

+ 4
- 59
LLama.Examples/Program.cs View File

@@ -1,6 +1,8 @@
using LLama;
using LLama.Common;
using LLama.Examples;
using LLama.Examples.NewVersion;
using LLama.Examples.Old;

Console.WriteLine("======================================================================================================");

@@ -22,66 +24,9 @@ Console.WriteLine();

if(version == 1)
{
Console.WriteLine("The examples for new versions are under working now. We'll soon update the examples." +
" Thank you for your support!");
string modelPath = "D:\\development\\llama\\weights\\wizard-vicuna-13B.ggmlv3.q4_1.bin";
var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim();
//string prompt = " Qeustion: how to do binary search for an array in C#? Answer: ";

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

while (prompt != "skip")
{
await foreach (var text in session.ChatAsync(prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List<string> { "User:" } }, default(CancellationToken)))
{
Console.Write(text);
}
prompt = Console.ReadLine();
if(prompt == "save")
{
session.SaveSession("./SessionState");
Console.WriteLine("Saved session!");
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:" }));
session.LoadSession("./SessionState");
Console.WriteLine("Loaded session!");
prompt = Console.ReadLine();
}
}

ex.Model.Dispose();

//StatelessExecutor ex = new(new LLamaModel(new ModelParams(modelPath, contextSize: 256)));
//while (true)
//{
// foreach (var text in ex.Infer(prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List<string> { "user:" }, MaxTokens = 256 }))
// {
// Console.Write(text);
// }
// prompt = Console.ReadLine();
//}

//LLama.Examples.NewVersion.SaveAndLoadState runner = new(modelPath, prompt);
//while (true)
//{
// var input = Console.ReadLine();
// if(input == "save")
// {
// Console.Write("Your path to save state: ");
// input = Console.ReadLine();
// runner.SaveState("./ex_state.json", input);
// runner.LoadState("./ex_state.json", input);
// }
// else
// {
// runner.Run(input);
// }
//}
await NewVersionTestRunner.Run();
}
else
{
LLama.Examples.Old.OldTestRunner.Run();
OldTestRunner.Run();
}

+ 2
- 0
LLama/LLamaExecutorBase.cs View File

@@ -151,6 +151,8 @@ namespace LLama
protected abstract bool PostProcess(InferenceParams inferenceParams, InferStateArgs args, out IEnumerable<string>? extraOutputs);
protected abstract void InferInternal(InferenceParams inferenceParams, InferStateArgs args);
public abstract void SaveState(string filename);
public abstract ExecutorBaseState GetStateData();
public abstract void LoadState(ExecutorBaseState data);
public abstract void LoadState(string filename);




+ 29
- 12
LLama/LLamaInstructExecutor.cs View File

@@ -16,14 +16,14 @@ namespace LLama
bool _is_prompt_run = true;
llama_token[] _inp_pfx;
llama_token[] _inp_sfx;
public InstructExecutor(LLamaModel model, string inputPrefix = "\n\n### Instruction:\n\n",
string inputSuffix = "\n\n### Response:\n\n") : base(model)
public InstructExecutor(LLamaModel model, string instructionPrefix = "\n\n### Instruction:\n\n",
string instructionSuffix = "\n\n### Response:\n\n") : base(model)
{
_inp_pfx = _model.Tokenize(inputPrefix, true).ToArray();
_inp_sfx = _model.Tokenize(inputSuffix, false).ToArray();
_inp_pfx = _model.Tokenize(instructionPrefix, true).ToArray();
_inp_sfx = _model.Tokenize(instructionSuffix, false).ToArray();
}

public override void SaveState(string filename)
public override ExecutorBaseState GetStateData()
{
InstructExecutorState state = new()
{
@@ -41,16 +41,12 @@ namespace LLama
SessionTokens = _session_tokens,
LastTokensCapacity = _last_n_tokens.Capacity
};
using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write))
{
JsonSerializer.Serialize<InstructExecutorState>(fs, state);
}
return state;
}
public override void LoadState(string filename)
public override void LoadState(ExecutorBaseState data)
{
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
if(data is InstructExecutorState state)
{
var state = JsonSerializer.Deserialize<InstructExecutorState>(fs);
_n_session_consumed = state.ConsumedSessionCount;
_embed_inps = state.EmbedInps;
_is_prompt_run = state.IsPromptRun;
@@ -64,6 +60,27 @@ namespace LLama
_pathSession = state.SessionFilePath;
_session_tokens = state.SessionTokens;
}
else
{
throw new ArgumentException("Invalid state data type.");
}
}

public override void SaveState(string filename)
{
InstructExecutorState state = GetStateData() as InstructExecutorState;
using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write))
{
JsonSerializer.Serialize<InstructExecutorState>(fs, state);
}
}
public override void LoadState(string filename)
{
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
var state = JsonSerializer.Deserialize<InstructExecutorState>(fs);
LoadState(state);
}
}

protected override bool GetLoopCondition(InferStateArgs args)


+ 23
- 8
LLama/LLamaInteractExecutor.cs View File

@@ -23,7 +23,7 @@ namespace LLama
_llama_token_newline = Utils.Tokenize(_model.NativeHandle, "\n", false, _model.Encoding).ToArray();
}

public override void SaveState(string filename)
public override ExecutorBaseState GetStateData()
{
InteractiveExecutorState state = new()
{
@@ -40,16 +40,12 @@ namespace LLama
SessionTokens = _session_tokens,
LastTokensCapacity = _last_n_tokens.Capacity
};
using(FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write))
{
JsonSerializer.Serialize<InteractiveExecutorState>(fs, state);
}
return state;
}
public override void LoadState(string filename)
public override void LoadState(ExecutorBaseState data)
{
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
if (data is InteractiveExecutorState state)
{
var state = JsonSerializer.Deserialize<InteractiveExecutorState>(fs);
_n_session_consumed = state.ConsumedSessionCount;
_embed_inps = state.EmbedInps;
_is_prompt_run = state.IsPromptRun;
@@ -62,6 +58,25 @@ namespace LLama
_pathSession = state.SessionFilePath;
_session_tokens = state.SessionTokens;
}
else
throw new ArgumentException("Invalid state data type.");
}

public override void SaveState(string filename)
{
InteractiveExecutorState state = GetStateData() as InteractiveExecutorState;
using(FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write))
{
JsonSerializer.Serialize<InteractiveExecutorState>(fs, state);
}
}
public override void LoadState(string filename)
{
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
var state = JsonSerializer.Deserialize<InteractiveExecutorState>(fs);
LoadState(state);
}
}

/// <summary>


+ 25
- 4
LLama/LLamaTransforms.cs View File

@@ -155,18 +155,39 @@ namespace LLama
var current = string.Join("", window);
if (_keywords.Any(x => current.Contains(x)))
{
var matchedKeyword = _keywords.First(x => current.Contains(x));
int total = window.Count;
for (int i = 0; i < total; i++)
{
window.Dequeue();
}
if (!_removeAllMatchedTokens)
{
yield return current.Replace(matchedKeyword, "");
}
}
if(current.Length >= _maxKeywordLength)
if (current.Length >= _maxKeywordLength)
{
int total = window.Count;
for (int i = 0; i < total; i++)
if (_keywords.Any(x => current.Contains(x)))
{
yield return window.Dequeue();
var matchedKeyword = _keywords.First(x => current.Contains(x));
int total = window.Count;
for (int i = 0; i < total; i++)
{
window.Dequeue();
}
if (!_removeAllMatchedTokens)
{
yield return current.Replace(matchedKeyword, "");
}
}
else
{
int total = window.Count;
for (int i = 0; i < total; i++)
{
yield return window.Dequeue();
}
}
}
}


Loading…
Cancel
Save