除主包外必须另装匹配设备的后端包,否则无法加载原生库;模型需为 GGUF 格式,需自行从 Huggingface 下载或转换;CUDA 后端版本要与系统 CUDA 版本匹配,且 GpuLayerCount 需大于 0 才会用 GPU;旧版 GGUF 模型可能与新版 LLamaSharp 不兼容,注意发布时间。

LLamaSharp
SciSharpMIT活跃维护在 .NET 项目里本地跑 LLaMA、LLaVA 等大模型的 C# 库,封装 llama.cpp,让 C# 开发者不用切 Python 就能做聊天和多模态推理。
A C#/.NET library to run LLM (🦙LLaMA/LLaVA) on your local device efficiently.
dotnet add package LLamaSharp
dotnet add package LLamaSharp.Backend.CpuCPU 可跑(LLamaSharp.Backend.Cpu 支持 Windows/Linux/Mac,Mac 上含 Metal GPU 支持);GPU 支持 CUDA 12/13(Windows/Linux)与 Vulkan(Windows/Linux)。README 未给出具体显存数值,只提示按显存调整 GpuLayerCount。
基于 llama.cpp 的 C#/.NET 库,让你在本地设备上高效跑 LLaMA 等大模型
在 .NET 项目里本地跑 LLaMA、LLaVA 等大模型的 C# 库,封装 llama.cpp,让 C# 开发者不用切 Python 就能做聊天和多模态推理。
核心能力
- 封装 llama.cpp 原生库,CPU 与 GPU 推理都高效
- 提供高层 API 与 RAG 支持,便于在应用中部署 LLM
- 支持流式输出与 ChatSession 管理多轮对话历史
- 通过 GpuLayerCount 控制卸载到 GPU 的层数
- 提供 CPU、CUDA12/13、Vulkan 等预编译后端包
容易踩的坑
- v0.30.0最新正式版2026-10-17 · 2026-10-17 00:15 UTC
Major Changes
- feat(cuda): add CUDA 13.0.0 jobs to the compile workflow by @modelpath-dev in https://github.com/SciSharp/LLamaSharp/pull/1439
- feat(cuda): probe cuda13 native libraries before cuda12 by @modelpath-dev in https://github.com/SciSharp/LLamaSharp/pull/1440
- feat(cuda): add LLamaSharp.Backend.Cuda13 packages by @modelpath-dev in https://github.com/SciSharp/LLamaSharp/pull/1441
- Add Microsoft Agent Framework (Microsoft.Agents.AI) with RAG Example by @zsogitbe in https://github.com/SciSharp/LLamaSharp/pull/1423
- Expose llama_model_params.devices as IModelParams.Devices by @ipevo-brooks in https://github.com/SciSharp/LLamaSharp/pull/1447
Bug Fixes
- Detect Vulkan via the loader before falling back to vulkaninfo by @ipevo-brooks in https://github.com/SciSharp/LLamaSharp/pull/1446
- Remove SemanticKernel and KernelMemory leftovers by @m0nsky in https://github.com/SciSharp/LLamaSharp/pull/1426
- Disable SSE4.2 in noavx native builds by @modelpath-dev in https://github.com/SciSharp/LLamaSharp/pull/1438
- Fix
RawRepresentationFactorysettings being overwritten inAsChatClientby @Laurianti in https://github.com/SciSharp/LLamaSharp/pull/1442 - Fixed CI by @martindevans in https://github.com/SciSharp/LLamaSharp/pull/1458
Other Changes
- Read the release version from LLamaSharp.csproj by @m0nsky in https://github.com/SciSharp/LLamaSharp/pull/1427
- Merge the minor and patch release workflows into one by @m0nsky in https://github.com/SciSharp/LLamaSharp/pull/1430
- Push the API references with GITHUB_TOKEN, falling back to a pull request by @Laurianti in https://github.com/SciSharp/LLamaSharp/pull/1453
- Fix the API references workflow for XMLDoc2Markdown 6 by @Laurianti in https://github.com/SciSharp/LLamaSharp/pull/1443
- Build the API references from net10.0 by @Laurianti in https://github.com/SciSharp/LLamaSharp/pull/1462
- Fixed release-trigger.yml by @martindevans in https://github.com/SciSharp/LLamaSharp/pull/1464
- Big package update by @martindevans in https://github.com/SciSharp/LLamaSharp/pull/1457
- Cleanup Unused Things by @martindevans in https://github.com/SciSharp/LLamaSharp/pull/1459
New Contributors
- @modelpath-dev made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1438
- @Laurianti made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1442
- @ipevo-brooks made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1446
Full Changelog: https://github.com/SciSharp/LLamaSharp/compare/v0.29.0...v0.30.0
- v0.29.02026-08-24 · 2026-08-24 13:34 UTC
查看更新说明
Major Changes
- Updated to llama.cpp
815a2a5915f22ce6a760c676389c5dfe8535c08f(b10221) by @martindevans in https://github.com/SciSharp/LLamaSharp/pull/1414
Bug Fixes
- Implement Sequence ID pooling in BatchedExecutor to prevent native SeqMax overflow and crashes by @zsogitbe in https://github.com/SciSharp/LLamaSharp/pull/1386
- Disable GGML_F16C GGML_BMI2 for noavx buid (Fixes #1407) by @butameron in https://github.com/SciSharp/LLamaSharp/pull/1408
- Allow arbitrary ChatSession message interleaving by @YoMosa2009 in https://github.com/SciSharp/LLamaSharp/pull/1417
- Fix null DryRun output by @martindevans in https://github.com/SciSharp/LLamaSharp/pull/1419
Other Changes
- Remove
LLama.Experimentalby @martindevans in https://github.com/SciSharp/LLamaSharp/pull/1421 - Remove SemanticKernel and KernelMemory by @martindevans in https://github.com/SciSharp/LLamaSharp/pull/1420
New Contributors
- @butameron made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1408
- @YoMosa2009 made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1417
Full Changelog: https://github.com/SciSharp/LLamaSharp/compare/v0.27.0...v0.29.0
- Updated to llama.cpp
- v0.27.02026-04-26 · 2026-04-26 17:00 UTC
查看更新说明
Major Changes
- Windows arm64 cpu support by @jihadkhawaja in https://github.com/SciSharp/LLamaSharp/pull/1226
- Update For Qwen3.5/Gemma4 Support by @martindevans in https://github.com/SciSharp/LLamaSharp/pull/1356
- Context Overflow Fix by @zsogitbe in https://github.com/SciSharp/LLamaSharp/pull/1373
Bug Fixes
- Fix flaky unit tests by @m0nsky in https://github.com/SciSharp/LLamaSharp/pull/1350
- Fix failing workflow caused by new python version by @m0nsky in https://github.com/SciSharp/LLamaSharp/pull/1329
- Fix/remove standalone metal shader by @m0nsky in https://github.com/SciSharp/LLamaSharp/pull/1331
- Replace -DLLAMA_CURL=OFF with -DLLAMA_OPENSSL=OFF by @m0nsky in https://github.com/SciSharp/LLamaSharp/pull/1333
- Linux RUNPATH by @m0nsky in https://github.com/SciSharp/LLamaSharp/pull/1330
- Fix compile.yml regressions + switch windows arm64 build to clang by @m0nsky in https://github.com/SciSharp/LLamaSharp/pull/1334
- Removed outdated reference to llava_shared.dll for Windows arm64 target by @martindevans in https://github.com/SciSharp/LLamaSharp/pull/1339
- Disable shared object versioning by @m0nsky in https://github.com/SciSharp/LLamaSharp/pull/1346
- Fix musl detection using RID instead of distro name by @m0nsky in https://github.com/SciSharp/LLamaSharp/pull/1347
- bugfix: bugfix: LLamaWeights.NativeHandle.LoadLoraFromFile need read & write access to lora file by @harry330 in https://github.com/SciSharp/LLamaSharp/pull/1368
Other Changes
- Expand CI matrix, new platforms + renames by @m0nsky in https://github.com/SciSharp/LLamaSharp/pull/1348
- Remove setting hardcoded value for n_seq_max in IContextParamsExtensions by @Ed-Pavlov in https://github.com/SciSharp/LLamaSharp/pull/1354
- docs: add FAQ entry for "unknown model architecture" error by @Klivess in https://github.com/SciSharp/LLamaSharp/pull/1371
New Contributors
- @jihadkhawaja made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1226
- @Ed-Pavlov made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1354
- @harry330 made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1368
- @Klivess made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1371
Full Changelog: https://github.com/SciSharp/LLamaSharp/compare/v0.26.0...v0.27.0
- v0.26.02026-02-15 · 2026-02-15 16:25 UTC
查看更新说明
Major Changes
- Add support for gemma 3n by @krisbiradar in https://github.com/SciSharp/LLamaSharp/pull/1248
- Mtmd implementation by @SignalRT in https://github.com/SciSharp/LLamaSharp/pull/1261
Bug Fixes
- Respect ChatOptions.Instructions in LlamaExecutorChatClient by @stephentoub in https://github.com/SciSharp/LLamaSharp/pull/1256
- set RPATH to "@loader_path" / "$ORIGIN" to ensure executables and dynamic libraries search for dependencies in their origin directory by @SignalRT in https://github.com/SciSharp/LLamaSharp/pull/1279
- Fixes Extended grammar optimization mode would crash if TopK sampling is set to zero. by @SerialKicked in https://github.com/SciSharp/LLamaSharp/pull/1282
- Fix issue #382 (multiple publish output files with same relative path) by @LucaMaccarini in https://github.com/SciSharp/LLamaSharp/pull/1234
Other Changes
- Optimization - A queue with fixed storage size backed by a circular buffer by @SignalRT in https://github.com/SciSharp/LLamaSharp/pull/1262
- Fix some warnings. by @Lamothe in https://github.com/SciSharp/LLamaSharp/pull/1274
- Accept
CancellationTokeninChatSession.InitializeSessionFromHistoryAsyncby @Cryptoc1 in https://github.com/SciSharp/LLamaSharp/pull/1260 - Downgrade compile.yml from macos-latest to macos-14 by @m0nsky in https://github.com/SciSharp/LLamaSharp/pull/1302
- Update Vulkan from 1.3.261.1 to 1.4.335.0 and fix download url by @m0nsky in https://github.com/SciSharp/LLamaSharp/pull/1303
- Update BinaryReleaseId by @m0nsky in https://github.com/SciSharp/LLamaSharp/pull/1304
- Add KaiROS AI as example application by @avikeid2007 in https://github.com/SciSharp/LLamaSharp/pull/1313
New Contributors
- @Cryptoc1 made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1260
- @krisbiradar made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1248
- @LucaMaccarini made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1234
- @avikeid2007 made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1313
Full Changelog: https://github.com/SciSharp/LLamaSharp/compare/v0.25.0...v0.26.0
- v0.25.02025-08-16 · 2025-08-16 13:59 UTC
查看更新说明
Major Changes
- Bianries Update July 2025 by @martindevans in https://github.com/SciSharp/LLamaSharp/pull/1225
- docs: update the API references. by @AsakusaRinne in https://github.com/SciSharp/LLamaSharp/pull/1213, https://github.com/SciSharp/LLamaSharp/pull/1214
Bug Fixes
- Conversation
-1instead of_endby @martindevans in https://github.com/SciSharp/LLamaSharp/pull/1197 - Preserve cut off generated text by @OoLunar in https://github.com/SciSharp/LLamaSharp/pull/1202
- Fix for getting CUDA Version inside nvidia/cuda containers by @Crelex in https://github.com/SciSharp/LLamaSharp/pull/1222
Other Changes
- Update to stable Microsoft.Extensions.AI.Abstractions by @stephentoub in https://github.com/SciSharp/LLamaSharp/pull/1190
- Memory efficient context handling by @zsogitbe in https://github.com/SciSharp/LLamaSharp/pull/1183
- Expose
DecodeSpecialTokensthroughIInferenceParamsforStatelessExecutorby @jacob-mink-1996 in https://github.com/SciSharp/LLamaSharp/pull/1203 - Unseal the DefaultSamplingPipeline by @m0nsky in https://github.com/SciSharp/LLamaSharp/pull/1208
- Updated main workflow from windows-2019 to windows-2022 by @martindevans in https://github.com/SciSharp/LLamaSharp/pull/1216, https://github.com/SciSharp/LLamaSharp/pull/1218
- Multi modal (Just libraries) by @SignalRT in https://github.com/SciSharp/LLamaSharp/pull/1220
New Contributors
- @omahs made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1196
- @OoLunar made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1202
- @leopardracer made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1206
- @jacob-mink-1996 made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1203
- @kilavvy made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1207
- @vtjl10 made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1209
- @zeevick10 made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1210
- @maximevtush made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1211
- @Crelex made their first contribution in https://github.com/SciSharp/LLamaSharp/pull/1222
Full Changelog: https://github.com/SciSharp/LLamaSharp/compare/v0.24.0...v0.25.0
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。

LLamaSharp is a cross-platform library to run 🦙LLaMA model (and others) on your local device. Based on llama.cpp, inference with LLamaSharp is efficient on both CPU and GPU. With the higher-level APIs and RAG support, it's convenient to deploy LLMs (Large Language Models) in your application with LLamaSharp.
Please star the repo to show your support for this project!🤗
📖Documentation
📌Console Demo
🔗Integrations & Examples
There are integrations for the following libraries, making it easier to develop your APP. These integrations are developed in their own repositories.
- BotSharp: an open source machine learning framework for AI Bot platform builder.
- Langchain: a framework for developing applications powered by language models.
- MaIN.NET: simplistic approach to orchestrating agents/chats from different (llm) providers
The following examples show how to build APPs with LLamaSharp.
- Official Console Examples
- Unity Demo
- LLamaStack (with WPF and Web demo)
- Blazor Demo (with Model Explorer)
- ASP.NET Demo
- LLamaWorker (ASP.NET Web API like OAI and Function Calling Support)
- VirtualPet (Desktop Application)
- KaiROS AI (Local AI Assistant for Windows & Android)

🚀Get started
Installation
To gain high performance, LLamaSharp interacts with native libraries compiled from c++, these are called backends. We provide backend packages for Windows, Linux and Mac with CPU, CUDA, Metal and Vulkan. You don't need to compile any c++, just install the backend packages.
If no published backend matches your device, please open an issue to let us know. If compiling c++ code is not difficult for you, you could also follow this guide to compile a backend and run LLamaSharp with it.
- Install LLamaSharp package on NuGet:
PM> Install-Package LLamaSharp
-
Install one or more of these backends, or use a self-compiled backend.
LLamaSharp.Backend.Cpu: Pure CPU for Windows, Linux & Mac. Metal (GPU) support for Mac.LLamaSharp.Backend.Cuda12: CUDA 12 for Windows & Linux.LLamaSharp.Backend.Cuda13: CUDA 13 for Windows & Linux.LLamaSharp.Backend.Vulkan: Vulkan for Windows & Linux.
Model preparation
There are two popular formats of model file of LLMs, these are PyTorch format (.pth) and Huggingface format (.bin). LLamaSharp uses a GGUF format file, which can be converted from these two formats. To get a GGUF file, there are two options:
-
Search model name + 'gguf' in Huggingface, you will find lots of model files that have already been converted to GGUF format. Please take note of the publishing time of them because some old ones may only work with older versions of LLamaSharp.
-
Convert PyTorch or Huggingface format to GGUF format yourself. Please follow the instructions from this part of llama.cpp readme to convert them with python scripts.
Generally, we recommend downloading models with quantization rather than fp16, because it significantly reduces the required memory size while only slightly impacting the generation quality.
Example of LLaMA chat session
Here is a simple example to chat with a bot based on a LLM in LLamaSharp. Please replace the model path with yours.
using LLama;
using LLama.Common;
using LLama.Sampling;
string modelPath = @"<Your Model Path>"; // change it to your own model path.
var parameters = new ModelParams(modelPath)
{
ContextSize = 1024, // The longest length of chat as memory.
GpuLayerCount = 5 // How many layers to offload to GPU. Please adjust it according to your GPU memory.
};
using var model = LLamaWeights.LoadFromFile(parameters);
using var context = model.CreateContext(parameters);
var executor = new InteractiveExecutor(context);
// Add chat histories as prompt to tell AI how to act.
var chatHistory = new ChatHistory();
chatHistory.AddMessage(AuthorRole.System, "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.");
chatHistory.AddMessage(AuthorRole.User, "Hello, Bob.");
chatHistory.AddMessage(AuthorRole.Assistant, "Hello. How may I help you today?");
ChatSession session = new(executor, chatHistory);
InferenceParams inferenceParams = new InferenceParams()
{
MaxTokens = 256, // No more than 256 tokens should appear in answer. Remove it if antiprompt is enough for control.
AntiPrompts = new List<string> { "User:" }, // Stop generation once antiprompts appear.
SamplingPipeline = new DefaultSamplingPipeline(),
};
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("The chat session has started.\nUser: ");
Console.ForegroundColor = ConsoleColor.Green;
string userInput = Console.ReadLine() ?? "";
while (userInput != "exit")
{
await foreach ( // Generate the response streamingly.
var text
in session.ChatAsync(
new ChatHistory.Message(AuthorRole.User, userInput),
inferenceParams))
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write(text);
}
Console.ForegroundColor = ConsoleColor.Green;
userInput = Console.ReadLine() ?? "";
}
For more examples, please refer to LLamaSharp.Examples.
💡FAQ
Why is my GPU not used when I have installed CUDA?
- If you are using backend packages, please make sure you have installed the CUDA backend package which matches the CUDA version installed on your system.
- Add the following line to the very beginning of your code. The log will show which native library file is loaded. If the CPU library is loaded, please try to compile the native library yourself and open an issue for that. If the CUDA library is loaded, please check if
GpuLayerCount > 0when loading the model weight.
NativeLibraryConfig.All.WithLogCallback(delegate (LLamaLogLevel level, string message) { Console.Write($"{level}: {message}"); } )
Why is the inference so slow?
Firstly, due to the large size of LLM models, it requires more time to generate output than other models, especially when you are using models larger than 30B parameters.
To see if that's a LLamaSharp performance issue, please follow the two tips below.
- If you are using CUDA, Metal or Vulkan, please set
GpuLayerCountas large as possible. - If it's still slower than you expect it to be, please try to run the same model with same setting in llama.cpp examples. If llama.cpp outperforms LLamaSharp significantly, it's likely a LLamaSharp BUG and please report that to us.
Why does the program crash before any output is generated?
Generally, there are two possible cases for this problem:
- The native library (backend) you are using is not compatible with the LLamaSharp version. If you compiled the native library yourself, please make sure you have checked-out llama.cpp to the corresponding commit of LLamaSharp, which can be found at the bottom of README.
- The model file you are using is not compatible with the backend. If you are using a GGUF file downloaded from huggingface, please check its publishing time.
Why is my model generating output infinitely?
Please set anti-prompt or max-length when executing the inference.
🙌Contributing
All contributions are welcome! There's a TODO list in LLamaSharp Dev Project and you can pick an interesting one to start. Please read the contributing guide for more information.
You can also do one of the following to help us make LLamaSharp better:
- Submit a feature request.
- Star and share LLamaSharp to let others know about it.
- Write a blog or demo about LLamaSharp.
- Help to develop Web API and UI integration.
- Just open an issue about the problem you've found!
Join the community
Join our chat on Discord (please contact Rinne to join the dev channel if you want to be a contributor).
Join QQ group
Star history
Contributor wall of fame
Map of LLamaSharp and llama.cpp versions
If you want to compile llama.cpp yourself you must use the exact commit ID listed for each version.
License
This project is licensed under the terms of the MIT license.