故障排除

这一章列出使用 Ollama 时可能遇到的问题和解决方案。

连接问题

无法连接到 Ollama

问题

Connection refused: localhost:11434

解决方法

  1. 检查 Ollama 是否运行:
# macOS/Linux
ps aux | grep ollama

# Windows
tasklist | findstr ollama
  1. 启动 Ollama:
ollama serve
  1. 检查端口占用:
# macOS/Linux
lsof -i :11434

# Windows
netstat -ano | findstr :11434
  1. 检查防火墙设置

连接超时

问题

TimeoutError: Request timed out

解决方法

  1. 增加超时时间:
import requests

response = requests.post(
    "http://localhost:11434/api/chat",
    json={"model": "llama3.2", "messages": messages},
    timeout=300  # 5 分钟
)
  1. 检查网络连接

  2. 减少请求大小

模型问题

模型未找到

问题

model 'xxx' not found

解决方法

  1. 拉取模型:
ollama pull llama3.2
  1. 检查模型名称:
import ollama

models = ollamaari.list()
print([m['name'] for m in models['models']])
  1. 使用正确的标签:
# 错误
model='llama3.2'

# 正确
model='llama3.2:latest'

模型加载失败

问题

failed to load model

解决方法

  1. 检查内存是否足够

  2. 使用更小的模型或量化:

ollama pull llama3.2:1b
  1. 检查模型文件完整性:
ollama show llama3.2

显存不足

问题

CUDA out of memory

解决方法

  1. 减少上下文长度:
response = ollama.chat(
    model='llama3.2',
    messages=messages,
    options={'num_ctx': 2048}
)
  1. 使用更小的量化:
ollama pull llama3.2:3b-q4_K_S
  1. 减少使用 GPU:
response = ollama.chat(
    model='llama3.2',
    messages=messages,
    options={'num_gpu': 20}
)
  1. 使用 CPU:
response = ollama.chat(
    model='llama3.2',
    messages=messages,
    options={'num_gpu': 0}
)

性能问题

响应速度慢

解决方法

  1. 使用更小的模型

  2. 减少生成长度:

response = ollama.chat(
    model='llama3.2',
    messages=messages,
    options={'num_predict': 100}
)
  1. 启用 GPU 加速

  2. 使用流式输出提升体验

内存占用高

解决方法

  1. 及时卸载模型:
ollama.generate(
    model='llama3.2',
    prompt='',
    keep_alive='0'
)
  1. 使用更小的量化

  2. 限制并发数量

输出问题

输出不完整

问题: 模型输出在中间停止。

解决方法

  1. 增加 num_predict:
response = ollama.chat(
    model='llama3.2',
    messages=messages,
    options={'num_predict': 2000}
)
  1. 检查停止词设置

  2. 检查上下文长度

输出质量差

解决方法

  1. 调整温度:
response = ollama.chat(
    model='llama3.2',
    messages=messages,
    options={'temperature': 0.3}
)
  1. 改进提示词

  2. 使用更好的模型

  3. 添加系统提示

API 问题

400 错误

问题

400 Bad Request

解决方法

  1. 检查请求格式:
import json

request = {
    "model": "llama3.2",
    "messages": [{"role": "user", "content": "你好"}]
}

print(json.dumps(request, indent=2))
  1. 检查必需参数

  2. 验证参数类型

500 错误

问题

500 Internal Server Error

解决方法

  1. 查看日志:
journalctl -u ollama -f
  1. 重启 Ollama

  2. 检查模型文件

调试技巧

启用详细日志

import logging

logging.basicConfig(level=logging.DEBUG)

打印请求和响应

def debug_chat(messages):
    print("请求:")
    print(json.dumps(messages, indent=2, ensure_ascii=False))
    
    response = ollama.chat(
        model='llama3.2',
        messages=messages
    )
    
    print("\n响应:")
    print(json.dumps(response, indent=2, ensure_ascii=False))
    
    return response

性能分析

import time
import cProfile

def profile_chat():
    profiler = cProfile.Profile()
    
    profiler.enable()
    response = ollama.chat(
        model='llama3.2',
        messages=[{'role': 'user', 'content': '你好'}]
    )
    profiler.disable()
    
    profiler.print_stats()

profile_chat()

获取帮助

查看日志

# macOS/Linux
tail -f ~/.ollama/logs/server.log

# Windows
type %USERPROFILE%\.ollama\logs\server.log

检查版本

ollama --version

查看文档

ollama --help

社区支持