LLM 本地化部署5:安装 xinference 模型推理平台

目录

Xinference 是一款开源模型推理平台,支持多种推理引擎(如 Transformers、vLLM 和 GGML),它提供多种灵活的 API 和接口,除了支持 LLM,它还可以部署 Embedding 和 ReRank 模型,这在企业级 RAG 构建中非常关键。Xinference 还支持分布式多机部署,能够在多个设备或机器间高效分配模型推理任务,满足多模型和高可用的部署需要。

1. 安装 xinference

https://inference.readthedocs.io/zh-cn/latest/getting_started/installation.html
https://inference.readthedocs.io/zh-cn/latest/user_guide/client_api.html

bash
# uv 虚拟环境 (官方推荐3.10环境)
#pip install Cython
#uv pip install "xinference[all]" --no-build-isolation
#sudo apt-get install python3.12-dev libfst-dev
uv venv xinference --python 3.10 --seed
source xinference/bin/activate
uv pip install "xinference[all]"

2. 查询模型推理引擎

bash
# 启动 xinference 本地服务
XINFERENCE_MODEL_SRC=modelscope xinference-local --host 0.0.0.0 --port 9997

# 查询LLM模型支持的所有的推理引擎
xinference engine -e http://0.0.0.0:9997 --model-name deepseek-r1

# 进一步查询vllm引擎所支持的参数组合
xinference engine -e http://0.0.0.0:9997 --model-name deepseek-r1 --model-engine vllm

# 进一步查询ggufv2格式所支持的参数组合
xinference engine -e http://0.0.0.0:9997 --model-name deepseek-r1 -f ggufv2

3. 启动和停止模型

bash
xinference launch --model-name <MODEL_NAME> \
                  [--model-engine <MODEL_ENGINE>] \
                  [--model-type <MODEL_TYPE>] \
                  [--model-uid <MODEL_UID>] \
                  [--endpoint "http://<XINFERENCE_HOST>:<XINFERENCE_PORT>"] \

示例:deepseek-r1

bash
xinference launch --model-name deepseek-r1-distill-qwen \
                  --model-engine llama.cpp \
                  --model-type LLM \
                  --model-uid deepseek/deepseek-r1-distill-qwen \
                  --endpoint "http://0.0.0.0:9997" \
                  --model-format ggufv2 \
                  --size-in-billions 7 \
                  --quantization Q4_K_M \
                  --n-gpu auto \
                  --replica 1 \
                  --n-worker 1 \
                  --model-path DeepSeek-R1-Distill-Qwen-7B-Q4_K_M.gguf \
                  --n_ctx 10240

示例:whisper-large-v3-turbo

bash
xinference launch --model-name whisper-large-v3-turbo --model-type audio

4. 管理模型

bash
# 列出 Xinference 中所有可以启动的、某种类型的模型
# type: LLM / embedding / image /audio / rerank video
xinference registrations --model-type LLM --endpoint "http://0.0.0.0:9997"

# 列出所有在运行的模型
xinference list

# 释放模型资源
xinference terminate --model-uid "qwen2.5-instruct"
ESC