Documentation
¶
Overview ¶
Package ai provides a unified interface for AI provider implementations.
This package is designed to be used by:
- Eval harness (internal/eval_harness/)
- CLI --ai flag (cmd/ailang/)
- std/ai effect (internal/effects/)
Each provider (openai, gemini, anthropic) implements the Provider interface and can be wrapped with Handler for use with the effects system.
Index ¶
- func GetAPIKey(provider ProviderType) (string, error)
- func RequestsImage(req *Request) bool
- type Handler
- func (h *Handler) Call(input string) (string, error)
- func (h *Handler) CallImage(prompt, outputPath, options string) (string, error)
- func (h *Handler) CallImageBase64(prompt, options string) (string, error)
- func (h *Handler) CallJson(input string, schema string) (string, error)
- func (h *Handler) CallWithContext(ctx context.Context, input string) (string, error)
- func (h *Handler) GenerateWithDetails(ctx context.Context, input string) (*Response, error)
- func (h *Handler) Model() string
- func (h *Handler) Provider() Provider
- type HandlerOption
- type ImageOptions
- type ModelConfig
- type Provider
- type ProviderError
- type ProviderType
- type Request
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetAPIKey ¶
func GetAPIKey(provider ProviderType) (string, error)
GetAPIKey returns the API key for a provider from environment variables.
func RequestsImage ¶
RequestsImage returns true if the request asks for image generation.
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler wraps a Provider for use with the effects.AIHandler interface. This bridges the unified AI package with AILANG's effect system.
func NewHandler ¶
func NewHandler(provider Provider, model string, opts ...HandlerOption) *Handler
NewHandler creates a new Handler that wraps a Provider.
The Handler implements effects.AIHandler, allowing any Provider to be used with AILANG's AI effect system.
Example:
client := anthropic.NewClient(apiKey)
handler := ai.NewHandler(client, "claude-sonnet-4-5",
ai.WithSystemPrompt("You are a helpful assistant."),
ai.WithMaxTokens(4096),
)
effCtx.AI = effects.NewAIContext(handler)
func (*Handler) Call ¶
Call implements effects.AIHandler. It sends the input to the provider and returns the generated text.
func (*Handler) CallImage ¶
CallImage generates an image and writes it to outputPath. Options is a JSON string: {"aspect_ratio": "16:9", "mime_type": "image/png"}.
func (*Handler) CallImageBase64 ¶
CallImageBase64 generates an image and returns JSON with base64 data. Returns: {"base64": "...", "mime_type": "image/png"}
func (*Handler) CallJson ¶
CallJson sends a request configured for JSON structured output. If schema is non-empty, providers enforce the schema on the response. If schema is empty, providers return valid JSON without schema enforcement.
Uses at least 8192 max tokens (JSON responses need more room than freeform text). Trims whitespace from the response (some providers pad output to token boundary).
func (*Handler) CallWithContext ¶
CallWithContext is like Call but accepts a context for cancellation/timeout.
func (*Handler) GenerateWithDetails ¶
GenerateWithDetails returns the full response including token counts. This is useful for eval harness and cost tracking.
type HandlerOption ¶
type HandlerOption func(*Handler)
HandlerOption configures a Handler.
func WithMaxTokens ¶
func WithMaxTokens(tokens int) HandlerOption
WithMaxTokens sets the maximum response tokens.
func WithSystemPrompt ¶
func WithSystemPrompt(prompt string) HandlerOption
WithSystemPrompt sets the system prompt for all requests.
type ImageOptions ¶
type ImageOptions struct {
// AspectRatio controls the image aspect ratio (e.g., "1:1", "16:9", "9:16").
AspectRatio string
// MIMEType controls the output format (e.g., "image/png", "image/jpeg").
MIMEType string
}
ImageOptions configures image generation parameters.
type ModelConfig ¶
type ModelConfig struct {
APIName string // API name to send to provider
Provider ProviderType // Provider type
EnvVar string // Environment variable for API key
}
ModelConfig contains provider-specific model configuration. This is a minimal struct for the ai package; full config is in eval_harness.
type Provider ¶
type Provider interface {
// Generate makes a single completion request.
// The context can be used for cancellation and timeouts.
Generate(ctx context.Context, req *Request) (*Response, error)
// Name returns the provider name (e.g., "openai", "gemini", "anthropic")
Name() string
}
Provider interface for AI providers. Each provider (openai, gemini, anthropic) implements this interface.
type ProviderError ¶
type ProviderError struct {
Provider string // Provider name
StatusCode int // HTTP status code (0 if not applicable)
Message string // Error message
Err error // Underlying error (may be nil)
}
ProviderError represents an error from an AI provider.
func NewProviderError ¶
func NewProviderError(provider string, statusCode int, message string, err error) *ProviderError
NewProviderError creates a new ProviderError.
func (*ProviderError) Error ¶
func (e *ProviderError) Error() string
func (*ProviderError) Unwrap ¶
func (e *ProviderError) Unwrap() error
type ProviderType ¶
type ProviderType string
ProviderType represents an AI provider.
const ( ProviderOpenAI ProviderType = "openai" ProviderAnthropic ProviderType = "anthropic" ProviderGoogle ProviderType = "google" ProviderOllama ProviderType = "ollama" )
func GuessProvider ¶
func GuessProvider(modelName string) ProviderType
GuessProvider attempts to determine the provider from a model name. This is used when models.yml is not available.
func ProviderFromString ¶
func ProviderFromString(s string) ProviderType
ProviderFromString converts a string to ProviderType.
func (ProviderType) String ¶
func (p ProviderType) String() string
String returns the string representation of a ProviderType.
type Request ¶
type Request struct {
// Model is the model name (e.g., "gemini-2.5-flash", "gpt-5", "claude-sonnet-4-5")
Model string
// SystemPrompt contains system/developer instructions
SystemPrompt string
// UserPrompt contains the user message
UserPrompt string
// MaxTokens is the maximum number of response tokens (0 = provider default)
MaxTokens int
// Temperature controls randomness (0.0-2.0, 0 = provider default)
Temperature float64
// ResponseFormat controls structured output. Values: "json" or "" (text).
// When set to "json", providers configure their native structured output.
ResponseFormat string
// ResponseSchema is an optional JSON Schema string for structured output.
// When provided with ResponseFormat="json", providers enforce this schema.
// When empty with ResponseFormat="json", providers return valid JSON without schema.
ResponseSchema string
// ResponseModalities controls output types. Values: ["TEXT"], ["IMAGE"], ["TEXT", "IMAGE"].
// When set to ["IMAGE"], providers that support image generation will return image data.
ResponseModalities []string
// ImageOptions configures image generation parameters (used with ResponseModalities containing "IMAGE").
ImageOptions *ImageOptions
// Options contains provider-specific options
Options map[string]any
}
Request represents a generic AI request.
type Response ¶
type Response struct {
// Text is the generated text content
Text string
// ImageData contains raw image bytes (PNG/JPEG) when the response includes an image.
// Nil for text-only responses.
ImageData []byte
// ImageMIME is the MIME type of ImageData (e.g., "image/png").
// Empty for text-only responses.
ImageMIME string
// InputTokens is the number of prompt/input tokens
InputTokens int
// OutputTokens is the number of completion/output tokens
OutputTokens int
// TotalTokens is InputTokens + OutputTokens
TotalTokens int
// ReasonTokens is the number of reasoning tokens (for o1/codex models)
ReasonTokens int
// Model is the model that was actually used (may differ from request)
Model string
}
Response represents a generic AI response.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package anthropic provides an Anthropic Claude API client implementing the ai.Provider interface.
|
Package anthropic provides an Anthropic Claude API client implementing the ai.Provider interface. |
|
Package gemini provides a Google Gemini API client implementing the ai.Provider interface.
|
Package gemini provides a Google Gemini API client implementing the ai.Provider interface. |
|
Package ollama provides an Ollama API client implementing the ai.Provider interface.
|
Package ollama provides an Ollama API client implementing the ai.Provider interface. |
|
Package openai provides an OpenAI API client implementing the ai.Provider interface.
|
Package openai provides an OpenAI API client implementing the ai.Provider interface. |