Xorbits Inference(Xinference):一款性能强大且功能全面的大模型部署与分布式推理框架

devtools/2024/9/19 11:44:01/ 标签: Xinference, LLM, 语言模型, 推理框架, 部署

大模型部署与分布式推理框架Xinference

  • Xinference的基本使用
    • 概述
    • 安装
    • 启动服务
    • 模型部署
    • 模型参数配置说明
  • API接口
    • 概述
    • 对话接口
    • 模型列表
    • 嵌入模型
    • Rerank模型
    • 使用Xinference SDK
    • 使用OpenAI SDK
  • 命令行工具
    • 概述
    • 启动模型
    • 引擎参数
    • 其他操作
  • 集成LoRA
    • 启动时集成LoRA
    • 应用时集成LoRA
  • 部署其他模型
    • 视觉模型
    • Embedding模型
    • Rerank模型
    • 图像模型
    • 语音模型
    • 自定义模型
  • 异常
    • 异常1
    • 异常2
    • 异常3

Xinference_1">Xinference的基本使用

概述

Xorbits Inference(Xinference)是一个性能强大且功能全面的分布式推理框架。可用于大语言模型LLM),语音识别模型,多模态模型等各种模型的推理。通过Xorbits Inference,你可以轻松地一键部署你自己的模型或内置的前沿开源模型。

GitHub:https://github.com/xorbitsai/inference

官方文档:https://inference.readthedocs.io/zh-cn/latest/index.html

安装

Xinference 在 Linux, Windows, MacOS 上都可以通过 pip 来安装。如果需要使用Xinference进行模型推理,可以根据不同的模型指定不同的引擎。

目前Xinference支持以下推理引擎:

vllm
sglang
llama.cpp
transformers

创建一个xinference虚拟环境,使用Python版本3.10

conda create -n xinference python=3.10

如果希望能够推理所有支持的模型,可以用以下命令安装所有需要的依赖:

pip install "xinference[all]"

使用其他引擎

# Transformers引擎
pip install "xinference[transformers]"# vLLM 引擎
pip install "xinference[vllm]"# Llama.cpp 引擎
# 初始步骤:
pip install xinference
# Apple M系列
CMAKE_ARGS="-DLLAMA_METAL=on" pip install llama-cpp-python
# 英伟达显卡:
CMAKE_ARGS="-DLLAMA_CUBLAS=on" pip install llama-cpp-python
# AMD 显卡:
CMAKE_ARGS="-DLLAMA_HIPBLAS=on" pip install llama-cpp-python# SGLang 引擎
pip install 'xinference[sglang]'

注意:

在执行安装Xinference过程中,可能会出现异常,可参考下文异常项的异常1异常2,进行解决

启动服务

可以在本地运行Xinference,也可以使用Docker部署Xinference,甚至在集群环境中部署Xinference。这里采用本地运行Xinference

执行以下命令启动本地的Xinference服务

xinference-localxinference-local --host 0.0.0.0 --port 9997

启动日志如下:

(xinference) root@master:~# xinference-local --host 0.0.0.0 --port 9997
2024-07-22 06:24:11,551 xinference.core.supervisor 312280 INFO     Xinference supervisor 0.0.0.0:50699 started
2024-07-22 06:24:11,579 xinference.model.image.core 312280 WARNING  Cannot find builtin image model spec: stable-diffusion-inpainting
2024-07-22 06:24:11,579 xinference.model.image.core 312280 WARNING  Cannot find builtin image model spec: stable-diffusion-2-inpainting
2024-07-22 06:24:11,641 xinference.core.worker 312280 INFO     Starting metrics export server at 0.0.0.0:None
2024-07-22 06:24:11,644 xinference.core.worker 312280 INFO     Checking metrics export server...
2024-07-22 06:24:13,027 xinference.core.worker 312280 INFO     Metrics server is started at: http://0.0.0.0:35249
2024-07-22 06:24:13,029 xinference.core.worker 312280 INFO     Xinference worker 0.0.0.0:50699 started
2024-07-22 06:24:13,030 xinference.core.worker 312280 INFO     Purge cache directory: /root/.xinference/cache
2024-07-22 06:24:18,087 xinference.api.restful_api 311974 INFO     Starting Xinference at endpoint: http://0.0.0.0:9997
2024-07-22 06:24:18,535 uvicorn.error 311974 INFO     Uvicorn running on http://0.0.0.0:9997 (Press CTRL+C to quit)

注意:

Xinference默认使用`<HOME>/.xinference`作为主目录存储一些必要信息,如:日志文件和模型文件通过配置环境变量`XINFERENCE_HOME`修改主目录, 比如:XINFERENCE_HOME=/tmp/xinference xinference-local --host 0.0.0.0 --port 9997

查看存储信息

(xinference) root@master:~# ls .xinference/
cache  logs
(xinference) root@master:~# ls .xinference/cache/
chatglm3-pytorch-6b
(xinference) root@master:~# ls .xinference/logs/
local_1721628924181  local_1721629451488  local_1721697225558  local_1721698858667

通过访问http://localhost:9777地址来使用Web GUI界面
在这里插入图片描述

通过访问http://localhost:9997/docs来查看 API 文档。
在这里插入图片描述

模型部署

1.搜索选择模型

点击Launch Model菜单,选择LANGUAGE MODELS标签,输入关键词以搜索需要部署的模型。这里以搜索ChatGLM3 模型为例。

在这里插入图片描述
2.模型参数配置

模型的具体参数配置参考下文:模型参数配置说明
在这里插入图片描述

3.开始部署模型

模型参数填写完成后,点击卡片左下方的火箭图标按钮开始部署模型
在这里插入图片描述
后台根据配置参数下载量化或非量化LLM模型
在这里插入图片描述
注意:

当运行一个模型时,第一次运行是要从默认或指定的模型站点下载模型参数。当下载完成后,Xinference本地会有缓存的处理,以后再运行相同的模型不需要重新下载。

4.已部署模型列表

部署完成后,界面自动跳转到Running Models菜单,在LANGUAGE MODELS标签中,可以看到部署好的模型。
在这里插入图片描述
5.LLM模型对话

点击Launch Web UI图标,自动打开LLM模型的Web界面,可以直接与LLM模型进行对话
在这里插入图片描述
进行对话测试:
在这里插入图片描述
注意:当时在进行对话测试时出现了异常,参考下文异常中的异常3

模型参数配置说明

部署LLM模型时,有以下参数可供选择:

1.必选配置:

Model Engine:模型推理引擎,根据模型不同,可能支持的引擎不同Model Format: 模型格式,可以选择量化(ggml、gptq等)和非量化(pytorch)的格式Model Size:模型的参数量大小,不同模型参数量不同,可能是: 6B、7B、13B、70B等Quantization:量化精度,有4bit、8bit等量化精度选择N-GPU:模型使用的GPU数量:可选择Auto、CPU、GPU数量,默认AutoReplica:模型的副本,默认为1

点击chatglm3卡片,填写部署模型的相关信息
在这里插入图片描述

2.可选配置:

Model UID: 模型的UID,可理解为模型自定义名称,默认用原始模型名称Request Limits: 模型的请求限制数量,默认为None。None表示此模型没有限制Worker Ip: 指定分布式场景中模型所在的工作器ipGpu Idx: 指定模型所在的GPU索引Download hub: 模型从哪里下载,可选:none、huggingface、modelscope

在这里插入图片描述

3.Lora配置:

Lora Model Config:PEFT(参数高效微调)模型和路径的列表Lora Load Kwargs for Image Model:图像模型的 lora 加载参数字典Lora Fuse Kwargs for Image Model:图像模型的 lora fuse 参数字典

在这里插入图片描述

4.传递给推理引擎的其他参数:
在这里插入图片描述

API接口

概述

除了使用LLM模型的Web界面进行操作外,Xinference还提供了API接口,通过调用API接口来使用LLM模型。

在API文档中,存在大量API接口,不仅有LLM模型的接口,还有其他模型(如Embedding)的接口,并且这些接口都是兼容OpenAI API的接口。

通过访问http://localhost:9997/docs来查看API文档。
在这里插入图片描述

对话接口

使用Curl工具调用对话接口

curl -X 'POST' \'http://localhost:9997/v1/chat/completions' \-H 'accept: application/json' \-H 'Content-Type: application/json' \-d '{"model": "chatglm3","messages": [{"role": "user","content": "你好啊"}]}'{"id":"chat73f8c754-4898-11ef-89f6-000c2981d002","object":"chat.completion","created":1721700508,"model":"chatglm3","choices":[{"index":0,"message":{"role":"assistant","content":"你好👋!我是人工智能助手 ChatGLM3-6B,很高兴见到你,欢迎问我任何问题。"},"finish_reason":"stop"}],"usage":{"prompt_tokens":-1,"completion_tokens":-1,"total_tokens":-1}}root@master:~#

模型列表

使用Curl工具调用获取模型列表

curl -X 'GET' \'http://localhost:9997/v1/models' \-H 'accept: application/json' \-H 'Content-Type: application/json' \{"object":"list","data":[{"id":"chatglm3","object":"model","created":0,"owned_by":"xinference","model_type":"LLM","address":"0.0.0.0:38145","accelerators":["0"],"model_name":"chatglm3","model_lang":["en","zh"],"model_ability":["chat","tools"],"model_description":"ChatGLM3 is the third generation of ChatGLM, still open-source and trained on Chinese and English data.","model_format":"pytorch","model_size_in_billions":6,"model_family":"chatglm3","quantization":"4-bit","model_hub":"modelscope","revision":"v1.0.2","context_length":8192,"replica":1}]}

嵌入模型

使用Curl工具调用嵌入模型接口

curl -X 'POST' \'http://localhost:9997/v1/embeddings' \-H 'accept: application/json' \-H 'Content-Type: application/json' \-d '{"model": "嵌入模型名称、UID","input": "你好啊"}'

Rerank模型

使用Curl工具调用Rerank模型接口

curl -X 'POST' \'http://localhost:9997/v1/rerank' \-H 'accept: application/json' \-H 'Content-Type: application/json' \-d '{"model": "bge-reranker-base","query": "你是谁?","documents": ["你是一名乐于助人的AI助手。","你的名字叫'rerank'"]}'

Xinference_SDK_242">使用Xinference SDK

安装Xinference的Python SDK,使用以下命令安装最少依赖。注意: 版本必须和Xinference服务的版本保持匹配。

pip install xinference-client==${SERVER_VERSION}
from xinference.client import RESTfulClientclient = RESTfulClient("http://127.0.0.1:9997")
# 注意:my-llm是参数`--model-uid`指定的值
model = client.get_model("my-llm")
print(model.chat(prompt="你好啊",system_prompt="你是一个乐于助人的AI助手。",chat_history=[]
))

使用OpenAI SDK

Xinference提供了与OpenAI兼容的API,所以可以将Xinference运行的模型当成OpenAI的本地替代。

from openai import OpenAI
client = OpenAI(base_url="http://127.0.0.1:9997/v1", api_key="")response = client.chat.completions.create(model="my-llm",messages=[{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": "What is the largest animal?"}]
)
print(response)

命令行工具

概述

Xinference提供了管理模型整个生命周期的能力。同样也可以使用命令行、cURL以及Python代码来管理

执行以下命令以安装xinference命令行工具

pip install xinferenc

查看帮助命令

(xinference) root@master:~# xinference --help
Usage: xinference [OPTIONS] COMMAND [ARGS]...Xinference command-line interface for serving and deploying models.Options:-v, --version       Show the current version of the Xinference tool.--log-level TEXT    Set the logger level. Options listed from most log toleast log are: DEBUG > INFO > WARNING > ERROR > CRITICAL(Default level is INFO)-H, --host TEXT     Specify the host address for the Xinference server.-p, --port INTEGER  Specify the port number for the Xinference server.--help              Show this message and exit.Commands:cached         List all cached models in Xinference.cal-model-mem  calculate gpu mem usage with specified model size and...chat           Chat with a running LLM.engine         Query the applicable inference engine by model name.generate       Generate text using a running LLM.launch         Launch a model with the Xinference framework with the...list           List all running models in Xinference.login          Login when the cluster is authenticated.register       Register a new model with Xinference for deployment.registrations  List all registered models in Xinference.remove-cache   Remove selected cached models in Xinference.stop-cluster   Stop a cluster using the Xinference framework with the...terminate      Terminate a deployed model through unique identifier...unregister     Unregister a model from Xinference, removing it from...vllm-models    Query and display models compatible with vLLM.

启动模型

使用Xinference框架启动一个模型,Xinference提供了xinference launch命令帮助查询相关的参数配置。

(xinference) root@master:~# xinference launch --help
Usage: xinference launch [OPTIONS]Launch a model with the Xinference framework with the given parameters.Options:-e, --endpoint TEXT             Xinference endpoint.-n, --model-name TEXT           Provide the name of the model to belaunched.  [required]-t, --model-type TEXT           Specify type of model, LLM as default.-en, --model-engine TEXT        Specify the inference engine of the modelwhen launching LLM.-u, --model-uid TEXT            Specify UID of model, default is None.-s, --size-in-billions TEXT     Specify the model size in billions ofparameters.-f, --model-format TEXT         Specify the format of the model, e.g.pytorch, ggmlv3, etc.-q, --quantization TEXT         Define the quantization settings for themodel.-r, --replica INTEGER           The replica count of the model, default is1.--n-gpu TEXT                    The number of GPUs used by the model,default is "auto".-lm, --lora-modules <TEXT TEXT>...LoRA module configurations in the formatname=path. Multiple modules can bespecified.-ld, --image-lora-load-kwargs <TEXT TEXT>...-fd, --image-lora-fuse-kwargs <TEXT TEXT>...--worker-ip TEXT                Specify which worker this model runs on byip, for distributed situation.--gpu-idx TEXT                  Specify which GPUs of a worker this modelcan run on, separated with commas.--trust-remote-code BOOLEAN     Whether or not to allow for custom modelsdefined on the Hub in their own modelingfiles.-ak, --api-key TEXT             Api-Key for access xinference api withauthorization.--help                          Show this message and exit.
(xinference) root@master:~# xinference launch --help

启动一个模型:

xinference launch --model-engine transformers --model-uid my-llm --model-name chatglm3 --quantization 4-bit --size-in-billions 6 --model-format pytorch

参数说明:

--model-engine transformers:指定模型的推理引擎
--model-uid:指定模型的UID,如果没有指定,则随机生成一个ID
--model-name:指定模型名称
--quantization: 指定模型量化精度
--size-in-billions:指定模型参数大小,以十亿为单位
--model-format:指定模型的格式

成功启动日志如下:

(xinference) root@master:~# xinference launch --model-engine transformers --model-uid myllm --model-name chatglm3 --quantization 4-bit --size-in-billions 6 --model-format pytorch
Launch model name: chatglm3 with kwargs: {}
Model uid: myllm

访问http://localhost:9777,查看已运行的模型
在这里插入图片描述

引擎参数

当加载LLM模型时,推理引擎与模型的参数息息相关。Xinference提供了xinference engine命令帮助查询相关的参数组合。

(xinference) root@master:~# xinference engine --help
Usage: xinference engine [OPTIONS]Query the applicable inference engine by model name.Options:-n, --model-name TEXT           The model name you want to query.[required]-en, --model-engine TEXT        Specify the `model_engine` to query thecorresponding combination of otherparameters.-f, --model-format TEXT         Specify the `model_format` to query thecorresponding combination of otherparameters.-s, --model-size-in-billions TEXTSpecify the `model_size_in_billions` toquery the corresponding combination of otherparameters.-q, --quantization TEXT         Specify the `quantization` to query thecorresponding combination of otherparameters.-e, --endpoint TEXT             Xinference endpoint.-ak, --api-key TEXT             Api-Key for access xinference api withauthorization.--help                          Show this message and exit.

1.查询与chatglm3模型相关的参数组合,以决定它能够怎样跑在各种推理引擎上。

(xinference) root@master:~# xinference engine --model-name chatglm3
Name      Engine        Format      Size (in billions)  Quantization
--------  ------------  --------  --------------------  --------------
chatglm3  Transformers  pytorch                      6  4-bit
chatglm3  Transformers  pytorch                      6  8-bit
chatglm3  Transformers  pytorch                      6  none
chatglm3  vLLM          pytorch                      6  none

2.想将chatglm3跑在vllm、transformers推理引擎上,但是不知道什么样的其他参数符合这个要求

(xinference) root@master:~# xinference engine --model-name chatglm3 --model-engine vllm
Name      Engine    Format      Size (in billions)  Quantization
--------  --------  --------  --------------------  --------------
chatglm3  vLLM      pytorch                      6  none(xinference) root@master:~#  xinference engine --model-name chatglm3 --model-engine transformers
Name      Engine        Format      Size (in billions)  Quantization
--------  ------------  --------  --------------------  --------------
chatglm3  Transformers  pytorch                      6  4-bit
chatglm3  Transformers  pytorch                      6  8-bit
chatglm3  Transformers  pytorch                      6  none

3.加载GGUF格式的qwen-chat模型,需要知道其余的参数组合

chatglm3模型不支持参数: --model-format ggufv2

(xinference) root@master:~# xinference engine --model-name qwen-chat -f ggufv2
Name       Engine     Format      Size (in billions)  Quantization
---------  ---------  --------  --------------------  --------------
qwen-chat  llama.cpp  ggufv2                       7  Q4_K_M
qwen-chat  llama.cpp  ggufv2                      14  Q4_K_M

其他操作

列出所有 Xinference 支持的指定类型的模型:

xinference registrations -t LLM

列出所有在运行的模型:

xinference list

当不需要某个正在运行的模型,可以通过以下的方式来停止它并释放资源:

xinference terminate --model-uid "my-llm"

集成LoRA

Xinference 可以在启动 LLM 和 image 模型时连带一个 LoRA 微调模型用以辅助基础模型。

启动时集成LoRA

Xinference目前不会涉及管理 LoRA 模型。用户需要首先下载对应的 LoRA 模型,然后将模型存储路径提供给 Xinference

xinference launch <options>
--lora-modules <lora_name1> <lora_model_path1>
--lora-modules <lora_name2> <lora_model_path2>
--image-lora-load-kwargs <load_params1> <load_value1>
--image-lora-load-kwargs <load_params2> <load_value2>
--image-lora-fuse-kwargs <fuse_params1> <fuse_value1>
--image-lora-fuse-kwargs <fuse_params2> <fuse_value2>
from xinference.client import Clientclient = Client("http://<XINFERENCE_HOST>:<XINFERENCE_PORT>")lora_model1={'lora_name': <lora_name1>, 'local_path': <lora_model_path1>}
lora_model2={'lora_name': <lora_name2>, 'local_path': <lora_model_path2>}
lora_models=[lora_model1, lora_model2]
image_lora_load_kwargs={'<load_params1>': <load_value1>, '<load_params2>': <load_value2>},
image_lora_fuse_kwargs={'<fuse_params1>': <fuse_value1>, '<fuse_params2>': <fuse_value2>}peft_model_config = {
"image_lora_load_kwargs": image_lora_load_params,
"image_lora_fuse_kwargs": image_lora_fuse_params,
"lora_list": lora_models
}client.launch_model(<other_options>,peft_model_config=peft_model_config
)

注意: image_lora_load_kwargsimage_lora_fuse_kwargs 选项只应用于 image 模型。它们对应于 diffusers 库的 load_lora_weights 和 fuse_lora 接口中的额外参数。如果启动的是 LLM 模型,则无需设置这些选项。

应用时集成LoRA

对于大语言模型,使用时指定其中一个 lora 。具体地,在 generate_config 参数中配置 lora_name 参数。lora_name 对应 launch 过程中你的配置。

from xinference.client import Clientclient = Client("http://<XINFERENCE_HOST>:<XINFERENCE_PORT>")
model = client.get_model("<model_uid>")
model.chat("<prompt>",<other_options>,generate_config={"lora_name": "<your_lora_name>"}
)

部署其他模型

注意:可能由于Xinference版本或者与模型不完全适配会出现一些问题,可选择降低Xinference版本或更换类似模型。相信Xinference会越来越完善。

视觉模型

部署

视觉模型是指用于处理和分析视觉数据(如图像和视频)的机器学习或深度学习模型。这些模型的主要目标是理解和解释视觉信息,执行多种任务,包括图像分类、目标检测、图像分割、图像生成等。

可以让模型接收图像并回答有关它们的问题。

视觉模型部署方式与LLM模型部署大同小异,首先点击Launch Model菜单,在LANGUAGE MODELS标签下选择多模态模型。

输入关键词以搜索需要部署的模型。这里以先过滤模型,再搜索选择glm-4v模型为例。
在这里插入图片描述
填写部署模型相关参数,执行部署操作
在这里插入图片描述
后台同样可以看到模型下载信息
在这里插入图片描述
部署完成,查看运行的模型
在这里插入图片描述

使用Web

使用图片和文字与视觉模型进行对话
在这里插入图片描述

使用API

模型可以通过两种主要方式获取图像:通过传递图像的链接或直接在请求中传递 base64 编码的图像。

1.使用OpenAI

import openaiclient = openai.Client(api_key="cannot be empty",base_url=f"http://<XINFERENCE_HOST>:<XINFERENCE_PORT>/v1"
)
response = client.chat.completions.create(model="<MODEL_UID>",messages=[{"role": "user","content": [{"type": "text", "text": "What’s in this image?"},{"type": "image_url","image_url": {"url": "http://xxxx.jpg",},},],}],
)
print(response.choices[0])

2.上传Base64编码的图片

import openai
import base64# Function to encode the image
def encode_image(image_path):
with open(image_path, "rb") as image_file:return base64.b64encode(image_file.read()).decode('utf-8')# Path to your image
image_path = "path_to_your_image.jpg"# Getting the base64 string
b64_img = encode_image(image_path)client = openai.Client(api_key="cannot be empty",base_url=f"http://<XINFERENCE_HOST>:<XINFERENCE_PORT>/v1"
)
response = client.chat.completions.create(model="<MODEL_UID>",messages=[{"role": "user","content": [{"type": "text", "text": "What’s in this image?"},{"type": "image_url","image_url": {"url": f"data:image/jpeg;base64,{b64_img}",},},],}],
)
print(response.choices[0])

Embedding模型

部署

Embedding模型是一种用于将高维数据(如文本、图像或其他类型的数据)转换为低维向量表示的模型。这种表示方式能够捕捉数据的语义和结构信息,使得相似的对象在向量空间中距离更近。

文本嵌入用于量化不同文本之间的相关性。它们可以应用于各种应用程序,包括搜索、聚类、推荐、异常检测、多样性度量和分类。

嵌入是一组浮点数的向量。两个向量之间的接近程度可以作为它们相似性的指标。距离越小表示相关性越高,而距离越大则表示相关性降低。

首先点击Launch Model菜单,在Embedding Models标签下选择嵌入模型。输入关键词以搜索需要部署的模型,这里搜索选择bge-base-zh-v1.5模型为例。
在这里插入图片描述
对于模型参数,几乎不需要设置,直接部署模型即可。
在这里插入图片描述
等待部署、运行成功
在这里插入图片描述

使用API

使用Curl调用API接口

curl -X 'POST' \'http://localhost:9997/v1/embeddings' \-H 'accept: application/json' \-H 'Content-Type: application/json' \-d '{"model": "bge-base-zh-v1.5","input": "你好啊"
}'

Embedding模型响应结果:

{
"object":"list","model":"bge-base-zh-v1.5-1-0",
"data":[{"index":0,"object":"embedding",
"embedding":[0.029834920540452003,-0.019862590357661247,.......,-0.006424838211387396,0.012447659857571125,-0.05162930488586426]}],
"usage":{"prompt_tokens":37,"total_tokens":37}
}
import openaiclient = openai.Client(api_key="cannot be empty",base_url="http://<XINFERENCE_HOST>:<XINFERENCE_PORT>/v1"
)
client.embeddings.create(model=model_uid,input=["What is the capital of China?"]
)
from xinference.client import Clientclient = Client("http://<XINFERENCE_HOST>:<XINFERENCE_PORT>")model = client.get_model("<MODEL_UID>")
input = "What is the capital of China?"
model.create_embedding(input)

Rerank模型

部署

给定一个查询和一系列文档,Rerank 会根据与查询的语义相关性从最相关到最不相关对文档进行重新排序。在 Xinference 中,可以通过 Rerank 端点调用 Rerank 模型来对一系列文档进行排序。

首先点击Launch Model菜单,在Rerank Models标签下选择Rerank模型。输入关键词以搜索需要部署的模型,这里搜索选择bge-reranker-base模型为例。
在这里插入图片描述
对于模型参数,几乎不需要设置,直接部署模型即可。
在这里插入图片描述
等待模型部署、运行成功

使用API

可以通过cURL、OpenAI Client或Xinference的来尝试使用Rerank API:

curl -X 'POST' \'http://<XINFERENCE_HOST>:<XINFERENCE_PORT>/v1/rerank' \-H 'accept: application/json' \-H 'Content-Type: application/json' \-d '{"model": "<MODEL_UID>","query": "A man is eating pasta.","documents": ["A man is eating food.","A man is eating a piece of bread.","The girl is carrying a baby.","A man is riding a horse.","A woman is playing violin."]}'
from xinference.client import Clientclient = Client("http://<XINFERENCE_HOST>:<XINFERENCE_HOST>")
model = client.get_model(<MODEL_UID>)query = "A man is eating pasta."
corpus = ["A man is eating food.","A man is eating a piece of bread.","The girl is carrying a baby.","A man is riding a horse.","A woman is playing violin."
]
print(model.rerank(corpus, query))

图像模型

部署

图像模型是指用于处理、分析和理解图像数据的机器学习或深度学习模型。这些模型可以执行多种任务,如图像分类、目标检测、图像分割、图像生成等。

首先点击Launch Model菜单,在Image Models标签下选择嵌入模型。这里搜索选择stable-diffusion-v1.5模型为例。
在这里插入图片描述
对于模型参数,几乎不需要设置,直接部署模型即可。这里指定模型下载站点。
在这里插入图片描述
部署完成,查看运行的模型
在这里插入图片描述

使用Web

在这个Web界面可以使用文生图、图生图等功能
在这里插入图片描述

使用API

通过 cURL、OpenAI Client 或 Xinference 的方式尝试使用 Text-to-image API。

Images API提供了两种与图像交互的方法:

文生图端点根据文本从零开始创建图像。

图生图端点允许您生成给定图像的变体。

API 端点OpenAI 兼容端点
Text-to-Image API/v1/images/generations
Image-to-image API/v1/images/variations

使用curl

curl -X 'POST' \'http://<XINFERENCE_HOST>:<XINFERENCE_PORT>/v1/images/generations' \-H 'accept: application/json' \-H 'Content-Type: application/json' \-d '{"model": "<MODEL_UID>","prompt": "an apple",}'

使用openai

import openaiclient = openai.Client(api_key="cannot be empty",base_url="http://<XINFERENCE_HOST>:<XINFERENCE_PORT>/v1"
)
client.images.generate(model=<MODEL_UID>,prompt="an apple"
)

使用Xinference Client

from xinference.client import Clientclient = Client("http://<XINFERENCE_HOST>:<XINFERENCE_PORT>")model = client.get_model("<MODEL_UID>")
input_text = "an apple"
model.text_to_image(input_text)

语音模型

部署

语音模型是指用于处理和分析语音数据的机器学习或深度学习模型。这些模型旨在理解语音信号,执行各种任务,如语音识别、语音合成、说话人识别等。

使用 Xinference 将音频转换为文本或将文本转换为音频。
在这里插入图片描述
在这里插入图片描述

使用API

Audio API提供了三种与音频交互的方法:

API端点OpenAI兼容端点描述
Transcription API/v1/audio/transcriptions转录终端将音频转录为输入语言
Translation API/v1/audio/translations翻译端点将音频转换为英文
Speech API/v1/audio/speech转录终端将音频转录为输入语言

可以通过 cURL、OpenAI Client 或者 Xinference 的 Python 客户端来尝试 Transcription API:

1.转录

curl -X 'POST' \'http://<XINFERENCE_HOST>:<XINFERENCE_PORT>/v1/audio/transcriptions' \-H 'accept: application/json' \-H 'Content-Type: application/json' \-d '{"model": "<MODEL_UID>","file": "<audio bytes>",}'
import openaiclient = openai.Client(api_key="cannot be empty",base_url="http://<XINFERENCE_HOST>:<XINFERENCE_PORT>/v1"
)
with open("speech.mp3", "rb") as audio_file:client.audio.transcriptions.create(model=<MODEL_UID>,file=audio_file,)
from xinference.client import Clientclient = Client("http://<XINFERENCE_HOST>:<XINFERENCE_PORT>")model = client.get_model("<MODEL_UID>")
with open("speech.mp3", "rb") as audio_file:model.transcriptions(audio=audio_file.read())

2.翻译

curl -X 'POST' \'http://<XINFERENCE_HOST>:<XINFERENCE_PORT>/v1/audio/translations' \-H 'accept: application/json' \-H 'Content-Type: application/json' \-d '{"model": "<MODEL_UID>","file": "<audio bytes>",}'
import openaiclient = openai.Client(api_key="cannot be empty",base_url="http://<XINFERENCE_HOST>:<XINFERENCE_PORT>/v1"
)
with open("speech.mp3", "rb") as audio_file:client.audio.translations.create(model=<MODEL_UID>,file=audio_file,)
from xinference.client import Clientclient = Client("http://<XINFERENCE_HOST>:<XINFERENCE_PORT>")model = client.get_model("<MODEL_UID>")
with open("speech.mp3", "rb") as audio_file:model.translations(audio=audio_file.read())

3.语音

curl -X 'POST' \'http://<XINFERENCE_HOST>:<XINFERENCE_PORT>/v1/audio/speech' \-H 'accept: application/json' \-H 'Content-Type: application/json' \-d '{"model": "<MODEL_UID>","text": "<The text to generate audio for>","voice": "echo","stream": True,}'
import openaiclient = openai.Client(api_key="cannot be empty",base_url="http://<XINFERENCE_HOST>:<XINFERENCE_PORT>/v1"
)
client.audio.speech.create(model=<MODEL_UID>,input=<The text to generate audio for>,voice="echo",
)
from xinference.client import Clientclient = Client("http://<XINFERENCE_HOST>:<XINFERENCE_PORT>")model = client.get_model("<MODEL_UID>")
model.speech(input=<The text to generate audio for>,voice="echo",stream: True,
)

自定义模型

Xinference 提供了一种灵活而全面的方式来集成、管理和应用自定义模型。
在这里插入图片描述

异常

异常1

在执行过程中,出现安装llama-cpp-python时,出现以下问题:

Building wheels for collected packages: llama-cpp-pythonBuilding wheel for llama-cpp-python (pyproject.toml) ... errorerror: subprocess-exited-with-error× Building wheel for llama-cpp-python (pyproject.toml) did not run successfully.│ exit code: 1╰─> [76 lines of output]*** scikit-build-core 0.9.8 using CMake 3.22.1 (wheel)*** Configuring CMake...loading initial cache file /tmp/tmp0pe3_qsj/build/CMakeInit.txt-- The C compiler identification is GNU 11.4.0-- The CXX compiler identification is GNU 11.4.0-- Detecting C compiler ABI info-- Detecting C compiler ABI info - done-- Check for working C compiler: /usr/bin/gcc - skipped*** CMake build failed[end of output]note: This error originates from a subprocess, and is likely not a problem with pip.ERROR: Failed building wheel for llama-cpp-python
Failed to build llama-cpp-python
ERROR: Could not build wheels for llama-cpp-python, which is required to install pyproject.toml-based projects

访问:llama-cpp-python项目
在这里插入图片描述
目前llama-cpp-python最新版本v0.2.82-cu123,根据系统版本、python版本选择下载

wget https://github.com/abetlen/llama-cpp-python/releases/download/v0.2.82-cu124/llama_cpp_python-0.2.82-cp310-cp310-linux_x86_64.whl

执行以下命令安装

pip install llama_cpp_python-0.2.82-cp310-cp310-linux_x86_64.whl

异常2

在执行过程中,如果出现安装chatglm.cpp相关异常,执行如下操作解决。

访问:chatglm.cpp项目
在这里插入图片描述
目前 chatglm.cpp最新版本v0.4.0,根据系统版本、python版本选择下载

wget https://github.com/li-plus/chatglm.cpp/releases/download/v0.4.0/chatglm_cpp-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

执行以下命令安装

pip install chatglm_cpp-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

异常3

LLM模型对话界面出现异常提示:

Error
[address=0.0.0.0:34387, pid=354238] GenerationMixin._get_logits_warper() missing 1 required positional argument: 'device'

后台运行异常提示:

Exception: [address=0.0.0.0:34387, pid=354238] GenerationMixin._get_logits_warper() missing 1 required positional argument: 'device'

参阅GitHub项目的issues,对transformers进行降级

(xinference) root@master:~# pip list | grep  transformers
sentence-transformers             3.0.1
transformers                      4.42.4
transformers-stream-generator     0.0.5
(xinference) root@master:~# pip install 'transformers==4.41.2'

注意:应该是目前Xinference版本原因,后台任然会有异常输出,但是界面LLM对话是正常的。预计未来版本会更新处理。


http://www.ppmy.cn/devtools/114133.html

相关文章

STM32 单片机最小系统全解析

STM32 单片机最小系统全解析 本文详细介绍了 STM32 单片机最小系统&#xff0c;包括其各个组成部分及设计要点与注意事项。STM32 最小系统在嵌入式开发中至关重要&#xff0c;由电源、时钟、复位、调试接口和启动电路等组成。 在电源电路方面&#xff0c;采用 3.3V 直流电源供…

【微服务-注册中心】

注册中心的作用&#xff1a; 微服务将业务拆分成了一个一个服务&#xff0c;当实现一个业务的时需要调用多个服务&#xff0c;那么每个服务的调用都需要知道它的URL。如何更方便的调用&#xff0c;注册中心就出现了。 我们可以把注册中心当作通讯录&#xff0c;通讯录中记录了服…

【C++】猜数字小游戏

写一个简单的C程序&#xff0c;用于实现一个猜数字游戏。 要求&#xff1a; 生成一个1到100之间的随机数&#xff0c;并让玩家猜测这个数是什么。如果玩家的猜测不正确&#xff0c;程序会提示猜测过大或过小&#xff0c;直到玩家猜对为止。 要点&#xff1a; _CRT_SECURE_NO…

[数据集][目标检测]智慧养殖场肉鸡目标检测数据集VOC+YOLO格式3548张1类别

数据集格式&#xff1a;Pascal VOC格式YOLO格式(不包含分割路径的txt文件&#xff0c;仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数)&#xff1a;3548 标注数量(xml文件个数)&#xff1a;3548 标注数量(txt文件个数)&#xff1a;3548 标注…

智能体趋势:未来科技的核心驱动力

随着人工智能&#xff08;AI&#xff09;技术的不断发展&#xff0c;**智能体&#xff08;intelligent agents&#xff09;**逐渐成为当今科技发展的重要趋势。这些智能体不仅仅是软件&#xff0c;它们正在改变我们生活和工作的方式&#xff0c;成为推动科技和社会变革的核心力…

C#中的委托

Action委托 Action 委托是 C# 中预定义的委托类型之一&#xff0c;它是泛型委托 Action<T...> 的非泛型形式&#xff0c;用于表示不接受返回值的方法。Action 委托通常用于需要执行操作但不需要返回结果的场景&#xff0c;比如事件处理、异步编程或回调函数。 Action 委…

QT实现TCP协议

QT中实现服务器原理 QT中实现客户端原理 网络聊天室服务器实现 用QTcpServer服务器类实例化一个服务器对象通过listen&#xff08;&#xff09;函数&#xff0c;监听客户端&#xff0c;监听可以监听指定主机&#xff0c;也可以监听任意主机&#xff0c;监听的端口号&#xff0…

数据库连接池与Druid【后端 16】

数据库连接池与Druid 在现代软件开发中&#xff0c;数据库连接池作为一种关键的技术手段&#xff0c;被广泛用于提升数据库访问的效率和稳定性。本文将深入探讨数据库连接池的概念、常见实现&#xff0c;并重点介绍我国阿里集团开源的数据库连接池——Druid&#xff0c;以及如何…

10款超好用的文档加密软件|企业常用的文档加密软件排行榜

在当今信息化时代&#xff0c;文档加密已成为企业保障数据安全的关键手段之一。无论是保护敏感的财务数据、合同文件&#xff0c;还是防止机密信息泄露&#xff0c;文档加密软件都是企业日常运营不可或缺的工具。本文将介绍10款企业常用的文档加密软件&#xff0c;并为您提供详…

web技术栈总体概念

Web技术栈是指用于开发和运行Web应用程序的一组技术集合&#xff0c;它涵盖了前端、后端、数据库以及相关的开发工具和实用程序等多个方面。具体来说&#xff0c;Web技术栈主要包括以下几个部分&#xff1a; 一、前端技术栈 前端技术栈主要负责构建用户界面和处理用户交互。它…

【TabBar嵌套Navigation案例-复习昨天的内容-预习今天的内容 Objective-C语言】

一、复习与预习 1.我们昨天呢,是从发现开始讲的 发现那个页面,就是一个静态单元格, 点第一个合买,首先,隐藏tabbar, 一开始,是用hideBottomBarOnPush,然后,你需要把你自定义的这个tabbar,加到系统的tabbar里边,然后,再去勾选这个选项,因为那个选项,只是隐藏系统…

C++编程:多线程环境下std::vector内存越界导致的coredump问题分析

文章目录 1. 背景描述2. 问题分析3. 问题复现示例4. 数据竞争&#xff1a;并发访问未加锁的共享数据 5. 解决方案5.1 方法一&#xff1a;提前resize分配足够的内存5.2 方法二&#xff1a;使用同步机制保护共享资源&#xff08;最优解&#xff09; 6. 问题定位总结6.1 内存越界难…

6年前倒闭的机器人独角兽,再次杀入AGV市场

导语 大家好&#xff0c;我是社长&#xff0c;老K。专注分享智能制造和智能仓储物流等内容。 新书《智能物流系统构成与技术实践》 在科技创新的浪潮中&#xff0c;一个曾经辉煌又迅速陨落的企业正悄然重生&#xff0c;引发业界广泛关注。 曾经的协作机器人鼻祖Rethink Robotic…

堆的概念与实现

目录 一、堆的介绍 1.堆的概念 2.堆的性质&#xff1a; 3.堆的结构 二、堆的实现 1.堆的定义 2.接口函数 三、堆的实现 1.堆的初始化 2.堆的销毁 3.获取堆顶数据 4.判断堆是否为空 5. 堆的插入 向上调整算法&#xff08;重点&#xff09; 向下调整算法(重点) 6.删除…

el-table多选,分页切换时,选中内容不变;清空多选的内容

el-table中添加:row-key“getRowKeys” 设置true【 :reserve-selection“true”】 :row-key"getRowKeys" <el-table-column type"selection" :reserve-selection"true" width"55" align"center" fixed"left" …

day14-单例设计模式动态代理

一、单例设计模式 单例设计模式作用&#xff1a;确保一个类只有一个对象。场景&#xff1a;计算机中的回收站、任务管理器、Java中的Runtime类等好处&#xff1a;在这些业务场景下&#xff0c;使用单例模式&#xff0c;可以避免浪费内存。 1.1 饿汉式 饿汉式(提前创建对象)把类…

828华为云征文|华为云Flexus X实例docker部署Rocket.Chat构建属于自己的团队通讯协作平台

828华为云征文&#xff5c;华为云Flexus X实例docker部署Rocket.Chat构建属于自己的团队通讯协作平台 华为云最近正在举办828 B2B企业节&#xff0c;Flexus X实例的促销力度非常大&#xff0c;特别适合那些对算力性能有高要求的小伙伴。如果你有自建MySQL、Redis、Nginx等服务…

闲鱼网页版开放,爬虫的难度指数级降低。

爬虫&#xff0c;可以说是程序员最基础的热手项目。 之前我也一直说阿里系的签名系统搞得太复杂&#xff0c;风控太高&#xff0c;很不利于正常的自动化工具开发&#xff0c;这对于需要阿里应用的客户来说&#xff0c;也是一个很难覆盖的成本支出不是。 当然&#xff0c;我做项…

iPhone 16系列:摄影艺术的全新演绎,探索影像新境界

在科技的浪潮中&#xff0c;智能手机摄影功能的进化从未停歇。 苹果公司即将推出的iPhone 16系列&#xff0c;以其卓越的相机升级和创新特性&#xff0c;再次站在了手机摄影的前沿。 从硬件到软件&#xff0c;从拍照体验到图像处理&#xff0c;iPhone 16系列都展现了其在移动…

python毕业设计基于django+vue医院社区医疗挂号预约综合管理系统7918h-pycharm-flask

目录 技术栈和环境说明预期达到的目标具体实现截图系统设计Python技术介绍django框架介绍flask框架介绍解决的思路性能/安全/负载方面可行性分析论证python-flask核心代码部分展示python-django核心代码部分展示操作可行性技术路线感恩大学老师和同学详细视频演示源码获取 技术…