模型工具安装部署(Ollama/vLLM)

一、Ollama

# 安装 Ollama,本地部署模型
irm https://ollama.com/install.ps1 | iex
# 本地部署模型
ollama pull qwen3:4b
ollama pull qwen2.5:7b
ollama pull nomic-embed-text

接口地址:http://127.0.0.1:11434

本地已拉取模型:
http://127.0.0.1:11434/api/tags

二、vLLM - WSL2/Ubuntu

1.环境准备

Debian/Ubuntu 等系统从 Python 3.11 开始禁止直接使用 pip 安装包 到系统全局环境,避免污染系统 Python,防止破坏系统依赖。

# 验证驱动
nvidia-smi

# 安装依赖
sudo apt update
sudo apt install python3-full python3-pip

# 创建虚拟环境:在当前目录新建venv文件夹作为虚拟环境
python3 -m venv vllm-venv
# 激活虚拟环境(每次使用vLLM前),退出:deactivate
source vllm-venv/bin/activate
> 激活后命令行前缀出现 `(vllm-venv)`,则进入独立 Python 环境,装包不影响系统。

2.安装/使用 vLLM

# 安装
pip install vllm

# 启动服务
export HF_ENDPOINT=https://hf-mirror.com

vllm serve microsoft/Phi-4-mini-reasoning \
  --host 0.0.0.0 \
  --port 8000 \
  --max-model-len 4096 \
  --gpu-memory-utilization 0.75 \
  --enforce-eager

命令参数:

  • vllm --version:看vLLM版本。
  • --host 0.0.0.0:允许外部访问
  • --gpu-memory-utilization 0.75:显存占用上限,0~1
  • --max-model-len:最大上下文长度
  • --enforce-eager:关闭 CUDA graph。开启 reasoning 解析时规避编译报错,但推理速度略降。
  • --enable-reasoning:“思考”和“答案”分开返回。(vLLM 0.9 弃用、0.10 移除)
  • --reasoning-parser phi4/deepseek_r1:用指定推理解析器​ 从模型输出里拆出“思考”和“答案”
  • --trust-remote-code:允许加载模型仓库里的自定义 Python 代码。(Phi-4-mini-reasoning)
  • --api-key:接口鉴权(Authorization: Bearer tokenvalue)
  • --max-num-seqs 1:单次调度迭代最多同时处理 1 个请求。
  • --block-size 16:PagedAttention 里 KV Cache 一个块存 16 个 token。
  • --kv-cache-dtype fp8_e5m2:看vLLM版本。
  • --device cpu/auto:强制 vLLM 在 CPU 上跑推理,默认auto。

3.systemd 后台常驻(开机自启)

新建 /etc/systemd/system/vllm.service

[Unit]
Description=vLLM Serve Service
After=network.target

[Service]
User=用户名
WorkingDirectory=/home/用户名
Environment="HF_ENDPOINT=[https://hf-mirror.com](https://hf-mirror.com)" #国内 HuggingFace 加速
ExecStart=/home/用户名/vllm-env/bin/vllm serve Qwen/Qwen2.5-7B-Instruct --host 0.0.0.0 --port 8000 --api-key tokenvalue
Restart=on-failure

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable vllm
sudo systemctl start vllm
sudo journalctl -u vllm -f

三、vLLM - Docker

Ubuntu

docker run \
  -v $HOME/.cache/huggingface:/root/.cache/huggingface \
  -p 8000:8000 \
  --gpus all \
  --ipc=host \
  -e HF_ENDPOINT=https://hf-mirror.com \
  vllm/vllm-openai:latest \
  deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B  \
  --host 0.0.0.0 \
  --max-model-len 4096 \
  --gpu-memory-utilization 0.75 \
  --enforce-eager

Windows-PowerShell

docker run `
  -v $HOME/.cache/huggingface:/root/.cache/huggingface `
  -p 8400:8000 `
  --gpus all `
  --ipc=host `
  -e HF_ENDPOINT=https://hf-mirror.com `
  vllm/vllm-openai:latest `
  steadyflow/Phi-4-mini-reasoning-AWQ `
  --host 0.0.0.0 `
  --max-model-len 4096 `
  --gpu-memory-utilization 0.75 `
  --enforce-eager `
  --reasoning-parser phi4 --trust-remote-code `
  --api-key tokenvalue `
  --kv-cache-dtype fp8_e5m2 `
  --max-num-seqs 1 `
  --block-size 16 `
  --device cpu `

命令参数:

  • docker run --rm --entrypoint pip vllm/vllm-openai:latest show vllm:看vLLM版本。
  • -v $HOME/.cache/huggingface:/root/.cache/huggingface:挂载宿主机目录,避免重复下载模型。
  • --gpus all:把宿主机全部 NVIDIA GPU 传给容器,需要 nvidia-container-toolkit。
  • --ipc=host:共享宿主机 IPC 命名空间,作废shm-size。
  • --shm-size=8g:容器独享的共享内存大小,优先ipc=host。
  • -e HF_ENDPOINT=https://hf-mirror.com:镜像下载加速。

四、vLLM - 验证访问

curl http://localhost:8000/v1/models

接口文档:
http://127.0.0.1:8000/docs

posted on 2026-10-22 21:49  鱼歌  阅读(34)  评论(0)    收藏  举报

导航