中文金融知識問答神器!LLaMA智能系統(tǒng)微調(diào)模型教學(xué)全解析
基于中文金融知識的 LLaMA 系微調(diào)模型的智能問答系統(tǒng):LLaMA大模型訓(xùn)練微調(diào)推理等詳細(xì)教學(xué)
基于 LLaMA 系基模型經(jīng)過中文金融知識指令精調(diào)/指令微調(diào)(Instruct-tuning) 的微調(diào)模型。通過中文金融公開問答數(shù)據(jù)+爬取的金融問答數(shù)據(jù)構(gòu)建指令數(shù)據(jù)集,并在此基礎(chǔ)上對 LLaMA 系模型進(jìn)行了指令微調(diào),提高了 LLaMA 在金融領(lǐng)域的問答效果。
基于已有數(shù)據(jù)和繼續(xù)爬取的中文金融數(shù)據(jù),將繼續(xù)利用 GPT3.5/4.0 API 構(gòu)建高質(zhì)量的數(shù)據(jù)集,另在中文知識圖譜-金融、CFLEB 金融數(shù)據(jù)集等數(shù)據(jù)上進(jìn)一步擴(kuò)充高質(zhì)量指令數(shù)據(jù)集。
基于 Chinese-LLaMA 和中文金融數(shù)據(jù)進(jìn)行指令微調(diào)的模型。
基于 Meta-LLaMA 和中文金融數(shù)據(jù)進(jìn)行指令微調(diào)的模型。
1. 環(huán)境安裝
首先安裝依賴包,python環(huán)境建議3.9+
pip install -r requirements.txt
其次安裝 lfs 方便本地下載 LLaMa 大模型
git lfs install
# 下載7B模型到本地
bash ./base_models/load.sh
2.模型下載
LoRA 權(quán)重可以通過 Huggingface 下載,結(jié)構(gòu)如下:
? ? Fin-Alpaca-LoRA-7B-Meta/
? ? ? ?- adapter_config.json ? # LoRA權(quán)重配置文件
? ? ? ?- adapter_model.bin ? ? # LoRA權(quán)重文件
3.Inference:單模型推理-多模型對比
目前在./instruction_data/infer.json中提供了一些測試用例,也可替換成其它的數(shù)據(jù)集但注意格式保持一致
運行infer腳本
#單模型推理
bash ./scripts/infer.sh
#多模型對比
bash ./scripts/comparison_test.sh
4.數(shù)據(jù)集構(gòu)建
此前版本采用了公開和爬取的中文金融領(lǐng)域問答數(shù)據(jù),涉及到保險、理財、股票、基金、貸款、信用卡、社保等。
指令數(shù)據(jù)示例如下:
問題:辦理商業(yè)匯票應(yīng)遵守哪些原則和規(guī)定?
針對此前數(shù)據(jù)仍存在不準(zhǔn)確和類型單一等不完善的地方;目前我們利用GPT3.5/4.0 接口進(jìn)一步優(yōu)化數(shù)據(jù)、并擴(kuò)充中文金融知識庫,設(shè)置多種Prompt形式、multi-task形式拓展豐富指令數(shù)據(jù)集,實現(xiàn)金融領(lǐng)域多業(yè)務(wù)場景覆蓋。
最新模型情況:(即將發(fā)布,敬請期待~)

5.微調(diào)Finetune
若想用自己的數(shù)據(jù)集微調(diào) LLaMA,請按照./instruction_data/fin_data.json的格式構(gòu)建自己的數(shù)據(jù)集
運行 finetune 腳本
bash ./scripts/finetune.sh
6.訓(xùn)練細(xì)節(jié)
6.1 計算資源需求
目前訓(xùn)練設(shè)備為一張A100-SXM-80GB顯卡,訓(xùn)練總輪次10輪。batchsize=64的情況下顯存占用在40G左右、batchsize=96的情況下顯存占用在65G左右。預(yù)計3090/4090顯卡(24GB顯存)以上顯卡可以較好支持,根據(jù)顯存大小來調(diào)整batch_size。
6.2實驗記錄


6.3 模型效果對比

7.提示詞模板
此目錄包含用于 LoRA 微調(diào) LLaMa 模型的提示的模板樣式。
Format
模板是通過一個JSON文件描述的,該文件包含以下鍵:
prompt_input: The template to use when input is not None. Uses {instruction} and {input} placeholders.
prompt_no_input: The template to use when input is None. Uses {instruction} placeholders.
description: A short description of the template, with possible use cases.
response_split: The text to use as separator when cutting real response from the model output.
No {response} placeholder was used, since the response is always the last element of the template and is just to be concatenated to the rest.
7.1模板案例
The default template, used unless otherwise specified, is alpaca.json
{
? ?"description": "Template used by Alpaca-LoRA.",
? ?"prompt_input": "Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n\n### Instruction:\n{instruction}\n\n### Input:\n{input}\n\n### Response:\n",
? ?"prompt_no_input": "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instruction:\n{instruction}\n\n### Response:\n",
? ?"response_split": "### Response:" ? ?
}
7.2 現(xiàn)有模板
7.2.1 alpaca
到目前為止,用于通用LoRA微調(diào)的默認(rèn)模板。
7.2.2 alpaca_legacy
原始羊駝使用的舊模板,響應(yīng)字段后沒有“\n”。保留以供參考和實驗。
7.2.3 alpaca_short
一個修剪過的羊駝模板,它似乎也表現(xiàn)得很好,并保留了一些 tokens。使用默認(rèn)模板創(chuàng)建的模型似乎也可以通過短時間查詢。
8.提示構(gòu)建模塊
prompter.py
Prompter class, a template manager.
from utils.prompter import Prompter
[項目鏈接跳轉(zhuǎn)]https://blog.csdn.net/sinat_39620217/article/details/132007040
更多優(yōu)質(zhì)內(nèi)容請關(guān)注公號:汀丶人工智能;會提供一些相關(guān)的資源和優(yōu)質(zhì)文章,免費獲取閱讀。