litertlmgo

package module
v0.2.3 Latest Latest
Warning

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

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

README ¶

litertlm-go

A Go binding for Google's LiteRT-LM for high-performance local on-device LLM inference.

Inspired by Hybridgroup's Yzma.

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

Features

  • Client — single-call Generate, range-over-func GenerateStream, rich GenerateResponse. Functional options for every engine setting. Context-driven cancellation. → Client guide
  • Chat — multi-turn conversations with system prompts, tool declarations, and structured tool_calls parsing. → Chat guide
  • GenerateData[T] — generic helper that returns *T populated from the model's JSON output, with retry and tolerant parsing. → Structured output
  • Low-level API — every C-API symbol exposed as a Go method. Useful when you need explicit prefill→decode, scoring, token introspection, or deterministic resource lifetimes. → Low-level guide

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 {
        fmt.Println(err)
        os.Exit(1)
    }
    defer client.Close()

    text, err := client.Generate(ctx, "Write a haiku about the sea.")
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    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

Currently, LiteRT-LM doesn't ship a prebuilt C API shared libraries. The followings walk you through how to build them:

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)
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

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