@j-o-r/hello-dave 0.0.10 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/README.md.bak.1779452127 +240 -0
- package/TODO.md +30 -8
- package/agents/code_agent.js +6 -6
- package/agents/daisy_agent.js +10 -7
- package/agents/minimax.js +173 -0
- package/agents/stability.js +173 -0
- package/bin/codeDave +1 -1
- package/bin/dave.js +1 -1
- package/docs/music-toolsets.md +137 -0
- package/docs/plans/minimax-music-generation.md +80 -0
- package/docs/plans/unified-agent-architecture.md +146 -0
- package/docs/plans/websocket-streaming-plan.md.bak +317 -0
- package/docs/prompt/task_clarification_and_documentation.md +35 -0
- package/lib/API/minimax/ImageToolset.js +169 -0
- package/lib/API/minimax/MusicToolset.js +290 -0
- package/lib/API/minimax/VideoToolset.js +296 -0
- package/lib/API/minimax/image.generation.md +239 -0
- package/lib/API/minimax/image.js +219 -0
- package/lib/API/minimax/image.to.image.md +257 -0
- package/lib/API/minimax/index.js +16 -0
- package/lib/API/minimax/music.cover.preprocess.md +206 -0
- package/lib/API/minimax/music.generation.md +346 -0
- package/lib/API/minimax/music.js +257 -0
- package/lib/API/minimax/music.lyrics.generation.md +205 -0
- package/lib/API/minimax/video.download.md +133 -0
- package/lib/API/minimax/video.first.last.image.md +186 -0
- package/lib/API/minimax/video.from.image.md +206 -0
- package/lib/API/minimax/video.from.subject.md +164 -0
- package/lib/API/minimax/video.generation.md +192 -0
- package/lib/API/minimax/video.js +339 -0
- package/lib/API/minimax/video.query.md +128 -0
- package/lib/API/stability.ai/ImageToolset.js +357 -0
- package/lib/API/stability.ai/MusicToolset.js +302 -0
- package/lib/API/stability.ai/audio-3.md +205 -0
- package/lib/API/stability.ai/audio.js +679 -0
- package/lib/API/stability.ai/image.js +911 -0
- package/lib/API/stability.ai/image.md +271 -0
- package/lib/API/stability.ai/index.js +11 -0
- package/lib/API/stability.ai/openapi.json +17118 -0
- package/lib/API/x.ai/ImageToolset.js +165 -0
- package/lib/API/x.ai/image.editing.md +86 -0
- package/lib/API/x.ai/image.js +393 -0
- package/lib/API/x.ai/image.md +213 -0
- package/lib/API/x.ai/image.to.generation.md +494 -0
- package/lib/API/x.ai/image.to.video.md +23 -0
- package/lib/API/x.ai/index.js +9 -0
- package/lib/AgentManager.js +1 -1
- package/lib/CdnToolset.js +191 -0
- package/lib/ToolSet.js +19 -1
- package/lib/cdn.js +373 -0
- package/lib/fafs.js +3 -1
- package/lib/genericToolset.js +43 -166
- package/lib/index.js +9 -1
- package/package.json +2 -2
- package/types/API/minimax/ImageToolset.d.ts +3 -0
- package/types/API/minimax/MusicToolset.d.ts +3 -0
- package/types/API/minimax/VideoToolset.d.ts +3 -0
- package/types/API/minimax/image.d.ts +109 -0
- package/types/API/minimax/index.d.ts +15 -0
- package/types/API/minimax/music.d.ts +46 -0
- package/types/API/minimax/video.d.ts +165 -0
- package/types/API/stability.ai/ImageToolset.d.ts +3 -0
- package/types/API/stability.ai/MusicToolset.d.ts +3 -0
- package/types/API/stability.ai/audio.d.ts +193 -0
- package/types/API/stability.ai/image.d.ts +274 -0
- package/types/API/stability.ai/index.d.ts +11 -0
- package/types/API/x.ai/ImageToolset.d.ts +3 -0
- package/types/API/x.ai/image.d.ts +82 -0
- package/types/API/x.ai/index.d.ts +9 -0
- package/types/AgentManager.d.ts +1 -1
- package/types/CdnToolset.d.ts +20 -0
- package/types/ToolSet.d.ts +8 -0
- package/types/cdn.d.ts +141 -0
- package/types/index.d.ts +8 -2
- package/docs/multi-agent-clusters.md.bak +0 -229
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
#### Model Capabilities
|
|
2
|
+
|
|
3
|
+
# Imagine Overview
|
|
4
|
+
|
|
5
|
+
The Imagine API lets you generate and edit images and videos with Grok Imagine models. Use it for image generation, image editing with up to 3 reference images, video generation from text or still images, video editing, and more.
|
|
6
|
+
|
|
7
|
+
## Pricing
|
|
8
|
+
|
|
9
|
+
Image generation uses flat per-image pricing regardless of prompt length. Each generated image incurs a fixed fee. Image edits are billed for both the input image and the generated output image. Video generation uses per-second pricing where both duration and resolution affect the total cost. For full pricing details, see the [models page](/developers/models#imagine-pricing).
|
|
10
|
+
|
|
11
|
+
## Image Editing
|
|
12
|
+
|
|
13
|
+
Edit a source image with natural language. Provide a public image URL or base64-encoded data URI, then describe the change you want Grok Imagine to apply. Multi-image editing supports up to 3 source images in a single request for combining subjects, transferring styles, and composing scenes.
|
|
14
|
+
|
|
15
|
+
```python customLanguage="pythonXAI"
|
|
16
|
+
import base64
|
|
17
|
+
import xai_sdk
|
|
18
|
+
|
|
19
|
+
client = xai_sdk.Client()
|
|
20
|
+
|
|
21
|
+
# Load image from file and encode as base64
|
|
22
|
+
with open("photo.png", "rb") as f:
|
|
23
|
+
image_data = base64.b64encode(f.read()).decode("utf-8")
|
|
24
|
+
|
|
25
|
+
response = client.image.sample(
|
|
26
|
+
prompt="Render this as a pencil sketch with detailed shading",
|
|
27
|
+
model="grok-imagine-image-quality",
|
|
28
|
+
image_url=f"data:image/png;base64,{image_data}",
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
print(response.url)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
# Using a public URL as the source image
|
|
36
|
+
curl -X POST https://api.x.ai/v1/images/edits \
|
|
37
|
+
-H "Content-Type: application/json" \
|
|
38
|
+
-H "Authorization: Bearer $XAI_API_KEY" \
|
|
39
|
+
-d '{
|
|
40
|
+
"model": "grok-imagine-image-quality",
|
|
41
|
+
"prompt": "Render this as a pencil sketch with detailed shading",
|
|
42
|
+
"image": {
|
|
43
|
+
"url": "https://docs.x.ai/assets/api-examples/images/style-realistic.png",
|
|
44
|
+
"type": "image_url"
|
|
45
|
+
}
|
|
46
|
+
}'
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
```javascript customLanguage="javascriptAISDK"
|
|
50
|
+
import { xai } from "@ai-sdk/xai";
|
|
51
|
+
import { generateImage } from "ai";
|
|
52
|
+
import fs from "fs";
|
|
53
|
+
|
|
54
|
+
// Load image and encode as base64
|
|
55
|
+
const imageBuffer = fs.readFileSync("photo.png");
|
|
56
|
+
const base64Image = imageBuffer.toString("base64");
|
|
57
|
+
|
|
58
|
+
const { image } = await generateImage({
|
|
59
|
+
model: xai.image("grok-imagine-image-quality"),
|
|
60
|
+
prompt: {
|
|
61
|
+
text: "Render this as a pencil sketch with detailed shading",
|
|
62
|
+
images: [`data:image/png;base64,${base64Image}`],
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
console.log(image.base64);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Image Generation
|
|
70
|
+
|
|
71
|
+
Generate new images from text prompts with Grok Imagine models. Configure output count (up to 10 images per request), aspect ratio, resolution, and response format.
|
|
72
|
+
|
|
73
|
+
```python customLanguage="pythonXAI"
|
|
74
|
+
import xai_sdk
|
|
75
|
+
|
|
76
|
+
client = xai_sdk.Client()
|
|
77
|
+
|
|
78
|
+
response = client.image.sample(
|
|
79
|
+
prompt="A collage of London landmarks in a stenciled street‑art style",
|
|
80
|
+
model="grok-imagine-image-quality",
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
print(response.url)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
curl -X POST https://api.x.ai/v1/images/generations \
|
|
88
|
+
-H "Content-Type: application/json" \
|
|
89
|
+
-H "Authorization: Bearer $XAI_API_KEY" \
|
|
90
|
+
-d '{
|
|
91
|
+
"model": "grok-imagine-image-quality",
|
|
92
|
+
"prompt": "A collage of London landmarks in a stenciled street‑art style"
|
|
93
|
+
}'
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
```python customLanguage="pythonOpenAISDK"
|
|
97
|
+
from openai import OpenAI
|
|
98
|
+
|
|
99
|
+
client = OpenAI(
|
|
100
|
+
base_url="https://api.x.ai/v1",
|
|
101
|
+
api_key="YOUR_API_KEY",
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
response = client.images.generate(
|
|
105
|
+
model="grok-imagine-image-quality",
|
|
106
|
+
prompt="A collage of London landmarks in a stenciled street‑art style",
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
print(response.data[0].url)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
```javascript customLanguage="javascriptAISDK"
|
|
113
|
+
import { xai } from "@ai-sdk/xai";
|
|
114
|
+
import { generateImage } from "ai";
|
|
115
|
+
|
|
116
|
+
const { image } = await generateImage({
|
|
117
|
+
model: xai.image("grok-imagine-image-quality"),
|
|
118
|
+
prompt: "A collage of London landmarks in a stenciled street‑art style",
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
console.log(image.base64);
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Image-to-Video
|
|
125
|
+
|
|
126
|
+
Animate a still image with a text prompt. The source image becomes the starting point for the generated video. Video requests are asynchronous: start a request, poll with the returned request ID, and use the completed video URL when ready. The xAI SDK and AI SDK handle polling for you.
|
|
127
|
+
|
|
128
|
+
```python customLanguage="pythonXAI"
|
|
129
|
+
import os
|
|
130
|
+
import xai_sdk
|
|
131
|
+
|
|
132
|
+
client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))
|
|
133
|
+
|
|
134
|
+
response = client.video.generate(
|
|
135
|
+
prompt="Make the water crash down and slowly pan out the camera",
|
|
136
|
+
model="grok-imagine-video",
|
|
137
|
+
image_url="https://docs.x.ai/assets/api-examples/video/waterfall-still.png",
|
|
138
|
+
duration=12,
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
print(response.url)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
```javascript customLanguage="javascriptAISDK"
|
|
145
|
+
import { xai } from "@ai-sdk/xai";
|
|
146
|
+
import { experimental_generateVideo as generateVideo } from "ai";
|
|
147
|
+
|
|
148
|
+
const result = await generateVideo({
|
|
149
|
+
model: xai.video("grok-imagine-video"),
|
|
150
|
+
prompt: {
|
|
151
|
+
image: "https://docs.x.ai/assets/api-examples/video/waterfall-still.png",
|
|
152
|
+
text: "Make the water crash down and slowly pan out the camera",
|
|
153
|
+
},
|
|
154
|
+
duration: 12,
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
const videoUrl = result.providerMetadata?.xai?.videoUrl;
|
|
158
|
+
console.log(videoUrl);
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
# Start the video generation request
|
|
163
|
+
REQUEST_ID=$(curl -s -X POST https://api.x.ai/v1/videos/generations \
|
|
164
|
+
-H "Content-Type: application/json" \
|
|
165
|
+
-H "Authorization: Bearer $XAI_API_KEY" \
|
|
166
|
+
-d '{
|
|
167
|
+
"model": "grok-imagine-video",
|
|
168
|
+
"prompt": "Make the water crash down and slowly pan out the camera",
|
|
169
|
+
"image": {"url": "https://docs.x.ai/assets/api-examples/video/waterfall-still.png"},
|
|
170
|
+
"duration": 12
|
|
171
|
+
}' | jq -r '.request_id')
|
|
172
|
+
|
|
173
|
+
# Poll until the video is ready
|
|
174
|
+
while true; do
|
|
175
|
+
RESULT=$(curl -s https://api.x.ai/v1/videos/$REQUEST_ID \
|
|
176
|
+
-H "Authorization: Bearer $XAI_API_KEY")
|
|
177
|
+
STATUS=$(echo "$RESULT" | jq -r '.status')
|
|
178
|
+
if [ "$STATUS" = "done" ]; then
|
|
179
|
+
echo "$RESULT" | jq -r '.video.url'
|
|
180
|
+
break
|
|
181
|
+
elif [ "$STATUS" = "failed" ] || [ "$STATUS" = "expired" ]; then
|
|
182
|
+
echo "Request $STATUS"; echo "$RESULT" | jq .
|
|
183
|
+
break
|
|
184
|
+
fi
|
|
185
|
+
sleep 5
|
|
186
|
+
done
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## More Capabilities
|
|
190
|
+
|
|
191
|
+
Beyond the top use cases above, the Imagine API supports several additional workflows:
|
|
192
|
+
|
|
193
|
+
* **[Multi-Image Editing](/developers/model-capabilities/images/multi-image-editing)** — Combine up to 3 source images in a single edit for compositing subjects, transferring styles, and building scenes from multiple references.
|
|
194
|
+
* **[Video Generation](/developers/model-capabilities/video/generation)** — Generate videos from text prompts with configurable duration (up to 15s), aspect ratio, and resolution.
|
|
195
|
+
* **[Video Editing](/developers/model-capabilities/video/editing)** — Modify an existing video with a text prompt while preserving the rest of the scene.
|
|
196
|
+
* **[Reference-to-Video](/developers/model-capabilities/video/reference-to-video)** — Guide a generated video with one or more reference images that influence the output without forcing the first frame.
|
|
197
|
+
* **[Video Extension](/developers/model-capabilities/video/extension)** — Continue an existing video from its last frame, combining the original and extension into one clip.
|
|
198
|
+
|
|
199
|
+
## Enterprise Compliance & Security
|
|
200
|
+
|
|
201
|
+
The Imagine APIs are built for production workloads with strict security and compliance requirements. Generated media is subject to content policy review and is not used for training.
|
|
202
|
+
|
|
203
|
+
* **SOC 2 Type II** — Audited controls for security, availability, and confidentiality
|
|
204
|
+
|
|
205
|
+
* **HIPAA Eligible** — BAA available for healthcare applications handling PHI
|
|
206
|
+
|
|
207
|
+
* **GDPR Compliant** — Data processing agreements and EU data residency options
|
|
208
|
+
|
|
209
|
+
* **Data Residency** — Regional processing for compliance requirements
|
|
210
|
+
|
|
211
|
+
* **High Availability** — Multi-region infrastructure with custom SLAs for enterprise workloads
|
|
212
|
+
|
|
213
|
+
* **SSO & RBAC** — SAML SSO, role-based access, and audit logging
|
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
#### Model Capabilities
|
|
2
|
+
|
|
3
|
+
# Image Generation
|
|
4
|
+
|
|
5
|
+
Generate images from text prompts with Grok Imagine models. The API supports batch generation of multiple images, and control over aspect ratio and resolution.
|
|
6
|
+
|
|
7
|
+
> [!WARNING]
|
|
8
|
+
>
|
|
9
|
+
> **`grok-imagine-image-pro` will be deprecated as of May 15, 2026.** Use `grok-imagine-image-quality` for all new image generation requests. Existing `-pro` requests will continue to work during a transition period, but we recommend migrating promptly.
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
Generate an image with a single API call:
|
|
14
|
+
|
|
15
|
+
```python customLanguage="pythonXAI"
|
|
16
|
+
import xai_sdk
|
|
17
|
+
|
|
18
|
+
client = xai_sdk.Client()
|
|
19
|
+
|
|
20
|
+
response = client.image.sample(
|
|
21
|
+
prompt="A collage of London landmarks in a stenciled street‑art style",
|
|
22
|
+
model="grok-imagine-image-quality",
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
print(response.url)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
curl -X POST https://api.x.ai/v1/images/generations \
|
|
30
|
+
-H "Content-Type: application/json" \
|
|
31
|
+
-H "Authorization: Bearer $XAI_API_KEY" \
|
|
32
|
+
-d '{
|
|
33
|
+
"model": "grok-imagine-image-quality",
|
|
34
|
+
"prompt": "A collage of London landmarks in a stenciled street‑art style"
|
|
35
|
+
}'
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```python customLanguage="pythonOpenAISDK"
|
|
39
|
+
from openai import OpenAI
|
|
40
|
+
|
|
41
|
+
client = OpenAI(
|
|
42
|
+
base_url="https://api.x.ai/v1",
|
|
43
|
+
api_key="YOUR_API_KEY",
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
response = client.images.generate(
|
|
47
|
+
model="grok-imagine-image-quality",
|
|
48
|
+
prompt="A collage of London landmarks in a stenciled street‑art style",
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
print(response.data[0].url)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
```javascript customLanguage="javascriptOpenAISDK"
|
|
55
|
+
import OpenAI from "openai";
|
|
56
|
+
|
|
57
|
+
const client = new OpenAI({
|
|
58
|
+
apiKey: process.env.XAI_API_KEY,
|
|
59
|
+
baseURL: 'https://api.x.ai/v1',
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const response = await client.images.generate({
|
|
63
|
+
model: "grok-imagine-image-quality",
|
|
64
|
+
prompt: "A collage of London landmarks in a stenciled street‑art style",
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
console.log(response.data[0].url);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
```javascript customLanguage="javascriptAISDK"
|
|
71
|
+
import { xai } from "@ai-sdk/xai";
|
|
72
|
+
import { generateImage } from "ai";
|
|
73
|
+
|
|
74
|
+
const { image } = await generateImage({
|
|
75
|
+
model: xai.image("grok-imagine-image-quality"),
|
|
76
|
+
prompt: "A collage of London landmarks in a stenciled street‑art style",
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
console.log(image.base64);
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Images are returned as URLs by default. URLs are temporary, so download or process promptly. You can also request [base64 output](#base64-output) for embedding images directly.
|
|
83
|
+
|
|
84
|
+
## Configuration
|
|
85
|
+
|
|
86
|
+
### Multiple Images
|
|
87
|
+
|
|
88
|
+
Generate multiple images in a single request using the `sample_batch()` method and the `n` parameter. This returns a list of `ImageResponse` objects.
|
|
89
|
+
|
|
90
|
+
```python customLanguage="pythonXAI"
|
|
91
|
+
import xai_sdk
|
|
92
|
+
|
|
93
|
+
client = xai_sdk.Client()
|
|
94
|
+
|
|
95
|
+
responses = client.image.sample_batch(
|
|
96
|
+
prompt="A futuristic city skyline at night",
|
|
97
|
+
model="grok-imagine-image-quality",
|
|
98
|
+
n=4,
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
for i, image in enumerate(responses):
|
|
102
|
+
print(f"Variation {i + 1}: {image.url}")
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
```python customLanguage="pythonOpenAISDK"
|
|
106
|
+
from openai import OpenAI
|
|
107
|
+
|
|
108
|
+
client = OpenAI(
|
|
109
|
+
base_url="https://api.x.ai/v1",
|
|
110
|
+
api_key="YOUR_API_KEY",
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
response = client.images.generate(
|
|
114
|
+
model="grok-imagine-image-quality",
|
|
115
|
+
prompt="A futuristic city skyline at night",
|
|
116
|
+
n=4,
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
for i, image in enumerate(response.data):
|
|
120
|
+
print(f"Variation {i + 1}: {image.url}")
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
```javascript customLanguage="javascriptOpenAISDK"
|
|
124
|
+
import OpenAI from "openai";
|
|
125
|
+
|
|
126
|
+
const client = new OpenAI({
|
|
127
|
+
apiKey: process.env.XAI_API_KEY,
|
|
128
|
+
baseURL: "https://api.x.ai/v1",
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const response = await client.images.generate({
|
|
132
|
+
model: "grok-imagine-image-quality",
|
|
133
|
+
prompt: "A futuristic city skyline at night",
|
|
134
|
+
n: 4,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
response.data.forEach((image, i) => {
|
|
138
|
+
console.log(`Variation ${i + 1}: ${image.url}`);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
```javascript customLanguage="javascriptAISDK"
|
|
144
|
+
import { xai } from "@ai-sdk/xai";
|
|
145
|
+
import { generateImage } from "ai";
|
|
146
|
+
|
|
147
|
+
const { images } = await generateImage({
|
|
148
|
+
model: xai.image("grok-imagine-image-quality"),
|
|
149
|
+
prompt: "A futuristic city skyline at night",
|
|
150
|
+
n: 4,
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
images.forEach((image, i) => {
|
|
154
|
+
console.log(`Variation ${i + 1}: ${image.base64.slice(0, 50)}...`);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
curl -X POST https://api.x.ai/v1/images/generations \
|
|
161
|
+
-H "Content-Type: application/json" \
|
|
162
|
+
-H "Authorization: Bearer $XAI_API_KEY" \
|
|
163
|
+
-d '{
|
|
164
|
+
"model": "grok-imagine-image-quality",
|
|
165
|
+
"prompt": "A futuristic city skyline at night",
|
|
166
|
+
"n": 4
|
|
167
|
+
}'
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Aspect Ratio
|
|
171
|
+
|
|
172
|
+
Control image dimensions with the `aspect_ratio` parameter. This works for image generation and image editing with multiple images.
|
|
173
|
+
For image editing with a single image, the output aspect ratio respects the input image's aspect ratio.
|
|
174
|
+
|
|
175
|
+
| Ratio | Use case |
|
|
176
|
+
|-------|----------|
|
|
177
|
+
| `1:1` | Social media, thumbnails |
|
|
178
|
+
| `16:9` / `9:16` | Widescreen, mobile, stories |
|
|
179
|
+
| `4:3` / `3:4` | Presentations, portraits |
|
|
180
|
+
| `3:2` / `2:3` | Photography |
|
|
181
|
+
| `2:1` / `1:2` | Banners, headers |
|
|
182
|
+
| `19.5:9` / `9:19.5` | Modern smartphone displays |
|
|
183
|
+
| `20:9` / `9:20` | Ultra-wide displays |
|
|
184
|
+
| `auto` | Model auto-selects the best ratio for the prompt |
|
|
185
|
+
|
|
186
|
+
```python customLanguage="pythonXAI"
|
|
187
|
+
import xai_sdk
|
|
188
|
+
|
|
189
|
+
client = xai_sdk.Client()
|
|
190
|
+
|
|
191
|
+
response = client.image.sample(
|
|
192
|
+
prompt="Mountain landscape at sunrise",
|
|
193
|
+
model="grok-imagine-image-quality",
|
|
194
|
+
aspect_ratio="16:9",
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
print(response.url)
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
```python customLanguage="pythonOpenAISDK"
|
|
201
|
+
from openai import OpenAI
|
|
202
|
+
|
|
203
|
+
client = OpenAI(
|
|
204
|
+
base_url="https://api.x.ai/v1",
|
|
205
|
+
api_key="YOUR_API_KEY",
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
response = client.images.generate(
|
|
209
|
+
model="grok-imagine-image-quality",
|
|
210
|
+
prompt="Mountain landscape at sunrise",
|
|
211
|
+
extra_body={"aspect_ratio": "16:9"},
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
print(response.data[0].url)
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
```javascript customLanguage="javascriptOpenAISDK"
|
|
218
|
+
import OpenAI from "openai";
|
|
219
|
+
|
|
220
|
+
const client = new OpenAI({
|
|
221
|
+
apiKey: process.env.XAI_API_KEY,
|
|
222
|
+
baseURL: "https://api.x.ai/v1",
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
const response = await client.images.generate({
|
|
226
|
+
model: "grok-imagine-image-quality",
|
|
227
|
+
prompt: "Mountain landscape at sunrise",
|
|
228
|
+
|
|
229
|
+
aspect_ratio: "16:9",
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
console.log(response.data[0].url);
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
```javascript customLanguage="javascriptAISDK"
|
|
236
|
+
import { xai } from "@ai-sdk/xai";
|
|
237
|
+
import { generateImage } from "ai";
|
|
238
|
+
|
|
239
|
+
const { image } = await generateImage({
|
|
240
|
+
model: xai.image("grok-imagine-image-quality"),
|
|
241
|
+
prompt: "Mountain landscape at sunrise",
|
|
242
|
+
aspectRatio: "16:9",
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
console.log(image.base64);
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
curl -X POST https://api.x.ai/v1/images/generations \
|
|
250
|
+
-H "Content-Type: application/json" \
|
|
251
|
+
-H "Authorization: Bearer $XAI_API_KEY" \
|
|
252
|
+
-d '{
|
|
253
|
+
"model": "grok-imagine-image-quality",
|
|
254
|
+
"prompt": "Mountain landscape at sunrise",
|
|
255
|
+
"aspect_ratio": "16:9"
|
|
256
|
+
}'
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Resolution
|
|
260
|
+
|
|
261
|
+
You can specify different resolutions of the output image. Currently supported image resolutions are:
|
|
262
|
+
|
|
263
|
+
* 1k
|
|
264
|
+
* 2k
|
|
265
|
+
|
|
266
|
+
```python customLanguage="pythonXAI"
|
|
267
|
+
import xai_sdk
|
|
268
|
+
|
|
269
|
+
client = xai_sdk.Client()
|
|
270
|
+
|
|
271
|
+
response = client.image.sample(
|
|
272
|
+
prompt="An astronaut performing EVA in LEO.",
|
|
273
|
+
model="grok-imagine-image-quality",
|
|
274
|
+
resolution="2k"
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
print(response.url)
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
```python customLanguage="pythonOpenAISDK"
|
|
281
|
+
from openai import OpenAI
|
|
282
|
+
|
|
283
|
+
client = OpenAI(
|
|
284
|
+
base_url="https://api.x.ai/v1",
|
|
285
|
+
api_key="YOUR_API_KEY",
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
response = client.images.generate(
|
|
289
|
+
model="grok-imagine-image-quality",
|
|
290
|
+
prompt="An astronaut performing EVA in LEO.",
|
|
291
|
+
extra_body={"resolution": "2k"},
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
print(response.data[0].url)
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
```javascript customLanguage="javascriptOpenAISDK"
|
|
298
|
+
import OpenAI from "openai";
|
|
299
|
+
|
|
300
|
+
const client = new OpenAI({
|
|
301
|
+
apiKey: process.env.XAI_API_KEY,
|
|
302
|
+
baseURL: "https://api.x.ai/v1",
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
const response = await client.images.generate({
|
|
306
|
+
model: "grok-imagine-image-quality",
|
|
307
|
+
prompt: "An astronaut performing EVA in LEO.",
|
|
308
|
+
|
|
309
|
+
resolution: "2k",
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
console.log(response.data[0].url);
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
```javascript customLanguage="javascriptAISDK"
|
|
316
|
+
import { xai } from "@ai-sdk/xai";
|
|
317
|
+
import { generateImage } from "ai";
|
|
318
|
+
|
|
319
|
+
const { image } = await generateImage({
|
|
320
|
+
model: xai.image("grok-imagine-image-quality"),
|
|
321
|
+
prompt: "An astronaut performing EVA in LEO.",
|
|
322
|
+
providerOptions: {
|
|
323
|
+
xai: { resolution: "2k" },
|
|
324
|
+
},
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
console.log(image.base64);
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
```bash
|
|
331
|
+
curl -X POST https://api.x.ai/v1/images/generations \
|
|
332
|
+
-H "Content-Type: application/json" \
|
|
333
|
+
-H "Authorization: Bearer $XAI_API_KEY" \
|
|
334
|
+
-d '{
|
|
335
|
+
"model": "grok-imagine-image-quality",
|
|
336
|
+
"prompt": "An astronaut performing EVA in LEO.",
|
|
337
|
+
"resolution": "2k"
|
|
338
|
+
}'
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
### Base64 Output
|
|
342
|
+
|
|
343
|
+
For embedding images directly without downloading, request base64:
|
|
344
|
+
|
|
345
|
+
```python customLanguage="pythonXAI"
|
|
346
|
+
import xai_sdk
|
|
347
|
+
|
|
348
|
+
client = xai_sdk.Client()
|
|
349
|
+
|
|
350
|
+
response = client.image.sample(
|
|
351
|
+
prompt="A serene Japanese garden",
|
|
352
|
+
model="grok-imagine-image-quality",
|
|
353
|
+
image_format="base64",
|
|
354
|
+
)
|
|
355
|
+
|
|
356
|
+
# Save to file
|
|
357
|
+
with open("garden.jpg", "wb") as f:
|
|
358
|
+
f.write(response.image)
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
```python customLanguage="pythonOpenAISDK"
|
|
362
|
+
import base64
|
|
363
|
+
from openai import OpenAI
|
|
364
|
+
|
|
365
|
+
client = OpenAI(
|
|
366
|
+
base_url="https://api.x.ai/v1",
|
|
367
|
+
api_key="YOUR_API_KEY",
|
|
368
|
+
)
|
|
369
|
+
|
|
370
|
+
response = client.images.generate(
|
|
371
|
+
model="grok-imagine-image-quality",
|
|
372
|
+
prompt="A serene Japanese garden",
|
|
373
|
+
response_format="b64_json",
|
|
374
|
+
)
|
|
375
|
+
|
|
376
|
+
# Save to file
|
|
377
|
+
image_bytes = base64.b64decode(response.data[0].b64_json)
|
|
378
|
+
with open("garden.jpg", "wb") as f:
|
|
379
|
+
f.write(image_bytes)
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
```javascript customLanguage="javascriptOpenAISDK"
|
|
383
|
+
import OpenAI from "openai";
|
|
384
|
+
import fs from "fs";
|
|
385
|
+
|
|
386
|
+
const client = new OpenAI({
|
|
387
|
+
apiKey: process.env.XAI_API_KEY,
|
|
388
|
+
baseURL: "https://api.x.ai/v1",
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
const response = await client.images.generate({
|
|
392
|
+
model: "grok-imagine-image-quality",
|
|
393
|
+
prompt: "A serene Japanese garden",
|
|
394
|
+
response_format: "b64_json",
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
// Save to file
|
|
398
|
+
const imageBuffer = Buffer.from(response.data[0].b64_json, "base64");
|
|
399
|
+
fs.writeFileSync("garden.jpg", imageBuffer);
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
```javascript customLanguage="javascriptAISDK"
|
|
403
|
+
import { xai } from "@ai-sdk/xai";
|
|
404
|
+
import { generateImage } from "ai";
|
|
405
|
+
import fs from "fs";
|
|
406
|
+
|
|
407
|
+
const { image } = await generateImage({
|
|
408
|
+
model: xai.image("grok-imagine-image-quality"),
|
|
409
|
+
prompt: "A serene Japanese garden",
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
// Save to file (AI SDK returns base64 by default)
|
|
413
|
+
const imageBuffer = Buffer.from(image.base64, "base64");
|
|
414
|
+
fs.writeFileSync("garden.jpg", imageBuffer);
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
```bash
|
|
418
|
+
curl -X POST https://api.x.ai/v1/images/generations \
|
|
419
|
+
-H "Content-Type: application/json" \
|
|
420
|
+
-H "Authorization: Bearer $XAI_API_KEY" \
|
|
421
|
+
-d '{
|
|
422
|
+
"model": "grok-imagine-image-quality",
|
|
423
|
+
"prompt": "A serene Japanese garden",
|
|
424
|
+
"response_format": "b64_json"
|
|
425
|
+
}'
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
### Response Details
|
|
429
|
+
|
|
430
|
+
The xAI SDK exposes additional metadata on the response object beyond the image URL or base64 data.
|
|
431
|
+
|
|
432
|
+
**Moderation** — Check whether the generated image passed content moderation:
|
|
433
|
+
|
|
434
|
+
```python customLanguage="pythonXAI"
|
|
435
|
+
if response.respect_moderation:
|
|
436
|
+
print(response.url)
|
|
437
|
+
else:
|
|
438
|
+
print("Image filtered by moderation")
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
**Model** — Get the actual model used (resolving any aliases):
|
|
442
|
+
|
|
443
|
+
```python customLanguage="pythonXAI"
|
|
444
|
+
print(f"Model: {response.model}")
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
## Concurrent Requests
|
|
448
|
+
|
|
449
|
+
When you need to generate multiple images with **different prompts**, such as generating unrelated images in parallel, use `AsyncClient` with `asyncio.gather` to fire requests concurrently. This is significantly faster than issuing them one at a time.
|
|
450
|
+
|
|
451
|
+
> [!TIP]
|
|
452
|
+
>
|
|
453
|
+
> If you want multiple variations from the **same prompt**, use [`sample_batch()` with the `n` parameter\`](#multiple-images) instead. That generates all images in a single request and is the most efficient approach for same-prompt generation.
|
|
454
|
+
|
|
455
|
+
```python customLanguage="pythonXAI"
|
|
456
|
+
import asyncio
|
|
457
|
+
import xai_sdk
|
|
458
|
+
|
|
459
|
+
async def generate_concurrently():
|
|
460
|
+
client = xai_sdk.AsyncClient()
|
|
461
|
+
|
|
462
|
+
# Each request uses a different prompt
|
|
463
|
+
prompts = [
|
|
464
|
+
"A futuristic city skyline at sunset",
|
|
465
|
+
"A serene Japanese garden in winter",
|
|
466
|
+
"An astronaut floating above Earth",
|
|
467
|
+
"A medieval castle on a misty mountain",
|
|
468
|
+
]
|
|
469
|
+
|
|
470
|
+
# Fire all requests concurrently
|
|
471
|
+
tasks = [
|
|
472
|
+
client.image.sample(
|
|
473
|
+
prompt=prompt,
|
|
474
|
+
model="grok-imagine-image-quality",
|
|
475
|
+
)
|
|
476
|
+
for prompt in prompts
|
|
477
|
+
]
|
|
478
|
+
|
|
479
|
+
results = await asyncio.gather(*tasks)
|
|
480
|
+
|
|
481
|
+
for prompt, result in zip(prompts, results):
|
|
482
|
+
print(f"{prompt}: {result.url}")
|
|
483
|
+
|
|
484
|
+
asyncio.run(generate_concurrently())
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
## Related
|
|
488
|
+
|
|
489
|
+
* [Models](/developers/models) — Available image models
|
|
490
|
+
* [Image Editing](/developers/model-capabilities/images/editing) — Edit images with natural language
|
|
491
|
+
* [Video Generation](/developers/model-capabilities/video/generation) — Generate videos from text prompts
|
|
492
|
+
* [API Reference](/developers/rest-api-reference) — Full endpoint documentation
|
|
493
|
+
* [Imagine API Landing Page](https://x.ai/api/imagine) — Showcase of the Imagine API in action
|
|
494
|
+
|