图像理解
注意:仅支持 Base64 内联图片,不支持图片 URL。
- Python
- JavaScript
- curl
import base64
from openai import OpenAI
client = OpenAI(api_key="your-api-key", base_url="https://www.cheapertoken.work/v1")
with open("image.jpg", "rb") as f:
image_data = base64.b64encode(f.read()).decode("utf-8")
response = client.chat.completions.create(
model="gemini-2.5-flash",
messages=[
{
"role": "user",
"content": [
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_data}"}},
{"type": "text", "text": "请描述这张图片"},
],
}
],
)
print(response.choices[0].message.content)
import OpenAI from "openai";
import fs from "fs";
const client = new OpenAI({ apiKey: "your-api-key", baseURL: "https://www.cheapertoken.work/v1" });
const imageData = fs.readFileSync("image.jpg").toString("base64");
const response = await client.chat.completions.create({
model: "gemini-2.5-flash",
messages: [
{
role: "user",
content: [
{ type: "image_url", image_url: { url: `data:image/jpeg;base64,${imageData}` } },
{ type: "text", text: "请描述这张图片" },
],
},
],
});
console.log(response.choices[0].message.content);
IMAGE_BASE64=$(base64 -w 0 image.jpg)
curl https://www.cheapertoken.work/v1/chat/completions \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json" \
-d "{
\"model\": \"gemini-2.5-flash\",
\"messages\": [{
\"role\": \"user\",
\"content\": [
{\"type\": \"image_url\", \"image_url\": {\"url\": \"data:image/jpeg;base64,${IMAGE_BASE64}\"}},
{\"type\": \"text\", \"text\": \"请描述这张图片\"}
]
}]
}"