基于医疗领域数据微调LLaMA——ChatDoctor模型

文章目录

ChatDoctor简介

CHatDoctor论文:
ChatDoctor: A Medical Chat Model Fine-tuned on LLaMA Model using Medical Domain Knowledge

项目地址:https://github.com/Kent0n-Li/ChatDoctor

ChatDoctor是一款使用LLaMA模型并结合医学知识进行训练的医疗助手,研究人员先收集了50多万条真实医患对话,然后使用这些数据对LLaMA模型进行微调。

ChatDoctor不仅具备流畅的对话能力,在医疗领域的理解和诊断也达到了很高的水平。

用户只需描述症状,ChatDoctor就会像真人医生一样询问其他症状与体征,然后给出初步诊断和治疗建议,而且完全开源免费!

当然,ChatDoctor只是一个AI助手,不能完全替代人医生,但在常见病症诊断方面,它已经表现已经非常不错了。

微调实战

下载仓库并进入目录

bash 复制代码
git clone https://github.com/Kent0n-Li/ChatDoctor.git
cd ChatDoctor

创建conda环境并配置环境(安装相关依赖)

bash 复制代码
conda create -n chatdoctor python=3.10
pip install -r requirements.txt 
pip install datasets

因为我们要基于lora进行微调,故需要安装peft框架,安装方式参考:
https://github.com/AGI-Edgerunners/LLM-Adapters/tree/main

将该仓库中的peft目录复制到本仓库中,然后通过下面命令进行安装。

bash 复制代码
cd peft/
pip install -e .

下载模型文件

推荐使用git命令下载模型文件,但注意需要提前下载git-lfs工具包,安装步骤如下:

bash 复制代码
# 先安装git(如已安装可忽略)
sudo apt-get install git
# 安装apt-get源
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
# 安装git-lfs
sudo apt-get install git-lfs
# 初始化git-lfs
git lfs install

模型文件下载地址:https://huggingface.co/decapoda-research/llama-7b-hf

下载命令:

bash 复制代码
git clone https://huggingface.co/decapoda-research/llama-7b-hf

微调数据

ChatDoctor数据集

来自HealthCareMagic.com的10万例实际患者与医生之间的对话HealthCareMagic-100k

来自icliniq.com的1万例实际患者与医生之间的对话icliniq-10k

5k例由ChatGPT生成的患者与医生之间的对话GenMedGPT-5k疾病数据库

微调过程

项目中提供了两种微调方式:一种是全量微调,一种是基于lora的微调

全量微调

如果是多卡进行微调可以直接使用项目中提供的命令

bash 复制代码
torchrun --nproc_per_node=4 --master_port=<your_random_port> train.py \
   --model_name_or_path <your_path_to_hf_converted_llama_ckpt_and_tokenizer> \
   --data_path ./HealthCareMagic-100k.json \
   --bf16 True \
   --output_dir pretrained \
   --num_train_epochs 1 \
   --per_device_train_batch_size 4 \
   --per_device_eval_batch_size 4 \
   --gradient_accumulation_steps 8 \
   --evaluation_strategy "no" \
   --save_strategy "steps" \
   --save_steps 2000 \
   --save_total_limit 1 \
   --learning_rate 2e-6 \
   --weight_decay 0. \
   --warmup_ratio 0.03 \
   --lr_scheduler_type "cosine" \
   --logging_steps 1 \
   --fsdp "full_shard auto_wrap" \
   --fsdp_transformer_layer_cls_to_wrap 'LLaMADecoderLayer' \
   --tf32 True

基于LoRA的微调

bash 复制代码
python train_lora.py \
  --base_model '/data/sim_chatgpt/llama-7b-hf/models--decapoda-research--llama-7b-hf/snapshots/5f98eefcc80e437ef68d457ad7bf167c2c6a1348/' \
  --data_path 'chatdoctor5k.json' \
  --output_dir './lora_models/' \
  --batch_size 1 \
  --micro_batch_size 1 \
  --num_epochs 1 \
  --learning_rate 3e-5 \
  --cutoff_len 256 \
  --val_set_size 120 \
  --adapter_name lora

显存占用情况:约占用11G。

基于微调后的模型推理

使用全量微调好的模型进行推理:mncai/chatdoctor

transformers-cli download mncai/chatdoctor --cache-dir ./chatdoctor

修改chat.py

bash 复制代码
load_model("/data/chatdoctor/models--mncai--chatdoctor/snapshots/8fdcfdda6877d7f21173dfac48b2c14499ba8264/")

执行 python chat.py即可

报错:

ImportError: LlamaConverter requires the protobuf library but it was

not found in your environment.

解决方法:

bash 复制代码
pip install protobuf==3.19.0

执行 python chat.py

显存占用,约为14G

相关推荐
ECHO飞跃 0125 天前
Unity2019 本地推理 通义千问0.5-1.5B微调导入
人工智能·深度学习·unity·llama
黑白极客5 天前
ACP大模型认证刷题工具开源,助力高效备考
java·ai·github·llama·认证
迷之程序员5 天前
llama-cpp-python用法,模型加载gpu踩坑全记录
开发语言·python·llama
~kiss~6 天前
Ollama 底层的 llama.cpp 和 GGUF
llama
小雨中_7 天前
4.1 Megatron-LM:千卡级集群预训练的“硬核”框架
人工智能·python·深度学习·机器学习·llama
重生之我要成为代码大佬7 天前
AI框架设计与选型
人工智能·langchain·大模型·llama·qwen
小雨中_8 天前
4.1 LLaMA 系列:从 LLaMA-1 到 LLaMA-3
人工智能·python·深度学习·机器学习·自然语言处理·llama
l1t10 天前
DeepSeek总结的llama.cpp使用说明
llama
爱跑步的程序员~13 天前
SpringBoot集成SpringAI与Ollama本地大模型
java·后端·spring·ai·llama·springai
向量引擎小橙15 天前
视觉艺术的“奇点”:深度拆解 Gemini-3-Pro-Image-Preview 绘画模型,看这只“香蕉”如何重塑 AI 创作逻辑!
人工智能·python·gpt·深度学习·llama