Documentation
¶
Overview ¶
Package ai is the togo AI plugin: a unified LLM interface with a pluggable driver registry. Provider plugins (ai-openai, ai-anthropic, ai-gemini, ai-ollama, …) register a driver via init(); select one with AI_DRIVER. Mirrors the mail/storage driver-plugin pattern. A safe "echo" driver is the dev default.
Index ¶
- Constants
- func Drivers() []string
- func Handler(k *togo.Kernel) http.Handler
- func RegisterDriver(name string, f DriverFactory)
- type ChatRequest
- type ChatResponse
- type Chunk
- type DriverFactory
- type EmbedRequest
- type EmbedResponse
- type Message
- type Provider
- type Service
- func (s *Service) Chat(ctx context.Context, req ChatRequest) (ChatResponse, error)
- func (s *Service) ChatStream(ctx context.Context, req ChatRequest, onChunk func(Chunk) error) error
- func (s *Service) Driver() string
- func (s *Service) Embed(ctx context.Context, req EmbedRequest) (EmbedResponse, error)
- type Streamer
- type Tool
- type ToolCall
- type Usage
Constants ¶
const ( RoleSystem = "system" RoleUser = "user" RoleAssistant = "assistant" RoleTool = "tool" )
Chat roles.
Variables ¶
This section is empty.
Functions ¶
func Handler ¶
Handler exposes the AI service over REST. Mount under /api/ai in your app:
mux.Handle("/api/ai/", http.StripPrefix("/api/ai", ai.Handler(k)))
Routes: POST /chat (ChatRequest -> ChatResponse), POST /embed (EmbedRequest -> EmbedResponse).
func RegisterDriver ¶
func RegisterDriver(name string, f DriverFactory)
RegisterDriver registers an AI provider driver (called from a provider plugin's init()).
Types ¶
type ChatRequest ¶
type ChatRequest struct {
Model string `json:"model,omitempty"`
Messages []Message `json:"messages"`
Temperature float64 `json:"temperature,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
Tools []Tool `json:"tools,omitempty"`
}
ChatRequest is a chat-completion request.
type ChatResponse ¶
type ChatResponse struct {
Content string `json:"content"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
Model string `json:"model,omitempty"`
Usage Usage `json:"usage"`
}
ChatResponse is a chat-completion result.
type DriverFactory ¶
DriverFactory builds a Provider from the kernel/env.
type EmbedRequest ¶
EmbedRequest requests embeddings for one or more inputs.
type EmbedResponse ¶
EmbedResponse holds the resulting vectors.
type Message ¶
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
Name string `json:"name,omitempty"`
}
Message is a single chat message.
type Provider ¶
type Provider interface {
Chat(ctx context.Context, req ChatRequest) (ChatResponse, error)
Embed(ctx context.Context, req EmbedRequest) (EmbedResponse, error)
}
Provider is the LLM driver interface every ai-* provider plugin implements.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the kernel-bound AI service.
func FromKernel ¶
FromKernel returns the AI service bound to the kernel.
func (*Service) Chat ¶
func (s *Service) Chat(ctx context.Context, req ChatRequest) (ChatResponse, error)
Chat performs a chat completion via the configured provider.
func (*Service) ChatStream ¶
ChatStream streams a chat completion; falls back to a single chunk if the provider doesn't implement Streamer.
func (*Service) Embed ¶
func (s *Service) Embed(ctx context.Context, req EmbedRequest) (EmbedResponse, error)
Embed returns embeddings via the configured provider.
type Streamer ¶
type Streamer interface {
ChatStream(ctx context.Context, req ChatRequest, onChunk func(Chunk) error) error
}
Streamer is an optional interface for streaming chat completions.
type Tool ¶
type Tool struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters map[string]any `json:"parameters,omitempty"`
}
Tool describes a function the model may call.