Documentation
¶
Overview ¶
Package imageproc normalizes an image once before it enters the conversation history: it detects the format, converts it to a format models accept, and limits its dimensions and size. The framework never calls it; the caller decides which images need it.
An oversized image makes the server reject every later request in the session, not just the failing turn, and modifying an image already in history breaks the prompt cache, so process images when they first enter history and never change them afterwards.
Index ¶
Constants ¶
const ( MIMEPNG = "image/png" MIMEJPEG = "image/jpeg" MIMEGIF = "image/gif" MIMEWebP = "image/webp" )
Inline image formats accepted by all providers; other formats (such as BMP) are converted to PNG.
Variables ¶
var ( // ErrUnsupportedFormat means the data cannot be recognized or decoded as an image. ErrUnsupportedFormat = errors.New("imageproc: unsupported image format") // ErrTooLarge means the image cannot be compressed within the limits. ErrTooLarge = errors.New("imageproc: image cannot be compressed within the limits") )
Functions ¶
func DetectMIME ¶
DetectMIME detects the image format from its magic bytes, ignoring the extension and any declared MIME; it returns "" when unrecognized.
Types ¶
type Result ¶
type Result struct {
Data []byte
MIMEType string
Width int
Height int
// Notes are remarks the model should know, for example that coordinates must be rescaled after the image was shrunk.
Notes []string
}
Result is a normalized image.
func Process ¶
func Process(data []byte, limits chat.ImageLimits) (Result, error)
Process normalizes an image within limits. Zero fields of limits use chat.DefaultImageLimits.
PNG, JPEG, GIF and WebP images within the dimension and size limits are returned unchanged; other formats become PNG; oversized images are scaled down proportionally and encoded as the smaller of PNG and JPEG, lowering quality and size when needed.
func (Result) Block ¶
func (r Result) Block() chat.ContentBlock
Block returns the image block to put in a chat.Message.
func (Result) Blocks ¶
func (r Result) Blocks() []chat.ContentBlock
Blocks returns the image block, followed by a text block when there are notes.