Documentation
¶
Index ¶
- Variables
- func Drivers() []string
- func Register(name string, driver DriverFunc)
- func WithUsageRecorder(ctx context.Context, r *UsageRecorder) context.Context
- type AgentProvider
- type BatchJob
- type BatchOptions
- type BatchProvider
- type BatchRequest
- type BatchRequestCounts
- type BatchResult
- type CompletionUsage
- type Config
- type DriverFunc
- type Options
- type Provider
- type ProviderFactory
- type Schema
- type ToolCallRequest
- type ToolCallResult
- type ToolDefinition
- type UsageRecorder
Constants ¶
This section is empty.
Variables ¶
var ErrBatchOutputExpired = errors.New("batch output file expired or unavailable")
ErrBatchOutputExpired is returned by GetBatchResults when the provider's output file is no longer available (deleted after retention window). Callers should treat this the same as an expired batch and reset entities.
Functions ¶
func Drivers ¶
func Drivers() []string
Drivers returns a list of the names of the registered drivers.
func Register ¶
func Register(name string, driver DriverFunc)
Register makes a driver available by the provided name. If Register is called twice with the same name or if driver is nil, it panics.
func WithUsageRecorder ¶
func WithUsageRecorder(ctx context.Context, r *UsageRecorder) context.Context
WithUsageRecorder returns a child context carrying r. The Provider will call r.Record() after a successful completion.
Types ¶
type AgentProvider ¶
type AgentProvider interface {
Provider
// CompleteWithTools runs an agentic loop: the model receives tool definitions
// and may call them via execTools before returning a final JSON response.
CompleteWithTools(
ctx context.Context,
systemPrompt, userPrompt, jsonSchema string,
tools []ToolDefinition,
opts Options,
execTools func(context.Context, []ToolCallRequest) ([]ToolCallResult, error),
) (string, error)
}
AgentProvider extends Provider with an agentic tool-calling loop. The model may call tools zero or more times before producing its final JSON response. Usage across all iterations is recorded via the context UsageRecorder.
type BatchJob ¶
type BatchJob struct {
ID string `json:"id"`
Provider string `json:"provider"`
Model string `json:"model,omitempty"`
Status string `json:"status"`
InputFileID string `json:"input_file_id,omitempty"`
OutputFileID string `json:"output_file_id,omitempty"`
ErrorFileID string `json:"error_file_id,omitempty"`
RequestCounts BatchRequestCounts `json:"request_counts"`
CreatedAt *time.Time `json:"created_at,omitempty"`
StartedAt *time.Time `json:"started_at,omitempty"`
CompletedAt *time.Time `json:"completed_at,omitempty"`
FailedAt *time.Time `json:"failed_at,omitempty"`
CancelledAt *time.Time `json:"cancelled_at,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
Done bool `json:"done"`
ResultInline bool `json:"result_inline,omitempty"`
ProviderResponse json.RawMessage `json:"provider_response,omitempty"`
}
BatchJob describes a provider batch at a normalized level.
type BatchOptions ¶
type BatchOptions struct {
// CompletionWindow is the requested provider turnaround window, if supported.
CompletionWindow string
// DisplayName is an optional human-readable label for the batch.
DisplayName string
// Metadata carries provider-supported batch metadata.
Metadata map[string]string
// ForceFile prefers file-backed submission when the provider supports both
// inline and file input modes.
ForceFile bool
}
BatchOptions controls provider batch submission behavior.
type BatchProvider ¶
type BatchProvider interface {
Provider
// SubmitBatch submits a provider-managed asynchronous batch job.
SubmitBatch(ctx context.Context, requests []BatchRequest, opts BatchOptions) (*BatchJob, error)
// GetBatch retrieves the current status for a submitted batch.
GetBatch(ctx context.Context, batchID string) (*BatchJob, error)
// CancelBatch attempts to cancel an in-flight batch.
CancelBatch(ctx context.Context, batchID string) (*BatchJob, error)
// GetBatchResults retrieves all currently available batch results. Providers
// may return partial results for completed, cancelled, or expired batches.
GetBatchResults(ctx context.Context, batchID string) ([]BatchResult, error)
}
BatchProvider extends Provider with asynchronous batch submission support. Implementations should translate each BatchRequest into the provider's native request shape and preserve CustomID in batch results for reconciliation.
type BatchRequest ¶
type BatchRequest struct {
CustomID string
SystemPrompt string
UserPrompt string
JSONSchema string
Options Options
}
BatchRequest is one structured completion request inside an asynchronous batch.
type BatchRequestCounts ¶
type BatchRequestCounts struct {
Total int `json:"total"`
Completed int `json:"completed"`
Failed int `json:"failed"`
}
BatchRequestCounts summarizes request completion status inside a batch.
type BatchResult ¶
type BatchResult struct {
CustomID string `json:"custom_id"`
Output string `json:"output,omitempty"`
Error string `json:"error,omitempty"`
StatusCode int `json:"status_code,omitempty"`
RequestID string `json:"request_id,omitempty"`
Usage *CompletionUsage `json:"usage,omitempty"`
ProviderResponse json.RawMessage `json:"provider_response,omitempty"`
}
BatchResult is one normalized result row from a batch output.
type CompletionUsage ¶
type CompletionUsage struct {
PromptTokens int
CompletionTokens int
TotalTokens int
CachedTokens int
Cost float64 // estimated USD; computed by the provider
}
CompletionUsage holds token usage and cost from a provider completion call.
type Config ¶
type Config struct {
// Provider is the AI provider name (e.g., "openai", "anthropic").
Provider string
// Model is the specific model to use (e.g., "gpt-4o-mini").
Model string
// APIKey is the API key for cloud providers.
APIKey string
// BaseURL is the base URL for the API (optional, provider-specific).
BaseURL string
// Options allows provider-specific configuration.
Options map[string]interface{}
// MaxRetries is the number of retry attempts on transient errors (429, 5xx).
// Zero uses the default (3). Set to -1 to disable retries.
MaxRetries int
}
Config holds configuration for creating a Provider.
type DriverFunc ¶
DriverFunc is a function that creates a new Provider from a Config.
type Options ¶
type Options struct {
// Temperature controls randomness (nil = use provider default).
Temperature *float64
// MaxTokens limits the response length (0 = use provider default).
MaxTokens int
// ImageURL is a publicly accessible URL of an image to include in the user
// message. When set, providers that support vision will send the image
// alongside the text prompt. Empty string means no image.
ImageURL string
}
Options contains per-request tuning knobs.
type Provider ¶
type Provider interface {
// Complete sends a prompt and returns the raw JSON response string.
// jsonSchema constrains the response format (passed to the provider's
// structured output / function calling API).
Complete(ctx context.Context, systemPrompt, userPrompt, jsonSchema string, opts Options) (string, error)
// ProviderName returns the name of the AI provider (e.g., "openai").
ProviderName() string
// ModelName returns the specific model being used (e.g., "gpt-4o-mini").
ModelName() string
// Close releases any resources held by the provider.
Close() error
}
Provider is the generic interface for AI completions with structured output. Implementations must be safe for concurrent use.
type ProviderFactory ¶
type ProviderFactory func(model string) (BatchProvider, error)
ProviderFactory creates a BatchProvider for a given model on demand. Calling the factory at task run time (rather than at startup) means infrequent tasks don't hold a live connection for their entire idle period.
func NewProviderFactory ¶
func NewProviderFactory(providerName, apiKey string) ProviderFactory
NewProviderFactory returns a ProviderFactory that constructs BatchProviders using the given provider name and API key, with the model supplied per-call.
type Schema ¶
type Schema struct {
// Name is a unique identifier for this schema.
Name string
// SystemPrompt is the system message with instructions.
SystemPrompt string
// UserPromptTemplate is a Go text/template for the user message.
UserPromptTemplate string
// JSONSchema is the JSON Schema for structured output.
JSONSchema string
}
Schema defines the prompt template and JSON schema for a completion task.
type ToolCallRequest ¶
type ToolCallRequest struct {
// ID is the provider-assigned call identifier; must be echoed back in ToolCallResult.
ID string
// Name is the tool name, matching a ToolDefinition.Name.
Name string
// ArgsJSON is the JSON-encoded arguments matching the tool's parameter schema.
ArgsJSON string
}
ToolCallRequest is one tool invocation requested by the AI model.
type ToolCallResult ¶
type ToolCallResult struct {
ID string // matches ToolCallRequest.ID
Content string // tool output, typically JSON
}
ToolCallResult is the response to one ToolCallRequest.
type ToolDefinition ¶
type ToolDefinition struct {
Name string
Description string
// Parameters is a JSON Schema object describing the function arguments.
Parameters json.RawMessage
}
ToolDefinition describes a callable function for providers that support tool use.
type UsageRecorder ¶
type UsageRecorder struct {
// contains filtered or unexported fields
}
UsageRecorder captures token usage from a Provider.Complete() call via context. Safe for concurrent use.
func UsageRecorderFromContext ¶
func UsageRecorderFromContext(ctx context.Context) *UsageRecorder
UsageRecorderFromContext retrieves the UsageRecorder from ctx. Returns nil if none was set. Intended for Provider implementations.
func (*UsageRecorder) Record ¶
func (r *UsageRecorder) Record(u CompletionUsage)
Record is called by Provider implementations to report usage after a completion.
func (*UsageRecorder) Usage ¶
func (r *UsageRecorder) Usage() *CompletionUsage
Usage returns the recorded usage, or nil if Complete() has not been called.
Directories
¶
| Path | Synopsis |
|---|---|
|
drivers
|
|
|
otium
Package otium is a driver for Otium, a batch-first inference service that runs open models on low-cost compute behind an OpenAI-compatible API.
|
Package otium is a driver for Otium, a batch-first inference service that runs open models on low-cost compute behind an OpenAI-compatible API. |
|
internal
|
|