项目 / shibing624/MedicalGPT
MedicalGPT: Training Your Own Medical GPT Model with ChatGPT Training Pipeline. 训练医疗大模型,实现了包括增量预训练(PT)、有监督微调(SFT)、RLHF、DPO、ORPO、GRPO。
本站整理,非 GitHub 官方 description。
官方快照(gh_history.db,2026-11-25):总榜 未进 Top50 · 日榜 未进 Top50 · 周榜未满 7 天基线(官方不生成) · 月榜暂无官方口径;下方四卡为站内趋势聚合(周卡按上榜次数,非官方 7 日增量窗)。
以下三段按该项目自己的 GitHub README 章节摘录(可合理截断并标明);README 没有对应章节的留空,不编造。
项目依赖文件会随最新功能不时更新,用户需按说明使用命令更新依赖。
该项目参照ChatGPT训练流程构建医疗大模型,涵盖增量预训练、SFT指令微调、RLHF(奖励建模加强化学习)、DPO、ORPO及OPD蒸馏等多个可选训练阶段。
MedicalGPT 的依赖清单会随新功能不定期更新,安装时需克隆仓库并通过 pip install -r requirements.txt --upgrade 升级依赖。
来自 GitHub 仓库 README / 主页介绍正文,正文较长,已截断:保留前 20000 字符(原始 56,251 字节),完整内容见 GitHub 仓库。
[**🇨🇳中文**](https://github.com/shibing624/MedicalGPT/blob/main/README.md) | [**🌐English**](https://github.com/shibing624/MedicalGPT/blob/main/README_EN.md) | [**📖文档/Docs**](https://github.com/shibing624/MedicalGPT/wiki) | [**🤖模型/Models**](https://huggingface.co/shibing624)
<div align="center">
<a href="https://github.com/shibing624/MedicalGPT">
<img src="https://github.com/shibing624/MedicalGPT/blob/main/docs/logo.png" height="100" alt="Logo">
</a>
</div>
-----------------
# MedicalGPT: Training Medical GPT Model
[](https://huggingface.co/shibing624)
[](https://star-history.com/#shibing624/MedicalGPT&Timeline)
[](CONTRIBUTING.md)
[](LICENSE)
[](requirements.txt)
[](https://github.com/shibing624/MedicalGPT/issues)
[](#Contact)
## 📖 Introduction
**MedicalGPT** training medical GPT model with ChatGPT training pipeline, implemantation of Pretraining,
Supervised Finetuning, RLHF(Reward Modeling and Reinforcement Learning), DPO(Direct Preference Optimization)
and standalone OPD(On-Policy Distillation).
**MedicalGPT** 训练医疗大模型,实现了包括增量预训练、有监督微调、RLHF(奖励建模、强化学习训练)、DPO(直接偏好优化)
和独立OPD(On-Policy Distillation)。
<img src="https://github.com/shibing624/MedicalGPT/blob/main/docs/dpo.jpg" width="860" />
- RLHF training pipeline来自Andrej Karpathy的演讲PDF [State of GPT](https://karpathy.ai/stateofgpt.pdf),视频 [Video](https://build.microsoft.com/en-US/sessions/db3f4859-cd30-4445-a0cd-553c3304f8e2)
- DPO方法来自论文[Direct Preference Optimization:Your Language Model is Secretly a Reward Model](https://arxiv.org/pdf/2305.18290.pdf)
- ORPO方法来自论文[ORPO: Monolithic Preference Optimization without Reference Model](https://arxiv.org/abs/2403.07691)
## 🔥 News
[2026/04/20] v2.7版本:支持了 **OPD(On-Policy Distillation)** 蒸馏训练,新增独立 `training/opd_training.py` 训练入口、`scripts/run_opd.sh` 启动脚本,并补充了OPD参数与使用文档,详见[Release-v2.7](https://github.com/shibing624/MedicalGPT/releases/tag/2.7.0)
[2026/04/09] v2.6版本:支持了 **[Agent工具调用/Function Call]** 模型微调训练,新增了支持不同模型的工具数据格式转换和解析代码。并在 `data` 目录下补充了 `toolcall` 数据样例。详见[Release-v2.6](https://github.com/shibing624/MedicalGPT/releases/tag/2.6.0)
[2026/04/07] v2.5版本:支持了 **[Qwen3.5](https://huggingface.co/collections/Qwen/qwen35)** 系列模型(包括Base、Instruct和MoE变体),PT/SFT/DPO/ORPO/GRPO全流程适配,新增`qwen3`、`qwen3_5`、`qwen3_nothink`、`qwen3_5_nothink`对话模板,支持DeepSpeed ZeRO-3 MoE训练,详见[Release-v2.5](https://github.com/shibing624/MedicalGPT/releases/tag/2.5.0)
[2025/04/18] v2.4版本:支持了LoRA和全参的 **[GRPO](https://arxiv.org/pdf/2402.03300)** 训练方法,GRPO通过纯RL方法可以体验`aha moment`,详见[Release-v2.4](https://github.com/shibing624/MedicalGPT/releases/tag/2.4.0)
[2024/09/21] v2.3版本:支持了 **[Qwen-2.5](https://qwenlm.github.io/zh/blog/qwen2.5/)** 系列模型,详见[Release-v2.3](https://github.com/shibing624/MedicalGPT/releases/tag/2.3.0)
[2024/08/02] v2.2版本:支持了角色扮演模型训练,新增了医患对话SFT数据生成脚本[role_play_data](https://github.com/shibing624/MedicalGPT/blob/main/role_play_data/README.md),支持OpenAI、豆包、[MiniMax](https://platform.minimaxi.com/)等多种LLM Provider,详见[Release-v2.2](https://github.com/shibing624/MedicalGPT/releases/tag/2.2.0)
<details><summary>展开日志</summary>
[2024/06/11] v2.1版本:支持了 **[Qwen-2](https://qwenlm.github.io/blog/qwen2/)** 系列模型,详见[Release-v2.1](https://github.com/shibing624/MedicalGPT/releases/tag/2.1.0)
[2024/04/24] v2.0版本:支持了 **[Llama-3](https://huggingface.co/meta-llama)** 系列模型,详见[Release-v2.0](https://github.com/shibing624/MedicalGPT/releases/tag/2.0.0)
[2024/04/17] v1.9版本:支持了 **[ORPO](https://arxiv.org/abs/2403.07691)**,详细用法请参照 `scripts/run_orpo.sh`。详见[Release-v1.9](https://github.com/shibing624/MedicalGPT/releases/tag/1.9.0)
[2024/01/26] v1.8版本:支持微调Mixtral混合专家MoE模型 **[Mixtral 8x7B](https://huggingface.co/mistralai/Mixtral-8x7B-v0.1)**。详见[Release-v1.8](https://github.com/shibing624/MedicalGPT/releases/tag/1.8.0)
[2024/01/14] v1.7版本:新增检索增强生成(RAG)的基于文件问答[ChatPDF](https://github.com/shibing624/ChatPDF)功能,代码`demo/chatpdf.py`,可以基于微调后的LLM结合知识库文件问答提升行业问答准确率。详见[Release-v1.7](https://github.com/shibing624/MedicalGPT/releases/tag/1.7.0)
[2023/10/23] v1.6版本:新增RoPE插值来扩展GPT模型的上下文长度;针对LLaMA模型支持了[FlashAttention-2](https://github.com/Dao-AILab/flash-attention)和[LongLoRA](https://github.com/dvlab-research/LongLoRA) 提出的 **$S^2$-Attn**;支持了[NEFTune](https://github.com/neelsjain/NEFTune)给embedding加噪训练方法。详见[Release-v1.6](https://github.com/shibing624/MedicalGPT/releases/tag/1.6.0)
[2023/08/28] v1.5版本: 新增[DPO(直接偏好优化)](https://arxiv.org/pdf/2305.18290.pdf)方法,DPO通过直接优化语言模型来实现对其行为的精确控制,可以有效学习到人类偏好。详见[Release-v1.5](https://github.com/shibing624/MedicalGPT/releases/tag/1.5.0)
[2023/08/08] v1.4版本: 发布基于ShareGPT4数据集微调的中英文Vicuna-13B模型[shibing624/vicuna-baichuan-13b-chat](https://huggingface.co/shibing624/vicuna-baichuan-13b-chat),和对应的LoRA模型[shibing624/vicuna-baichuan-13b-chat-lora](https://huggingface.co/shibing624/vicuna-baichuan-13b-chat-lora),详见[Release-v1.4](https://github.com/shibing624/MedicalGPT/releases/tag/1.4.0)
[2023/08/02] v1.3版本: 新增LLaMA, LLaMA2, Bloom, ChatGLM, ChatGLM2, Baichuan模型的多轮对话微调训练;新增领域词表扩充功能;新增中文预训练数据集和中文ShareGPT微调训练集,详见[Release-v1.3](https://github.com/shibing624/MedicalGPT/releases/tag/1.3.0)
[2023/07/13] v1.1版本: 发布中文医疗LLaMA-13B模型[shibing624/ziya-llama-13b-medical-merged](https://huggingface.co/shibing624/ziya-llama-13b-medical-merged),基于Ziya-LLaMA-13B-v1模型,SFT微调了一版医疗模型,医疗问答效果有提升,发布微调后的完整模型权重,详见[Release-v1.1](https://github.com/shibing624/MedicalGPT/releases/tag/1.1)
[2023/06/15] v1.0版本: 发布中文医疗LoRA模型[shibing624/ziya-llama-13b-medical-lora](https://huggingface.co/shibing624/ziya-llama-13b-medical-lora),基于Ziya-LLaMA-13B-v1模型,SFT微调了一版医疗模型,医疗问答效果有提升,发布微调后的LoRA权重,详见[Release-v1.0](https://github.com/shibing624/MedicalGPT/releases/tag/1.0.0)
[2023/06/05] v0.2版本: 以医疗为例,训练领域大模型,实现了四阶段训练:包括二次预训练、有监督微调、奖励建模、强化学习训练。详见[Release-v0.2](https://github.com/shibing624/MedicalGPT/releases/tag/0.2.0)
</details>
## 😊 Features
基于ChatGPT Training Pipeline,本项目实现了领域模型--医疗行业语言大模型的训练:
- 第一阶段:PT(Continue PreTraining)增量预训练,在海量领域文档数据上二次预训练GPT模型,以适应领域数据分布(可选)
- 第二阶段:SFT(Supervised Fine-tuning)有监督微调,构造指令微调数据集,在预训练模型基础上做指令精调,以对齐指令意图,并注入领域知识
- 第三阶段
- RLHF(Reinforcement Learning from Human Feedback)基于人类反馈对语言模型进行强化学习,分为两步:
- RM(Reward Model)奖励模型建模,构造人类偏好排序数据集,训练奖励模型,用来建模人类偏好,主要是"HHH"原则,具体是"helpful, honest, harmless"
- RL(Reinforcement Learning)强化学习,用奖励模型来训练SFT模型,生成模型使用奖励或惩罚来更新其策略,以便生成更高质量、更符合人类偏好的文本
- [DPO(Direct Preference Optimization)](https://arxiv.org/pdf/2305.18290.pdf)直接偏好优化方法,DPO通过直接优化语言模型来实现对其行为的精确控制,而无需使用复杂的强化学习,也可以有效学习到人类偏好,DPO相较于RLHF更容易实现且易于训练,效果更好
- [ORPO](https://arxiv.org/abs/2403.07691)比值比偏好优化,不需要参考模型(ref_model)的优化方法,通过ORPO,LLM可以同时学习SFT和对齐,将两个过程整合为单一步骤,缓解模型灾难性遗忘问题
- OPD(On-Policy Distillation)独立蒸馏训练,使用更强的teacher模型对student在自身rollout轨迹上的输出分布进行监督,首版不和PPO/GRPO联训,训练产物可以像SFT一样独立部署
### Release Models
| Model | Base Model | Introduction |
|:------------------------------------------------------------------------------------------------------------------|:----------------------------------------------------------------------------------------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [shibing624/ziya-llama-13b-medical-lora](https://huggingface.co/shibing624/ziya-llama-13b-medical-lora) | [IDEA-CCNL/Ziya-LLaMA-13B-v1](https://huggingface.co/IDEA-CCNL/Ziya-LLaMA-13B-v1) | 在240万条中英文医疗数据集[shibing624/medical](https://huggingface.co/datasets/shibing624/medical)上SFT微调了一版Ziya-LLaMA-13B模型,医疗问答效果有提升,发布微调后的LoRA权重(单轮对话) |
| [shibing624/ziya-llama-13b-medical-merged](https://huggingface.co/shibing624/ziya-llama-13b-medical-merged) | [IDEA-CCNL/Ziya-LLaMA-13B-v1](https://huggingface.co/IDEA-CCNL/Ziya-LLaMA-13B-v1) | 在240万条中英文医疗数据集[shibing624/medical](https://huggingface.co/datasets/shibing624/medical)上SFT微调了一版Ziya-LLaMA-13B模型,医疗问答效果有提升,发布微调后的完整模型权重(单轮对话) |
| [shibing624/vicuna-baichuan-13b-chat-lora](https://huggingface.co/shibing624/vicuna-baichuan-13b-chat-lora) | [baichuan-inc/Baichuan-13B-Chat](https://huggingface.co/baichuan-inc/Baichuan-13B-Chat) | 在10万条多语言ShareGPT GPT4多轮对话数据集[shibing624/sharegpt_gpt4](https://huggingface.co/datasets/shibing624/sharegpt_gpt4) 和 医疗数据集[shibing624/medical](https://huggingface.co/datasets/shibing624/medical) 上SFT微调了一版baichuan-13b-chat多轮问答模型,日常问答和医疗问答效果有提升,发布微调后的LoRA权重 |
| [shibing624/vicuna-baichuan-13b-chat](https://huggingface.co/shibing624/vicuna-baichuan-13b-chat) | [baichuan-inc/Baichuan-13B-Chat](https://huggingface.co/baichuan-inc/Baichuan-13B-Chat) | 在10万条多语言ShareGPT GPT4多轮对话数据集[shibing624/sharegpt_gpt4](https://huggingface.co/datasets/shibing624/sharegpt_gpt4) 和 医疗数据集[shibing624/medical](https://huggingface.co/datasets/shibing624/medical) 上SFT微调了一版baichuan-13b-chat多轮问答模型,日常问答和医疗问答效果有提升,发布微调后的完整模型权重 |
| [shibing624/llama-3-8b-instruct-262k-chinese](https://huggingface.co/shibing624/llama-3-8b-instruct-262k-chinese) | [Llama-3-8B-Instruct-262k](https://huggingface.co/gradientai/Llama-3-8B-Instruct-262k) | 在2万条中英文偏好数据集[shibing624/DPO-En-Zh-20k-Preference](https://huggingface.co/datasets/shibing624/DPO-En-Zh-20k-Preference)上使用ORPO方法微调得到的超长文本多轮对话模型,适用于RAG、多轮对话 |
演示[shibing624/vicuna-baichuan-13b-chat](https://huggingface.co/shibing624/vicuna-baichuan-13b-chat)模型效果:
<img src="https://github.com/shibing624/MedicalGPT/blob/main/docs/demo-screen.gif" width="860" />
具体case见[Inference Examples](#inference-examples)
## ▶️ Demo
我们提供了一个简洁的基于gradio的交互式web界面,启动服务后,可通过浏览器访问,输入问题,模型会返回答案。
启动服务,命令如下:
```shell
CUDA_VISIBLE_DEVICES=0 python demo/gradio_demo.py --base_model path_to_llama_hf_dir --lora_model path_to_lora_dir
```
参数说明:
- `--base_model {base_model}`:存放HF格式的LLaMA模型权重和配置文件的目录,也可使用HF Model Hub模型调用名称
- `--lora_model {lora_model}`:LoRA文件所在目录,也可使用HF Model Hub模型调用名称。若lora权重已经合并到预训练模型,则删除--lora_model参数
- `--tokenizer_path {tokenizer_path}`:存放对应tokenizer的目录。若不提供此参数,则其默认值与--base_model相同
- `--template_name`:模板名称,如`vicuna`、`alpaca`等。若不提供此参数,则其默认值是vicuna
- `--only_cpu`: 仅使用CPU进行推理
- `--resize_emb`:是否调整embedding大小,若不调整,则使用预训练模型的embedding大小,默认不调整
## 💾 Install
#### Updating the requirements
`requirements.txt`会不时更新以适配最新功能,使用以下命令更新依赖:
```markdown
git clone https://github.com/shibing624/MedicalGPT
cd MedicalGPT
pip install -r requirements.txt --upgrade
```
#### Hardware Requirement (显存/VRAM)
\* *估算值*
| 训练方法 | 精度 | 7B | 13B | 30B | 70B | 110B | 8x7B | 8x22B |
|-------|-------------| ----- | ----- | ----- | ------ | ------ | ----- | ------ |
| 全参数 | AMP(自动混合精度) | 120GB | 240GB | 600GB | 1200GB | 2000GB | 900GB | 2400GB |
| 全参数 | 16 | 60GB | 120GB | 300GB | 600GB | 900GB | 400GB | 1200GB |
| LoRA | 16 | 16GB | 32GB | 64GB | 160GB | 240GB | 120GB | 320GB |
| QLoRA | 8 | 10GB | 20GB | 40GB | 80GB | 140GB | 60GB | 160GB |
| QLoRA | 4 | 6GB | 12GB | 24GB | 48GB | 72GB | 30GB | 96GB |
| QLoRA | 2 | 4GB | 8GB | 16GB | 24GB | 48GB | 18GB | 48GB |
## 📁 Project Structure
```
MedicalGPT/
├── training/ # 核心训练脚本(训练主线)
│ ├── template.py # 对话模板定义
│ ├── tool_utils.py # Agent工具调用格式化工具
│ ├── pretraining.py # Stage 1: 增量预训练(PT)
│ ├── supervised_finetuning.py # Stage 2: 有监督微调(SFT, 支持Agent)
│ ├── opd_training.py # Stage 2.5: 独立OPD蒸馏
│ ├── reward_modeling.py # Stage 3: 奖励模型(RM)
│ ├── ppo_training.py # Stage 3: 强化学习(PPO/RLOO)
│ ├── dpo_training.py # Stage 3: 直接偏好优化(DPO, 支持Agent)
│ ├── orpo_training.py # Stage 3: ORPO
│ └── grpo_training.py # Stage 3: GRPO
│
├── scripts/ # 一键运行脚本 + DeepSpeed配置
│ ├── run_pt.sh / run_sft.sh / run_dpo.sh / ...
│ └── zero1.json / zero2.json / zero3.json
│
├── demo/ # 推理、部署、应用示例
│ ├── inference.py / gradio_demo.py / fastapi_server_demo.py
│ ├── openai_api.py / chatpdf.py
│ └── inference_multigpu_demo.py
│
├── tools/ # 模型合并、量化、数据处理工具
│ ├── merge_peft_adapter.py / merge_tokenizers.py
│ ├── model_quant.py / eval_quantize.py
│ └── convert_dataset.py / validate_jsonl.py
│
├── notebooks/ # Colab 教程 Notebook
│ ├── run_training_dpo_pipeline.ipynb
│ └── run_training_ppo_pipeline.ipynb
│
├── data/ # 训练数据
│ ├── sft/ # SFT数据(含普通问答和Tool Call)
│ └── reward/ # DPO/RM偏好数据(含普通偏好和Tool Call偏好)
├── docs/ # 文档
└── tests/ # 测试
```
| 目录 | 说明 | 适用人群 |
|------|------|---------|
| `training/` | 核心训练代码,覆盖PT→SFT→RM→PPO/DPO/ORPO/GRPO全流程 | 训练模型 |
| `scripts/` | 一键运行脚本和DeepSpeed配置,复制即用 | 快速上手训练 |
| `demo/` | 推理、Gradio UI、FastAPI服务、RAG问答等示例 | 想部署和体验模型 |
| `tools/` | LoRA合并、量化、词表扩充、数据格式转换等工具 | 需要模型后处理 |
| `notebooks/` | Colab一键运行的端到端教程 | 零基础快速体验 |
> 所有脚本均从**项目根目录**运行,例如:`bash scripts/run_sft.sh`
## 🚀 Training Pipeline
### 全流程概览
```mermaid
flowchart TD
subgraph PRE["Pre-train 预训练 — 学知识"]
PT["增量预训练 PT(可选)<br/>领域文档灌知识 / Causal-LM 全 token loss"]
end
subgraph POST["Post-train 后训练 — 学做事 + 对齐人类偏好"]
SFT["有监督微调 SFT(必做)<br/>问答对齐 / 只在回答上算 loss"]
RM["奖励模型 RM<br/>学偏好排序,chosen 优于 rejected"]
PPO["强化学习 PPO/RLOO<br/>生成→打分→策略优化"]
DPO["直接偏好优化 DPO<br/>跳过 RM,直接对齐"]
ORPO["ORPO 单阶段对齐<br/>SFT + 对齐一次做完,无需 ref 模型"]
GRPO["GRPO 推理强化<br/>规则奖励训练推理链"]
OPD["独立蒸馏 OPD<br/>强 teacher 蒸馏 student"]
end
subgraph SERVE["部署 — 落地"]
MERGE["合并 LoRA<br/>每阶段合并后再进下一阶段"]
DEPLOY["推理 / 量化 / 部署"]
end
PT --> SFT
SFT --> RM --> PPO --> MERGE
SFT --> DPO --> MERGE
SFT --> ORPO --> MERGE
SFT --> GRPO --> MERGE
SFT --> OPD --> MERGE
MERGE --> DEPLOY
```
> 新手推荐路线:`SFT → DPO`;经典 RLHF 路线:`SFT → RM → PPO`;推理能力:`GRPO`;有更强同系列 teacher 时:`OPD`。每阶段的 LoRA 都用 `tools/merge_peft_adapter.py` 合并后再进入下一阶段。
Training Stage:
| Stage | Introduction | Python script | Shell script |
|:-------------------------------|:-------------|:-----------------------------------------------------------------------------------------------------------------|:---------------------------------------------------------------------------------------|
| Continue Pretraining | 增量预训练 | [pretraining.py](https://github.com/shibing624/MedicalGPT/blob/main/training/pretraining.py) | [run_pt.sh](https://github.com/shibing624/MedicalGPT/blob/main/scripts/run_pt.sh) |
| Supervised Fine-tuning | 有监督微调 | [supervised_finetuning.py](https://github.com/shibing624/MedicalGPT/blob/main/training/supervised_finetuning.py) | [run_sft.sh](https://github.com/shibing624/MedicalGPT/blob/main/scripts/run_sft.sh) |
| On-Policy Distillation | 独立OPD蒸馏 | [opd_training.py](https://github.com/shibing624/MedicalGPT/blob/main/training/opd_training.py) | [run_opd.sh](https://github.com/shibing624/MedicalGPT/blob/main/scripts/run_opd.sh) |
| Direct Preference Optimization | 直接偏好优化 | [dpo_training.py](https://github.com/shibing624/MedicalGPT/blob/main/training/dpo_training.py) | [run_dpo.sh](https://github.com/shibing624/MedicalGPT/blob/main/scripts/run_dpo.sh) |
| Reward Modeling | 奖励模型建模 | [reward_modeling.py](https://github.com/shibing624/MedicalGPT/blob/main/training/reward_modeling.py) | [run_rm.sh](https://github.com/shibing624/MedicalGPT/blob/main/scripts/run_rm.sh) |
| Reinforcement Learning | 强化学习 | [ppo_training.py](https://github.com/shibing624/MedicalGPT/blob/main/training/ppo_training.py) | [run_ppo.sh](https://github.com/shibing624/MedicalGPT/blob/main/scripts/run_ppo.sh) |
| ORPO | 概率偏好优化 | [orpo_training.py](https://github.com/shibing624/MedicalGPT/blob/main/training/orpo_training.py) | [run_orpo.sh](https://github.com/shibing624/MedicalGPT/blob/main/scripts/run_orpo.sh) |
- 提供完整PT+SFT+DPO全阶段串起来训练的pipeline:[run_training_dpo_pipeline.ipynb](https://github.com/shibing624/MedicalGPT/blob/main/notebooks/run_training_dpo_pipeline.ipynb) ,其对应的colab: [](https://colab.research.google.com/github/shibing624/MedicalGPT/blob/main/notebooks/run_training_dpo_pipeline.ipynb),运行完大概需要15分钟
- 提供完整PT+SFT+RLHF全阶段串起来训练的pipeline:[run_training_ppo_pipeline.ipynb](https://github.com/shibing624/MedicalGPT/blob/main/notebooks/run_training_ppo_pipeline.ipynb) ,其对应的colab: [](https://colab.research.google.com/github/shibing624/MedicalGPT/blob/main/notebooks/run_training_ppo_pipeline.ipynb) ,运行完大概需要20分钟
- 支持Agent工具调用微调训练(Agent Finetuning),SFT和DPO阶段均支持。详见下方 [Agent 训练](#agent-训练agent-finetuning) 章节
- 支持独立 OPD 训练,复用 `data/sft` 的 ShareGPT 多轮数据格式,训练输出与 SFT/LoRA 一样可直接部署;如果是LoRA训练,可继续使用 `tools/merge_peft_adapter.py` 合并权重
- 提供基于知识库文件的LLM问答功能(RAG):[chatpdf.py](https://github.com/shibing624/MedicalGPT/blob/main/demo/chatpdf.py)
- [训练参数说明](https://github.com/shibing624/MedicalGPT/blob/main/docs/training_params.md) | [训练参数说明wiki](https://github.com/shibing624/MedicalGPT/wiki/%E8%AE%AD%E7%BB%83%E5%8F%82%E6%95%B0%E8%AF%B4%E6%98%8E)
- [数据集](https://github.com/shibing624/MedicalGPT/blob/main/docs/datasets.md) | [数据集wiki](https://github.com/shibing624/MedicalGPT/wiki/%E6%95%B0%E6%8D%AE%E9%9B%86)
- [扩充词表](https://github.com/shibing624/MedicalGPT/blob/main/docs/extend_vocab.md) | [扩充词表wiki](https://github.com/shibing624/MedicalGPT/wiki/%E6%89%A9%E5%85%85%E4%B8%AD%E6%96%87%E8%AF%8D%E8%A1%A8)
- [FAQ](https://github.com/shibing624/MedicalGPT/blob/main/docs/FAQ.md) | [FAQ_wiki](https://github.com/shibing624/MedicalGPT/wiki/FAQ)
#### Agent 训练(Agent Finetuning)
本项目支持通过 SFT 和 DPO 两种方式训练具有工具调用(Function Call / Tool Use)能力的 Agent 模型。
**核心原理:** Tool Call 本质上是特殊的多轮对话,在标准的 `human`/`gpt` 角色之外新增了 `function_call`(模型决定调用工具)和 `observation`(工具返回结果)两种角色。Template 系统统一处理所有角色,有工具描述就拼接到 system message,没有就跳过,对 loss 计算和训练过程完全透明。
**数据格式:** 统一使用 jsonl(一行一条),ShareGPT 格式。
SFT 数据(`data/sft/glaive_toolcall_zh_demo.jsonl`):
```
{"conversations": [{"from": "human", "value": "帮我查一下北京天气"}, {"from": "function_call", "value": "{\"name\": \"get_weather\", \"arguments\": {\"city\": \"北京\"}}"}, {"from": "observation", "value": "{\"temperature\": \"28°C\", \"weather\": \"晴\"}"}, {"from": "gpt", "value": "北京今天天气晴朗,气温28°C。"}], "tools": "[...]"}
```
DPO 数据(`data/reward/toolcall_dpo_zh_demo.jsonl`):
```
{"conversations": [{"from": "human", "value": "帮我查一下北京天气"}], "tools": "[...]", "chosen": "Action: get_weather\nAction Input: {\"city\": \"北京\"}", "rejected": "北京今天天气晴朗,气温25度。"}
```
> **注意:** 所有训练数据统一为 `.jsonl` 格式。`chosen`/`rejected` 为纯字符串。如有 `.json` 文件(JSON 数