inference

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 12 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 inference is an app daemon's client of lodge's inference socket, /run/lodge/inference.sock: OpenAI's chat completions and responses, and the aliases the app may use (https://github.com/frostyard/lodge/blob/main/docs/specs/app-contract.md#inference).

ai := inference.New(env, "mail") // env from lodgeapp.LoadEnv
models, err := ai.Models(ctx)
var out inference.ChatCompletion
err = ai.Chat(ctx, inference.Interactive, inference.ChatRequest{
	Model:    "local",
	Messages: []inference.Message{{Role: "user", Content: "Summarize this thread."}},
}, &out)
stream, err := ai.ChatStream(ctx, inference.Background, req)

lodge knows the app by its socket peer's uid. Under LODGE_ROOT, where every app runs as the developer, the client also names its app in Lodge-App. It sends no key: lodge adds the provider's.

Index

Constants

View Source
const (
	HeaderPriority = "Lodge-Priority"
	HeaderApp      = "Lodge-App"
)

The headers the client sends.

View Source
const MaxEvent = 32 << 20

MaxEvent bounds one event's data: 32 MiB.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatChunk

type ChatChunk struct {
	ID      string        `json:"id"`
	Model   string        `json:"model"`
	Choices []ChunkChoice `json:"choices"`
	// Usage comes in the last chunk, when the request asked for it
	// (stream_options.include_usage).
	Usage *Usage `json:"usage,omitzero"`
	Extra Extra  `json:",embed"`
}

ChatChunk is one event of a streamed chat completion.

type ChatCompletion

type ChatCompletion struct {
	ID      string   `json:"id"`
	Model   string   `json:"model"`
	Choices []Choice `json:"choices"`
	Usage   *Usage   `json:"usage,omitzero"`
	Extra   Extra    `json:",embed"`
}

ChatCompletion is a chat completion's answer.

type ChatRequest

type ChatRequest struct {
	Model    string    `json:"model"`
	Messages []Message `json:"messages"`
	Extra    Extra     `json:",embed"`
}

ChatRequest is a chat completion's request body. lodge replaces Model, an alias, with the provider's model.

type Choice

type Choice struct {
	Index        int     `json:"index"`
	Message      Message `json:"message"`
	FinishReason string  `json:"finish_reason"`
	Extra        Extra   `json:",embed"`
}

Choice is one of a completion's answers.

type ChunkChoice

type ChunkChoice struct {
	Index        int     `json:"index"`
	Delta        Message `json:"delta"`
	FinishReason *string `json:"finish_reason"`
	Extra        Extra   `json:",embed"`
}

ChunkChoice is one answer's part in a chunk.

type Client

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

Client calls lodge's inference socket. It is safe for concurrent use.

func Dial

func Dial(path, app string) *Client

Dial returns a client of the inference socket at path, naming app in Lodge-App when app is not empty.

func New

func New(env lodgeapp.Env, id string) *Client

New returns app id's client of the inference socket under env. Under env.Root it names the app in Lodge-App.

func (*Client) Chat

func (c *Client) Chat(ctx context.Context, p Priority, body, out any) error

Chat posts a chat completion (POST /v1/chat/completions) and decodes the answer into out, a *ChatCompletion or the app's own type. body is a ChatRequest or any JSON object; its stream member is set false.

func (*Client) ChatStream

func (c *Client) ChatStream(ctx context.Context, p Priority, body any) (*Stream, error)

ChatStream posts a chat completion with stream set true, and returns its events: each a ChatChunk in Data, until [DONE].

func (*Client) Close

func (c *Client) Close()

Close releases the client's idle connections.

func (*Client) Models

func (c *Client) Models(ctx context.Context) ([]Model, error)

Models lists the aliases the app may use (GET /v1/models).

func (*Client) Respond

func (c *Client) Respond(ctx context.Context, p Priority, body, out any) error

Respond posts to the responses API (POST /v1/responses) and decodes the answer into out. body is any JSON object; its stream member is set false.

func (*Client) RespondStream

func (c *Client) RespondStream(ctx context.Context, p Priority, body any) (*Stream, error)

RespondStream posts to the responses API with stream set true, and returns its events, such as response.output_text.delta and response.completed.

type Error

type Error struct {
	// Status is the HTTP status: 404 for an alias the app may not use, 503
	// for one whose provider is unavailable, 429 when the queue is full.
	Status  int
	Message string `json:"message"`
	Type    string `json:"type"`
	Code    string `json:"code"`
}

Error is lodge's or the provider's error answer, in OpenAI's shape.

func (*Error) Error

func (e *Error) Error() string

type Event

type Event struct {
	Type string
	Data jsontext.Value
}

Event is one server-sent event of a stream: its type (event:, empty for chat chunks) and its data.

func (Event) Decode

func (e Event) Decode(v any) error

Decode decodes the event's data into v.

type Extra

type Extra = map[string]jsontext.Value

Extra carries the members a type does not name, such as a provider's own parameters (the local model's thinking control) or tool calls; they pass through unchanged.

type Message

type Message struct {
	Role    string `json:"role,omitzero"`
	Content string `json:"content"`
	Extra   Extra  `json:",embed"`
}

Message is one chat message. Content is text; a message of another shape, such as content parts, goes in a request of the app's own type.

type Model

type Model struct {
	ID      string `json:"id"`
	Object  string `json:"object"`
	OwnedBy string `json:"owned_by"`
	// Available is false while the alias's provider cannot be reached; a
	// request to it then fails at once.
	Available bool `json:"available"`
}

Model is an alias the app may use.

type Priority

type Priority string

Priority is a request's class in lodge's queue (Lodge-Priority).

const (
	// Interactive is work a person waits for: it goes first, and one slot
	// is always left for it.
	Interactive Priority = "interactive"
	// Background is everything else.
	Background Priority = "background"
)

type Stream

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

Stream reads a streamed answer's events. It is not safe for concurrent use; Close it when done, or cancel the request's context.

func (*Stream) Close

func (s *Stream) Close() error

Close ends the stream.

func (*Stream) Next

func (s *Stream) Next() (Event, error)

Next returns the next event, or io.EOF after the last: after a chat stream's [DONE], or when the answer ends.

type Usage

type Usage struct {
	PromptTokens     int64 `json:"prompt_tokens,omitzero"`
	CompletionTokens int64 `json:"completion_tokens,omitzero"`
	InputTokens      int64 `json:"input_tokens,omitzero"`
	OutputTokens     int64 `json:"output_tokens,omitzero"`
	TotalTokens      int64 `json:"total_tokens,omitzero"`
	Extra            Extra `json:",embed"`
}

Usage is an answer's token counts: prompt and completion tokens for chat completions, input and output tokens for responses.

Jump to

Keyboard shortcuts

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