Go bindings for llama.cpp
  • C++ 50.4%
  • Go 34%
  • C 14.5%
  • Makefile 1.1%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Morgan a8fad95216
All checks were successful
CI/CD Pipeline / test (push) Successful in 33m8s
CI/CD Pipeline / libs-image (push) Successful in 2m3s
chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk
2026-11-01 21:18:24 +00:00
.forgejo/workflows chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
cgo_headers/llama.cpp feat(deps): update llama.cpp to b11982 2026-10-29 10:52:10 +00:00
docs chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
examples chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
internal/exampleui chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
llama.cpp@8a199ad31a feat(deps): update llama.cpp to b11982 2026-10-29 10:52:10 +00:00
.gitignore fix(build): rebuild the wrapper and llama.cpp objects when they change 2026-10-28 07:27:41 +00:00
.gitmodules chore(deps): update llama.cpp submodule URL to ggml-org 2026-04-02 21:31:35 +01:00
.markdownlint.yaml feat(fork): establish active fork with modern development workflow 2025-09-27 12:48:35 +01:00
.pre-commit-config.yaml chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
channel_test.go chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
chat.go fix(errors): keep each thread's error message apart 2026-10-31 16:55:08 +00:00
chat_options.go fix(chat): extract reasoning by default, as llama.cpp does 2026-10-27 23:11:51 +00:00
chat_test.go chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
chat_tools.go feat(api): add channel-based generation and native chat support 2025-10-09 20:56:26 +01:00
chat_types.go feat(media): accept images and audio through a multimodal projector 2026-10-28 08:26:55 +00:00
context.go fix(errors): keep each thread's error message apart 2026-10-31 16:55:08 +00:00
decision.go fix(errors): keep each thread's error message apart 2026-10-31 16:55:08 +00:00
decision_test.go chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
devices.go feat(model): place tensors on chosen devices 2026-10-31 04:53:59 +00:00
doc.go feat(speculative)!: speculate within the context's own generation loop 2026-10-27 22:25:13 +00:00
Dockerfile.build refactor(bindings): rewrite to use llama.cpp API directly 2025-09-29 21:00:17 +01:00
Dockerfile.cuda build(image): add libopus-dev to the CUDA build image 2026-10-30 09:59:10 +00:00
Dockerfile.libs-cuda build(server): build and link llama.cpp's server context library 2026-10-28 22:51:49 +00:00
embeddings_test.go chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
error_handling_test.go chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
export_test.go test(speculative): check rollback against plain generation exactly 2026-10-28 07:21:28 +00:00
generation_test.go chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
go.mod chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
go.sum chore(deps): bump Go toolchain to 1.27.3 and modules 2026-10-22 16:08:28 +01:00
gpu_layers_test.go chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
lasterror.go fix(errors): keep each thread's error message apart 2026-10-31 16:55:08 +00:00
LICENSE feat(fork): establish active fork with modern development workflow 2025-09-27 12:48:35 +01:00
linkage_shared.go build(server): build and link llama.cpp's server context library 2026-10-28 22:51:49 +00:00
linkage_static.go build(server): build and link llama.cpp's server context library 2026-10-28 22:51:49 +00:00
llama_cublas.go feat(deps): update llama.cpp to b9002 with static-default linkage redesign 2026-05-02 11:42:19 +01:00
llama_cublas_shared.go fix(cuda): add stubs path so -lcuda resolves without GPU at build time 2026-05-03 10:33:22 +01:00
llama_cublas_static.go fix(cuda): add stubs path so -lcuda resolves without GPU at build time 2026-05-03 10:33:22 +01:00
llama_hipblas.go feat(sampling): add 26 advanced sampling parameters 2025-10-08 20:51:11 +01:00
llama_metal.go feat(sampling): add 26 advanced sampling parameters 2025-10-08 20:51:11 +01:00
llama_openblas.go feat(deps): update llama.cpp to b9002 with static-default linkage redesign 2026-05-02 11:42:19 +01:00
llama_opencl.go feat(sampling): add 26 advanced sampling parameters 2025-10-08 20:51:11 +01:00
llama_rpc.go feat(sampling): add 26 advanced sampling parameters 2025-10-08 20:51:11 +01:00
llama_suite_test.go refactor(project): rename from go-llama.cpp to llama-go 2025-09-29 23:03:15 +01:00
llama_sycl.go feat(sampling): add 26 advanced sampling parameters 2025-10-08 20:51:11 +01:00
llama_vulkan.go feat(sampling): add 26 advanced sampling parameters 2025-10-08 20:51:11 +01:00
Makefile build(server): build and link llama.cpp's server context library 2026-10-28 22:51:49 +00:00
media_test.go chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
model.go fix(errors): keep each thread's error message apart 2026-10-31 16:55:08 +00:00
model_loading_test.go chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
options_context.go test(speculative): check rollback against plain generation exactly 2026-10-28 07:21:28 +00:00
options_generate.go feat(media): accept images and audio through a multimodal projector 2026-10-28 08:26:55 +00:00
options_model.go feat(model): place tensors on chosen devices 2026-10-31 04:53:59 +00:00
prefix_caching_test.go chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
progress_callback.go fix unsafe pointer 2026-07-02 18:03:43 +01:00
README.md chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
reasoning_test.go chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
RELEASE.md chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
renovate.json Add renovate.json 2023-04-24 12:05:59 +00:00
rope_scaling_test.go chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
speculative_test.go chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
stats.go refactor(api): separate model weights from execution contexts 2025-10-25 08:39:15 +01:00
streaming_test.go chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
tensor_override_test.go chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
thread_config_test.go chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
tokenisation_test.go chore(module): move to github.com/liminalpurple/llama-go and git.cisnt.uk 2026-11-01 21:18:24 +00:00
types.go feat(model): place tensors on chosen devices 2026-10-31 04:53:59 +00:00
wrapper.cpp fix(errors): keep each thread's error message apart 2026-10-31 16:55:08 +00:00
wrapper.h fix(errors): keep each thread's error message apart 2026-10-31 16:55:08 +00:00

llama-go: Run LLMs locally with Go

Go Reference

Go bindings for llama.cpp, enabling you to run large language models locally with GPU acceleration. Production-ready library with thread-safe concurrent inference and comprehensive test coverage. Integrate LLM inference directly into Go applications with a clean, idiomatic API.

This is an active fork of go-skynet/go-llama.cpp, which hasn't been maintained since October 2023. The goal is keeping Go developers up-to-date with llama.cpp whilst offering a lighter, more performant alternative to Python-based ML stacks like PyTorch and/or vLLM.

Documentation:

Quick start

# Clone with submodules
git clone --recurse-submodules https://github.com/liminalpurple/llama-go
cd llama-go

# Build the library (default: static linkage, single-binary friendly)
make libbinding.a

# Download a test model
wget https://huggingface.co/Qwen/Qwen3-0.6B-GGUF/resolve/main/Qwen3-0.6B-Q8_0.gguf

# Run an example
export LIBRARY_PATH=$PWD C_INCLUDE_PATH=$PWD
go run ./examples/simple -m Qwen3-0.6B-Q8_0.gguf -p "Hello world" -n 50

Basic usage

package main

import (
    "context"
    "fmt"
    llama "github.com/liminalpurple/llama-go"
)

func main() {
    // Load model weights (ModelOption: WithGPULayers, WithMLock, etc.)
    model, err := llama.LoadModel(
        "/path/to/model.gguf",
        llama.WithGPULayers(-1), // Offload all layers to GPU
    )
    if err != nil {
        panic(err)
    }
    defer model.Close()

    // Create execution context (ContextOption: WithContext, WithBatch, etc.)
    ctx, err := model.NewContext(
        llama.WithContext(2048),
        llama.WithF16Memory(),
    )
    if err != nil {
        panic(err)
    }
    defer ctx.Close()

    // Chat completion (uses model's chat template)
    messages := []llama.ChatMessage{
        {Role: "system", Content: "You are a helpful assistant."},
        {Role: "user", Content: "What is the capital of France?"},
    }
    response, err := ctx.Chat(context.Background(), messages, llama.ChatOptions{
        MaxTokens: llama.Int(100),
    })
    if err != nil {
        panic(err)
    }
    fmt.Println(response.Content)

    // Or raw text generation
    text, err := ctx.Generate("Hello world", llama.WithMaxTokens(50))
    if err != nil {
        panic(err)
    }
    fmt.Println(text)
}

When building, set these environment variables:

export LIBRARY_PATH=$PWD C_INCLUDE_PATH=$PWD

The default static linkage links every llama.cpp library directly into your Go binary — no LD_LIBRARY_PATH setup, no shared libraries to ship alongside the executable. If you'd rather use shared libraries, build with BUILD_LINKAGE=shared make libbinding.a and pass -tags shared_lib to go build; the shared mode bakes -Wl,-rpath,$ORIGIN into the binary so the .so files only need to sit next to the executable.

Key capabilities

Text generation and chat: Generate text with LLMs using native chat completion (with automatic chat template formatting) or raw text generation. Give vision and audio models images and audio clips through their multimodal projectors. Ask decision models typed questions for calibrated answers. Extract embeddings for semantic search, clustering, and similarity tasks.

GPU acceleration: Supports NVIDIA (CUDA), AMD (ROCm), Apple Silicon (Metal), Intel (SYCL), and cross-platform acceleration (Vulkan, OpenCL). Eight backend options cover virtually all modern GPU hardware, plus distributed inference via RPC.

Production ready: Comprehensive test suite with almost 400 test cases and CI validation including CUDA builds. Active development tracking llama.cpp releases - maintained for production use, not a demo project.

Advanced features: Model/Context separation enables efficient VRAM usage - load model weights once, create multiple contexts with different configurations. Cache common prompt prefixes to avoid recomputing system prompts across thousands of generations. Serve multiple concurrent requests with a single model loaded in VRAM (no weight duplication). Stream tokens via callbacks or buffered channels (decouples GPU inference from slow processing). Speculative decoding for 2-3× generation speedup.

Architecture

The library bridges Go and C++ using CGO, keeping the heavy computation in llama.cpp's optimised C++ code whilst providing a clean Go API. This minimises CGO overhead whilst maximising performance.

Model/Context separation: The API separates model weights (Model) from execution state (Context). Load model weights once, create multiple contexts with different configurations. Each context maintains its own KV cache and state for independent inference operations.

Key components:

  • wrapper.cpp/wrapper.h - CGO interface to llama.cpp
  • model.go - Model loading and weight management (thread-safe)
  • context.go - Execution contexts for inference (one per goroutine)
  • Clean Go API with comprehensive godoc comments
  • llama.cpp/ - Git submodule tracking upstream releases

The design uses functional options for configuration (ModelOption vs ContextOption), explicit context creation for thread safety, automatic KV cache prefix reuse for performance, resource management with finalizers, and streaming callbacks via cgo.Handle for safe Go-C interaction.

Licence

MIT