安装
git clone https://github.com/ztxz16/fastllm.git
bash install.sh -DUSE_CUDA=ON -D CMAKE_CUDA_COMPILER=$(which nvcc)
以Qwen1.5-0.5B为例
# 通过huggingface接口创建模型,参考每个模型readme.md中的加载方式
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained('/home/qwen0_5B/Qwen1___5-0___5B-Chat', trust_remote_code = True)
model = AutoModelForCausalLM.from_pretrained('/home/qwen0_5B/Qwen1___5-0___5B-Chat', trust_remote_code = True)
# 将huggingface模型转换成fastllm模型
# from_hf接口只能接受原始模型,或者ChatGLM的int4, int8量化模型,不能转换其它量化模型
from ftllm import llm
model = llm.from_hf(model, tokenizer, dtype = "float16")
model.save("qwen0_5B.flm")
🚀现在可以使用fastllm_pytools包来启动一个大模型对话服务了:
python3 -m fastllm_pytools.chat --path /home/qwen0_5B.flm
也可以根据webui.py指定的参数来启动webui服务:
python3 -m fastllm_pytools.webui --path /home/qwen0_5B.flm --port 8000
以及部署API Server:
python3 -m fastllm_pytools.server --model_name qwen --port 8000 -p /home/qwen0_5B.flm
import requests
url = "http://localhost:8000/v1/chat/completions"
# API文档/v1/chat/completions
headers = {
"Content-Type": "application/json"
}
data = {
"model": "qwen",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "你好呀"}
]
}
response = requests.post(url, headers=headers, json=data)
print(response.json())
响应:
{'id': 'fastllm-qwen-89c424649a404daca6a5e9aaa70e21b0', 'object': 'chat.completion', 'created': 1732087721, 'model': 'qwen', 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': '你好!有什么我可以帮助你的吗?'}, 'finish_reason': 'stop'}], 'usage': {'prompt_tokens': 19, 'total_tokens': 27, 'completion_tokens': 8}}
我们也可以使用官方给出的python api接口: pyfastllm