ollama 本地运行大型语言模型

发布时间: 更新时间: 总字数:1795 阅读时间:4m 作者: IP上海 分享 网址

Ollama 是一个开源的深度学习框架,专为本地机器上便捷部署和运行大型语言模型(LLM)而设计,它提供了一套完整的深度学习工具链,包括数据预处理、模型构建、训练、评估和部署等功能。

介绍

  • ollama 使用 Golang 开发
  • 功能
    • 本地部署与运行:支持运行 Llama 3.1、Phi 3、Mistral、Gemma 2 等模型,并支持自定义创建的模型
    • 轻量级与可扩展性
    • API 兼容 OpenAI 的 API
    • 预构建模型库:Ollama 包含了一系列预先训练好的大型语言模型,用户可以直接 pull、push
    • 跨平台支持
    • 命令行工具
  • 使用
    • 本地运行模型
    • 模型微调(fine-tuning)
  • open-webui 可参考

安装 ollama

linux

curl -fsSL https://ollama.com/install.sh | sh
# 指定版本
curl -fsSL https://ollama.com/install.sh | OLLAMA_VERSION=0.5.7 sh
  • AMD GPU install
curl -L https://ollama.com/download/ollama-linux-amd64-rocm.tgz -o ollama-linux-amd64-rocm.tgz
sudo tar -C /usr -xzf ollama-linux-amd64-rocm.tgz
  • ARM64 install
curl -L https://ollama.com/download/ollama-linux-arm64.tgz -o ollama-linux-arm64.tgz
sudo tar -C /usr -xzf ollama-linux-arm64.tgz

linux 启动方式

  • 为 Ollama 创建一个用户和组:
sudo useradd -r -s /bin/false -U -m -d /usr/share/ollama ollama
sudo usermod -a -G ollama $(whoami)
  • /etc/systemd/system/ollama.service
[Unit]
Description=Ollama Service
After=network-online.target

[Service]
ExecStart=/usr/bin/ollama serve
User=ollama
Group=ollama
Restart=always
RestartSec=3
Environment="PATH=$PATH"

[Install]
WantedBy=default.target
  • 启动服务
sudo systemctl daemon-reload
sudo systemctl enable ollama
  • debug 日志,sudo systemctl edit ollama 或手动创建文件 /etc/systemd/system/ollama.service.d/override.conf
    • 查看日志 journalctl -e -u ollamajournalctl -u ollama
    • docker 启动的,通过 docker logs 查看日志
[Service]
Environment="OLLAMA_DEBUG=1"
  • 资源不足时,配置资源限制,sudo systemctl edit ollama 或手动创建文件 /etc/systemd/system/ollama.service.d/override.conf
[Service]
Environment="OLLAMA_NUM_PARALLEL=4"       # 并行处理请求的数量
Environment="OLLAMA_MAX_LOADED_MODELS=4"  # 同时加载的模型数量
  • 配置 Ollama 监听地址
[Service]
Environment="OLLAMA_HOST=0.0.0.0"

mac

wget https://ollama.com/download/Ollama-darwin.zip

使用

ollam help

ollama --help ...
$ ollama --help
Large language model runner

Usage:
  ollama [flags]
  ollama [command]

Available Commands:
  serve       Start ollama
  create      Create a model from a Modelfile
  show        Show information for a model
  run         Run a model
  pull        Pull a model from a registry
  push        Push a model to a registry
  list        List models
  ps          List running models
  cp          Copy a model
  rm          Remove a model
  help        Help about any command

Flags:
  -h, --help      help for ollama
  -v, --version   Show version information

Use "ollama [command] --help" for more information about a command.

支持的模型

$ ollama run qwen2
pulling manifest
pulling 87048bcd5521... 100% ▕███████████████████████████████████████████████████████▏ 4.7 GB
pulling 8cf247399e57... 100% ▕███████████████████████████████████████████████████████▏ 1.7 KB
pulling f1cd752815fc... 100% ▕███████████████████████████████████████████████████████▏  12 KB
pulling 56bb8bd477a5... 100% ▕███████████████████████████████████████████████████████▏   96 B
pulling e711233e7343... 100% ▕███████████████████████████████████████████████████████▏  485 B
verifying sha256 digest
writing manifest
removing any unused layers
success
>>> hi
How's it going? Is there something I can help you with or would you like to chat?

>>> /?
Available Commands:
  /set            Set session variables
  /show           Show model information
  /load <model>   Load a session or model
  /save <model>   Save your current session
  /bye            Exit
  /?, /help       Help for a command
  /? shortcuts    Help for keyboard shortcuts

Use """ to begin a multi-line message.

>>> /bye

常用命令

# 查看模本模型
$ ollama list
NAME           	ID          	SIZE  	MODIFIED
llama3.1:latest	62757c860e01	4.7 GB	3 hours ago

# 下载模型
$ ollama pull llama3.1

# 创建模型
ollama create mymodel -f ./Modelfile

# 删除模型
ollama rm llama3.1

# 复制模型
ollama cp llama3.1 my-model

# 启动模型
ollama run llama3.1
ollama run llama3.1 "Summarize this file: $(cat README.md)"

# 查看模型详情
ollama show llama3.1
# 查看模型 Modelfile
ollama show --modelfile llama3.1

# 模型以服务启动,当您想在不运行桌面应用程序的情况下启动 ollama 时
ollama serve

# 查看运行中的模型,如在 GPU 还是 CPU,参考 https://github.com/ollama/ollama/blob/main/docs/faq.md#how-can-i-tell-if-my-model-was-loaded-onto-the-gpu
# 100% GPU 表示模型完全加载到 GPU 中
# 100% CPU 表示模型完全加载到系统内存中
# 48%/52% CPU/GPU 表示模型被部分加载到 GPU 和系统内存中
$ ollama ps
NAME      	ID          	SIZE 	PROCESSOR	UNTIL
llama3:70b	bcfb190ca3a7	42 GB	100% GPU 	4 minutes from now

环境变量

  • OLLAMA_HOME=~/.ollama
  • OLLAMA_MODELS=$OLLAMA_HOME/models 指定模型的存储位置

扩展

  • unsloth 一个用来训练和微调语言模型的工具

F&Q

msg=“truncating input prompt” limit=2048 prompt=17624 keep=5 new=2048_xxx

默认情况下,Ollama 使用上下文窗口(参数 num_ctx)大小为 2048 tokens,对于比较大的上下文窗口数据会出现被截断的问题,可通过修改 num_ctx 参数控制(参考):

  • 运行时,通过命令行设置 /set parameter num_ctx 4096
  • 调用 API 时指定
curl http://localhost:11434/api/generate -d '{
  "model": "llama3.2",
  "prompt": "Why is the sky blue?",
  "options": {
    "num_ctx": 4096
  }
}'
  • 通过 Modelfile 设置参数,然后使用 ollama create xxx -f Modefile 创建新模型
FROM qwen2:7b

PARAMETER num_ctx 32768

如何将模型保存在内存中或使其立即卸载

curl http://localhost:11434/api/generate -d '{"model": "llama3.2", "keep_alive": -1}'

卸载模型时立即释放内存:

curl http://localhost:11434/api/generate -d '{"model": "llama2", "keep_alive": 0}'
  • 方法二:通过环境变量 OLLAMA_KEEP_ALIVE 控制

指定不同的 LLM 库启动

OLLAMA_LLM_LIBRARY="cpu_avx2" ollama serve

error looking up nvidia GPU memory

  • 错误日志
cuda driver library failed to get device context 800 time= level=WARN source=gpu.go:449 msg="error looking up nvidia GPU memory"

the full capacity of the model will not be utilized

n_ctx_per_seq (2048) < n_ctx_train (131072) -- the full capacity of the model will not be utilized
  • n_ctx_per_seq 是输入上限(num_ctx),Ollama 配置参数里的输入上限只有 2048 个 token,而 Llama-70B 训练时的输入上限是 131072 个 token,输入上限非常影响模型的表现,特别是做阅读理解时,能否吞下整个材料直接决定结果是否准确
  • 解决方式,通过 Modelfile 调整模型镜像参数
PARAMETER num_ctx 131072
PARAMETER num_predict -1

# ollama create <new-model-name> -f Modelfile

Out of memory when offloading layers on ROCm

$ ollama show --modelfile llama3.1:70b-instruct-q4_0 > Modelfile
# edit Modelfile and add "PARAMETER num_gpu 46"
$ ollama create llama3.1:70b-instruct-n46-q4_0 -f Modelfile

参考

  1. https://ollama.com/
  2. https://github.com/ollama/ollama
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数