Skip to content

Images ​

@xsai/image generates images from a prompt. It needs a service with an images/generations endpoint that returns base64 data.

sh
pnpm add @xsai/image
ts
import { 
writeFile
} from 'node:fs/promises'
import {
generateImage
,
generations
} from '@xsai/image'
const
model
=
generations
({
apiKey
:
process
.
env
.
OPENAI_API_KEY
,
baseURL
: 'https://api.openai.com/v1/',
model
: 'YOUR_IMAGE_MODEL_ID',
}) const {
image
} = await
generateImage
(
model
, {
input
: 'A small cabin in a quiet forest.' })
await
writeFile
('cabin.png', new
Uint8Array
(await
image
.
arrayBuffer
()))

generateImage returns { image, images }. image is a Blob with the first picture, and its type is the detected media type, for example image/png. The adapter reads PNG, JPEG, GIF, WebP, and AVIF.

Reference ​

OptionDescription
inputRequired. The prompt.
nThe number of images. The endpoint decides the default and the limit.
sizeA string like 1024x1024. The endpoint decides which sizes it accepts.
providerOptions.generationsbackground, outputCompression, outputFormat, and quality.
signalAn AbortSignal.
providerOptions.generations fieldValues
backgroundauto, opaque, or transparent.
outputCompressionA number that the provider accepts.
outputFormatjpeg, png, or webp.
qualityauto, high, low, max, medium, or xhigh.

generations({ baseURL, model, apiKey?, headers?, fetch? }) sends POST images/generations. The adapter does not download image URLs from a response. If the service returns revised prompts, they are in providerMetadata.generations.images, in the order of the images.

An empty image list, invalid base64, or an unknown image format throws invalid-response. HTTP failures throw HttpError, and network failures throw network-error.

Contributors

Changelog