import base64
import os
from pathlib import Path
from openai import OpenAI, APIConnectionError, APIStatusError
# This example's local limit is not the gateway or model's documented limit.
image_limit = 5 * 1024 * 1024
with Path(os.environ["API_IMAGE_PATH"]).open("rb") as image_file:
image = image_file.read(image_limit + 1)
if not image or len(image) > image_limit:
raise SystemExit("Choose a nonempty image up to 5 MiB for this example.")
if image.startswith(b"\x89PNG\r\n\x1a\n"):
mime_type = "image/png"
elif image.startswith(b"\xff\xd8\xff"):
mime_type = "image/jpeg"
else:
raise SystemExit("This example accepts PNG or JPEG images only.")
image_url = f"data:{mime_type};base64,{base64.b64encode(image).decode('ascii')}"
with OpenAI(
base_url=os.environ["API_BASE_URL"].rstrip("/"),
api_key=os.environ["API_KEY"],
timeout=60.0,
max_retries=0,
) as client:
try:
result = client.chat.completions.create(
model=os.environ["API_MODEL"],
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "Describe this image in one sentence."},
{"type": "image_url", "image_url": {"url": image_url}},
],
}],
)
choice = result.choices[0] if result.choices else None
if (choice is None or choice.finish_reason != "stop"
or choice.message.refusal or choice.message.tool_calls
or not choice.message.content):
raise SystemExit("Image request did not produce a complete text reply.")
print(choice.message.content)
except APIStatusError as error:
raise SystemExit(f"Request failed (HTTP {error.status_code}). Check the request logs.")
except APIConnectionError:
raise SystemExit("Connection failed. Check the API address and network.")