Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Spectre.Console;
using LLama.Examples.Examples;

namespace LLama.Examples.Examples;

public class Runner
public class ExampleRunner
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The namespace appears to have disappeared from this file altogether

{
private static readonly Dictionary<string, Func<Task>> Examples = new()
{
Expand All @@ -26,7 +25,7 @@ public class Runner
{ "Batched Executor (Fork)", BatchedExecutorFork.Run },
{ "Batched Executor (Rewind)", BatchedExecutorRewind.Run },
{ "SK Kernel Memory.", KernelMemory.Run },
{ "Exit", async () => Environment.Exit(0) }
{ "Exit", () => { Environment.Exit(0); return Task.CompletedTask; } }
};

public static async Task Run()
Expand Down
3 changes: 1 addition & 2 deletions LLama.Examples/Examples/BatchedExecutorFork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public class BatchedExecutorFork

public static async Task Run()
{
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
string modelPath = UserSettings.GetModelPath();

var parameters = new ModelParams(modelPath);
using var model = LLamaWeights.LoadFromFile(parameters);
Expand Down
3 changes: 1 addition & 2 deletions LLama.Examples/Examples/BatchedExecutorRewind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public class BatchedExecutorRewind

public static async Task Run()
{
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
string modelPath = UserSettings.GetModelPath();

var parameters = new ModelParams(modelPath);
using var model = LLamaWeights.LoadFromFile(parameters);
Expand Down
3 changes: 1 addition & 2 deletions LLama.Examples/Examples/ChatChineseGB2312.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public static async Task Run()
" to use https://huggingface.co/hfl/chinese-alpaca-2-7b-gguf/blob/main/ggml-model-q5_0.gguf, which has been verified by LLamaSharp developers.");
Console.ForegroundColor = ConsoleColor.White;

Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
string modelPath = UserSettings.GetModelPath();

var parameters = new ModelParams(modelPath)
{
Expand Down
3 changes: 1 addition & 2 deletions LLama.Examples/Examples/ChatSessionStripRoleName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ public class ChatSessionStripRoleName
{
public static async Task Run()
{
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
string modelPath = UserSettings.GetModelPath();

var parameters = new ModelParams(modelPath)
{
Expand Down
3 changes: 1 addition & 2 deletions LLama.Examples/Examples/ChatSessionWithHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ public class ChatSessionWithHistory
{
public static async Task Run()
{
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
string modelPath = UserSettings.GetModelPath();

var parameters = new ModelParams(modelPath)
{
Expand Down
3 changes: 1 addition & 2 deletions LLama.Examples/Examples/ChatSessionWithRoleName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ public class ChatSessionWithRoleName
{
public static async Task Run()
{
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
string modelPath = UserSettings.GetModelPath();

var parameters = new ModelParams(modelPath)
{
Expand Down
59 changes: 16 additions & 43 deletions LLama.Examples/Examples/CodingAssistant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@
{
using LLama.Common;
using System;
using System.Reflection;

internal class CodingAssistant
{
const string DefaultModelUri = "https://huggingface.co/TheBloke/CodeLlama-7B-Instruct-GGUF/resolve/main/codellama-7b-instruct.Q4_K_S.gguf";

// Source paper with example prompts:
// https://doi.org/10.48550/arXiv.2308.12950
const string InstructionPrefix = "[INST]";
const string InstructionSuffix = "[/INST]";
const string SystemInstruction = "You're an intelligent, concise coding assistant. Wrap code in ``` for readability. Don't repeat yourself. Use best practice and good coding standards.";
private static string ModelsDirectory = Path.Combine(Directory.GetParent(Assembly.GetExecutingAssembly().Location)!.FullName, "Models");
const string SystemInstruction = "You're an intelligent, concise coding assistant. " +
"Wrap code in ``` for readability. Don't repeat yourself. " +
"Use best practice and good coding standards.";

public static async Task Run()
{
Console.Write("Please input your model path (if left empty, a default model will be downloaded for you): ");
var modelPath = Console.ReadLine();

if(string.IsNullOrWhiteSpace(modelPath) )
string modelPath = UserSettings.GetModelPath();
if (!modelPath.Contains("codellama", StringComparison.InvariantCultureIgnoreCase))
{
modelPath = await GetDefaultModel();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("WARNING: the model you selected is not a Code LLama model!");
Console.WriteLine("For this example we specifically recommend 'codellama-7b-instruct.Q4_K_S.gguf'");
Console.WriteLine("Press ENTER to continue...");
Console.ReadLine();
}

var parameters = new ModelParams(modelPath)
Expand All @@ -35,12 +35,14 @@ public static async Task Run()

Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("The executor has been enabled. In this example, the LLM will follow your instructions." +
"\nIt's a 7B Code Llama, so it's trained for programming tasks like \"Write a C# function reading a file name from a given URI\" or \"Write some programming interview questions\"." +
"\nIt's a 7B Code Llama, so it's trained for programming tasks like \"Write a C# function reading " +
"a file name from a given URI\" or \"Write some programming interview questions\"." +
"\nWrite 'exit' to exit");
Console.ForegroundColor = ConsoleColor.White;

var inferenceParams = new InferenceParams() {
Temperature = 0.8f,
var inferenceParams = new InferenceParams()
{
Temperature = 0.8f,
MaxTokens = -1,
};

Expand All @@ -51,7 +53,7 @@ public static async Task Run()
{

Console.ForegroundColor = ConsoleColor.Green;
await foreach (var text in executor.InferAsync(instruction + System.Environment.NewLine, inferenceParams))
await foreach (var text in executor.InferAsync(instruction + Environment.NewLine, inferenceParams))
{
Console.Write(text);
}
Expand All @@ -61,34 +63,5 @@ public static async Task Run()
instruction = Console.ReadLine() ?? "Ask me for instructions.";
}
}

private static async Task<string> GetDefaultModel()
{
var uri = new Uri(DefaultModelUri);
var modelName = uri.Segments[^1];
await Console.Out.WriteLineAsync($"The following model will be used: {modelName}");
var modelPath = Path.Combine(ModelsDirectory, modelName);
if(!Directory.Exists(ModelsDirectory))
{
Directory.CreateDirectory(ModelsDirectory);
}

if (File.Exists(modelPath))
{
await Console.Out.WriteLineAsync($"Existing model found, using {modelPath}");
}
else
{
await Console.Out.WriteLineAsync($"Model not found locally, downloading {DefaultModelUri}...");
using var http = new HttpClient();
await using var downloadStream = await http.GetStreamAsync(uri);
await using var fileStream = new FileStream(modelPath, FileMode.Create, FileAccess.Write);
await downloadStream.CopyToAsync(fileStream);
await Console.Out.WriteLineAsync($"Model downloaded and saved to {modelPath}");
}


return modelPath;
}
}
}
4 changes: 1 addition & 3 deletions LLama.Examples/Examples/GetEmbeddings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ public class GetEmbeddings
{
public static void Run()
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
string modelPath = UserSettings.GetModelPath();

Console.ForegroundColor = ConsoleColor.DarkGray;
var @params = new ModelParams(modelPath) { EmbeddingMode = true };
Expand Down
15 changes: 7 additions & 8 deletions LLama.Examples/Examples/GrammarJsonResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ public class GrammarJsonResponse
{
public static async Task Run()
{
var gbnf = (await File.ReadAllTextAsync("Assets/json.gbnf")).Trim();
var grammar = Grammar.Parse(gbnf, "root");
string modelPath = UserSettings.GetModelPath();

Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
var gbnf = File.ReadAllText("Assets/json.gbnf").Trim();
var grammar = Grammar.Parse(gbnf, "root");

var parameters = new ModelParams(modelPath)
{
Expand All @@ -27,10 +26,10 @@ public static async Task Run()
Console.ForegroundColor = ConsoleColor.White;

using var grammarInstance = grammar.CreateInstance();
var inferenceParams = new InferenceParams()
{
Temperature = 0.6f,
AntiPrompts = new List<string> { "Question:", "#", "Question: ", ".\n" },
var inferenceParams = new InferenceParams()
{
Temperature = 0.6f,
AntiPrompts = new List<string> { "Question:", "#", "Question: ", ".\n" },
MaxTokens = 50,
Grammar = grammarInstance
};
Expand Down
4 changes: 2 additions & 2 deletions LLama.Examples/Examples/InstructModeExecute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public class InstructModeExecute
{
public static async Task Run()
{
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
string modelPath = UserSettings.GetModelPath();

var prompt = File.ReadAllText("Assets/dan.txt").Trim();

var parameters = new ModelParams(modelPath)
Expand Down
4 changes: 2 additions & 2 deletions LLama.Examples/Examples/InteractiveModeExecute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public class InteractiveModeExecute
{
public static async Task Run()
{
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
string modelPath = UserSettings.GetModelPath();

var prompt = (await File.ReadAllTextAsync("Assets/chat-with-bob.txt")).Trim();

var parameters = new ModelParams(modelPath)
Expand Down
18 changes: 8 additions & 10 deletions LLama.Examples/Examples/KernelMemory.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LLamaSharp.KernelMemory;
using LLamaSharp.KernelMemory;
using Microsoft.KernelMemory;
using Microsoft.KernelMemory.Configuration;
using Microsoft.KernelMemory.Handlers;

namespace LLama.Examples.Examples
{
public class KernelMemory
{
public static async Task Run()
{
Console.WriteLine("Example from: https://github.com/microsoft/kernel-memory/blob/main/examples/101-using-core-nuget/Program.cs");
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
string modelPath = UserSettings.GetModelPath();

Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("This example is from : \n" +
"https://github.com/microsoft/kernel-memory/blob/main/examples/101-using-core-nuget/Program.cs");

var searchClientConfig = new SearchClientConfig
{
MaxMatchesCount = 1,
AnswerTokens = 100,
};

var memory = new KernelMemoryBuilder()
.WithLLamaSharpDefaults(new LLamaSharpConfig(modelPath)
{
Expand Down
7 changes: 3 additions & 4 deletions LLama.Examples/Examples/LoadAndSaveSession.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using DocumentFormat.OpenXml.Bibliography;
using LLama.Common;
using LLama.Common;

namespace LLama.Examples.Examples
{
public class SaveAndLoadSession
{
public static async Task Run()
{
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
string modelPath = UserSettings.GetModelPath();

var prompt = (await File.ReadAllTextAsync("Assets/chat-with-bob.txt")).Trim();

var parameters = new ModelParams(modelPath)
Expand Down
9 changes: 5 additions & 4 deletions LLama.Examples/Examples/LoadAndSaveState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public class LoadAndSaveState
{
public static async Task Run()
{
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
string modelPath = UserSettings.GetModelPath();

var prompt = (await File.ReadAllTextAsync("Assets/chat-with-bob.txt")).Trim();

var parameters = new ModelParams(modelPath)
Expand All @@ -21,9 +21,10 @@ public static async Task Run()
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)");
Console.ForegroundColor = ConsoleColor.White;
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:" } };
Expand Down
3 changes: 1 addition & 2 deletions LLama.Examples/Examples/QuantizeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ public class QuantizeModel
{
public static void Run()
{
Console.Write("Please input your original model path: ");
var inputPath = Console.ReadLine();
string inputPath = UserSettings.GetModelPath();

Console.Write("Please input your output model path: ");
var outputPath = Console.ReadLine();
Expand Down
14 changes: 9 additions & 5 deletions LLama.Examples/Examples/SemanticKernelChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ public class SemanticKernelChat
{
public static async Task Run()
{
Console.WriteLine("Example from: https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/KernelSyntaxExamples/Example17_ChatGPT.cs");
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
string modelPath = UserSettings.GetModelPath();

Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("This example is from: \n" +
"https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/KernelSyntaxExamples/Example17_ChatGPT.cs");

// Load weights into memory
var parameters = new ModelParams(modelPath);
Expand All @@ -19,7 +21,8 @@ public static async Task Run()

var chatGPT = new LLamaSharpChatCompletion(ex);

var chatHistory = chatGPT.CreateNewChat("This is a conversation between the assistant and the user. \n\n You are a librarian, expert about books. ");
var chatHistory = chatGPT.CreateNewChat("This is a conversation between the " +
"assistant and the user. \n\n You are a librarian, expert about books. ");

Console.WriteLine("Chat content:");
Console.WriteLine("------------------------");
Expand All @@ -33,7 +36,8 @@ public static async Task Run()
await MessageOutputAsync(chatHistory);

// Second user message
chatHistory.AddUserMessage("I love history and philosophy, I'd like to learn something new about Greece, any suggestion");
chatHistory.AddUserMessage("I love history and philosophy, I'd like to learn " +
"something new about Greece, any suggestion");
await MessageOutputAsync(chatHistory);

// Second bot assistant message
Expand Down
11 changes: 4 additions & 7 deletions LLama.Examples/Examples/SemanticKernelMemory.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using LLama.Common;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Memory;
using LLamaSharp.SemanticKernel.TextEmbedding;
using Microsoft.SemanticKernel.AI.Embeddings;
using Microsoft.SemanticKernel.Plugins.Memory;

namespace LLama.Examples.Examples
{
Expand All @@ -13,10 +10,10 @@ public class SemanticKernelMemory

public static async Task Run()
{
var loggerFactory = ConsoleLogger.LoggerFactory;
Console.WriteLine("Example from: https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/KernelSyntaxExamples/Example14_SemanticMemory.cs");
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
string modelPath = UserSettings.GetModelPath();

Console.WriteLine("This example is from: \n" +
"https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/KernelSyntaxExamples/Example14_SemanticMemory.cs");

var seed = 1337u;
// Load weights into memory
Expand Down
Loading