litertlmgo

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2026 License: Apache-2.0 Imports: 0 Imported by: 0

README ¶

litertlm-go

A purego-backed, cgo-free Go wrapper for Google's LiteRT-LM C API. Run local LLM inference from Go without a C toolchain — the C library is loaded dynamically at runtime via ebitengine/purego.

Inspired by Hybridgroup's Yzma.

📖 Full documentation: vladimirvivien.github.io/litertlm-go

Install

go get github.com/vladimirvivien/litertlm-go@latest

Quickstart

package main

import (
    "context"
    "fmt"
    "os"

    "github.com/vladimirvivien/litertlm-go/pkg/litertlm"
)

func main() {
    ctx := context.Background()
    client, err := litertlm.New(ctx,
        litertlm.WithLib(os.Getenv("LITERTLM_LIB")),
        litertlm.WithModel(os.Getenv("LITERTLM_MODEL")),
    )
    if err != nil {
        panic(err)
    }
    defer client.Close()

    text, err := client.Generate(ctx, "Write a haiku about the sea.")
    if err != nil {
        panic(err)
    }
    fmt.Println(text)
}
LITERTLM_LIB=/abs/path/to/dist/lib \
LITERTLM_MODEL=/abs/path/to/gemma-4-E2B-it.litertlm \
    go run main.go

Full walkthrough → docs/getting-started.

Building the C library

LiteRT-LM doesn't ship a prebuilt C API. Build it yourself:

Examples

Path What it shows
examples/hello/ Minimal Generate
examples/stream/ GenerateStream with range-over-func
examples/chat/ Multi-turn Chat with a system prompt
examples/conversation/ Chat + tools + structured tool_calls
examples/structured/ GenerateData[T] (typed JSON output via reflection + retries)
examples/cancel/ Cancelling a streaming generation via context.WithCancel
examples/prefill-decode/ Explicit two-phase generation (low-level)
examples/score/ ScoreTexts + Score / TokenLength (low-level)
examples/tokenize/ Engine.Tokenize / Detokenize + start/stop tokens (low-level)
examples/gpu/ GPU-backed generation + benchmark metrics

Documentation

The high-level API (recommended for most use cases) is documented at vladimirvivien.github.io/litertlm-go:

The site is built with MkDocs Material and published from the docs/ directory on tagged pushes (see .github/workflows/docs.yml). Preview locally:

pip install -r docs/requirements.txt
mkdocs serve  # http://localhost:8000/

License

Apache-2.0, same as LiteRT-LM itself.

Documentation ¶

Overview ¶

Package litertlmgo is the module root for litertlm-go.

Consumers import the actual Go API from the subpackages:

import "github.com/vladimirvivien/litertlm-go/pkg/litertlm"
import "github.com/vladimirvivien/litertlm-go/pkg/loader"

See README.md for the full usage guide and LITERTLM-BUILD.md for the native-library build recipe.

Directories ¶

Path Synopsis
examples
cancel command
cancel demonstrates aborting an in-flight streaming generation using the high-level Client.GenerateStream API and context cancellation.
cancel demonstrates aborting an in-flight streaming generation using the high-level Client.GenerateStream API and context cancellation.
chat command
chat demonstrates multi-turn conversation using the high-level Client.NewChat / Chat.Send API.
chat demonstrates multi-turn conversation using the high-level Client.NewChat / Chat.Send API.
conversation command
conversation showcases the full tool-using flow on the high-level Chat API: declare a tool, send a user prompt, dispatch the structured tool_call the model returns, send the result back, read the final natural-language answer.
conversation showcases the full tool-using flow on the high-level Chat API: declare a tool, send a user prompt, dispatch the structured tool_call the model returns, send the result back, read the final natural-language answer.
gpu command
gpu demonstrates GPU-accelerated local inference plus BenchmarkInfo readout (init time, time-to-first-token, prefill/decode throughput).
gpu demonstrates GPU-accelerated local inference plus BenchmarkInfo readout (init time, time-to-first-token, prefill/decode throughput).
hello command
hello demonstrates a minimal synchronous inference using the high-level Client API.
hello demonstrates a minimal synchronous inference using the high-level Client API.
prefill-decode command
prefill-decode demonstrates the explicit two-phase generation flow: RunPrefill seeds the session with the prompt context, RunDecode then produces the response.
prefill-decode demonstrates the explicit two-phase generation flow: RunPrefill seeds the session with the prompt context, RunDecode then produces the response.
score command
score demonstrates per-target text scoring: prefill the prompt, then score one candidate completion and inspect its log-probability score and tokenized length.
score demonstrates per-target text scoring: prefill the prompt, then score one candidate completion and inspect its log-probability score and tokenized length.
stream command
stream demonstrates token-by-token streaming using the high-level Client.GenerateStream API (range-over-func iterator).
stream demonstrates token-by-token streaming using the high-level Client.GenerateStream API (range-over-func iterator).
structured command
structured demonstrates type-safe structured-output extraction with litertlm.GenerateData[T].
structured demonstrates type-safe structured-output extraction with litertlm.GenerateData[T].
tokenize command
tokenize demonstrates the engine's tokenizer round-trip: text → []int32 token ids → text.
tokenize demonstrates the engine's tokenizer round-trip: text → []int32 token ids → text.
pkg
litertlm
Package litertlm is a purego-backed, cgo-free Go wrapper around Google's LiteRT-LM C API (see c/engine.h in the LiteRT-LM repository).
Package litertlm is a purego-backed, cgo-free Go wrapper around Google's LiteRT-LM C API (see c/engine.h in the LiteRT-LM repository).
loader
Package loader resolves and loads the LiteRT-LM native shared library.
Package loader resolves and loads the LiteRT-LM native shared library.
utils
Package utils provides platform-abstracted helpers for marshalling strings across the Go/C FFI boundary.
Package utils provides platform-abstracted helpers for marshalling strings across the Go/C FFI boundary.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL