LLamaSharp 0.10.0
See the version list below for details.
dotnet add package LLamaSharp --version 0.10.0
NuGet\Install-Package LLamaSharp -Version 0.10.0
<PackageReference Include="LLamaSharp" Version="0.10.0" />
paket add LLamaSharp --version 0.10.0
#r "nuget: LLamaSharp, 0.10.0"
// Install LLamaSharp as a Cake Addin #addin nuget:?package=LLamaSharp&version=0.10.0 // Install LLamaSharp as a Cake Tool #tool nuget:?package=LLamaSharp&version=0.10.0
The C#/.NET binding of llama.cpp. It provides higher-level APIs to inference the LLaMA Models and deploy it on local device with C#/.NET. It works on Windows, Linux and Mac without need to compile llama.cpp yourself. Even without a GPU or not enough GPU memory, you can still use LLaMA models! 🤗
Furthermore, it provides integrations with other projects such as semantic-kernel, kernel-memory and BotSharp to provide higher-level applications.
Discussions about the roadmap to v1.0.0: #287
<details> <summary>Table of Contents</summary> <ul> <li><a href="#Documentation">Documentation</a></li> <li><a href="#Examples">Examples</a></li> <li><a href="#Installation">Installation</a></li> <li> <a href="#(Quick Start)">Quick Start</a> <ul> <li><a href="#Model Inference and Chat Session">Model Inference and Chat Session</a></li> <li><a href="#Quantization">Quantization</a></li> <li><a href="#Web API">Web API</a></li> </ul> </li> <li><a href="#Features">Features</a></li> <li><a href="#Console Demo">Console Demo</a></li> <li><a href="#FAQ">FAQ</a></li> <li><a href="#Contributing">Contributing</a></li> <li><a href="#Contact us">Contact us</a></li> <li> <a href="#Appendix">Appendix</a> <ul> <li><a href="#LLamaSharp and llama.cpp versions">LLamaSharp and llama.cpp versions</a></li> </ul> </li> </ul> </details>
Documentation
Examples
Installation
- Install
LLamaSharp
package in NuGet:
PM> Install-Package LLamaSharp
Install one of these backends:
LLamaSharp.Backend.Cpu
: Pure CPU for Windows & Linux. Metal for Mac.LLamaSharp.Backend.Cuda11
: CUDA11 for Windows and LinuxLLamaSharp.Backend.Cuda12
@ CUDA 12 for Windows and Linux- If none of these backends is suitable you can compile llama.cpp yourself. In this case, please DO NOT install the backend packages! Instead, add your DLL to your project and ensure it will be copied to the output directory when compiling your project. If you do this you must use exactly the correct llama.cpp commit, refer to the version table further down.
(optional) For Microsoft semantic-kernel integration, install the LLamaSharp.semantic-kernel package.
(optional) For Microsoft kernel-memory integration, install the LLamaSharp.kernel-memory package (this package currently only supports
net6.0
).
Tips for choosing a version
Llama.cpp is a fast moving project with frequent breaking changes, therefore breaking changes are expected frequently in LLamaSharp. LLamaSharp follows semantic versioning and will not introduce breaking API changes on patch versions.
It is suggested to update to the latest patch version as soon as it is released, and to update to new major versions as soon as possible.
Quick Start
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.
// Load a model
var parameters = new ModelParams(modelPath)
{
ContextSize = 1024,
Seed = 1337,
GpuLayerCount = 5
};
using var model = LLamaWeights.LoadFromFile(parameters);
// Initialize a chat session
using var context = model.CreateContext(parameters);
var ex = new InteractiveExecutor(context);
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")
{
await foreach (var text in session.ChatAsync(new ChatHistory.Message(AuthorRole.User, prompt), new InferenceParams { Temperature = 0.6f, AntiPrompts = [ "User:" ] }))
{
Console.Write(text);
}
prompt = Console.ReadLine() ?? "";
}
// save the session
session.SaveSession("SavedSessionPath");
Quantization
The following example shows how to quantize the model:
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 usage, please refer to Examples.
Web API
We provide an integration with ASP.NET core and a web app demo. 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.
Features
✅: completed. ⚠️: outdated for latest release but will be updated. 🔳: not completed
✅ LLaMa model inference<br /> ✅ Embeddings generation, tokenization and detokenization<br /> ✅ Chat session<br /> ✅ Quantization<br /> ✅ Grammar<br /> ✅ State saving and loading<br /> ✅ BotSharp Integration Online Demo<br /> ✅ ASP.NET core Integration<br /> ✅ Semantic-kernel Integration<br /> 🔳 Fine-tune<br /> ✅ Local document search (enabled by kernel-memory)<br /> 🔳 MAUI Integration<br />
Console Demo
FAQ
- GPU out of memory: Please try setting
n_gpu_layers
to a smaller number. - Unsupported model:
llama.cpp
is under quick development and often has breaking changes. Please check the release date of the model and find a suitable version of LLamaSharp to install, or generategguf
format weights from original weights yourself. - Cannot load native library:
- Ensure you have installed one of the backend packages.
- Run
NativeLibraryConfig.WithLogs()
at the very beginning of your code to print more information.
- Models in GGUF format are compatible with LLamaSharp. It's a good idea to search for
gguf
on huggingface to find a model. Another choice is generate a GGUF format file yourself, please refer to convert.py for more information.
Contributing
Any contribution is welcomed! There's a TODO list in LLamaSharp Dev Project and you could pick an interesting one to start. Please read the contributing guide for more information.
You can also do one of the followings to help us make LLamaSharp better:
- Submit a feature request.
- Star and share LLamaSharp to let others know it.
- Write a blog or demo about LLamaSharp.
- Help to develop Web API and UI integration.
- Just open an issue about the problem you met!
Contact us
Join our chat on Discord (please contact Rinne to join the dev channel if you want to be a contributor).
Join QQ group
Appendix
LLamaSharp and llama.cpp versions
If you want to compile llama.cpp yourself you must use the exact commit ID listed for each version.
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.3 | WizardLM, Vicuna (filenames without "old") | 63d2046 |
v0.3.0, v0.4.0 | LLamaSharpSamples v0.3.0, WizardLM | 7e4ea5b |
v0.4.1-preview | Open llama 3b, Open Buddy | aacdbd4 |
v0.4.2-preview | Llama2 7B (GGML) | 3323112 |
v0.5.1 | Llama2 7B (GGUF) | 6b73ef1 |
v0.6.0 | cb33f43 |
|
v0.7.0, v0.8.0 | Thespis-13B, LLaMA2-7B | 207b519 |
v0.8.1 | e937066 |
|
v0.9.0, v0.9.1 | Mixtral-8x7B | 9fb13f9 |
v0.10.0 | Phi2 | d71ac90 |
License
This project is licensed under the terms of the MIT license.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- System.Linq.Async (>= 6.0.1)
- System.Text.Json (>= 8.0.1)
-
net6.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
-
net7.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
-
net8.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
NuGet packages (10)
Showing the top 5 NuGet packages that depend on LLamaSharp:
Package | Downloads |
---|---|
Microsoft.KernelMemory.AI.LlamaSharp
Provide access to OpenAI LLM models in Kernel Memory to generate text |
|
LLamaSharp.semantic-kernel
The integration of LLamaSharp and Microsoft semantic-kernel. |
|
LLamaSharp.kernel-memory
The integration of LLamaSharp and Microsoft kernel-memory. It could make it easy to support document search for LLamaSharp model inference. |
|
LangChain.Providers.LLamaSharp
LLamaSharp Chat model provider. |
|
LangChain.Providers.Automatic1111
Automatic1111 Stable DIffusion model provider. |
GitHub repositories (4)
Showing the top 4 popular GitHub repositories that depend on LLamaSharp:
Repository | Stars |
---|---|
SciSharp/BotSharp
AI Multi-Agent Framework in .NET
|
|
microsoft/kernel-memory
RAG architecture: index and query any data using LLM and natural language, track sources, show citations, asynchronous memory patterns.
|
|
CodeMazeBlog/CodeMazeGuides
The main repository for all the Code Maze guides
|
|
jxq1997216/AITranslator
使用大语言模型来翻译MTool导出的待翻译文件的图像化UI软件
|
Version | Downloads | Last updated |
---|---|---|
0.19.0 | 1,310 | 11/8/2024 |
0.18.0 | 7,617 | 10/19/2024 |
0.17.0 | 838 | 10/13/2024 |
0.16.0 | 45,917 | 9/1/2024 |
0.15.0 | 18,168 | 8/3/2024 |
0.14.0 | 3,132 | 7/16/2024 |
0.13.0 | 51,694 | 6/4/2024 |
0.12.0 | 82,462 | 5/12/2024 |
0.11.2 | 15,000 | 4/6/2024 |
0.11.1 | 843 | 3/31/2024 |
0.10.0 | 6,298 | 2/15/2024 |
0.9.1 | 10,052 | 1/6/2024 |
0.9.0 | 547 | 1/6/2024 |
0.8.1 | 22,222 | 11/28/2023 |
0.8.0 | 21,477 | 11/12/2023 |
0.7.0 | 1,849 | 10/31/2023 |
0.6.0 | 2,369 | 10/24/2023 |
0.5.1 | 6,971 | 9/5/2023 |
0.4.2-preview | 1,945 | 8/6/2023 |
0.4.1-preview | 1,251 | 6/21/2023 |
0.4.0 | 11,418 | 6/19/2023 |
0.3.0 | 10,703 | 5/22/2023 |
0.2.3 | 714 | 5/17/2023 |
0.2.2 | 639 | 5/17/2023 |
0.2.1 | 677 | 5/12/2023 |
0.2.0 | 866 | 5/12/2023 |
LLamaSharp 0.10.0 supports automatically device feature detection, adds integration with kernel-memory and fixes some performance issues.