llm

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

README

go-llm

Go Reference License

A multi-provider LLM tool and SDK for Go.

Interact with large language models from Google Gemini, Anthropic Claude, and Mistral through a unified CLI, HTTP API server, or Go SDK. Supports stateless completion (ask), stateful multi-turn conversations (chat), embeddings, tool use, MCP integration, and a Telegram bot.

Features

  • Multi-Provider: Unified interface across Google Gemini, Anthropic Claude, and Mistral
  • CLI: Interactive terminal UI chat with markdown rendering, single-shot ask, and embedding generation
  • HTTP API Server: REST endpoints for models, sessions, tools, chat, ask, and embeddings with SSE streaming
  • Session Management: Stateful multi-turn conversations with persistent storage (file-backed JSON)
  • Tool Use: Extensible tool framework with built-in integrations for Home Assistant, NewsAPI, and WeatherAPI
  • MCP Server: Model Context Protocol (stdio JSON-RPC 2.0) server exposing registered tools and prompts
  • Telegram Bot: Full-featured bot with per-conversation sessions, file/image attachments, and slash commands
  • Attachments: Send files, images, and URLs as context in chat and ask
  • Agents: Reusable, versioned task definitions with input/output schemas, Go templates, and tool selection
  • Structured Output: JSON schema-based structured output for ask and agents
  • Thinking/Reasoning: Extended thinking support with configurable budgets (Anthropic, Gemini)
  • OpenTelemetry: Distributed tracing support for the HTTP API

Quick Start

Server

The server provides the REST API for all providers and tools. Run it with Docker:

docker volume create go-llm
docker run -d --name go-llm \
  -v go-llm:/data -p 8085:8085 \
  -e GEMINI_API_KEY="your-key" \
  ghcr.io/mutablelogic/llm run
Client

The client-only CLI can be downloaded from the releases page, or use go install -tags client github.com/mutablelogic/go-llm/cmd/llm@latest. It does not include the server or the Telegram bot. Point it at a running server:

export LLM_ADDR="localhost:8085"

# List providers and models
llm providers
llm models

# Single-shot completion
llm ask --model gemini-2.0-flash "Explain KV cache in two sentences"

# Interactive chat (launches terminal UI)
llm chat --model gemini-2.0-flash

# Single-shot chat with session persistence
llm chat --model gemini-2.0-flash "What is the capital of France?"

# Generate embeddings
llm embedding --model text-embedding-004 "Hello, world"
Telegram Bot

The Telegram bot runs as a sidecar to the server, providing chat access via Telegram. It requires a bot token from @BotFather:

docker run -d --name go-llm-telegram \
  -e LLM_ADDR="your-server:8085" \
  -e TELEGRAM_TOKEN="your-bot-token" \
  ghcr.io/mutablelogic/llm telegram --model gemini-2.0-flash

Server Configuration

Providers

Providers are sources of models and generation capabilities. Some providers may support additional features like thinking/reasoning, tool use and embeddings. API keys for access to provider models are configured on the server via flags or environment variables:

Provider Flag Env Variable Models
Google Gemini --gemini-api-key GEMINI_API_KEY Gemini 2.0 Flash, Flash Lite, embedding models, etc.
Anthropic Claude --anthropic-api-key ANTHROPIC_API_KEY Claude Sonnet, Haiku, Opus, etc.
Mistral --mistral-api-key MISTRAL_API_KEY Mistral Large, Small, embedding models, etc.
ELIZA --eliza (none) Mock provider based on Weizenbaum's 1966 chatbot; no API key needed (en, de, fr)
Tools

Tools are external functions that can be called by the agent during generation. They are registered with the agent and exposed via the API and MCP server. Tools can be configured with flags or environment variables as needed for authentication or connection details. The following tools are included as examples of how to build tool integrations with the SDK:

Tool Flag Env Variable Description
Home Assistant --ha-endpoint, --ha-token HA_ENDPOINT, HA_TOKEN Query and control smart home devices
NewsAPI --news-api-key NEWS_API_KEY Search news articles and sources
WeatherAPI --weather-api-key WEATHER_API_KEY Current weather and forecasts
HTTP & TLS
Flag Env Variable Default Description
--http.addr LLM_ADDR localhost:8085 HTTP listen address
--http.prefix /api HTTP path prefix
--http.timeout 15m HTTP read/write timeout
--http.origin (same-origin) CORS origin (* to allow all)
--tls.name TLS server name
--tls.cert TLS certificate file
--tls.key TLS key file
Observability
Flag Env Variable Description
--otel.endpoint OTEL_EXPORTER_OTLP_ENDPOINT OpenTelemetry collector endpoint
--otel.header OTEL_EXPORTER_OTLP_HEADERS OpenTelemetry collector headers
--otel.name OTEL_SERVICE_NAME OpenTelemetry service name

Sessions and defaults are stored in $XDG_CACHE_HOME (or system temp if unset).

CLI Commands

The llm command-line tool can connect to any running go-llm server (set LLM_ADDR or --http.addr to point at it) and provides commands for generating text, managing models and sessions, inspecting tools, and running the server itself.

Generate
Command Description Example
ask Stateless single-shot completion llm ask --model gemini-2.0-flash --file "*.go" "Summarize this source code"
chat Stateful chat (terminal UI or single-shot) llm chat --model gemini-2.0-flash
embedding Generate embedding vectors llm embedding --model text-embedding-004 "text"
Model
Command Description Example
providers List available providers llm providers
models List models llm models
model Get model details llm model gemini-2.0-flash
Session

Sessions represent stateful conversations with context and memory. They can be managed via the CLI or API, and are persisted on the server for later retrieval.

Command Description Example
sessions List sessions llm sessions
session Get session details llm session <id>
create-session Create a new session llm create-session gemini-2.0-flash
update-session Update session metadata llm update-session <id>
delete-session Delete a session llm delete-session <id>
Tool
Command Description Example
tools List registered tools llm tools
tool Get tool details llm tool <name>
Agent

Agents are reusable, versioned task definitions that bundle a model, system prompt, optional input/output JSON schemas, Go template, and tool list into a single unit. They are defined as markdown files with YAML front matter and managed via the CLI or API. See the Agent Definition Files documentation for the full file format, field reference, template syntax, and examples.

Command Description Example
agents List agents llm agents
agent Get agent details llm agent <name>
create-agent Create agents from markdown files llm create-agent agents/*.md
delete-agent Delete an agent llm delete-agent <name>
run-agent Run an agent (create session, chat, return result) llm run-agent summarizer --input '{"text":"..."}'
Running an Agent
# Create the agent from a file
llm create-agent agents/summarizer.md

# Run with JSON input
llm run-agent summarizer --input '{"text": "The quick brown fox..."}'

# Run with file attachments
llm run-agent caption --file photo.jpg

# Run with URL attachment
llm run-agent summarizer --url https://example.com/article

# Keep the session after completion (for inspection or follow-up)
llm run-agent summarizer --no-delete --input '{"text": "..."}'

The run-agent command creates a temporary session, sends the templated message, and returns the structured JSON result. By default the session is deleted after completion; use --no-delete to persist it.

Server
Command Description Example
run Start the server llm run
telegram Run as a Telegram bot, requiring a server llm telegram --model gemini-2.0-flash

Use llm --help or llm <command> --help for full options.

Interactive Chat

The chat command launches a terminal UI with markdown rendering, streaming responses, and slash commands:

Interactive Chat

Slash Commands
Command Description
/model Switch model
/models List available models
/providers List providers
/session Show current session
/sessions List sessions
/name Rename current session
/label Set session label
/system Set system prompt
/thinking Toggle thinking/reasoning
/tools Toggle tool use
/agents List agents
/agent Show agent details (running agents from chat is not yet supported)
/file Attach a file
/url Attach a URL
/reset Reset conversation
/delete Delete current session
/help Show help

Development

Architecture
%%{init: {"flowchart": {"htmlLabels": false}} }%%
flowchart LR
    subgraph Clients["Clients"]
        direction TB
        CLI["`**cmd/llm**
        CLI Tool`"]
        TUI["`**pkg/ui/bubbletea**
        Terminal UI`"]
        SDK["`**pkg/httpclient**
        SDK Client`"]
        Telegram["`**pkg/ui/telegram**
        Telegram Bot`"]
    end

    subgraph Server["go-llm Server"]
        direction TB
        API["`**pkg/httphandler**
        REST API`"]
        Agent["`**pkg/manager**
        Manager`"]
        AgentDef["`**pkg/agent**
        Agent Parsing`"]
        Sessions["`**pkg/store**
        Memory, File`"]
        Tools["`**pkg/tool**
        Tool Registry`"]
        MCP["`**pkg/mcp**
        MCP Server`"]
    end

    subgraph Providers["LLM Providers"]
        direction TB
        Gemini["`**pkg/provider/google**
        Google Gemini`"]
        Claude["`**pkg/provider/anthropic**
        Anthropic Claude`"]
        Mistral["`**pkg/provider/mistral**
        Mistral`"]
        Eliza["`**pkg/provider/eliza**
        ELIZA (mock)`"]
    end

    subgraph ToolImpls["Example Tools"]
        direction TB
        HA["`**pkg/homeassistant**
        Home Assistant`"]
        News["`**pkg/newsapi**
        NewsAPI`"]
        Weather["`**pkg/weatherapi**
        WeatherAPI`"]
    end

    CLI --> API
    TUI --> API
    SDK --> API
    Telegram --> API
    API --> Agent
    Agent --> AgentDef
    Agent --> Sessions
    Agent --> Tools
    Agent --> Gemini
    Agent --> Claude
    Agent --> Mistral
    Agent --> Eliza
    Tools --> HA
    Tools --> News
    Tools --> Weather
    MCP --> Tools
SDK Example

The Go SDK provides interfaces for building LLM-powered applications:

package main

import (
    "context"
    "fmt"
    "log"

    llm "github.com/mutablelogic/go-llm"
    "github.com/mutablelogic/go-llm/pkg/provider/google"
)

func main() {
    ctx := context.Background()

    // Create a provider
    client, err := google.New(ctx, "your-api-key")
    if err != nil {
        log.Fatal(err)
    }

    // Get a model
    model, err := client.GetModel(ctx, "gemini-2.0-flash")
    if err != nil {
        log.Fatal(err)
    }

    // Stateless completion
    gen := model.(llm.Generator)
    response, err := gen.WithoutSession(ctx, "Explain KV cache in two sentences")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(response)
}
Key Packages
Package Description
pkg/manager Central manager orchestrating providers, sessions, and tools
pkg/agent Agent definition parsing (template execution, input validation, funcmap)
pkg/provider/{google,anthropic,mistral,eliza} Provider implementations
pkg/store Storage backends for sessions and agents (in-memory, file-backed JSON)
pkg/tool Tool interface and toolkit registry
pkg/schema Core types (Model, Message, ContentBlock, Attachment, Session, etc.)
pkg/httpclient HTTP client for the go-llm API
pkg/httphandler HTTP handler layer for the REST API server
pkg/mcp Model Context Protocol server (stdio JSON-RPC 2.0)
pkg/ui Chat UI abstraction (bubbletea terminal UI, Telegram bot, shared command handler)
Core Interfaces
Interface Description
Client Provider connection — Name(), ListModels(), GetModel()
Generator Text generation — WithoutSession() (stateless), WithSession() (stateful)
Embedder Embedding generation — Embedding(), BatchEmbedding()
Downloader Model management — DownloadModel(), DeleteModel()
Building
git clone https://github.com/mutablelogic/go-llm.git
cd go-llm
make

Build the client-only CLI (no server or Telegram support):

make llm-client
Makefile Targets
Target Description
make all Build all binaries
make llm-client Build client-only CLI (-tags client)
make docker Build Docker image
make docker-push Push Docker image to GHCR
make docker-version Print version tag
make test Run all tests
make unit-test Run unit tests
make coverage-test Run tests with coverage
make tidy Run go mod tidy
make clean Remove build artifacts

Cross-compilation is supported via OS and ARCH variables:

OS=linux ARCH=arm64 make

Contributing & License

Please file issues and feature requests in GitHub Issues. Licensed under Apache 2.0.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client added in v0.1.0

type Client interface {
	// Return the provider name
	Name() string

	// ListModels returns the list of available models
	ListModels(context.Context, ...opt.Opt) ([]schema.Model, error)

	// GetModel returns the model with the given name
	GetModel(context.Context, string, ...opt.Opt) (*schema.Model, error)
}

Client is the interface that wraps basic LLM client methods

type Downloader added in v0.1.0

type Downloader interface {
	// DownloadModel downloads the specified model, and otherwise loads the model if already present
	DownloadModel(context.Context, string, ...opt.Opt) (*schema.Model, error)

	// DeleteModel deletes the specified model from local storage
	DeleteModel(context.Context, schema.Model) error
}

Downloader is an interface for managing model files

type Embedder added in v0.1.0

type Embedder interface {
	// Embedding generates an embedding vector for a single text
	Embedding(context.Context, schema.Model, string, ...opt.Opt) ([]float64, error)

	// BatchEmbedding generates embedding vectors for multiple texts
	BatchEmbedding(context.Context, schema.Model, []string, ...opt.Opt) ([][]float64, error)
}

Embedder is an interface for generating text embeddings

type Err

type Err int

Errors

const (
	ErrSuccess Err = iota
	ErrNotFound
	ErrBadParameter
	ErrNotImplemented
	ErrConflict
	ErrInternalServerError
	ErrMaxTokens
	ErrRefusal
	ErrPauseTurn
)

func (Err) Error

func (e Err) Error() string

func (Err) With

func (e Err) With(args ...interface{}) error

func (Err) Withf

func (e Err) Withf(format string, args ...interface{}) error

type Generator added in v0.1.0

type Generator interface {
	// WithoutSession sends a single message and returns the response (stateless)
	WithoutSession(context.Context, schema.Model, *schema.Message, ...opt.Opt) (*schema.Message, *schema.Usage, error)

	// WithSession sends a message within a session and returns the response (stateful)
	WithSession(context.Context, schema.Model, *schema.Conversation, *schema.Message, ...opt.Opt) (*schema.Message, *schema.Usage, error)
}

Generator is an interface for generating response messages and conducting conversations

Directories

Path Synopsis
cmd
llm command
etc
_old/deepseek
deepseek implements an API client for Deepseek's LLM https://api-docs.deepseek.com/
deepseek implements an API client for Deepseek's LLM https://api-docs.deepseek.com/
_old/ollama
ollama implements an API client for ollama https://github.com/ollama/ollama/blob/main/docs/api.md
ollama implements an API client for ollama https://github.com/ollama/ollama/blob/main/docs/api.md
_old/openai
openai implements an API client for OpenAI https://platform.openai.com/docs/api-reference
openai implements an API client for OpenAI https://platform.openai.com/docs/api-reference
pkg
homeassistant
homeassistant implements an API client for Home Assistant API https://developers.home-assistant.io/docs/api/rest/
homeassistant implements an API client for Home Assistant API https://developers.home-assistant.io/docs/api/rest/
mcp
Implements an MCP server based on the following specification: https://modelcontextprotocol.io/specification/2025-03-26/basic/lifecycle
Implements an MCP server based on the following specification: https://modelcontextprotocol.io/specification/2025-03-26/basic/lifecycle
newsapi
newsapi implements an API client for NewsAPI (https://newsapi.org/docs)
newsapi implements an API client for NewsAPI (https://newsapi.org/docs)
opt
provider/anthropic
anthropic implements an API client for the Anthropic Messages API.
anthropic implements an API client for the Anthropic Messages API.
provider/eliza
eliza implements a mock LLM provider based on the classic ELIZA chatbot created by Joseph Weizenbaum at MIT in 1966.
eliza implements a mock LLM provider based on the classic ELIZA chatbot created by Joseph Weizenbaum at MIT in 1966.
provider/google
google implements an API client for the Google Gemini REST API.
google implements an API client for the Google Gemini REST API.
provider/mistral
mistral implements an API client for the Mistral AI API.
mistral implements an API client for the Mistral AI API.
ui
Package ui defines the interface for chat user interfaces.
Package ui defines the interface for chat user interfaces.
ui/bubbletea
Package bubbletea implements ui.ChatUI for interactive terminals using the Charm bubbletea framework.
Package bubbletea implements ui.ChatUI for interactive terminals using the Charm bubbletea framework.
ui/command
Package command implements shared slash-command handling for chat UIs.
Package command implements shared slash-command handling for chat UIs.
ui/table
Package table provides a terminal table renderer backed by lipgloss.
Package table provides a terminal table renderer backed by lipgloss.
ui/telegram
Package telegram implements ui.ChatUI for Telegram bots using telebot v4.
Package telegram implements ui.ChatUI for Telegram bots using telebot v4.
weatherapi
weatherapi implements an API client for WeatherAPI https://www.weatherapi.com/docs/
weatherapi implements an API client for WeatherAPI https://www.weatherapi.com/docs/

Jump to

Keyboard shortcuts

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