|
| 1 | +# call-ai Image Generation API Documentation |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The call-ai library includes image generation capabilities through the `imageGen` function, which integrates with a custom image generation API. This functionality supports both simple image generation from text prompts and image editing with multiple input images. |
| 6 | + |
| 7 | +## Getting Started |
| 8 | + |
| 9 | +Import the `imageGen` function: |
| 10 | + |
| 11 | +```javascript |
| 12 | +import { imageGen } from 'call-ai'; |
| 13 | +``` |
| 14 | + |
| 15 | +## Basic Usage |
| 16 | + |
| 17 | +### Simple Image Generation |
| 18 | + |
| 19 | +Generate an image from a text prompt: |
| 20 | + |
| 21 | +```javascript |
| 22 | +const generateResponse = await imageGen( |
| 23 | + 'A children\'s book drawing of a veterinarian using a stethoscope to listen to the heartbeat of a baby otter.' |
| 24 | +); |
| 25 | + |
| 26 | +// Access the base64-encoded image |
| 27 | +const imageBase64 = generateResponse.data[0].b64_json; |
| 28 | + |
| 29 | +// Use in an HTML element |
| 30 | +const imgElement = document.createElement('img'); |
| 31 | +imgElement.src = `data:image/png;base64,${imageBase64}`; |
| 32 | +document.body.appendChild(imgElement); |
| 33 | + |
| 34 | +// Error handling is built-in |
| 35 | +try { |
| 36 | + const result = await imageGen('Your prompt'); |
| 37 | +} catch (error) { |
| 38 | + // All errors are proper Error objects with descriptive messages |
| 39 | + console.error(error.message); |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +### Image Editing with Multiple Input Images |
| 44 | + |
| 45 | +Edit or combine multiple images with a text prompt: |
| 46 | + |
| 47 | +```javascript |
| 48 | +// Get image files (e.g., from a file input) |
| 49 | +const imageFiles = [bathBombFile, bodyLotionFile, incenseKitFile, soapFile]; |
| 50 | + |
| 51 | +// Request image editing |
| 52 | +// Uses the same async pattern as callAI for consistency |
| 53 | +const editResponse = await imageGen( |
| 54 | + 'Create a lovely gift basket with these four items in it', |
| 55 | + { |
| 56 | + model: 'gpt-image-1', |
| 57 | + images: imageFiles, |
| 58 | + size: '1024x1024', // optional |
| 59 | + quality: 'hd', // optional |
| 60 | + style: 'natural' // optional |
| 61 | + } |
| 62 | +); |
| 63 | + |
| 64 | +// Access the base64-encoded edited image |
| 65 | +const editedImageBase64 = editResponse.data[0].b64_json; |
| 66 | + |
| 67 | +// Use in an HTML element |
| 68 | +const editedImg = document.createElement('img'); |
| 69 | +editedImg.src = `data:image/png;base64,${editedImageBase64}`; |
| 70 | +document.body.appendChild(editedImg); |
| 71 | +``` |
| 72 | + |
| 73 | +## API Reference |
| 74 | + |
| 75 | +### `imageGen(prompt, options)` |
| 76 | + |
| 77 | +Generates or edits images based on a text prompt and optional images. |
| 78 | + |
| 79 | +#### Parameters |
| 80 | + |
| 81 | +- `prompt` (string, required): Text prompt describing the desired image |
| 82 | +- `options` (object, optional): Configuration options |
| 83 | + - `model` (string, optional): Model to use for image generation, defaults to 'gpt-image-1' |
| 84 | + - `apiKey` (string, optional): API key, defaults to 'VIBES_DIY' |
| 85 | + - `images` (File[], optional): Array of File objects to edit, if provided uses the edit endpoint |
| 86 | + - `size` (string, optional): Size of the generated image (Must be one of 1024x1024, 1536x1024 (landscape), 1024x1536 (portrait), or 'auto' (default value) for gpt-image-1.) |
| 87 | + - `quality` (string, optional): Quality of the generated image ( high | medium | low . Defaults to auto.) |
| 88 | + - `style` (string, optional): Style of the generated image (e.g., 'vivid', 'natural'). Note: Style parameter may have limited effect depending on the model. |
| 89 | + - `debug` (boolean, optional): Enable debug logging, defaults to false |
| 90 | + |
| 91 | +#### Returns |
| 92 | + |
| 93 | +Returns a Promise that resolves to an `ImageResponse` object: |
| 94 | + |
| 95 | +```typescript |
| 96 | +interface ImageResponse { |
| 97 | + created: number; |
| 98 | + data: { |
| 99 | + b64_json: string; |
| 100 | + url?: string; |
| 101 | + revised_prompt?: string; |
| 102 | + }[]; |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +## Implementation Details |
| 107 | + |
| 108 | +The image generation functionality uses two custom API endpoints: |
| 109 | + |
| 110 | +- `/api/openai-image/generate`: For simple image generation with a text prompt |
| 111 | +- `/api/openai-image/edit`: For editing/combining multiple images with a text prompt |
| 112 | + |
| 113 | +These endpoints return base64-encoded image data that can be directly used in HTML `<img>` tags or saved as files. All API calls use proper async/await patterns for consistent error handling across the library. |
| 114 | + |
| 115 | +## Error Handling |
| 116 | + |
| 117 | +The `imageGen` function includes proper error handling and will throw Error objects with descriptive messages, consistent with the rest of the call-ai library: |
| 118 | + |
| 119 | +```javascript |
| 120 | +try { |
| 121 | + const result = await imageGen('An impossible prompt', { debug: true }); |
| 122 | + // Use the result... |
| 123 | +} catch (error) { |
| 124 | + // Error objects include useful properties |
| 125 | + console.error('Image generation failed:', error.message); |
| 126 | + |
| 127 | + // Access additional properties if available |
| 128 | + if ('status' in error) console.error('Status code:', error.status); |
| 129 | + if ('errorType' in error) console.error('Error type:', error.errorType); |
| 130 | + if ('details' in error) console.error('Details:', error.details); |
| 131 | + |
| 132 | + // Handle the error appropriately |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +## Helper Functions |
| 137 | + |
| 138 | +### Converting Base64 to File |
| 139 | + |
| 140 | +To save the generated image as a file: |
| 141 | + |
| 142 | +```javascript |
| 143 | +function base64ToFile(base64Data, filename = 'generated-image.png', mimeType = 'image/png') { |
| 144 | + // Remove data URL prefix if present |
| 145 | + const base64Content = base64Data.includes('base64,') |
| 146 | + ? base64Data.split('base64,')[1] |
| 147 | + : base64Data; |
| 148 | + |
| 149 | + // Convert base64 to binary |
| 150 | + const binaryStr = atob(base64Content); |
| 151 | + |
| 152 | + // Create array buffer |
| 153 | + const bytes = new Uint8Array(binaryStr.length); |
| 154 | + for (let i = 0; i < binaryStr.length; i++) { |
| 155 | + bytes[i] = binaryStr.charCodeAt(i); |
| 156 | + } |
| 157 | + |
| 158 | + // Create blob and file |
| 159 | + const blob = new Blob([bytes], { type: mimeType }); |
| 160 | + return new File([blob], filename, { type: mimeType }); |
| 161 | +} |
| 162 | + |
| 163 | +// Usage |
| 164 | +const imageFile = base64ToFile(imageResponse.data[0].b64_json, 'my-image.png'); |
| 165 | +``` |
| 166 | + |
| 167 | +## Browser Compatibility |
| 168 | + |
| 169 | +This functionality is designed to work in modern browsers and requires: |
| 170 | + |
| 171 | +- `fetch` API |
| 172 | +- `FormData` API |
| 173 | +- `File` and `Blob` APIs |
| 174 | +- Support for async/await |
| 175 | + |
| 176 | +These are available in all modern browsers without polyfills. |
| 177 | + |
| 178 | +## TypeScript Support |
| 179 | + |
| 180 | +The `imageGen` function is fully typed with TypeScript interfaces: |
| 181 | + |
| 182 | +```typescript |
| 183 | +// Input options interface |
| 184 | +interface ImageGenOptions { |
| 185 | + apiKey?: string; |
| 186 | + model?: string; |
| 187 | + images?: File[]; |
| 188 | + size?: string; |
| 189 | + quality?: string; |
| 190 | + style?: string; |
| 191 | + debug?: boolean; |
| 192 | +} |
| 193 | + |
| 194 | +// Response interface |
| 195 | +interface ImageResponse { |
| 196 | + created: number; |
| 197 | + data: { |
| 198 | + b64_json: string; |
| 199 | + url?: string; |
| 200 | + revised_prompt?: string; |
| 201 | + }[]; |
| 202 | +} |
| 203 | + |
| 204 | +// Function signature |
| 205 | +declare function imageGen(prompt: string, options?: ImageGenOptions): Promise<ImageResponse>; |
| 206 | +``` |
0 commit comments