返回顶部
热门问答 更多热门问答
技术文章 更多技术文章

Xinference 本地运行大模型

[复制链接]
链载Ai 显示全部楼层 发表于 10 小时前 |阅读模式 打印 上一主题 下一主题

ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">

ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">大语言模型(LLM)部署和推理工具——Xinference[1],其特点是部署快捷、使用简单、推理高效,并且支持多种形式的开源模型,还提供了 WebGUI 界面和 API 接口,方便用户进行模型部署和推理。现在就让我们一起来了解和使用 Xinference 吧!

ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-optical-sizing: inherit;font-size-adjust: inherit;font-kerning: inherit;font-feature-settings: inherit;font-variation-settings: inherit;margin: calc(2.33333em) 0px calc(1.16667em);clear: left;color: rgb(25, 27, 31);letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">1. Xinference 介绍

ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">Xorbits Inference(Xinference)是一个性能强大且功能全面的分布式推理框架。可用于各种模型的推理。通过 Xinference,你可以轻松地一键部署你自己的模型或内置的前沿开源模型。无论你是研究者,开发者,或是数据科学家,都可以通过 Xinference 与最前沿的 AI 模型,发掘更多可能。下面是 Xinference 与其他模型部署推理工具的对比:

ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">

ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-optical-sizing: inherit;font-size-adjust: inherit;font-kerning: inherit;font-feature-settings: inherit;font-variation-settings: inherit;margin: calc(2.33333em) 0px calc(1.16667em);clear: left;color: rgb(25, 27, 31);letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">2. Xinference 安装

ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">Xinference 支持两种方式的安装,一种是使用Docker镜像安装,另外一种是直接在本地源码进行安装。

ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-optical-sizing: inherit;font-size-adjust: inherit;font-kerning: inherit;font-feature-settings: inherit;font-variation-settings: inherit;margin: calc(1.90909em) 0px calc(1.27273em);clear: left;color: rgb(25, 27, 31);letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">2.1. Xinference Docker安装

ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">如果你希望在 Linux 或者 Window 服务器上部署大模型,可以使用 Xinference 官方的 Docker 镜像来一键安装和启动 Xinference 服务(确保你的机器上已经安装了 Docker 和CUDA),命令如下:

docker pull xprobe/xinference:latest
docker run -p 9997:9997 --gpus all xprobe/xinference:latest xinference-local -H 0.0.0.0

ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">启动后,我们可以通过机器的 9997 端口来访问 Xinference 服务。

2.2. Xinference 本地源码安装

首先我们需要准备一个 3.9 以上的 Python 环境运行来 Xinference,建议先根据 conda 官网文档安装 conda。 然后使用以下命令来创建 3.11 的 Python 环境:

conda create --name xinference python=3.10
conda activate xinference

以下两条命令在安装 Xinference 时,将安装 Transformers 和 vLLM 作为 Xinference 的推理引擎后端:

pip install "xinference[transformers]" -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install "xinference[vllm]" -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install "xinference[transformers,vllm]" # 同时安装
#或者一次安装所有的推理后端引擎
pip install "xinference[all]" -i https://pypi.tuna.tsinghua.edu.cn/simple

pip 在 安装 Transformers 和 vLLM 时会自动安装PyTorch,但自动安装的 CUDA 版本可能与你的环境不匹配,此时你可以根据 PyTorch 官网中的安装指南[6]来手动安装。

只需要输入如下命令,就可以在服务上启动 Xinference 服务:

xinference-local-H0.0.0.0

Xinference 默认会在本地启动服务,端口默认为 9997。因为这里配置了-H 0.0.0.0参数,非本地客户端也可以通过机器的 IP 地址来访问 Xinference 服务。

启动成功后,我们可以通过地址 http://localhost:9777来访问 Xinference 的 WebGUI 界面了。



注意:在 Xinference 安装过程中,有可能会安装 PyTorch 的其他版本(其依赖的vllm[3]组件需要安装),从而导致 GPU 服务器无法正常使用,因此在安装完 Xinference 之后,可以执行以下命令看 PyTorch 是否正常:

python -c "import torch; print(torch.cuda.is_available())"

如果输出结果为True,则表示 PyTorch 正常,否则需要重新安装 PyTorch,PyTorch 的安装方式可以参考PyTorch 的页面[4]

3. 部署本地模型(以 Qwen-14B 模型为例)

3.1 Web UI 方式启动模型

Xinference 启动之后,在浏览器中输入: http://localhost:9997,我们可以访问到本地 Xinference 的 Web UI。

打开“Launch Model”标签,搜索到 qwen-chat,选择模型启动的相关参数,然后点击模型卡片左下方的小火箭 按钮,就可以部署该模型到 Xinference。 默认 Model UID 是 qwen-chat(后续通过将通过这个 ID 来访问模型)。

当你第一次启动 Qwen 模型时,Xinference 会从HuggingFace下载模型参数,大概需要几分钟的时间。Xinference 将模型文件缓存在本地,这样之后启动时就不需要重新下载了。 Xinference 还支持从其他模型站点下载模型文件,例如modelscope[9]

3.2 命令行方式启动模型

我们也可以使用 Xinference 的命令行工具来启动模型,默认 Model UID 是 qwen-chat(后续通过将通过这个 ID 来访问模型)。

xinferencelaunch-nqwen-chat-s14-fpytorch

除了 WebUI 和命令行工具, Xinference 还提供了 Python SDK 和 RESTful API 等多种交互方式, 更多用法可以参考 Xinference官方文档[10]

3.3 模型下载源的设置

Xinference 模型下载缺省是从Huggingface官方网站下载https://huggingface.co/models。在国内因为网络原因,可以通过下面的环境变量设计为其它镜像网站:

HF_ENDPOINT=https://hf-mirror.com.

或者直接设置为:ModelScope:

通过环境变量"XINFERENCE_MODEL_SRC"设置。

XINFERENCE_MODEL_SRC=modelscope.

另外,可以通过环境变量XINFERENCE_HOME设置运行时缓存文件主目录。

export HF_ENDPOINT=https://hf-mirror.com
export XINFERENCE_MODEL_SRC=modelscope
export XINFERENCE_HOME=/jppeng/app/xinference

可以设置环境变量,临时启作用,或者设置在用户环境变量中,登陆即自动生效。

3.4. 启动Xinference服务

  • 在命令行中输入xinference,这将在本地启动后台服务。
  • 服务启动后,会自动打开一个Gradio风格的本地网页。在这里,你可以选择希望启动的模型、选择参数并启动。
  • 请确保选择的模型在右下方标明为“Chat Model”,因为只有这些模型才能接入Chatbox。

3.5. 选择合适的模型

  • Model Size 模型大小越大,对硬件资源的要求也越多。
  • 根据你的硬件配置,选择合适的模型大小。例如,对于M2芯片的MacBook Pro,可以选择7B参数规模的llama-2-chat模型。
  • 如果是第一次启动选择的模型,xinference会自动下载模型文件。

4. 模型应用

4.1. 准备工作

  • 模型下载并启动后,会自动打开一个本地网页,你可以在这里与模型进行简单的对话,测试其是否成功运行。
  • 请记得复制标题下方的Model ID,因为这将在接入Chatbox时使用。

4.2. curl命令行方式

这里仅给出curl命令行方式,不难依据下面代码构造postman,apifox之类的相关请求。其中授权码可以随便写。总体来说,兼容openAI接口方式。

curl --location --request POST 'http://192.2.22.55:9997/v1/chat/completions' \
--header 'Authorization: Bearer sk-EfEaLLdEdsco5vBa2cCaD7F6Ac7941969d51700d5f477296' \
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
--header 'Content-Type: application/json' \
--header 'Accept: */*' \
--header 'Host: 192.2.22.55:9997' \
--header 'Connection: keep-alive' \
--data-raw '{
"model": "qwen1.5-chat",
"messages": [
{
"role": "user",
"content": "假设食品类型只包括:1.豆及豆制品、2.坚果籽类及其制品、3.菌类及其制品、4.酒类及其制品、5.糖果、6.巧克力、6.蜂蜜及其制品、7.藻类及其制品、8.油脂类、9.调味品、10.其他食品、11.多种食品、12.混合食品、13.不明食品、14.保健品。依据上面给出的分类,炒花生属于哪一类?请直接给出对应的分类号,不需要分析总结。"
}
]
}'

这个接口也可以通过one-API进行包装,包装为标准的openAI接口形式。如下在的代码,只是端口改成了one-API的服务地址,且相应的授权码是通过one-API获得的有检验要求的KEY。

curl --location --request POST 'http://192.2.22.55:13000/v1/chat/completions' \
--header 'Authorization: Bearer sk-EfEaLLdEdsco5vBa2cCaD7F6Ac7941969d51700d5f477296' \
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
--header 'Content-Type: application/json' \
--header 'Accept: */*' \
--header 'Host: 192.2.22.55:13000' \
--header 'Connection: keep-alive' \
--data-raw '{
"model": "qwen1.5-chat",
"messages": [
{
"role": "user",
"content": "假设食品类型只包括:1.豆及豆制品、2.坚果籽类及其制品、3.菌类及其制品、4.酒类及其制品、5.糖果、6.巧克力、6.蜂蜜及其制品、7.藻类及其制品、8.油脂类、9.调味品、10.其他食品、11.多种食品、12.混合食品、13.不明食品、14.保健品。依据上面给出的分类,炒花生属于哪一类?请直接给出对应的分类号,不需要分析总结。"
}
]
}'

4.3. 接入ChatBot

最后就是 Chatbox 的接入环节了。

  • 确保你已安装Chatbox v1.0.0版本或更高版本。
  • 打开Chatbox,进入设置。
  • 在模型提供方选择OpenAI(Xinference完全兼容OpenAI的RESTful协议)。
  • 密钥可以随便填写。
  • 在API域名中输入xinference的地址,通常为http://192.2.22.55:997/
  • 在模型选择中选择“自定义模型”,然后在自定义模型名中输入之前复制的Model ID

4.4. 开始与AI对话

完成上述步骤后,你就可以使用Chatbox与本地的开源大模型进行聊天了。虽然回答速度可能会受到电脑的GPU配置的影响,但最大的优点是它完全免费、完全离线,不需要联网,所有数据都保存在本地,确保数据的隐私和安全性。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

链载AI是专业的生成式人工智能教程平台。提供Stable Diffusion、Midjourney AI绘画教程,Suno AI音乐生成指南,以及Runway、Pika等AI视频制作与动画生成实战案例。从提示词编写到参数调整,手把手助您从入门到精通。
  • 官方手机版

  • 微信公众号

  • 商务合作

  • Powered by Discuz! X3.5 | Copyright © 2025-2025. | 链载Ai
  • 桂ICP备2024021734号 | 营业执照 | |广西笔趣文化传媒有限公司|| QQ