模型能力
将文件引用为输入
只要 Imagine endpoint 接受公开 URL 或 base64-encoded 图像/视频,就可以改为传入 file_id(来自你的 Files API 存储)。文件会由服务端从你的私有存储中获取,因此:
无需耗费带宽重复上传同一张图像,这对迭代编辑循环很有帮助。
原始文件会保持私有(作为输入使用时无需将其设为公开)。
既适用于上传的文件,也适用于先前 Imagine 调用生成的 asset(通过
storage_options)。
引用的文件必须具有 endpoint 所要求的正确 content type(图像:PNG/JPEG/WebP;视频:MP4),并且必须已完整上传。
编辑已存储的图像
import os
import xai_sdk
client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))
response = client.image.sample(
prompt="Add a party hat to the dog",
model="grok-imagine-image-quality",
image_file_id="file_7de029f4-eb66-42ee-87f8-b2a9d9e7466a", # instead of image_url
)
print(response.url)使用多张已存储图像进行编辑
response = client.image.sample(
prompt="Blend these two scenes into one cohesive composition",
model="grok-imagine-image-quality",
image_file_ids=[
"file_7de029f4-eb66-42ee-87f8-b2a9d9e7466a",
"file_2cd998e7-bf12-44aa-92c8-e3d1f1c1234f",
],
)使用已存储首帧生成视频
response = client.video.generate(
prompt="Pan across the scene as the sky darkens",
model="grok-imagine-video-1.5",
duration=5,
image_file_id="file_7de029f4-eb66-42ee-87f8-b2a9d9e7466a",
)
print(response.url)编辑已存储的视频
response = client.video.generate(
prompt="Add rain and a moody atmosphere",
model="grok-imagine-video",
video_file_id="file_5be118c3-da55-31dd-76e7-a1b8c8d6355b",
)使用多张已存储图像进行 Reference-to-Video
response = client.video.generate(
prompt="A woman in this dress walks down a city street at night",
model="grok-imagine-video-1.5",
duration=5,
reference_image_file_ids=[
"file_5be118c3-da55-31dd-76e7-a1b8c8d6355b", # subject
"file_2cd998e7-bf12-44aa-92c8-e3d1f1c1234f", # outfit
],
)相关内容
Files API 集成 — 概览以及组合使用输入与输出的完整示例。
持久保存生成的输出 — 输出方向:
storage_options、public URL 和过期语义。管理文件 — 上传、列出、获取、更新和删除文件。