litertlmgo

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: Apache-2.0 Imports: 0 Imported by: 0

README

litertlm-go

A Go wrapper for Google's LiteRT-LM for running local LLM inference.

litertlm-go uses ebitengine/purego to dynamically load the LiteRT-LM C API shared library at runtime. No CGo toolchain is required to build applications with this package. Note: this approach was inspired by project Hybridgroup's project Yzma.

Building LiteRT-LM C shared object libraries

LiteRT-LM is a C++ project and does not distribute a C API by default. However, you can folllow instructions here to build the LiteRT-ML source code with C shared object libraries.

Install

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

You will need to download the *.litertlm model that you want to use for inference. You can get the models from Hugging Face's LiteRT Community. For the example below, we will use litert-community/gemma-4-E2B-it-litert-lm.

Using litertlm-go

package main

import (
    "fmt"
    "os"

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

func main() {
    if err := litertlm.Load(os.Getenv("LITERTLM_LIB"), "cpu"); err != nil {
        panic(err)
    }
    defer litertlm.Close()

    settings, _ := litertlm.NewEngineSettings(
        "/abs/path/to/gemma.litertlm", "cpu", nil, nil)
    defer settings.Delete()

    engine, _ := litertlm.NewEngine(settings)
    defer engine.Delete()

    session, _ := engine.NewSession(0)
    defer session.Delete()

    resp, _ := session.GenerateContent([]litertlm.InputData{
        litertlm.NewTextInputString("Write a haiku about the sea."),
    })
    defer resp.Delete()

    fmt.Println(resp.Text(0))
}

Run the code with:

LITERTLM_LIB=/path/to/shared-objects/lib \
    go run ./examples/hello -model /abs/path/to/model.litertlm

Examples

Path What it shows
examples/hello/ Minimal synchronous generation
examples/stream/ Token-by-token streaming using the Go channel variant
examples/chat/ Multi-turn Conversation API with JSON messages
examples/gpu/ GPU-backed generation + BenchmarkInfo metrics

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
chat command
chat demonstrates the higher-level Conversation API with multi-turn JSON messages.
chat demonstrates the higher-level Conversation API with multi-turn JSON messages.
conversation command
conversation showcases the full Conversation API: system prompt + declared tools + structured tool_calls in the assistant reply + tool-role response message.
conversation showcases the full Conversation API: system prompt + declared tools + structured tool_calls in the assistant reply + tool-role response message.
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 with litertlm-go using the low-level Session API.
hello demonstrates a minimal synchronous inference with litertlm-go using the low-level Session API.
stream command
stream demonstrates token-by-token streaming generation using a Go channel.
stream demonstrates token-by-token streaming generation using a Go channel.
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