備註
本頁尚未翻譯,暫時以英文顯示。
Image input (vision)
Chat models whose page on Models lists image input under Capabilities read images: screenshots, photos, charts, scanned pages. The image goes in the message next to the question.
This sample is a file we run against the service before publishing:
# pip install openai
# Send an image with the question. Any chat model whose page lists vision
# takes images; give a public https URL, or the file itself as a data URL.
import os
from openai import OpenAI
client = OpenAI(
base_url=os.getenv("OCEANTOKEN_BASE_URL", "https://api.oceantoken.ai/v1"),
api_key=os.environ["OCEANTOKEN_API_KEY"],
)
response = client.chat.completions.create(
model="openai/gpt-5-mini",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What does the large text in this image say?"},
{"type": "image_url", "image_url": {"url": "https://docs.oceantoken.ai/img/oceantoken-og.png"}},
],
}
],
)
print(response.choices[0].message.content)
How to send an image
content becomes a list of parts instead of a string: text parts and image_url parts, in the order the model should read them. The image is either
- a public
https://URL, which the model fetches, or - the file itself as a data URL,
data:image/png;base64,..., for a local file or one behind a login.
import base64
with open("receipt.jpg", "rb") as f:
data_url = "data:image/jpeg;base64," + base64.b64encode(f.read()).decode()
messages = [{
"role": "user",
"content": [
{"type": "text", "text": "What is the total on this receipt?"},
{"type": "image_url", "image_url": {"url": data_url}},
],
}]
PNG, JPEG and WebP are the safe formats. For several images, add several image_url parts to the same message.
Limits
- The whole request is at most 10 MB (Limits). Base64 makes a file about a third larger, so send large images as URLs rather than data URLs.
- Every model has a maximum number of images per request, counting the images in every earlier turn of the conversation. Past it, the request is refused with
400 invalid_requestand the message gives the limit; drop older images, such as an agent's old screenshots. A model without image input cannot read images at all. - Images are billed as input tokens. How many tokens an image counts as depends on the model and the image's size; the request's
usage.prompt_tokensand its line in Logs show what it was.
With the Anthropic Messages API
On /v1/messages, an image is a block in the Anthropic shape, for every chat model with image input:
{"type": "image", "source": {"type": "url", "url": "https://example.com/chart.png"}}
or with "source": {"type": "base64", "media_type": "image/png", "data": "..."} for the file itself.