当前位置: 首页 > news >正文

VLLM实现大模型服务的部署

文章目录

  • 安装
  • 离线推理
  • 适配openAI-API的API服务
    • 使用python命令行部署
    • 使用docker部署
    • 调用启动成功的API

安装

# (Recommended) Create a new conda environment.
conda create -n myenv python=3.9 -y
conda activate myenv# Install vLLM with CUDA 12.1.
pip install vllm -i https://pypi.tuna.tsinghua.edu.cn/simple/

离线推理

from vllm import LLM, SamplingParamsllm = LLM(model="meta-llama/Meta-Llama-3-8B-Instruct")  # 你的模型路径
sampling_params = SamplingParams(temperature=0.5)  # 生成文本的控制参数,temperature越大,生成的内容越随机# 处理输出
def print_outputs(outputs):for output in outputs:prompt = output.promptgenerated_text = output.outputs[0].textprint(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")print("-" * 80)print("=" * 80)# 模型的输入对话
conversation = [{"role": "system","content": "You are a helpful assistant"},{"role": "user","content": "Hello"},{"role": "assistant","content": "Hello! How can I assist you today?"},{"role": "user","content": "Write an essay about the importance of higher education.",},
]# 将输入对话,采样参数传给模型
# use_tqdm=False是不启用进度条
outputs = llm.chat(conversation,sampling_params=sampling_params,use_tqdm=False)
print_outputs(outputs)# A chat template can be optionally supplied.
# If not, the model will use its default chat template.# with open('template_falcon_180b.jinja', "r") as f:
#     chat_template = f.read()# outputs = llm.chat(
#     conversations,
#     sampling_params=sampling_params,
#     use_tqdm=False,
#     chat_template=chat_template,
# )

适配openAI-API的API服务

使用vllm将大模型推理做成api服务非常方便,你可以通过 - -host和 - -port参数来自定义地址,而且无需担心chat模板,因为它默认会使用由tokenizer提供的chat模板

使用python命令行部署

关于下面的参数设置,建议去官网查看,解释比较清晰全面
官网:https://docs.vllm.ai/en/latest/serving/openai_compatible_server.html

conda activate vllm \   # 激活环境
CUDA_VISIBLE_DEVICES=6 \  # 设置gpu device
nohup python -m vllm.entrypoints.openai.api_server \
--model /LLM/Qwen_1_5_4B_chat/ \  # 指定你的模型路径
--max_model_len=2048  \    # 设置模型生成长度
--served-model-name sevice_name \   # 设置你起的模型服务名
--trust-remote-code \
--api-key 123456 \   # 设置你api key 
--port 1234 \        # 设置你的端口
--dtype=half \       # 设置你的数据精度
> /vllm.log 2>&1 &   # 设置你的输出log

使用docker部署

docker run --runtime nvidia --gpus all \
-v /path:/path   \    # 本地模型的路径/path,挂载到容器下面的路径/path
--rm --name docker_name   \   # 你容器的名字
-p 8000:8000   \    # 端口映射,本地端口8000映射到容器端口8000,可修改
--ipc=host  \
e********a   \    # 镜像id
--model model_path \   # 你的模型路径
--api-key=123456 \   # 设置你的api key
--served-model-name=sevice_name    # 设置你起的模型服务名

调用启动成功的API

from openai import OpenAI
openai_api_key = "123456"  # 这里是你上述设置的api-key
openai_api_base = "http://ip:8000/v1"  # 这里的ip是运行你上述部署脚本所在的主机ip,8000是你脚本里面设置的端口client = OpenAI(api_key=openai_api_key,base_url=openai_api_base,
)response = client.chat.completions.create(model="sevice_name",  # 这里的sevice_name是你上述脚本中定义的服务名messages=[{"role": "system", "content": ""},{"role": "user", "content": "讲一个笑话"},],temperature=0.7,stream=True   # 设置流式返回
)
# 这里是流式返回
for chunk in response:# print(chunk.choices[0])if chunk.choices[0].delta:print(chunk.choices[0].delta.content)
http://www.lryc.cn/news/460617.html

相关文章:

  • Java 基数排序
  • 红帽发送邮件操作
  • 学习记录:js算法(六十一):添加与搜索单词 - 数据结构设计
  • Jetpack-ObservableField实现双向绑定
  • STARnak, LTR 模型笔记
  • 【数据结构】:破译排序算法--数字世界的秩序密码(二)
  • 2024年《生成式ai大模型》都学什么内容呢?
  • kubernetes自定义pod启动用户
  • C4T避风型电动采光排烟天窗(图集09J621-2)
  • 多态常见面试问题
  • 案例-登录认证(上)
  • 对BSV区块链下一代节点Teranode的答疑解惑(上篇)
  • vue父子组件传参的方法
  • 关于this指针
  • 机器学习西瓜书
  • 如何使用 Puppeteer 和 Browserless 运行自动化测试?
  • python菜鸟知识
  • GPT4o,GPTo1-preview, 拼
  • 论文笔记:Pre-training to Match for Unified Low-shot Relation Extraction
  • 一篇文章带你快速了解linux中关于信号的核心内容
  • openEuler、Linux操作系统常见操作-(6)如何登录Linux
  • Python基础语法条件
  • 006-MAVEN 的使用
  • npm使用时报错:Could not retrieve https://npm.taobao.org/mirrors/node/index.json.
  • 软考中级网络工程师——高级配置
  • Leetcode 第 141 场双周赛题解
  • Linux性能调优,还可以从这些方面入手
  • STM32的独立看门狗定时器(IWDG)技术介绍
  • 自动化生成工作流?英伟达提出ComfyGen:通过LLM来匹配给定的文本提示与合适的工作流程
  • indicatorTree-v10练习(有问题)