視覺模型可同時接收圖像與文字,讓模型能夠描述、分類並針對其所見內容回答問題。
快速上手
shell
ollama run gemma4 ./image.png whats in this image?與 Ollama API 的使用方法
提供一個 images 陣列。SDK 可接受檔案路徑、URL 或原始位元組(raw bytes),而 REST API 則預期為 base64 編碼的圖像數據。
cURL
shell
# 1. 下載範例圖片
curl -L -o test.jpg "https://upload.wikimedia.org/wikipedia/commons/3/3a/Cat03.jpg"
# 2. 編碼圖片
IMG=$(base64 < test.jpg | tr -d '\n')
# 3. 將其傳送至 Ollama
curl -X POST http://localhost:11434/api/chat \
-H "Content-Type: application/json" \
-d '{
"model": "gemma4",
"messages": [{
"role": "user",
"content": "What is in this image?",
"images": ["'"$IMG"'"]
}],
"stream": false
}'Python
python
from ollama import chat
# from pathlib import Path
# 輸入圖片的路徑
path = input('請輸入圖片的路徑:')
# 您也可以傳入 base64 編碼的圖像數據
# img = base64.b64encode(Path(path).read_bytes()).decode()
# 或原始位元組
# img = Path(path).read_bytes()
response = chat(
model='gemma4',
messages=[
{
'role': 'user',
'content': 'What is in this image? Be concise.',
'images': [path],
}
],
)
print(response.message.content)JavaScript
javascript
import ollama from 'ollama'
const imagePath = '/absolute/path/to/image.jpg'
const response = await ollama.chat({
model: 'gemma4',
messages: [
{ role: 'user', content: 'What is in this image?', images: [imagePath] }
],
stream: false,
})
console.log(response.message.content)