跳转到内容

推理 API

图片

POST /v1/images/generations

根据提示生成图像。这是向图像生成模型发送生成请求的端点。

请求体

  • aspect_ratio ("1:1" | "3:4" | "4:3" | "9:16" | "16:9" | "2:3" | "3:2" | "9:19.5" | "19.5:9" | "9:20" | "20:9" | "1:2" | "2:1" | "auto")

  • model (string | null) — 要使用的模型。

  • n (integer | null) — 要生成的图像数量

  • prompt (string) — 图像生成的提示。

  • resolution ("1k" | "2k")

  • response_format (string | null) — 返回图像的响应格式。可以是 url 或 b64_json。如果指定了 b64_json,图像将作为 base64 编码的字符串返回,而不是生成图像文件的 url。

  • storage_options (object)

    • expires_after (integer | null) — 从现在起直到文件自动过期的秒数。最大值为 2592000(30天)。 如果省略,文件永不过期。

    • filename (string, required) — 存储文件的文件名。

    • public_url (boolean | object)

  • user (string | null) — 代表您终端用户的唯一标识符,可以帮助 xAI 监控和检测滥用。

响应体

  • data (array<object>, required) — 生成的图像对象列表。

    • b64_json (string | null) — 生成的图像的 base64 编码字符串表示(不带 data-URI 前缀),如果请求中指定了 b64_json 作为 response_format

    • file_output (object)

      • expires_at (integer | null) — 存储文件过期并将被自动删除的 Unix 时间戳(秒)。仅在文件有过期时间时存在。

      • file_id (string, required) — 存储文件的 Files API file_id。

      • filename (string, required) — 存储文件的文件名。

      • public_url (string | null) — 存储文件的公共 URL。仅在请求包含 storage_options.public_url 且创建成功时存在。

      • public_url_error (string | null) — 当设置 storage_options.public_url 但公共 URL 创建失败时的人可读错误。文件已成功存储。

      • public_url_expires_at (integer | null) — 公共 URL 过期的 Unix 时间戳(秒)。当公共 URL 有过期时间时存在,可能来自请求中的显式 expires_after 或从文件的 TTL 继承。

    • mime_type (string | null) — 生成的图像的 MIME 类型(例如 image/pngimage/jpegimage/webp)。

    • storage_error (string | null) — 当设置 storage_options 但上传失败时的人可读错误。成功或未请求存储时不存在。

    • url (string | null) — 生成图像的 url,如果未指定 response_format 或请求中使用了 url

  • usage (object)

    • cost_in_usd_ticks (integer, required) — 以 USD ticks 表示的此请求成本。 一美元等于 100,000,000 ticks,所以一美元等于 10,000,000,000 ticks。

代码示例

bash
curl -s 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
import os

from openai import OpenAI

client = OpenAI(
    api_key=os.environ["XAI_API_KEY"],
    base_url="https://api.x.ai/v1",
)

response = client.images.generate(
    model="grok-imagine-image-quality",
    prompt="A collage of London landmarks in a stenciled street‑art style",
)

print(response.model_dump_json(indent=2))
javascript
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.XAI_API_KEY,
  baseURL: "https://api.x.ai/v1",
});

const response = await client.images.generate({
  model: "grok-imagine-image-quality",
  prompt: "A collage of London landmarks in a stenciled street‑art style",
});

console.log(JSON.stringify(response, null, 2));

响应示例:

json
{
  "data": [
    {
      "url": "..."
    },
    {
      "url": "..."
    }
  ]
}

POST /v1/images/edits

根据提示编辑图像。这是向图像生成模型发送编辑请求的端点。

请求体

  • aspect_ratio ("1:1" | "3:4" | "4:3" | "9:16" | "16:9" | "2:3" | "3:2" | "9:19.5" | "19.5:9" | "9:20" | "20:9" | "1:2" | "2:1" | "auto")

  • image (object)

    • file_id (string | null) — 来自 xAI Files API 的文件 ID。与 url 互斥。 文件必须是图像(JPEG、PNG 或 WebP)且已完全上传。

    • url (string) — 图像的公共 URL 或 base64 编码的数据 URL(JPEG、PNG 或 WebP)。 也接受 image_url 以保持兼容性。 在未设置 file_id 时必需。

  • images (array<object>) — 用于多参考编辑的输入图像列表。 与 image 互斥。提供多个图像时, 在提示中引用它们为 <IMAGE_0>、<IMAGE_1> 等。

    • file_id (string | null) — 来自 xAI Files API 的文件 ID。与 url 互斥。 文件必须是图像(JPEG、PNG 或 WebP)且已完全上传。

    • url (string) — 图像的公共 URL 或 base64 编码的数据 URL(JPEG、PNG 或 WebP)。 也接受 image_url 以保持兼容性。 在未设置 file_id 时必需。

  • model (string | null) — 要使用的模型。

  • n (integer | null) — 要生成的图像编辑数量。

  • prompt (string, required) — 图像编辑的提示。

  • resolution ("1k" | "2k")

  • response_format (string | null) — 返回图像的响应格式。可以是 urlb64_json。如果指定了 b64_json,图像将作为 base64 编码的字符串返回,而不是生成图像文件的 url。

  • storage_options (object)

    • expires_after (integer | null) — 从现在起直到文件自动过期的秒数。最大值为 2592000(30天)。 如果省略,文件永不过期。

    • filename (string, required) — 存储文件的文件名。

    • public_url (boolean | object)

  • user (string | null) — 代表您终端用户的唯一标识符,可以帮助 xAI 监控和检测滥用。

响应体

  • data (array<object>, required) — 生成的图像对象列表。

    • b64_json (string | null) — 生成的图像的 base64 编码字符串表示(不带 data-URI 前缀),如果请求中指定了 b64_json 作为 response_format

    • file_output (object)

      • expires_at (integer | null) — 存储文件过期并将被自动删除的 Unix 时间戳(秒)。仅在文件有过期时间时存在。

      • file_id (string, required) — 存储文件的 Files API file_id。

      • filename (string, required) — 存储文件的文件名。

      • public_url (string | null) — 存储文件的公共 URL。仅在请求包含 storage_options.public_url 且创建成功时存在。

      • public_url_error (string | null) — 当设置 storage_options.public_url 但公共 URL 创建失败时的人可读错误。文件已成功存储。

      • public_url_expires_at (integer | null) — 公共 URL 过期的 Unix 时间戳(秒)。当公共 URL 有过期时间时存在,可能来自请求中的显式 expires_after 或从文件的 TTL 继承。

    • mime_type (string | null) — 生成的图像的 MIME 类型(例如 image/pngimage/jpegimage/webp)。

    • storage_error (string | null) — 当设置 storage_options 但上传失败时的人可读错误。成功或未请求存储时不存在。

    • url (string | null) — 生成图像的 url,如果未指定 response_format 或请求中使用了 url

  • usage (object)

    • cost_in_usd_ticks (integer, required) — 以 USD ticks 表示的此请求成本。 一美元等于 100,000,000 ticks,所以一美元等于 10,000,000,000 ticks。

代码示例

bash
curl -s 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
const response = await fetch("https://api.x.ai/v1/images/edits", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.XAI_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    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",
    },
  }),
});

console.log(JSON.stringify(await response.json(), null, 2));
python
import json
import os

import requests

response = requests.post(
    "https://api.x.ai/v1/images/edits",
    headers={
        "Authorization": f"Bearer {os.environ['XAI_API_KEY']}",
        "Content-Type": "application/json",
    },
    json={
        "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",
        },
    },
)

print(json.dumps(response.json(), indent=2))

响应示例:

json
{
  "data": [
    {
      "url": "..."
    }
  ]
}

本文档为 docs.x.ai 全站中文翻译,由 AI 自动翻译生成。代码示例请以原文为准。