模型能力

图像理解

查看 Markdown

部分模型允许在输入中包含图像。模型生成响应时会考虑图像上下文。

构建消息正文:与纯文本 prompt 的区别

图像理解的请求消息与纯文本 prompt 类似。主要区别在于,纯文本输入原本如下:

JSON

[
  {
    "role": "user",
    "content": "What is in this image?"
  }
]

现在将 content 作为对象列表传入:

JSON

[
  {
    "role": "user",
    "content": [
      {
        "type": "input_image",
        "image_url": "data:image/jpeg;base64,<base64_image_string>"
      },
      {
        "type": "input_text",
        "text": "What is in this image?"
      }
    ]
  }
]

image_url 的值也可以是互联网上的公开 URL,而不是 base64 data URL。

图像理解示例

import os
from xai_sdk import Client
from xai_sdk.chat import user, image

client = Client(
    api_key=os.getenv("XAI_API_KEY"),
    management_api_key=os.getenv("XAI_MANAGEMENT_API_KEY"),
    timeout=3600,
)

image_url = "https://science.nasa.gov/wp-content/uploads/2023/09/web-first-images-release.png"
chat = client.chat.create(model="grok-4.7")
chat.append(
    user(
        "What's in this image?",
        image(image_url=image_url, detail="high"),
    )
)

response = chat.sample()
print(response)

# The response ID that can be used to continue the conversation later

print(response.id)

图像输入的一般限制

  • 最大图像大小:20MiB

  • 图像最大数量:不限

  • 支持的图像文件类型:jpg/jpegpng

  • 可接受任意图像/文本输入顺序(例如文本 prompt 可以位于图像 prompt 之前)


最后更新:2026 年 5 月 30 日