跳至主要內容

使用 Vision 模型

快速入門

將圖片傳遞給模型的範例

import os 
from litellm import completion

os.environ["OPENAI_API_KEY"] = "your-api-key"

# openai call
response = completion(
model = "gpt-4-vision-preview",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "What’s in this image?"
},
{
"type": "image_url",
"image_url": {
"url": "https://awsmp-logos.s3.amazonaws.com/seller-xw5kijmvmzasy/c233c9ade2ccb5491072ae232c814942.png"
}
}
]
}
],
)

檢查模型是否支援 vision

使用 litellm.supports_vision(model="") -> 若模型支援 vision 則回傳 True,若不支援則回傳 False

assert litellm.supports_vision(model="openai/gpt-4-vision-preview") == True
assert litellm.supports_vision(model="vertex_ai/gemini-1.0-pro-vision") == True
assert litellm.supports_vision(model="openai/gpt-3.5-turbo") == False
assert litellm.supports_vision(model="xai/grok-2-vision-latest") == True
assert litellm.supports_vision(model="xai/grok-2-latest") == False

明確指定影像類型

如果您有沒有 mime-type 的圖片,或是 litellm 錯誤地推斷了您圖片的 mime type(例如在 vertex ai 中呼叫 gs:// 的網址),您可以透過 format 參數明確設定。

"image_url": {
"url": "gs://my-gs-image",
"format": "image/jpeg"
}

LiteLLM 會將其用於任何支援指定 mime-type 的 API 端點(例如 anthropic/bedrock/vertex ai)。

對於其他端點(例如 openai),則會忽略。

import os 
from litellm import completion

os.environ["ANTHROPIC_API_KEY"] = "your-api-key"

# openai call
response = completion(
model = "claude-3-7-sonnet-latest",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "What’s in this image?"
},
{
"type": "image_url",
"image_url": {
"url": "https://awsmp-logos.s3.amazonaws.com/seller-xw5kijmvmzasy/c233c9ade2ccb5491072ae232c814942.png",
"format": "image/jpeg"
}
}
]
}
],
)

規格

"image_url": str

OR

"image_url": {
"url": "url OR base64 encoded str",
"detail": "openai-only param",
"format": "specify mime-type of image"
}
🚅
LiteLLM Enterprise
為正式環境打造的 SSO/SAML、稽核記錄、支出追蹤、多團隊管理與防護欄。
深入瞭解 →