在人工智能(AI)的黄金时代,我们见证了技术的边界不断扩展,模型的能力日益增强。今天,我要介绍的主角是Qwen2-0.5B模型,千问最新的大语言模型,以及如何在腾讯云云服务器上部署和使用它,开启AI应用的新旅程。
Qwen2-0.5B是Qwen2在2024年6月份发布的Qwen2中最小的LLM。由于其相对较小的尺寸,可以在一些嵌入式环境或者受限环境中使用。本次为了演示,则使用腾讯云云服务器。
我在腾讯云CVM中购买了一台GPU计算型GN7的服务器,其显存有16GB。详细配置见以上截图。
注意,在购买时,我选择了后台自动安装GPU驱动。见下图。
可以通过 ssh ubuntu@{腾讯云CVM云服务器外网IP地址} 登录到腾讯云CVM云服务器中。
通过apt安装相关依赖
sudo apt update
sudo apt install curl
sudo apt install python3-pip
通过pip安装相关依赖
pip install transformers -U
pip install modelscope
pip install vllm
pip install accelerate
pip install jinja2 -U
pip install jsonschema -U
在~/Workspace目录下,创建一个文件qwen2.py,粘贴如下内容到此文件中。
from transformers import AutoModelForCausalLM, AutoTokenizer
device = "cuda" # the device to load the model onto
# Now you do not need to add "trust_remote_code=True"
model = AutoModelForCausalLM.from_pretrained(
"Qwen/Qwen2-0.5B-Instruct",
torch_dtype="auto",
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-0.5B-Instruct")
# Instead of using model.chat(), we directly use model.generate()
# But you need to use tokenizer.apply_chat_template() to format your inputs as shown below
prompt = "Give me a short introduction to large language model."
prompt = "上海天气怎么样?"
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
model_inputs = tokenizer([text], return_tensors="pt").to(device)
# Directly use generate() and tokenizer.decode() to get the output.
# Use `max_new_tokens` to control the maximum output length.
generated_ids = model.generate(
model_inputs.input_ids,
max_new_tokens=512
)
generated_ids = [
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
print("response======>", str(response))
如果是在国内的服务器,建议第一行改为from modelscope import AutoModelForCausalLM, AutoTokenizer 。本文下述都是依赖于transformers而非modelscope,但流程基本类似。
执行此python3脚本文件,python3 qwen2.py。它会将依赖的模型下载到/home/ubuntu/.cache/huggingface/hub/models--Qwen--Qwen2-0.5B-Instruct中,模型如果有更新,则/home/ubuntu/.cache/huggingface/hub/models--Qwen--Qwen2-0.5B-Instruct/snapshots中会多一个文件夹。
在腾讯云云服务器的命令行中,通过以下命令,即可启动模型推理。
python3 -m vllm.entrypoints.openai.api_server --model /home/ubuntu/.cache/huggingface/hub/models--Qwen--Qwen2-0.5B-Instruct/snapshots/c291d6fce4804a1d39305f388dd32897d1f7acc4 --dtype=half
这样启动的模型占用显存大概12GB。
做为客户端,使用Postman来调用vllm构建的基于Qwen2-0.5B模型的类似OpenAI的API接口:
curl --location 'http://{腾讯云CVM云服务器外网IP地址}:8000/v1/chat/completions' \
--header 'Content-Type: application/json' \
--data '{
"model":"/home/ubuntu/.cache/huggingface/hub/models--Qwen--Qwen2-0.5B-Instruct/snapshots/c291d6fce4804a1d39305f388dd32897d1f7acc4",
"messages": [
{
"role": "system",
"content": "你是一个人工智能助手"
},
{
"role": "user",
"content": "你可以给我点个赞吗?"
}
],
"stream":false
}'
详见下图。
通过在带GPU的腾讯云服务器上部署Qwen2-0.5B模型,我们不仅能够有效利用腾讯云的高性能计算资源,还能够在各种语言处理任务上实现前所未有的效果。希望这篇文章能够帮助你在腾讯云上成功部署并运行Qwen2-0.5B模型,一起探索AI的未来。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。