模型能力
Imagine 概述
Imagine API 可让您使用 Grok Imagine 模型生成和编辑图像与视频。可用于图像生成、使用最多 3 张参考图像进行图像编辑、从文本或静态图像生成视频、视频编辑等。
定价
图像生成采用每张图像固定定价,与提示词长度无关。每张生成的图像会收取固定费用。图像编辑同时计费输入图像和生成的输出图像。视频生成采用按秒定价,时长和分辨率都会影响总成本。有关完整定价详情,请参阅定价页面。
图像生成
使用 Grok Imagine 模型根据文本提示生成新图像。配置输出数量(每请求最多 10 张图像)、宽高比、分辨率和响应格式。
python
import xai_sdk
client = xai_sdk.Client()
response = client.image.sample(
prompt="A collage of London landmarks in a stenciled street‑art style",
model="grok-imagine-image-quality",
)
print(response.url)bash
curl -X POST https://api.x.ai/v1/images/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $XAI_API_KEY" \
-d '{
"model": "grok-imagine-image-quality",
"prompt": "A collage of London landmarks in a stenciled street‑art style"
}'python
from openai import OpenAI
client = OpenAI(
base_url="https://api.x.ai/v1",
api_key="YOUR_API_KEY",
)
response = client.images.generate(
model="grok-imagine-image-quality",
prompt="A collage of London landmarks in a stenciled street‑art style",
)
print(response.data[0].url)javascript
import { xai } from "@ai-sdk/xai";
import { generateImage } from "ai";
const { image } = await generateImage({
model: xai.image("grok-imagine-image-quality"),
prompt: "A collage of London landmarks in a stenciled street‑art style",
});
console.log(image.base64);图像编辑
使用自然语言编辑源图像。提供公共图像 URL 或 base64 编码的数据 URI,然后描述您希望 Grok Imagine 应用的更改。多图像编辑支持在单个请求中使用最多 3 张源图像,用于组合主体、转换风格和构建场景。
python
import base64
import xai_sdk
client = xai_sdk.Client()
# Load image from file and encode as base64
with open("photo.png", "rb") as f:
image_data = base64.b64encode(f.read()).decode("utf-8")
response = client.image.sample(
prompt="Render this as a pencil sketch with detailed shading",
model="grok-imagine-image-quality",
image_url=f"data:image/png;base64,{image_data}",
)
print(response.url)bash
# Using a public URL as the source image
curl -X POST https://api.x.ai/v1/images/edits \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $XAI_API_KEY" \
-d '{
"model": "grok-imagine-image-quality",
"prompt": "Render this as a pencil sketch with detailed shading",
"image": {
"url": "https://docs.x.ai/assets/api-examples/images/style-realistic.png",
"type": "image_url"
}
}'javascript
import { xai } from "@ai-sdk/xai";
import { generateImage } from "ai";
import fs from "fs";
// Load image and encode as base64
const imageBuffer = fs.readFileSync("photo.png");
const base64Image = imageBuffer.toString("base64");
const { image } = await generateImage({
model: xai.image("grok-imagine-image-quality"),
prompt: {
text: "Render this as a pencil sketch with detailed shading",
images: [`data:image/png;base64,${base64Image}`],
},
});
console.log(image.base64);视频生成
使用文本提示为静态图像添加动画效果。源图像成为生成视频的起点。视频请求是异步的:启动请求,使用返回的请求 ID 轮询,并在准备就绪时使用完成后的视频 URL。xAI SDK 和 AI SDK 会为您处理轮询。
python
import os
import xai_sdk
client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))
response = client.video.generate(
prompt="Make the water crash down and slowly pan out the camera",
model="grok-imagine-video-1.5",
image_url="https://docs.x.ai/assets/api-examples/video/waterfall-still.png",
duration=12,
)
print(response.url)javascript
import { xai } from "@ai-sdk/xai";
import { experimental_generateVideo as generateVideo } from "ai";
const result = await generateVideo({
model: xai.video("grok-imagine-video-1.5"),
prompt: {
image: "https://docs.x.ai/assets/api-examples/video/waterfall-still.png",
text: "Make the water crash down and slowly pan out the camera",
},
duration: 12,
});
const videoUrl = result.providerMetadata?.xai?.videoUrl;
console.log(videoUrl);bash
# Start the video generation request
REQUEST_ID=$(curl -s -X POST https://api.x.ai/v1/videos/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $XAI_API_KEY" \
-d '{
"model": "grok-imagine-video-1.5",
"prompt": "Make the water crash down and slowly pan out the camera",
"image": {"url": "https://docs.x.ai/assets/api-examples/video/waterfall-still.png"},
"duration": 12
}' | jq -r '.request_id')
# Poll until the video is ready
while true; do
RESULT=$(curl -s https://api.x.ai/v1/videos/$REQUEST_ID \
-H "Authorization: Bearer $XAI_API_KEY")
STATUS=$(echo "$RESULT" | jq -r '.status')
if [ "$STATUS" = "done" ]; then
echo "$RESULT" | jq -r '.video.url'
break
elif [ "$STATUS" = "failed" ] || [ "$STATUS" = "expired" ]; then
echo "Request $STATUS"; echo "$RESULT" | jq .
break
fi
sleep 5
done更多能力
除了上述主要用例外,Imagine API 还支持多种额外工作流程:
- 多图像编辑 — 在单个编辑中组合最多 3 张源图像,用于合成主体、转换风格和从多个参考构建场景。
- 视频生成 — 使用可配置的时长(最多 15 秒)、宽高比和分辨率,从文本提示生成视频。
- 视频编辑 — 使用文本提示修改现有视频,同时保留场景的其他部分。
- 参考转视频 — 使用一个或多个参考图像引导生成的视频,这些图像会影响输出但不会强制第一帧。
- 视频扩展 — 从最后一帧继续现有视频,将原始视频和扩展部分合并为一个剪辑。
- Files API 集成 — 通过 ID 引用存储的文件作为 Imagine 输入,将生成的资源持久化到 Files API,并可选择创建永久可共享的公共 URL — 所有操作都在单个请求中完成。
企业合规与安全
Imagine API 专为具有严格安全和合规要求的生产工作负载而构建。生成的媒体内容需接受内容政策审查,不用于训练。
SOC 2 Type II — 安全、可用性和保密性的审计控制
符合 HIPAA — 提供适用于处理 PHI 的医疗保健应用的 BAA
符合 GDPR — 数据处理协议和欧盟数据驻留选项
数据驻留 — 为合规要求提供区域处理
高可用性 — 具有企业工作负载自定义 SLA 的多区域基础设施
SSO 与 RBAC — SAML SSO、基于角色的访问和审计日志