Qwen1.5:使用VLLM加速推理模型(包含:OpenAI如何调用)

发布于:2024-04-24 ⋅ 阅读:(21) ⋅ 点赞:(0)

如何安装和配置VLLM库

在开始使用VLLM库之前,首先需要确保安装正确的版本。以下指南将帮助你完成安装和配置。

安装VLLM库

根据你的需求选择合适的版本进行安装。如果你只需要基本功能,可以安装版本0.3。使用以下命令安装:

pip install vllm==0.3

如果你需要使用GPTQ-int8量化模型,那么应该安装0.4或更高版本:

pip install vllm>=0.4

启动模型服务

在安装完毕后,可以通过以下命令启动模型服务。注意根据你的实际需求调整GPU设备编号和模型路径:

CUDA_VISIBLE_DEVICES=1 python -m vllm.entrypoints.openai.api_server --host 0.0.0.0 --port 5000 --served-model-name Qwen1.5-14B-Chat-GPTQ-Int8 --model /home/Qwen1.5-14B-Chat-GPTQ-Int8 --quantization gptq --max-model-len 3840

参数解释:

  • --quantization gptq:此参数用于启动量化模型。对于非量化版本的模型不需要添加此参数,而对于AWQ模型则应改为--quantization awq
  • --max-model-len:此参数可根据GPU的性能自行调整。不添加此参数时,系统将尝试使用最大可能的序列长度。

调整模型序列长度

在某些情况下,可能会因为GPU内存限制而需要调整模型的最大序列长度。如果遇到以下错误,说明序列长度超出了GPU的KV缓存限制:

ValueError: The model's max seq len (19008) is larger than the maximum number of tokens that can be stored in KV cache (3840). Try increasing `gpu_memory_utilization` or decreasing `max_model_len` when initializing the engine.

这种情况下,你应该将--max-model-len参数设置为3840或更小,如下所示:

--max-model-len 3840

通过以上步骤,你可以成功安装并配置VLLM库,根据具体需求调整运行参数,以确保模型的稳定运行。


OpenAI如何调用

如果你想使用 OpenAI 的官方客户端库来调用模型。首先,确保安装了 openai 客户端库:

pip install openai

然后,使用 openai.Completion.create 方法来调用你的模型,并传入必要的参数。

from openai import OpenAI
client = OpenAI(base_url="http://0.0.0.0:5000/v1, api_key="EMPTY")

completion = client.chat.completions.create(
  model="Qwen1.5-14B-Chat-GPTQ-Int8",
  messages=[
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Who won the world series in 2020?"},
    {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
    {"role": "user", "content": "Where was it played?"}
  ]
)

模型的回复可以通过以下方式提取:

completion.choices[0].message.content