llm

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package llm is the LLM client interface agentloop's ai()/aiJSON() sandbox capability calls against, and what drives the agent loop's own turn-taking.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	Name() string
	Complete(ctx context.Context, req CompletionRequest) (CompletionResponse, error)
	// Stream delivers content deltas through onChunk as they arrive and
	// still returns the full accumulated response — "best-effort
	// incremental, full response always", so a caller that doesn't care
	// about incremental output can pass a nil onChunk and just use the
	// return value.
	Stream(ctx context.Context, req CompletionRequest, onChunk func(chunk string)) (CompletionResponse, error)
}

Client is what the ai()/aiJSON() sandbox capability, and the agent loop's own turn-taking, call against.

func NewOpenAI

func NewOpenAI(cfg Config) (Client, error)

NewOpenAI builds a Client from cfg. Returns an error if APIKey is empty — the caller decides what a missing key means for the run it's building (e.g. skip registering the "ai" capability).

type CompletionRequest

type CompletionRequest struct {
	Messages    []Message
	Model       string // empty uses the client's configured default
	Temperature *float64
	MaxTokens   *int
}

CompletionRequest is one call to Client.Complete or Client.Stream.

type CompletionResponse

type CompletionResponse struct {
	Content      string
	Model        string
	InputTokens  int
	OutputTokens int
}

CompletionResponse is what a completion call returns.

type Config

type Config struct {
	APIKey          string
	ChatModel       string
	BaseURL         string
	ReasoningEffort string
	// Headers carries extra request headers some gateways require or
	// recommend — e.g. OpenRouter's optional HTTP-Referer / X-Title.
	Headers map[string]string
	// HTTPClient is optional; nil falls back to a 5-minute-timeout client.
	HTTPClient *http.Client
}

Config configures the OpenAI-compatible client.

func ConfigFromEnv

func ConfigFromEnv() Config

ConfigFromEnv reads OPENAI_API_KEY / OPENAI_CHAT_MODEL / OPENAI_BASE_URL / OPENAI_REASONING_EFFORT.

OPENAI_API_KEY          — required to actually build a client.
OPENAI_CHAT_MODEL       — default "gpt-4o-mini"; override to match
                          whatever BaseURL expects (e.g.
                          "openai/gpt-4o-mini" on OpenRouter,
                          "google/gemini-2.5-flash" on EdenAI).
OPENAI_BASE_URL         — default the official OpenAI API; point this
                          at "https://openrouter.ai/api/v1",
                          "https://api.edenai.run/v3", or any other
                          OpenAI-wire-compatible host.
OPENAI_REASONING_EFFORT — optional ("low"|"medium"|"high"), caps
                          thinking on reasoning models.

type Message

type Message struct {
	Role    string // "system", "user", or "assistant"
	Content string
}

Message is one turn of a chat completion request.

Jump to

Keyboard shortcuts

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