Browse Source

test: update the examples after refactoring.

tags/v0.4.0
Yaohui Liu 2 years ago
parent
commit
aaa0cba9f8
No known key found for this signature in database GPG Key ID: E86D01E1809BD23E
10 changed files with 151 additions and 103 deletions
  1. +4
    -3
      LLama.Examples/Old/ChatSession.cs
  2. +4
    -3
      LLama.Examples/Old/ChatWithLLamaModel.cs
  3. +1
    -0
      LLama.Examples/Old/GetEmbeddings.cs
  4. +4
    -3
      LLama.Examples/Old/InstructMode.cs
  5. +0
    -0
      LLama.Examples/Old/Quantize.cs
  6. +3
    -2
      LLama.Examples/Old/SaveAndLoadState.cs
  7. +99
    -0
      LLama.Examples/Old/TestRunner.cs
  8. +33
    -76
      LLama.Examples/Program.cs
  9. +2
    -1
      LLama.WebAPI/Services/ChatService.cs
  10. +1
    -15
      LLamaSharp.sln

LLama.Examples/ChatSession.cs → LLama.Examples/Old/ChatSession.cs View File

@@ -3,16 +3,17 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using LLama.Old;


namespace LLama.Examples namespace LLama.Examples
{ {
public class ChatSession public class ChatSession
{ {
ChatSession<LLamaModel> _session;
LLama.Old.ChatSession<LLama.Old.LLamaModel> _session;
public ChatSession(string modelPath, string promptFilePath, string[] antiprompt) public ChatSession(string modelPath, string promptFilePath, string[] antiprompt)
{ {
LLamaModel model = new(new LLamaParams(model: modelPath, n_ctx: 512, interactive: true, repeat_penalty: 1.0f, verbose_prompt: false));
_session = new ChatSession<LLamaModel>(model)
LLama.Old.LLamaModel model = new(new LLamaParams(model: modelPath, n_ctx: 512, interactive: true, repeat_penalty: 1.0f, verbose_prompt: false));
_session = new ChatSession<LLama.Old.LLamaModel>(model)
.WithPromptFile(promptFilePath) .WithPromptFile(promptFilePath)
.WithAntiprompt(antiprompt); .WithAntiprompt(antiprompt);
} }

LLama.Examples/ChatWithLLamaModel.cs → LLama.Examples/Old/ChatWithLLamaModel.cs View File

@@ -3,15 +3,16 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using LLama.Old;


namespace LLama.Examples
namespace LLama.Examples.Old
{ {
public class ChatWithLLamaModel public class ChatWithLLamaModel
{ {
LLamaModel _model;
LLama.Old.LLamaModel _model;
public ChatWithLLamaModel(string modelPath, string promptFilePath, string[] antiprompt) public ChatWithLLamaModel(string modelPath, string promptFilePath, string[] antiprompt)
{ {
_model = new LLamaModel(new LLamaParams(model: modelPath, n_ctx: 512, interactive: true, antiprompt: antiprompt.ToList(),
_model = new LLama.Old.LLamaModel(new LLamaParams(model: modelPath, n_ctx: 512, interactive: true, antiprompt: antiprompt.ToList(),
repeat_penalty: 1.0f)).WithPromptFile(promptFilePath); repeat_penalty: 1.0f)).WithPromptFile(promptFilePath);
} }



LLama.Examples/GetEmbeddings.cs → LLama.Examples/Old/GetEmbeddings.cs View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using LLama.Old;


namespace LLama.Examples namespace LLama.Examples
{ {

LLama.Examples/InstructMode.cs → LLama.Examples/Old/InstructMode.cs View File

@@ -3,15 +3,16 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using LLama.Old;


namespace LLama.Examples
namespace LLama.Examples.Old
{ {
public class InstructMode public class InstructMode
{ {
LLamaModel _model;
LLama.Old.LLamaModel _model;
public InstructMode(string modelPath, string promptFile) public InstructMode(string modelPath, string promptFile)
{ {
_model = new LLamaModel(new LLamaParams(model: modelPath, n_ctx: 2048, n_predict: -1, top_k: 10000, instruct: true,
_model = new LLama.Old.LLamaModel(new LLamaParams(model: modelPath, n_ctx: 2048, n_predict: -1, top_k: 10000, instruct: true,
repeat_penalty: 1.1f, n_batch: 256, temp: 0.2f)).WithPromptFile(promptFile); repeat_penalty: 1.1f, n_batch: 256, temp: 0.2f)).WithPromptFile(promptFile);
} }



LLama.Examples/Quantize.cs → LLama.Examples/Old/Quantize.cs View File


LLama.Examples/SaveAndLoadState.cs → LLama.Examples/Old/SaveAndLoadState.cs View File

@@ -3,15 +3,16 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using LLama.Old;


namespace LLama.Examples namespace LLama.Examples
{ {
public class SaveAndLoadState: IDisposable public class SaveAndLoadState: IDisposable
{ {
LLamaModel _model;
LLama.Old.LLamaModel _model;
public SaveAndLoadState(string modelPath, string prompt) public SaveAndLoadState(string modelPath, string prompt)
{ {
_model = new LLamaModel(new LLamaParams(model: modelPath, n_ctx: 2048, n_predict: -1, top_k: 10000, instruct: true,
_model = new LLama.Old.LLamaModel(new LLamaParams(model: modelPath, n_ctx: 2048, n_predict: -1, top_k: 10000, instruct: true,
repeat_penalty: 1.1f, n_batch: 256, temp: 0.2f)).WithPrompt(prompt); repeat_penalty: 1.1f, n_batch: 256, temp: 0.2f)).WithPrompt(prompt);
} }



+ 99
- 0
LLama.Examples/Old/TestRunner.cs View File

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

namespace LLama.Examples.Old
{
public class OldTestRunner
{
public static void Run()
{
Console.WriteLine("================LLamaSharp Examples (Old Version)==================\n");

Console.WriteLine("Please input a number to choose an example to run:");
Console.WriteLine("0: Run a chat session.");
Console.WriteLine("1: Run a LLamaModel to chat.");
Console.WriteLine("2: Quantize a model.");
Console.WriteLine("3: Get the embeddings of a message.");
Console.WriteLine("4: Run a LLamaModel with instruct mode.");
Console.WriteLine("5: Load and save state of LLamaModel.");


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

if (choice == 0)
{
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
ChatSession chat = new(modelPath, "Assets/chat-with-bob.txt", new string[] { "User:" });
chat.Run();
}
else if (choice == 1)
{
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
ChatWithLLamaModel chat = new(modelPath, "Assets/chat-with-bob.txt", new string[] { "User:" });
chat.Run();
}
else if (choice == 2) // quantization
{
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();
Quantize q = new Quantize();
q.Run(inputPath, outputPath, quantizeType);
}
else if (choice == 3) // get the embeddings only
{
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
GetEmbeddings em = new GetEmbeddings(modelPath);
Console.Write("Please input the text: ");
var text = Console.ReadLine();
em.Run(text);
}
else if (choice == 4) // instruct mode
{
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
InstructMode im = new InstructMode(modelPath, "Assets/alpaca.txt");
Console.WriteLine("Here's a simple example for using instruct mode. You can input some words and let AI " +
"complete it for you. For example: Write a story about a fox that wants to make friend with human. No less than 200 words.");
im.Run();
}
else if (choice == 5) // load and save state
{
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
Console.Write("Please input your state file path: ");
var statePath = Console.ReadLine();
SaveAndLoadState sals = new(modelPath, File.ReadAllText(@"D:\development\llama\llama.cpp\prompts\alpaca.txt"));
sals.Run("Write a story about a fox that wants to make friend with human. No less than 200 words.");
sals.SaveState(statePath);
sals.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();

// create a new model to load the state.
SaveAndLoadState sals2 = new(modelPath, "");
sals2.LoadState(statePath);
sals2.Run("Tell me more things about the fox in the story you told me.");
}
else
{
Console.WriteLine("Cannot parse your choice. Please select again.");
continue;
}
break;
}
}
}
}

+ 33
- 76
LLama.Examples/Program.cs View File

@@ -1,87 +1,44 @@
using LLama; using LLama;
using LLama.Abstractions.Params;
using LLama.Examples; using LLama.Examples;
using LLama.Examples.Old;


Console.WriteLine("================LLamaSharp Examples==================\n");
Console.WriteLine("======================================================================================================");


Console.WriteLine("Please input a number to choose an example to run:");
Console.WriteLine("0: Run a chat session.");
Console.WriteLine("1: Run a LLamaModel to chat.");
Console.WriteLine("2: Quantize a model.");
Console.WriteLine("3: Get the embeddings of a message.");
Console.WriteLine("4: Run a LLamaModel with instruct mode.");
Console.WriteLine("5: Load and save state of LLamaModel.");
Console.WriteLine(" __ __ ____ __ \r\n/\\ \\ /\\ \\ /\\ _`\\ /\\ \\ \r\n\\ \\ \\ \\ \\ \\ __ ___ ___ __ \\ \\,\\L\\_\\\\ \\ \\___ __ _ __ _____ \r\n \\ \\ \\ __\\ \\ \\ __ /'__`\\ /' __` __`\\ /'__`\\ \\/_\\__ \\ \\ \\ _ `\\ /'__`\\ /\\`'__\\/\\ '__`\\ \r\n \\ \\ \\L\\ \\\\ \\ \\L\\ \\/\\ \\L\\.\\_ /\\ \\/\\ \\/\\ \\ /\\ \\L\\.\\_ /\\ \\L\\ \\\\ \\ \\ \\ \\ /\\ \\L\\.\\_\\ \\ \\/ \\ \\ \\L\\ \\\r\n \\ \\____/ \\ \\____/\\ \\__/.\\_\\\\ \\_\\ \\_\\ \\_\\\\ \\__/.\\_\\\\ `\\____\\\\ \\_\\ \\_\\\\ \\__/.\\_\\\\ \\_\\ \\ \\ ,__/\r\n \\/___/ \\/___/ \\/__/\\/_/ \\/_/\\/_/\\/_/ \\/__/\\/_/ \\/_____/ \\/_/\\/_/ \\/__/\\/_/ \\/_/ \\ \\ \\/ \r\n \\ \\_\\ \r\n \\/_/ ");


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


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


if (choice == 0)
{
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
ChatSession chat = new(modelPath, "Assets/chat-with-bob.txt", new string[] { "User:" });
chat.Run();
}
else if (choice == 1)
{
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
ChatWithLLamaModel chat = new(modelPath, "Assets/chat-with-bob.txt", new string[] { "User:" });
chat.Run();
}
else if (choice == 2) // quantization
{
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();
Quantize q = new Quantize();
q.Run(inputPath, outputPath, quantizeType);
}
else if (choice == 3) // get the embeddings only
{
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
GetEmbeddings em = new GetEmbeddings(modelPath);
Console.Write("Please input the text: ");
var text = Console.ReadLine();
em.Run(text);
}
else if (choice == 4) // instruct mode
{
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
InstructMode im = new InstructMode(modelPath, "Assets/alpaca.txt");
Console.WriteLine("Here's a simple example for using instruct mode. You can input some words and let AI " +
"complete it for you. For example: Write a story about a fox that wants to make friend with human. No less than 200 words.");
im.Run();
}
else if (choice == 5) // load and save state
{
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
Console.Write("Please input your state file path: ");
var statePath = Console.ReadLine();
SaveAndLoadState sals = new(modelPath, File.ReadAllText(@"D:\development\llama\llama.cpp\prompts\alpaca.txt"));
sals.Run("Write a story about a fox that wants to make friend with human. No less than 200 words.");
sals.SaveState(statePath);
sals.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();


// create a new model to load the state.
SaveAndLoadState sals2 = new(modelPath, "");
sals2.LoadState(statePath);
sals2.Run("Tell me more things about the fox in the story you told me.");
}
else
Console.WriteLine();

Console.WriteLine("Please choose the version you want to test: ");
Console.WriteLine("0. old version (for v0.3.0 or earlier version)");
Console.WriteLine("1. new version (for versions after v0.4.0)");

Console.Write("\nYour Choice: ");
int version = int.Parse(Console.ReadLine());
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/dan.txt").Trim();
LLamaInstructExecutor ex = new(new LLamaModel(new ModelParams(modelPath, contextSize: 1024)));

while (true)
{ {
Console.WriteLine("Cannot parse your choice. Please select again.");
continue;
foreach (var text in ex.Infer(prompt, new SessionParams() { Temperature = 0.6f }))
{
Console.Write(text);
}
prompt = Console.ReadLine();
} }
break;
}
else
{
OldTestRunner.Run();
} }

+ 2
- 1
LLama.WebAPI/Services/ChatService.cs View File

@@ -1,4 +1,5 @@
using LLama.WebAPI.Models;
using LLama.Old;
using LLama.WebAPI.Models;


namespace LLama.WebAPI.Services; namespace LLama.WebAPI.Services;




+ 1
- 15
LLamaSharp.sln View File

@@ -9,9 +9,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LLama.Examples", "LLama.Exa
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LLamaSharp", "LLama\LLamaSharp.csproj", "{01A12D68-DE95-425E-AEEE-2D099305036D}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LLamaSharp", "LLama\LLamaSharp.csproj", "{01A12D68-DE95-425E-AEEE-2D099305036D}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPFDemo", "WPFDemo\WPFDemo.csproj", "{1E952A70-B720-4F76-9856-EC3B4259A80B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LLama.WebAPI", "LLama.WebAPI\LLama.WebAPI.csproj", "{D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LLama.WebAPI", "LLama.WebAPI\LLama.WebAPI.csproj", "{D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -59,18 +57,6 @@ Global
{01A12D68-DE95-425E-AEEE-2D099305036D}.Release|Any CPU.Build.0 = Release|Any CPU {01A12D68-DE95-425E-AEEE-2D099305036D}.Release|Any CPU.Build.0 = Release|Any CPU
{01A12D68-DE95-425E-AEEE-2D099305036D}.Release|x64.ActiveCfg = Release|x64 {01A12D68-DE95-425E-AEEE-2D099305036D}.Release|x64.ActiveCfg = Release|x64
{01A12D68-DE95-425E-AEEE-2D099305036D}.Release|x64.Build.0 = Release|x64 {01A12D68-DE95-425E-AEEE-2D099305036D}.Release|x64.Build.0 = Release|x64
{1E952A70-B720-4F76-9856-EC3B4259A80B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1E952A70-B720-4F76-9856-EC3B4259A80B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1E952A70-B720-4F76-9856-EC3B4259A80B}.Debug|x64.ActiveCfg = Debug|Any CPU
{1E952A70-B720-4F76-9856-EC3B4259A80B}.Debug|x64.Build.0 = Debug|Any CPU
{1E952A70-B720-4F76-9856-EC3B4259A80B}.GPU|Any CPU.ActiveCfg = Debug|Any CPU
{1E952A70-B720-4F76-9856-EC3B4259A80B}.GPU|Any CPU.Build.0 = Debug|Any CPU
{1E952A70-B720-4F76-9856-EC3B4259A80B}.GPU|x64.ActiveCfg = Debug|Any CPU
{1E952A70-B720-4F76-9856-EC3B4259A80B}.GPU|x64.Build.0 = Debug|Any CPU
{1E952A70-B720-4F76-9856-EC3B4259A80B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1E952A70-B720-4F76-9856-EC3B4259A80B}.Release|Any CPU.Build.0 = Release|Any CPU
{1E952A70-B720-4F76-9856-EC3B4259A80B}.Release|x64.ActiveCfg = Release|Any CPU
{1E952A70-B720-4F76-9856-EC3B4259A80B}.Release|x64.Build.0 = Release|Any CPU
{D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Debug|Any CPU.Build.0 = Debug|Any CPU {D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Debug|x64.ActiveCfg = Debug|Any CPU {D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Debug|x64.ActiveCfg = Debug|Any CPU


Loading…
Cancel
Save