llm

package
v0.20.3 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package llm provides an OpenAI-compatible chat completion client. It is used by the Discord gateway to generate RAG-based answers from retrieved context documents.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is an OpenAI-compatible chat completion client. It communicates with any endpoint that speaks the /v1/chat/completions protocol (OpenAI, Azure OpenAI, local proxies such as cliproxy, etc.).

func New

func New(cfg Config, httpClient *http.Client) *Client

New returns a Client configured with the given Config. httpClient may be nil — an http.Client with cfg.Timeout (default 60 s) is used. When cfg.Timeout is positive it overrides the default. Pass a pre-built httpClient to take full control of transport settings.

func (*Client) Complete

func (c *Client) Complete(ctx context.Context, system, user string) (string, error)

Complete sends a single-turn chat completion request. system is the system prompt; user is the user turn content. 4xx responses are not retried. 5xx and network errors are retried up to 2 times.

func (*Client) CompleteWithMessages

func (c *Client) CompleteWithMessages(ctx context.Context, system string, messages []Message) (string, error)

CompleteWithMessages sends a multi-turn chat completion request. system is the system prompt; messages is the ordered conversation history including the final user turn. The system message is always prepended. 4xx responses are not retried. 5xx and network errors are retried up to 2 times.

func (*Client) Enabled

func (c *Client) Enabled() bool

Enabled reports whether the client has the minimum required configuration to make a request (non-empty base URL, model, and a token source configured).

type Completer

type Completer interface {
	Enabled() bool
	CompleteWithMessages(ctx context.Context, system string, messages []Message) (string, error)
}

Completer is the minimal interface for LLM chat completion. It is satisfied by *Client and can be used by callers that need to inject a mock or alternative implementation for testing.

type Config

type Config struct {
	BaseURL     string
	Model       string
	APIKey      string // static Bearer token (overrides AuthFile)
	AuthFile    string // path to CliProxyAPI OAuth JSON (e.g. ~/.cli-proxy-api/user.json)
	MaxTokens   int
	Temperature float64
	// Timeout is the per-request HTTP timeout for LLM calls.
	// When zero or negative, the default of 60 s is used.
	// Increase for slow local CPU inference (e.g. gemma3:4b) by setting
	// LLM_TIMEOUT_SECONDS in the environment.
	Timeout time.Duration
}

Config holds the parameters required to construct a Client.

Token resolution order:

  1. APIKey non-empty → static Bearer token
  2. AuthFile non-empty → CliProxyAPI OAuth token (auto-refreshed from disk, 5-min TTL)
  3. both empty → no Authorization header sent

type Message

type Message struct {
	Role    string `json:"role"` // "system", "user", or "assistant"
	Content string `json:"content"`
}

Message is a single chat message in the OpenAI format. It is exported so callers can build multi-turn conversation histories for CompleteWithMessages.

Jump to

Keyboard shortcuts

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