Documentation
¶
Overview ¶
Package grail provides a unified interface for AI text and image generation across multiple providers (OpenAI, Gemini, etc.). It supports multimodal inputs (ordered sequences of text, images, and PDFs) and provides type-safe error handling, structured logging, and flexible configuration options.
Example usage:
provider, _ := openai.New()
client := grail.NewClient(provider)
res, _ := client.Generate(ctx, grail.Request{
Inputs: []grail.Input{grail.InputText("Hello, world!")},
Output: grail.OutputText(),
})
Sub-packages:
This package provides the core client and interfaces. Provider implementations are available in sub-packages:
- providers - All providers (https://pkg.go.dev/github.com/montanaflynn/grail/providers)
- providers/openai - OpenAI provider (https://pkg.go.dev/github.com/montanaflynn/grail/providers/openai)
- providers/gemini - Google Gemini provider (https://pkg.go.dev/github.com/montanaflynn/grail/providers/gemini)
- providers/mock - Mock provider (https://pkg.go.dev/github.com/montanaflynn/grail/providers/mock)
Index ¶
- Constants
- Variables
- func AsFileInput(input Input) ([]byte, string, string, bool)
- func AsFileReaderInput(input Input) (io.Reader, int64, string, string, bool)
- func AsTextInput(input Input) (string, bool)
- func GetJSONOutput(output Output) (schema any, strict bool, ok bool)
- func IsRateLimited(err error) bool
- func IsRefused(err error) bool
- func IsRetryable(err error) bool
- func IsTextOutput(output Output) bool
- func NewGrailError(code ErrorCode, message string) *grailError
- func Pointer[T any](v T) *T
- func SniffImageMIME(data []byte) string
- type Client
- type ClientOption
- type ErrorCode
- type FileOpt
- type GrailError
- type ImageOutputInfo
- type ImageSpec
- type Input
- func InputFile(data []byte, mime string, opts ...FileOpt) Input
- func InputFileFromPath(path string, opts ...FileOpt) (Input, error)
- func InputFileReader(r io.Reader, size int64, mime string, opts ...FileOpt) Input
- func InputImage(data []byte, opts ...FileOpt) Input
- func InputImageFromPath(path string, opts ...FileOpt) (Input, error)
- func InputPDF(data []byte, opts ...FileOpt) Input
- func InputPDFFromPath(path string, opts ...FileOpt) (Input, error)
- func InputText(s string) Input
- func InputTextFile(text string, mime string, opts ...FileOpt) Input
- type JSONOpt
- type LoggerAware
- type LoggerLevel
- type Model
- type ModelCapabilities
- type ModelCatalog
- type ModelDescriber
- type ModelLister
- type ModelResolver
- type ModelRole
- type ModelTier
- type ModelUse
- type Output
- type OutputPart
- type Provider
- type ProviderExecutor
- type ProviderInfo
- type ProviderOption
- type Request
- type Response
- type Usage
- type Warning
Constants ¶
const ( MaxPDFSize = 50 * 1024 * 1024 // 50 MB MaxFileSize = 100 * 1024 * 1024 // 100 MB )
Variables ¶
var LoggerLevels = map[string]LoggerLevel{ "debug": LoggerLevelDebug, "info": LoggerLevelInfo, "warn": LoggerLevelWarn, "error": LoggerLevelError, }
Functions ¶
func AsFileReaderInput ¶ added in v0.2.0
func AsTextInput ¶ added in v0.2.0
Type assertion helpers for providers
func GetJSONOutput ¶ added in v0.2.0
func IsRateLimited ¶ added in v0.2.0
func IsRetryable ¶ added in v0.2.0
func IsTextOutput ¶ added in v0.2.0
Output type checking helpers for providers
func NewGrailError ¶ added in v0.2.0
func Pointer ¶
func Pointer[T any](v T) *T
Pointer is a helper to take the address of a literal value (e.g., grail.Pointer(0.0)).
func SniffImageMIME ¶ added in v0.2.1
SniffImageMIME detects image MIME type from magic bytes. It supports PNG, JPEG, GIF, and WebP formats.
Types ¶
type Client ¶
type Client interface {
Generate(ctx context.Context, req Request) (Response, error)
// Explicit helpers for loading remote content (HTTP/S only).
// These helpers perform network I/O using the client's HTTP client
// and return concrete Inputs (bytes + MIME).
InputFileFromURI(ctx context.Context, uri string, opts ...FileOpt) (Input, error)
InputImageFromURI(ctx context.Context, uri string, opts ...FileOpt) (Input, error)
InputPDFFromURI(ctx context.Context, uri string, opts ...FileOpt) (Input, error)
// ListModels returns all available models for the provider and their capabilities.
// Returns an error if the provider doesn't support model listing.
ListModels(ctx context.Context) ([]Model, error)
// GetModel returns the model matching the given role and tier.
// Returns an error if no matching model is found.
GetModel(ctx context.Context, role ModelRole, tier ModelTier) (Model, error)
}
func NewClient ¶
func NewClient(p Provider, opts ...ClientOption) Client
type ClientOption ¶
type ClientOption interface {
// contains filtered or unexported methods
}
func WithDownloadLimits ¶ added in v0.2.0
func WithDownloadLimits(maxBytes int64, timeout time.Duration) ClientOption
func WithHTTPClient ¶ added in v0.2.0
func WithHTTPClient(hc *http.Client) ClientOption
func WithLogger ¶
func WithLogger(l *slog.Logger) ClientOption
WithLogger sets a custom logger for client-level logs.
func WithLoggerFormat ¶
func WithLoggerFormat(format string, level LoggerLevel) ClientOption
WithLoggerFormat builds a default logger at the given level and format ("text" or "json"). This is a convenience if you don't want to construct a slog.Logger yourself.
type FileOpt ¶ added in v0.2.0
type FileOpt interface {
// contains filtered or unexported methods
}
func WithFileName ¶ added in v0.2.0
type GrailError ¶ added in v0.2.0
type ImageOutputInfo ¶ added in v0.2.0
ImageOutputInfo contains image data with MIME and optional name.
type ImageSpec ¶ added in v0.2.0
type ImageSpec struct {
Count int // default 1
}
func GetImageSpec ¶ added in v0.2.0
type Input ¶ added in v0.2.0
type Input interface {
// contains filtered or unexported methods
}
func InputFileFromPath ¶ added in v0.2.0
func InputFileReader ¶ added in v0.2.0
func InputImage ¶ added in v0.2.0
func InputImageFromPath ¶ added in v0.2.0
func InputPDFFromPath ¶ added in v0.2.0
type JSONOpt ¶ added in v0.2.0
type JSONOpt interface {
// contains filtered or unexported methods
}
func WithStrictJSON ¶ added in v0.2.0
type LoggerAware ¶
LoggerAware is an optional interface for providers to accept a logger from the client.
type LoggerLevel ¶
LoggerLevel is a small enum for convenience logger construction.
const ( LoggerLevelDebug LoggerLevel = LoggerLevel(slog.LevelDebug) LoggerLevelInfo LoggerLevel = LoggerLevel(slog.LevelInfo) LoggerLevelWarn LoggerLevel = LoggerLevel(slog.LevelWarn) LoggerLevelError LoggerLevel = LoggerLevel(slog.LevelError) )
type Model ¶ added in v0.3.0
type Model struct {
Name string // Model identifier (e.g., "gpt-5.2", "gemini-3-flash-preview")
Role ModelRole // text or image
Tier ModelTier // best or fast
Capabilities ModelCapabilities // What the model can do
}
Model describes a model and its capabilities. Providers export these as package-level variables for easy reference.
type ModelCapabilities ¶ added in v0.3.0
type ModelCapabilities struct {
TextGeneration bool // Can generate text from text input
ImageGeneration bool // Can generate images from text input
ImageUnderstanding bool // Can understand/describe images
PDFUnderstanding bool // Can understand/extract from PDFs
JSONOutput bool // Can output structured JSON
}
ModelCapabilities describes what a model can do.
type ModelCatalog ¶ added in v0.3.0
type ModelCatalog interface {
SetBestTextModel(model Model)
SetFastTextModel(model Model)
SetBestImageModel(model Model)
SetFastImageModel(model Model)
BestTextModel() Model
FastTextModel() Model
BestImageModel() Model
FastImageModel() Model
AllModels() []Model
}
ModelCatalog is an optional interface for providers to manage model selection. Providers implement this to allow users to override default models.
type ModelDescriber ¶ added in v0.3.0
ModelDescriber describes what models will be used for a request. Providers implement this to provide accurate logging when req.Model doesn't fully describe the models (e.g., OpenAI image generation uses both a text model and an image model).
type ModelLister ¶ added in v0.3.0
ModelLister is an optional interface for providers to list available models.
type ModelResolver ¶ added in v0.3.0
ModelResolver resolves a role+tier to a model name. Providers implement this to support tier-based selection.
type ModelRole ¶ added in v0.3.0
type ModelRole string
ModelRole describes the primary function of a model.
type ModelTier ¶ added in v0.3.0
type ModelTier string
ModelTier describes the quality/speed trade-off of a model.
type Output ¶ added in v0.2.0
type Output interface {
// contains filtered or unexported methods
}
func OutputImage ¶ added in v0.2.0
func OutputJSON ¶ added in v0.2.0
func OutputText ¶ added in v0.2.0
func OutputText() Output
type OutputPart ¶ added in v0.2.0
type OutputPart interface {
// contains filtered or unexported methods
}
func NewImageOutputPart ¶ added in v0.2.0
func NewImageOutputPart(data []byte, mime, name string) OutputPart
func NewJSONOutputPart ¶ added in v0.2.0
func NewJSONOutputPart(jsonData []byte) OutputPart
func NewTextOutputPart ¶ added in v0.2.0
func NewTextOutputPart(text string) OutputPart
OutputPart construction helpers for providers
type ProviderExecutor ¶ added in v0.2.0
type ProviderExecutor interface {
Provider
DoGenerate(ctx context.Context, req Request) (Response, error)
}
ProviderExecutor is the internal execution seam (implemented by provider packages). This is exported so provider packages can implement it, but it's not part of the public API contract - users should not implement this directly.
type ProviderInfo ¶ added in v0.2.0
type ProviderOption ¶
type ProviderOption interface {
ApplyProviderOption() // marker method - must be exported for provider packages
}
type Response ¶ added in v0.2.0
type Response struct {
Outputs []OutputPart
Usage Usage
Provider ProviderInfo
RequestID string
Warnings []Warning
}
func (Response) DecodeJSON ¶ added in v0.2.0
func (Response) ImageOutputs ¶ added in v0.2.0
func (r Response) ImageOutputs() []ImageOutputInfo
ImageOutputs returns image output parts with MIME and name information.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
gemini-image-options
command
Gemini-image-options demonstrates Gemini-specific image generation options.
|
Gemini-image-options demonstrates Gemini-specific image generation options. |
|
image-understanding
command
Image-understanding demonstrates text generation from image inputs.
|
Image-understanding demonstrates text generation from image inputs. |
|
model-selection
command
Model-selection demonstrates choosing between best and fast model tiers.
|
Model-selection demonstrates choosing between best and fast model tiers. |
|
openai-image-options
command
Openai-image-options demonstrates OpenAI-specific image generation options.
|
Openai-image-options demonstrates OpenAI-specific image generation options. |
|
pdf-to-image
command
Pdf-to-image demonstrates image generation from PDF documents.
|
Pdf-to-image demonstrates image generation from PDF documents. |
|
pdf-understanding
command
Pdf-understanding demonstrates text generation from PDF documents.
|
Pdf-understanding demonstrates text generation from PDF documents. |
|
simple-text
command
Simple-text demonstrates minimal text generation with default settings.
|
Simple-text demonstrates minimal text generation with default settings. |
|
text-generation
command
Text-generation demonstrates text generation with provider selection.
|
Text-generation demonstrates text generation with provider selection. |
|
text-to-image
command
Text-to-image demonstrates image generation from text prompts.
|
Text-to-image demonstrates image generation from text prompts. |
|
providers
|
|
|
gemini
Package gemini provides a Google Gemini implementation of the grail.Provider interface.
|
Package gemini provides a Google Gemini implementation of the grail.Provider interface. |
|
mock
Package mock provides a test double implementation of the grail.Provider interface.
|
Package mock provides a test double implementation of the grail.Provider interface. |
|
openai
Package openai provides an OpenAI implementation of the grail.Provider interface.
|
Package openai provides an OpenAI implementation of the grail.Provider interface. |