最近,基于LLM强大的意图识别能力,采用LLM作为控制中心,调用各种垂直领域的小模型,这一研究方向比较热门,即大模型的agent应用。常用的开源框架:langchain。
魔搭社区:https://modelscope.cn/home。
1. 模型下载
在modelscope上下载需要的模型文件,例如https://modelscope.cn/models上搜索某一模型。
如果是联网环境,可以通过脚本中指定ModelType参数,这样命令执行时会自动下载模型
from swift.llm import (
get_model_tokenizer, get_template, inference, ModelType,
get_default_template_type, inference_stream
)
model_type = ModelType.qwen1half_7b_chat
本次下载离线文件:chatglm2-6b,下载地址:https://modelscope.cn/models/ZhipuAI/chatglm2-6b/files
2. 环境配置
申请一台带GPU和cuda环境的Ubuntu服务器,安装swift。
pip install 'ms-swift[llm]' -U
pip install transformers==4.30.2
将下载好的llm模型文件放到某一路径以备调用,需要注意某些依赖包的版本要符合模型要求,例如chatglm2-6b的transformers版本如果为4.41.2时模型加载会报错。
3. 运行模型推理
from transformers import AutoTokenizer, AutoModel
path = "/mnt/glm2"
tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
model = AutoModel.from_pretrained(path, trust_remote_code=True, device='cuda')
model = model.eval()
while True:
print("input your question:")
input_text = input()
response, history = model.chat(tokenizer, input_text, history=[])
print(response)
4. 训练
参考文章:https://blog.csdn.net/weixin_48007632/article/details/140188530。
以gemma-2为例,比较详细的介绍了从modelscope下载模型文件以及训练的过程。
LLM训练平台,例如LLaMA-Factory平台:https://blog.csdn.net/weixin_48007632/article/details/138819599