视觉模型可以同时接受图像和文本,以便模型能够描述、分类并回答关于其所见内容的问题。
快速开始
ollama run gemma4 ./image.png what is in this image?
使用 Ollama API
提供一个 images 数组。SDK 接受文件路径、URL 或原始字节数据,而 REST API 期望 base64 编码的图像数据。
cURL
# 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
from ollama import chat
# from pathlib import Path
# 传入图像的路径
path = input('Please enter the path to the image: ')
# 你也可以传入 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
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)