Skip to main content

ImageModel Interface

Interface that all image generation model implementations must satisfy.

Interface Definition​

type ImageModel interface {
// Metadata
SpecificationVersion() string
Provider() string
ModelID() string

// Image generation
DoGenerate(ctx context.Context, opts *ImageGenerateOptions) (*types.ImageResult, error)
}

Methods​

MethodParametersReturnsDescription
SpecificationVersion()-stringSpecification version
Provider()-stringProvider name
ModelID()-stringModel identifier
DoGenerate()ctx, *ImageGenerateOptions*types.ImageResult, errorGenerate images

Optional Capability Methods​

A model may optionally implement these methods to advertise whether it consumes ImageGenerateOptions.Files / .Mask for image editing. They are not part of the ImageModel interface itself, so implementing them is optional; callers read them through provider.ImageModelSupportsFileInputs / provider.ImageModelSupportsMaskInputs, which return nil (unknown) for a model that doesn't implement the method:

SupportsFileInputs() *bool // nil means unknown; only route file-input edits when this is non-nil true
SupportsMaskInputs() *bool // nil means unknown; advertised separately since some models accept files without masks

middleware.WrapImageModel preserves these from the wrapped model and lets an ImageModelMiddleware override either one via OverrideSupportsFileInputs / OverrideSupportsMaskInputs.

ImageGenerateOptions​

type ImageGenerateOptions struct {
Prompt string
N *int
Size string
Quality string
Style string
}

Examples​

Using an Image Model​

package main

import (
"context"
"log"
"os"

"github.com/digitallysavvy/go-ai/pkg/provider"
"github.com/digitallysavvy/go-ai/pkg/providers/openai"
)

func main() {
p := openai.New(openai.Config{
APIKey: "your-api-key",
})
model, err := p.ImageModel("dall-e-3")
if err != nil {
log.Fatal(err)
}

result, err := model.DoGenerate(context.Background(), &provider.ImageGenerateOptions{
Prompt: "A serene mountain landscape",
Size: "1024x1024",
})
if err != nil {
log.Fatal(err)
}

// Save first image
if len(result.Images) > 0 {
os.WriteFile("output.png", result.Images[0], 0644)
}
}

See Also​