课程文档:Llama3-Tutorial/docs/agent.md at main · SmartFlowAI/Llama3-Tutorial · GitHub
用基于 Lagent 的 Web Demo 直观体验一下 Llama3 模型在 ReAct 范式下的智能体能力,让它使用 ArxivSearch 工具来搜索 InternLM2 的技术报告。Llama3-8B-Instruct 模型并没有成功调用工具。原因在于它输出了 query=InternLM2 Technical Report
而非 {'query': 'InternLM2 Technical Report'}
,这也就导致了 ReAct 在解析工具输入参数时发生错误,进而导致调用工具失败。
微调过程
使用XTuner 在 Agent-FLAN 数据集上微调 Llama3-8B-Instruct,以让 Llama3-8B-Instruct 模型获得智能体能力
环境配置
安装好一个 python=3.10 pytorch=2.1.2+cu121 的基础环境;XTuner;Llama3-Tutorial(前几课已经配置好的llama3环境)
模型准备
软链接Llama3-8B-Instruct 模型权重
mkdir -p ~/model
cd ~/model
ln -s /root/share/new_models/meta-llama/Meta-Llama-3-8B-Instruct .
数据集准备
HuggingFace 上的 Agent-FLAN 数据集暂时无法被 XTuner 直接加载,因此我们首先要下载到本地,然后转换成 XTuner 直接可用的格式。
在 InternStudio 上,已经准备好了一份转换好的数据,可以直接通过如下脚本准备
cd ~
cp -r /root/share/new_models/internlm/Agent-FLAN .
chmod -R 755 Agent-FLAN
启动微调
开发机上已经准备好了可以一键启动的配置文件,主要是修改好了模型路径、对话模板以及数据路径。使用如下指令以启动训练:
export MKL_SERVICE_FORCE_INTEL=1
xtuner train ~/Llama3-Tutorial/configs/llama3-agentflan/llama3_8b_instruct_qlora_agentflan_3e.py --work-dir ~/llama3_agent_pth --deepspeed deepspeed_zero2
由于训练时间太长,我这30%A100就不跟着玩了。直接使用已经训练好且转换为 HuggingFace 格式的权重,路径位于 /share/new_models/agent-flan/iter_2316_hf
。
- 合并权重
export MKL_SERVICE_FORCE_INTEL=1
xtuner convert merge /root/model/Meta-Llama-3-8B-Instruct \
/share/new_models/agent-flan/iter_2316_hf \
~/llama3_agent_pth/merged
Lagent Web Demo
安装 lagent并启动 Web Demo
pip install lagent
streamlit run ~/Llama3-Tutorial/tools/agent_web_demo.py 微调前/后 LLaMA3 模型路径
- 微调前 LLaMA3 路径:
/root/model/Meta-Llama-3-8B-Instruct
- 微调后 LLaMA3 路径:
/root/llama3_agent_pth/merged
经过 Agent-FLAN 数据集的微调后,Llama3-8B-Instruct 模型已经可以成功地调用工具了,其智能体能力有了很大的提升。