LlamaPure 1.0.3
dotnet add package LlamaPure --version 1.0.3
NuGet\Install-Package LlamaPure -Version 1.0.3
<PackageReference Include="LlamaPure" Version="1.0.3" />
<PackageVersion Include="LlamaPure" Version="1.0.3" />
<PackageReference Include="LlamaPure" />
paket add LlamaPure --version 1.0.3
#r "nuget: LlamaPure, 1.0.3"
#:package LlamaPure@1.0.3
#addin nuget:?package=LlamaPure&version=1.0.3
#tool nuget:?package=LlamaPure&version=1.0.3
LlamaPure
A dependency-free .NET Standard 2.0 P/Invoke wrapper for llama.cpp (CPU inference, Completion, and Embeddings).
Design Philosophy
LlamaPure is designed for environments where the addition of an extensive package dependency graph is restricted or undesirable — such as large legacy enterprise systems, strict compliance-driven build pipelines, or projects where tight control over every binary in the deployment package is required.
The library achieves this through a single architectural choice: a direct, thin P/Invoke layer over the official llama.dll. It introduces no third-party NuGet dependencies and references nothing beyond System and System.Runtime.InteropServices. The public API is intentionally narrow, covering only the two inference operations most commonly needed in line-of-business applications:
- Text Completion — autoregressive token generation from a prompt
- Text Embedding — dense float-vector extraction from text
Everything else is left to the caller, keeping the integration footprint as small as possible.
Requirements
| Component | Requirement |
|---|---|
| Runtime | .NET Framework 4.6.1 / 4.7.2 / 4.8, or .NET 6 / 7 / 8 / 9 / 10 |
| Platform | Windows x64 |
| Backend | CPU (llama.cpp CPU build) |
Installation
Add the NuGet package
dotnet add package LlamaPure
Or via the Package Manager Console:
Install-Package LlamaPure
Quick Start
Text Completion
using LlamaPure;
using (var client = new LlamaPureClient(@"C:\models\llama-3.2-1b.Q4_K_M.gguf"))
{
string result = client.Complete("The capital of France is", maxNewTokens: 32);
Console.WriteLine(result);
}
Text Embedding
using LlamaPure;
using (var client = new LlamaPureClient(@"C:\models\nomic-embed-text.Q4_K_M.gguf"))
{
float[] vector = client.GetEmbedding("Hello, world!");
Console.WriteLine("Embedding dimensions: " + vector.Length);
}
Configuring Context and Threads
using LlamaPure;
using (var client = new LlamaPureClient(
modelPath: @"C:\models\llama-3.2-1b.Q4_K_M.gguf",
contextSize: 4096,
threads: 8))
{
string answer = client.Complete("Explain the Pythagorean theorem.", maxNewTokens: 128);
Console.WriteLine(answer);
}
All examples use string and standard managed arrays and are compatible with C# 6.0 and later.
API Reference
LlamaPureClient
public sealed class LlamaPureClient : IDisposable
Constructor
LlamaPureClient(string modelPath, uint contextSize = 2048, int threads = 4)
Initializes the llama backend, loads the GGUF model, and creates an inference context. Throws InvalidOperationException if the model cannot be loaded.
| Parameter | Type | Default | Description |
|---|---|---|---|
modelPath |
string |
— | Absolute or relative path to a .gguf model file |
contextSize |
uint |
2048 |
Maximum context length in tokens (KV-cache size) |
threads |
int |
4 |
Number of CPU threads used during generation |
Complete
string Complete(string prompt, int maxNewTokens = 128)
Tokenizes prompt, performs a prefill decode, then generates up to maxNewTokens tokens using greedy sampling. Generation stops early when an end-of-generation token is produced. Returns only the generated continuation; the prompt text is not included in the return value.
GetEmbedding
float[] GetEmbedding(string text)
Tokenizes text, executes a single forward pass with embeddings enabled, and returns the last-token hidden state as a float[] of length equal to the model's embedding dimension (n_embd). Intended for use with embedding-capable models.
Dispose
void Dispose()
Releases the inference context and model, and frees the llama backend. Idempotent — safe to call multiple times.
Building from Source
git clone --recurse-submodules https://github.com/taqu/LlamaPure
cd LlamaPure
dotnet build LlamaPure/LlamaPure.csproj -c Release
To build the native DLLs from the included llama.cpp submodule:
cd LlamaWrapper
build.bat
License
This software is distributed under two licenses 'The MIT License' or 'Public Domain', choose whichever you like.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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 was computed. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.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
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.