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
- type ChatChunk
- type ChatCompletion
- type ChatRequest
- type Choice
- type ChunkChoice
- type Client
- func (c *Client) Chat(ctx context.Context, p Priority, body, out any) error
- func (c *Client) ChatStream(ctx context.Context, p Priority, body any) (*Stream, error)
- func (c *Client) Close()
- func (c *Client) Models(ctx context.Context) ([]Model, error)
- func (c *Client) Respond(ctx context.Context, p Priority, body, out any) error
- func (c *Client) RespondStream(ctx context.Context, p Priority, body any) (*Stream, error)
- type Error
- type Event
- type Extra
- type Message
- type Model
- type Priority
- type Stream
- type Usage
Constants ¶
const ( HeaderPriority = "Lodge-Priority" HeaderApp = "Lodge-App" )
The headers the client sends.
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 ¶
Dial returns a client of the inference socket at path, naming app in Lodge-App when app is not empty.
func New ¶
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 ¶
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 ¶
ChatStream posts a chat completion with stream set true, and returns its events: each a ChatChunk in Data, until [DONE].
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.
type Event ¶
Event is one server-sent event of a stream: its type (event:, empty for chat chunks) and its data.
type Extra ¶
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).
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.
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.