XInference 安装与使用指南一、安装与启动1. 创建虚拟环境# 在项目目录下创建虚拟环境uv venv# 激活虚拟环境WindowsD:\software\xinference\.venv\Scripts\activate所有 Python 项目都应使用虚拟环境避免依赖冲突。2. 安装 XInferenceuv pipinstallxinference3. 启动服务xinference-local--host0.0.0.0--port9997参数说明--host 0.0.0.0允许其他机器访问。Windows 下建议直接用实际 IP 替换--port 9997服务端口可按需修改4. 验证安装xinference--version启动后访问http://localhost:9997进入 Web UI。二、部署模型通过 Web UI 部署打开http://localhost:9997点击「启动模型」选择模型类型LLM / Embedding / Rerank填写模型参数点击小火箭图标启动注意基础版 XInference 依赖不全不同模型可能报错。根据控制台提示安装对应版本的依赖包即可。查看模型状态在 Web UI 的运行模型页面可以看到已部署模型的状态。状态为Ready表示加载完成可以正常调用。三、环境管理1. 主环境与子环境Xinference 有两层环境隔离环境路径用途主环境D:\software\xinference\.venv\运行 Xinference 服务本身子环境D:\models\virtualenv\v4\模型名\引擎\Python版本\每个模型独立的推理环境子环境路径示例D:\models\virtualenv\v4\bge-m3\sentence_transformers\3.12.10\ D:\models\virtualenv\v4\qwen2.5-instruct\llama.cpp\3.12.10\ D:\models\virtualenv\v4\Qwen3-Reranker-0.6B\sentence_transformers\3.12.10\为什么要有子环境不同模型依赖不同的推理引擎sentence-transformers / vllm / xllamacpp / diffusers 等如果全塞主环境里会版本冲突。子环境让每个模型各用各的依赖。2. 模型用没在用 GPU查看已部署模型的accelerators字段curl-shttp://127.0.0.1:9997/v1/modelsaccelerators: [0]表示绑定到 GPU 0accelerators: []表示纯 CPU。3. 判断依赖该装到哪个环境报错时机看报错路径该装哪启动 xinference 时...xinference\.venv\...主环境启动/运行模型时...virtualenv\v4\模型名\...子环境4. 安装依赖的两种方法方法一激活后安装# 激活主环境D:\software\xinference\.venv\Scripts\activate uv pipinstall包名# 激活子环境以 bge-m3 为例D:\models\virtualenv\v4\bge-m3\sentence_transformers\3.12.10\Scripts\activate uv pipinstall包名方法二不激活直接指定环境# 主环境uv pipinstall--pythonD:/software/xinference/.venv/Scripts/python.exe包名# 子环境uv pipinstall-pD:/models/virtualenv/v4/bge-m3/sentence_transformers/3.12.10包名5. 注意事项子环境的torch/torchvision/torchaudio必须从同一个索引源一起装不能混搭uv pipinstall-p子环境路径torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu130你本地 venv 没有 pipuv 默认不装安装依赖统一用uv pip install怕搞混环境时先uv pip list | grep torch确认当前在哪四、代码调用LangChain 示例LLM 模型fromlangchain_openaiimportChatOpenAI llmChatOpenAI(modelos.environ.get(LLM_MODEL,qwen2.5-7b),api_keyos.environ.get(OPENAI_API_KEY,not-needed),base_urlos.environ.get(OPENAI_BASEURL,http://ip:9997/v1))Embedding 模型fromlangchain_openaiimportOpenAIEmbeddings embeddingsOpenAIEmbeddings(modelbge-m3,api_keynot-needed,base_urlf{os.getenv(XINFERENCE_HOST,http://ip:9997)}/v1)Rerank 模型importrequests responserequests.post(f{host}/v1/rerank,json{model:os.getenv(RERANK_MODEL),query:query,documents:texts})