/LLamaSharp

C#/.NET binding of llama.cpp, including LLaMa/GPT model inference and quantization, ASP.NET core integration and UI.

Primary LanguageC#MIT LicenseMIT

LLamaSharp - .NET Binding for llama.cpp

logo

Discord QQ Group LLamaSharp Badge LLamaSharp Badge LLamaSharp Badge LLamaSharp Badge

The C#/.NET binding of llama.cpp. It provides APIs to inference the LLaMa Models and deploy it on local environment. It works on both Windows, Linux and MAC without requirment for compiling llama.cpp yourself. Its performance is close to llama.cpp.

Furthermore, it provides integrations with other projects such as BotSharp to provide higher-level applications and UI.

Documentation

Installation

Firstly, search LLamaSharp in nuget package manager and install it.

PM> Install-Package LLamaSharp

Then, search and install one of the following backends:

LLamaSharp.Backend.Cpu
LLamaSharp.Backend.Cuda11
LLamaSharp.Backend.Cuda12

Here's the mapping of them and corresponding model samples provided by LLamaSharp. If you're not sure which model is available for a version, please try our sample model.

LLamaSharp.Backend LLamaSharp Verified Model Resources llama.cpp commit id
- v0.2.0 This version is not recommended to use. -
- v0.2.1 WizardLM, Vicuna (filenames with "old") -
v0.2.2 v0.2.2, v0.2.3 WizardLM, Vicuna (filenames without "old") 63d2046
v0.3.0, v0.3.1 v0.3.0, v0.4.0 LLamaSharpSamples v0.3.0, WizardLM 7e4ea5b
v0.4.1-preview (cpu only) v0.4.1-preview Open llama 3b, Open Buddy aacdbd4
v0.4.2-preview (cpu,cuda11) v0.4.2-preview Llama2 7b 332311234a0aa2974b2450710e22e09d90dd6b0b

Many hands make light work. If you have found any other model resource that could work for a version, we'll appreciate it for opening an PR about it! 😊

We publish the backend with cpu, cuda11 and cuda12 because they are the most popular ones. If none of them matches, please compile the llama.cpp from source and put the libllama under your project's output path (guide).

FAQ

  1. GPU out of memory: Please try setting n_gpu_layers to a smaller number.
  2. Unsupported model: llama.cpp is under quick development and often has break changes. Please check the release date of the model and find a suitable version of LLamaSharp to install, or use the model we provide on huggingface.

Usages

Model Inference and Chat Session

LLamaSharp provides two ways to run inference: LLamaExecutor and ChatSession. The chat session is a higher-level wrapping of the executor and the model. Here's a simple example to use chat session.

using LLama.Common;
using LLama;

string modelPath = "<Your model path>"; // change it to your own model path
var prompt = "Transcript of a dialog, where the User interacts with an Assistant named Bob. Bob is helpful, kind, honest, good at writing, and never fails to answer the User's requests immediately and with precision.\r\n\r\nUser: Hello, Bob.\r\nBob: Hello. How may I help you today?\r\nUser: Please tell me the largest city in Europe.\r\nBob: Sure. The largest city in Europe is Moscow, the capital of Russia.\r\nUser:"; // use the "chat-with-bob" prompt here.

// Initialize a chat session
var ex = new InteractiveExecutor(new LLamaModel(new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5)));
ChatSession session = new ChatSession(ex);

// show the prompt
Console.WriteLine();
Console.Write(prompt);

// run the inference in a loop to chat with LLM
while (prompt != "stop")
{
    foreach (var text in session.Chat(prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List<string> { "User:" } }))
    {
        Console.Write(text);
    }
    prompt = Console.ReadLine();
}

// save the session
session.SaveSession("SavedSessionPath");

Quantization

The following example shows how to quantize the model. With LLamaSharp you needn't to compile c++ project and run scripts to quantize the model, instead, just run it in C#.

string srcFilename = "<Your source path>";
string dstFilename = "<Your destination path>";
string ftype = "q4_0";
if(Quantizer.Quantize(srcFileName, dstFilename, ftype))
{
    Console.WriteLine("Quantization succeed!");
}
else
{
    Console.WriteLine("Quantization failed!");
}

For more usages, please refer to Examples.

Web API

We provide the integration of ASP.NET core here. Since currently the API is not stable, please clone the repo and use it. In the future we'll publish it on NuGet.

Since we are in short of hands, if you're familiar with ASP.NET core, we'll appreciate it if you would like to help upgrading the Web API integration.

Demo

demo-console

Roadmap


✅: completed. ⚠️: outdated for latest release but will be updated. 🔳: not completed


✅ LLaMa model inference

✅ Embeddings generation, tokenization and detokenization

✅ Chat session

✅ Quantization

✅ State saving and loading

⚠️ BotSharp Integration

✅ ASP.NET core Integration

⚠️ Semantic-kernel Integration

🔳 Fine-tune

🔳 Local document search

🔳 MAUI Integration

🔳 Follow up llama.cpp and improve performance

Assets

Some extra model resources could be found below:

The weights included in the magnet is exactly the weights from Facebook LLaMa.

The prompts could be found below:

Contributing

Any contribution is welcomed! Please read the contributing guide. You can do one of the followings to help us make LLamaSharp better:

  • Append a model link that is available for a version. (This is very important!)
  • Star and share LLamaSharp to let others know it.
  • Add a feature or fix a BUG.
  • Help to develop Web API and UI integration.
  • Just start an issue about the problem you met!

Contact us

Join our chat on Discord.

Join QQ group

License

This project is licensed under the terms of the MIT license.