diff --git a/README.md b/README.md index c772c73..b928d06 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,142 @@ -# CoalLLM +# 煤矿安全大模型——矿途智护者 +## 项目背景 + +- 国家高度重视煤矿安全生产工作,提高从业人员的安全知识水平,是煤矿安全管理的重要基础。 +- 安全管理领域积累了海量数据,但难以为安全决策所利用。 +- 根据国家矿山安监局统计:90%以上煤矿事故都与人的不安全行为有关。 + +## 主要功能 + +- 支持煤矿安全领域常规题型解答,如:单选题、多选题、判断题、填空题等 (针对煤矿主要负责人及安管人员、煤矿各种作业人员) +- 支持针对安全规程规章制度、技术等文档内容回答(如《中华人民共和国矿山安全法》、《煤矿建设安全规程》) +- 支持煤矿历史事故案例,事故处理报告查询,提供事故原因详细分析、事故预防措施以及应急响应知识 + +## 创建环境 +```shell +conda create -n CoalLLM python=3.10 +conda activate CoalLLM +``` +## 依赖包安装 +```shell +pip install modelscope[framework] # 模型库 +pip install py-data-juicer[sci] # 数据处理库 +pip install ms-swift[llm] # 训练库 +pip install ms-swift[eval] # 评测库 +``` + + +## 数据构建 + +输入如下命令,会在data目录下生成CoalQA_data.json + +```shell +git clone https://gitlink.org.cn/yaosenJ/Coal_Mine_SAFETY.git +cd Coal_Mine_SAFETY/data_process +python Generate_QAdata.py ../data/CoalQA_data.json +``` + +CoalQA_data.json内容结构形如: + +```json +[ + { + "instruction": "请提供新建矿井立井井筒冻结法施工的具体要求。", + "output": "新建矿井立井井筒冻结法施工需要遵守以下要求:冻结深度必须穿过风化带延深至稳定的基岩10m以上,第一个冻结孔必须全孔取芯,钻孔时必须测定钻孔的方向和偏斜度,偏斜度超过规定时必须及时纠正,冻结管必须采用无缝钢管并焊接或螺纹连接,开始冻结后必须经常观察水文观测孔的水位变化,并在确定冻结壁已交圈后才能进行试挖。" + }, + ... + +] +``` +## 模型训练 +安装 flash-attention 加快推理速度 +```shell +pip install flash-attn --no-build-isolation +``` +使用ms-swift训练模型 +```shell +!CUDA_VISIBLE_DEVICES=0 swift sft \ + --sft_type lora \ + --model_type internlm2_5-7b-chat \ + --model_id_or_path /root/share/new_models/Shanghai_AI_Laboratory/internlm2_5-7b-chat \ + --dataset /root/Coal_Mine_SAFETY/data/data.json\ + --system "你是一个煤矿安全领域的知识达人,你对相关煤矿安全规章规程制度、技术等文档非常熟悉。请你专业正确地解答用户想问的煤矿安全相关问题。" \ + --dataset_test_ratio 0.01 \ + --output_dir output \ + --lora_target_modules ALL \ + --lora_rank 8 \ + --dtype bf16 \ + --seed 42 \ + --learning_rate 1e-4 \ + --warmup_ratio 0.05 \ + --max_length 2048 \ + --batch_size 4 \ + --eval_batch_size 4 \ + --num_train_epochs 3 \ + --gradient_accumulation_steps 4 \ + --save_total_limit 5 \ + --eval_steps 100 \ + --save_steps 100 + ``` +**显存占用** + +

+
+ +
+

+ +**训练日志** + +

+
+ +
+

+ +## 使用evalscope评估模型 + +### 1. 自定义数据集评估 + +```shell +!CUDA_VISIBLE_DEVICES=0 swift eval \ + --ckpt_dir /root/Coal_Mine_SAFETY/output/internlm2_5-7b-chat/v4-20240909-222741/checkpoint-1113 \ + --eval_dataset no \ + --infer_backend pt \ + --eval_backend Native \ + --eval_limit 10 \ + --seed 42 \ + --eval_batch_size 8 \ + --custom_eval_config custom_eval_config.json \ + --temperature 0.7 \ + --top_k 20 \ + --top_p 0.9 +``` +

+
+ +
+

+ +## 项目展示 + +

+
+ +
+

+

+
+ +
+

+

+
+ +
+

+

+
+ +
+

\ No newline at end of file diff --git a/data_process/Generate_QAdata.py b/data_process/Generate_QAdata.py new file mode 100644 index 0000000..dc2738c --- /dev/null +++ b/data_process/Generate_QAdata.py @@ -0,0 +1,45 @@ +#coding:utf-8 +import sys +import random +from zhipuai import ZhipuAI +client = ZhipuAI(api_key="your key") + +with open('19-中华人民共和国矿山安全法.txt', 'r', encoding='utf-8') as f: + content = f.read() + +def return_random_prompt(): + system_prompt = "根据下面提供有关煤矿安全领域文本,请你仔细通读全文,你需要依据该文本:\n\n######\n{}######\n尽可能给出多样化的问题和对应的回答。我们将用于人工评估GLM-4模型对问答对数据的完成情况。要求:\n".format(content) + system_prompt += "1. 生成问题有价值且遵守该文本信息,回答准确专业。\n" + system_prompt += "2. 生成问答对不能重复。\n" + system_prompt += "3. 问题多样化,同个问题可以换成不同表述方式,但意思保持不变。\n" + system_prompt += "4. 为问题生成作为,不应该只包含简单的占位符。应提供实质性的内容问题,具有挑战性。字数不超过" + str(random.randint(80, 120)) + "字。\n" + system_prompt += "5. 应该是对问题的适当且真实的回答,不能只回复答应或拒绝请求。如果需要额外信息才能回复时,请努力预测用户意图并尝试回复,但不能胡编乱造。的内容应少于" + str(random.randint(512,1024)) + "字。\n\n" + system_prompt += "请给出满足条件的20条JSON格式数据,并存储在一个列表中,便于整理使用,不要输出无法的字符,只要列表形式存储JSON数据\n" + return system_prompt + + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("Usage: python Generate_QAdata.py ") + exit(1) + + output_file = open(sys.argv[1], 'w',encoding='utf-8') + + MAX_EPOCHS = 1 # number of data to generate (each prompt contains 20 JSON-formatted data) + + for k in range(MAX_EPOCHS): + response = client.chat.completions.create( + model="glm-4", + messages=[ + { + "role": "user", + "content": return_random_prompt() + } + ], + top_p=0.7, + temperature=0.9, + stream=False, + max_tokens=2500, + ) + output_file.write(response.choices[0].message.content + '\n') + output_file.close() \ No newline at end of file diff --git a/img/train_log.png b/img/train_log.png new file mode 100644 index 0000000..3b5849a Binary files /dev/null and b/img/train_log.png differ diff --git a/img/图片1.png b/img/图片1.png new file mode 100644 index 0000000..4026719 Binary files /dev/null and b/img/图片1.png differ diff --git a/img/图片4.png b/img/图片4.png new file mode 100644 index 0000000..ff85e2b Binary files /dev/null and b/img/图片4.png differ diff --git a/img/图片5.png b/img/图片5.png new file mode 100644 index 0000000..df3a559 Binary files /dev/null and b/img/图片5.png differ diff --git a/img/图片6.png b/img/图片6.png new file mode 100644 index 0000000..3f10b03 Binary files /dev/null and b/img/图片6.png differ diff --git a/img/图片7.png b/img/图片7.png new file mode 100644 index 0000000..f96ac98 Binary files /dev/null and b/img/图片7.png differ diff --git a/img/显存占用.png b/img/显存占用.png new file mode 100644 index 0000000..04c108a Binary files /dev/null and b/img/显存占用.png differ diff --git a/img/评测.png b/img/评测.png new file mode 100644 index 0000000..c8beb80 Binary files /dev/null and b/img/评测.png differ