关于 HuggingFace 和 ModelScope

HuggingFace 是一个模型和数据集的托管平台, 其中托管了大量的用于机器学习的开源模型和数据集, 你可以将其视为针对 Machine Learning 的 Github.
ModelScope 是一个由阿里达摩院推出的开源模型服务平台, 可以视为中国版的 HuggingFace.

关于 HuggingFace 的使用

Use the CLI | docs

关于 ModelScope 的使用

模型的下载 | docs

例如下载 Qwen2-0.5B-Instruct 模型

1
2
# pip install modelscope
modelscope download --model qwen/Qwen2-0.5B-Instruct --local_dir /root/qwen/Qwen2-0.5B-Instruct

使用 vllm 的 api_server 在本地运行模型的一个服务

1
python -m vllm.entrypoints.openai.api_server --model /root/qwen/Qwen2-0.5B-Instruct --served-model-name qwen2 --trust-remote-code --port 10086 --api-key 123456

这个命令太长, 可以将其写成一个 shell 脚本

1
2
3
4
5
6
# run_qwen2_server.sh
unset http_proxy
unset https_proxy
model_id=/root/qwen/Qwen2-0.5B-Instruct
model_name_ext=qwen2
CUDA_VISIBLE_DEVICES=0 python -m vllm.entrypoints.openai.api_server --model ${model_id} --served-model-name ${model_name_ext} --trust-remote-code --port 10086 --api-key 123456

再运行这个脚本, 去启动一个大模型的服务

1
sh path/to/run_qwen2_server.sh

使用 openai 提供的方法尝试去访问这个模型, 进行测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import openai

client = openai.OpenAI(
base_url="http://localhost:10086/v1",
api_key="123456",
)

completion = client.chat.completions.create(
model="qwen2",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "tell me something about michael jordan"}
]
)
print(completion.choices[0].message.content)