Skip to content

视觉模型支持同时输入图像和文本,使模型能够描述、分类并回答关于其所见内容的问题。

快速入门

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('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

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)