├── .gitignore ├── LICENSE ├── README.md ├── README_EN.md ├── assets ├── benchmark.png ├── case.png ├── logo │ ├── logo.png │ └── logo_new.png ├── process_of_knowledge_tuning.png ├── wechat.jpg └── wechat_group.jpg ├── benchmark ├── README.md └── question.json ├── data-literature └── liver_cancer.json ├── data ├── infer.json ├── knowledge_tuning_data_sample.txt └── llama_data.json ├── doc └── Tuning_Methods_for_LLMs_towards_Health_Intelligence.pdf ├── export_hf_checkpoint.py ├── export_state_dict_checkpoint.py ├── finetune.py ├── generate.py ├── infer.py ├── infer_literature.py ├── requirements.txt ├── scripts ├── finetune.sh ├── infer-literature-multi.sh ├── infer-literature-single.sh ├── infer.sh └── test.sh ├── templates ├── bloom_deploy.json ├── literature_template.json └── med_template.json └── utils ├── README.md ├── __init__.py └── prompter.py /.gitignore: -------------------------------------------------------------------------------- 1 | out/ 2 | 7B/ 3 | 13B/ 4 | __pycache__/ 5 | checkpoint** 6 | minimal-llama** 7 | lora-llama-med 8 | upload.py 9 | lora-** 10 | *.out 11 | *result 12 | *ckpt 13 | wandb 14 | todo.txt 15 | .vscode/ 16 | *tmp* 17 | .DS_Store 18 | .idea 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2023 Rohan Taori, Ishaan Gulrajani, Tianyi Zhang, Yann Dubois, Xuechen Li 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [**中文**](./README.md) | [**English**](./README_EN.md) 2 | 3 |

4 | SCIR-HI-HuaTuo 5 |

6 | 7 | 8 | 9 | # 本草[原名:华驼(HuaTuo)]: 基于中文医学知识的大语言模型指令微调 10 | 11 | ### BenTsao (original name: HuaTuo): Instruction-tuning Large Language Models With Chinese Medical Knowledge 12 | 13 | [![Code License](https://img.shields.io/badge/Code%20License-Apache_2.0-green.svg)](https://github.com/SCIR-HI/Huatuo-Llama-Med-Chinese/blob/main/LICENSE) [![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/release/python-390/) 14 | 15 | 16 | 本项目开源了经过中文医学指令精调/指令微调(Instruction-tuning) 的大语言模型集,包括LLaMA、Alpaca-Chinese、Bloom、活字模型等。 17 | 18 | 19 | 我们基于医学知识图谱以及医学文献,结合ChatGPT API构建了中文医学指令微调数据集,并以此对各种基模型进行了指令微调,提高了基模型在医疗领域的问答效果。 20 | 21 | 22 | ## News 23 | 24 | **[2024/07/25][《基于知识微调的大语言模型可靠中文医学回复生成方法》](https://dl.acm.org/doi/abs/10.1145/3686807)被ACM TKDD录用** 25 | 26 | **[2023/09/24]发布[《面向智慧医疗的大语言模型微调技术》](https://github.com/SCIR-HI/Huatuo-Llama-Med-Chinese/tree/main/doc/Tuning_Methods_for_LLMs_towards_Health_Intelligence.pdf)** 27 | 28 | 29 |
30 | 历史新闻 31 | 32 | **[2023/09/12]在arxiv发布[《探索大模型从医学文献中交互式知识的获取》](https://arxiv.org/pdf/2309.04198.pdf)** 33 | 34 | **[2023/09/08]在arxiv发布[《基于知识微调的大语言模型可靠中文医学回复生成方法》](https://arxiv.org/pdf/2309.04175.pdf)** 35 | 36 | **[2023/08/07] 🔥🔥增加了基于[活字](https://github.com/HIT-SCIR/huozi)进行指令微调的模型发布,模型效果显著提升。🔥🔥** 37 | 38 | [2023/08/05] 本草模型在CCL 2023 Demo Track进行Poster展示。 39 | 40 | [2023/08/03] SCIR实验室开源[活字](https://github.com/HIT-SCIR/huozi)通用问答模型,欢迎大家关注🎉🎉 41 | 42 | [2023/07/19] 增加了基于[Bloom](https://huggingface.co/bigscience/bloom-7b1)进行指令微调的模型发布。 43 | 44 | [2023/05/12] 模型由"华驼"更名为"本草"。 45 | 46 | [2023/04/28] 增加了基于[中文Alpaca大模型](https://github.com/ymcui/Chinese-LLaMA-Alpaca)进行指令微调的模型发布。 47 | 48 | [2023/04/24] 增加了基于LLaMA和医学文献进行指令微调的模型发布。 49 | 50 |
51 | 52 | 53 | 54 | [2023/03/31] 增加了基于LLaMA和医学知识库进行指令微调的模型发布。 55 | 56 | 57 | ## A Quick Start 58 | 59 | 首先安装依赖包,python环境建议3.9+ 60 | 61 | ``` 62 | pip install -r requirements.txt 63 | 64 | ``` 65 | 66 | 针对所有基模型,我们采用了半精度基模型LoRA微调的方式进行指令微调训练,以在计算资源与模型性能之间进行权衡。 67 | 68 | ### 基模型 69 | - [活字1.0](https://github.com/HIT-SCIR/huozi),哈尔滨工业大学基于Bloom-7B二次开发的中文通用问答模型 70 | - [Bloom-7B](https://huggingface.co/bigscience/bloomz-7b1) 71 | - [Alpaca-Chinese-7B](https://github.com/ymcui/Chinese-LLaMA-Alpaca),基于LLaMA二次开发的中文问答模型 72 | - [LLaMA-7B](https://huggingface.co/decapoda-research/llama-7b-hf) 73 | 74 | ### LoRA模型权重下载 75 | 76 | LoRA权重可以通过百度网盘或Hugging Face下载: 77 | 78 | 1. 🔥对活字进行指令微调的LoRA权重文件 79 | - 基于医学知识库以及医学问答数据集 [百度网盘](https://pan.baidu.com/s/1BPnDNb1wQZTWy_Be6MfcnA?pwd=m21s) 80 | 2. 对Bloom进行指令微调的LoRA权重文件 81 | - 基于医学知识库以及医学问答数据集 [百度网盘](https://pan.baidu.com/s/1jPcuEOhesFGYpzJ7U52Fag?pwd=scir)和[Hugging Face](https://huggingface.co/lovepon/lora-bloom-med-bloom) 82 | 3. 对Alpaca进行指令微调的LoRA权重文件 83 | - 基于医学知识库 [百度网盘](https://pan.baidu.com/s/16oxcjzXnXjDpL8SKihgNxw?pwd=scir)和[Hugging Face](https://huggingface.co/lovepon/lora-alpaca-med) 84 | - 基于医学知识库和医学文献 [百度网盘](https://pan.baidu.com/s/1HDdK84ASHmzOFlkmypBIJw?pwd=scir)和[Hugging Face](https://huggingface.co/lovepon/lora-alpaca-med-alldata) 85 | 4. 对LLaMA进行指令微调的LoRA权重文件 86 | - 基于医学知识库 [百度网盘](https://pan.baidu.com/s/1jih-pEr6jzEa6n2u6sUMOg?pwd=jjpf)和[Hugging Face](https://huggingface.co/thinksoso/lora-llama-med) 87 | - 基于医学文献 [百度网盘](https://pan.baidu.com/s/1jADypClR2bLyXItuFfSjPA?pwd=odsk)和[Hugging Face](https://huggingface.co/lovepon/lora-llama-literature) 88 | 89 | 90 | 91 | 下载LoRA权重并解压,解压后的格式如下: 92 | 93 | 94 | ``` 95 | **lora-folder-name**/ 96 |   - adapter_config.json   # LoRA权重配置文件 97 |   - adapter_model.bin   # LoRA权重文件 98 | ``` 99 | 100 | 基于相同的数据,我们还训练了医疗版本的ChatGLM模型: [ChatGLM-6B-Med](https://github.com/SCIR-HI/Med-ChatGLM) 101 | 102 | 103 | ### Infer 104 | 105 | 我们在`./data/infer.json`中提供了一些测试用例,可以替换成其它的数据集,请注意保持格式一致 106 | 107 | 108 | 运行infer脚本 109 | 110 | 111 | ``` 112 | #基于医学知识库 113 | bash ./scripts/infer.sh 114 | 115 | #基于医学文献 116 | #单轮 117 | bash ./scripts/infer-literature-single.sh 118 | 119 | #多轮 120 | bash ./scripts/infer-literature-multi.sh 121 | ``` 122 | 123 | infer.sh脚本代码如下,请将下列代码中基模型base_model、lora权重lora_weights以及测试数据集路径instruct_dir进行替换后运行 124 | 125 | python infer.py \ 126 | --base_model 'BASE_MODEL_PATH' \ 127 | --lora_weights 'LORA_WEIGHTS_PATH' \ 128 | --use_lora True \ 129 | --instruct_dir 'INFER_DATA_PATH' \ 130 | --prompt_template 'TEMPLATE_PATH' 131 | 132 | 133 | **_提示模板的选择与模型相关,详情如下:_** 134 | 135 | | 活字&Bloom | LLaMA&Alpaca | 136 | |:------------------------------|:--------------------------------------------------------------------------------------| 137 | | `templates/bloom_deploy.json` | 基于医学知识库`templates/med_template.json`
基于医学文献`templates/literature_template.json` | 138 | 139 | 140 | 141 | 也可参考`./scripts/test.sh` 142 | 143 | 144 | 145 | ## 方法 146 | 147 | 基模型在医学问答场景下效果有限,指令微调是一种高效的使基模型拥有回答人类问题能力的方法。 148 | 149 | 150 | 151 | ### 数据集构建 152 | #### 医学知识库 153 | 我们采用了公开和自建的中文医学知识库,主要参考了[cMeKG](https://github.com/king-yyf/CMeKG_tools)。 154 | 155 | 医学知识库围绕疾病、药物、检查指标等构建,字段包括并发症,高危因素,组织学检查,临床症状,药物治疗,辅助治疗等。知识库示例如下: 156 | 157 | 158 | ``` 159 | 160 | {"中心词": "偏头痛", "相关疾病": ["妊娠合并偏头痛", "恶寒发热"], "相关症状": ["皮肤变硬", "头部及眼后部疼痛并能听到连续不断的隆隆声", "晨起头痛加重"], "所属科室": ["中西医结合科", "内科"], "发病部位": ["头部"]} 161 | 162 | ``` 163 | 164 | 我们利用GPT3.5接口围绕医学知识库构建问答数据,设置了多种Prompt形式来充分利用知识。 165 | 166 | 指令微调的训练集数据示例如下: 167 | 168 | ``` 169 | 170 | "问题:一位年轻男性长期使用可卡因,突然出现胸痛、呕吐、出汗等症状,经检查发现心电图反映心肌急性损伤,请问可能患的是什么疾病?治疗方式是什么?" 171 | 172 | 回答: 可能患的是心肌梗塞,需要进行维拉帕米、依普利酮、硝酸甘油、ß阻滞剂、吗啡等药物治疗,并进行溶栓治疗、低分子量肝素、钙通道阻滞剂等辅助治疗。此外需要及时停用可卡因等药物,以防止病情加重。" 173 | 174 | ``` 175 | 176 | 177 | 178 | 我们提供了模型的训练数据集,共计八千余条,需要注意的是,虽然训练集的构建融入了知识,但是仍存在错误和不完善的地方,后续我们会利用更好的策略迭代更新数据集。 179 | 180 | 指令微调数据集质量仍有限,后续将进行不断迭代,同时医学知识库和数据集构建代码还在整理中,整理完成将会发布。 181 | 182 | #### 医学文献 183 | 184 | 此外,我们收集了2023年关于肝癌疾病的中文医学文献,利用GPT3.5接口围绕医学文献的【结论】构建多轮问答数据。在·`./data_literature/liver_cancer.json`中我们提供了其中的1k条训练样例。目前,训练样本的质量仍然有限,在后续我们会进一步迭代数据,会以`公开数据集`的形式对外进行发布。训练样本的示例如下: 185 | 186 |

187 | 188 | SCIR-HI-HuaTuo-literature 189 | 190 |

191 | 192 | 目前,我们只开放针对"肝癌"单个疾病训练的模型参数。在未来,我们计划发布融入文献结论的医学对话数据集,并且会针对“肝胆胰”相关16种疾病训练模型。 193 | 194 | 相关细节可参考我们的文章:[《探索大模型从医学文献中交互式知识的获取》](https://arxiv.org/pdf/2309.04198.pdf) 195 | 196 | 197 | ### Finetune 198 | 199 | 如果想用自己的数据集微调大语言模型,请按照`./data/llama_data.json`中的格式构建自己的数据集 200 | 201 | 运行finetune脚本 202 | 203 | ``` 204 | 205 | bash ./scripts/finetune.sh 206 | 207 | ``` 208 | 209 | 210 | 211 | 212 | ### 知识微调 213 | 214 | 上述介绍了我们如何利用医学知识构造高质量问答对的过程,即从“knowledge”到“(question, answer)”,在此基础上,我们提出了知识微调,旨在让大语言模型在推理时显式利用知识库中的知识,使大模型学会从“question”到“knowledge”,过程可分为以下三阶段,其过程如下图所示。第一阶段:根据问题填充知识检索的参数,包括中心词和属性。第二阶段:查询获取带有填充参数所对应的知识。第三阶段:使用获得的知识来生成回答。[样例数据链接](data/knowledge_tuning_data_sample.txt) 215 |

216 | 217 | SCIR-HI-HuaTuo-literature 218 | 219 |

220 | 221 | 相关细节可参考我们的文章:[《基于知识微调的大语言模型可靠中文医学回复生成方法》](https://dl.acm.org/doi/abs/10.1145/3686807) 222 | 223 | 224 | 225 | 226 | ## 训练细节 227 | 228 | ### 计算资源需求参考 229 | 230 | 基于LLaMA模型的指令微调过程中,我们在一张A100-SXM-80GB显卡上进行了训练,训练总轮次10轮,耗时约2h17m。batch_size=128的情况下显存占用在40G左右。预计3090/4090显卡(24GB显存)以上显卡可以较好支持,根据显存大小来调整batch_size。 231 | 232 | 233 | ### 实验过程 234 | 235 | 超参设置和实验过程可以参考wandb的链接: 236 | 237 | https://wandb.ai/thinksoso/llama_med/runs/a5wgcnzt/overview?workspace=user-thinksoso 238 | 239 | 240 | 241 | ## 模型效果对比 242 | 243 | 注:测试于2023年3月 244 | 245 | | 测试输入 | Llama输出  | Alpaca输出 | BenTsao本草输出 | 246 | |:------------- |:---------------| :-------------|:--------------------------------------------------------| 247 | |小张最近感觉身体不适,出现心悸、气促等症状。体检发现心脏扩大、搏动减弱。|问题:小张最近感觉身体不适,出现心悸、气促等症状。体检发现心脏扩大、搏动减弱。| 最近,小张一直感觉身体不舒服,有心烦意乱、呼吸急促等症状。体格检查显示心脏增大,肌肉无力。| **小张可能患有心肌炎,建议尽快到医院进行检查和治疗。治疗方案包括药物治疗和手术治疗。** | 248 | |麻风病和儿童哮喘的病因是否一致?|儿童哮喘和麻风病的病因是一致,但是儿童哮喘和麻风病的病因不一致|是的,哮喘和儿童喘息有相似的原因。| **不一致,麻风病的病因是麻风杆菌感染,而儿童哮喘的病因是喂养不良、吸烟等因素。** | 249 | |一位患者出现了持续性的胀痛和黄疸,经影像学检查后确诊为肝胆管结石病,应该采取哪些治疗方案?|肝胆管结石病患者应该采取哪些治疗方案?|患者应接受手术和化疗相结合的治疗。| **针对肝胆管结石病,治疗方案包括手术治疗、药物治疗和辅助治疗等。具体治疗方案需要根据患者的具体情况而定。** | 250 | 251 | 252 | 253 | 254 | 255 | 256 | ## 常见问题 257 | 1. Q: 为什么更名为"本草"? 258 | 259 | A: 为SCIR实验室大语言模型命名一致性,中文医学大模型名称调整为"本草"。 260 | 261 | 2. Q: 为什么是"华驼"而不是"华佗"? 262 | 263 | A: 叫”驼“是因为我们的基模型LLaMA是美洲驼,Alpaca是羊驼,受他们名字的启发以及华佗的谐音梗,我们将我们的模型起名为华驼。 264 | 265 | 3. Q: 有使用中医理论或者中医数据吗? 266 | 267 | A: 目前还没有 268 | 269 | 4. Q: 模型运行的结果不同、效果有限 270 | 271 | A: 由于生成模型生成多样性的考量,多次运行的结果可能会有差异。当前开源的模型由于LLaMA及Alpaca中文语料有限,且知识结合的方式较为粗糙,请大家尝试bloom-based和活字-based的模型。 272 | 273 | 5. Q: 模型无法运行/推理内容完全无法接受 274 | 275 | A: 请确定已安装requirements中的依赖、配置好cuda环境并添加环境变量、正确输入下载好的模型以及lora的存储位置;推理内容如存在重复生成或部分错误内容属于llama-based模型的偶发现象,与llama模型的中文能力、训练数据规模以及超参设置均有一定的关系,请尝试基于活字的新模型。如存在严重问题,请将运行的文件名、模型名、lora等配置信息详细描述在issue中,谢谢大家。 276 | 277 | 6. Q: 发布的若干模型哪个最好? 278 | 279 | A: 根据我们的经验,基于活字模型的效果相对更好一些。 280 | 281 | 282 | 283 | 284 | ## 项目参与者 285 | 286 | 本项目由哈尔滨工业大学社会计算与信息检索研究中心健康智能组[王昊淳](https://haochun.wang) 、[杜晏睿](https://github.com/DYR1)、[刘驰](https://github.com/thinksoso)、[白睿](https://github.com/RuiBai1999)、[席奴瓦](https://github.com/rootnx)、[陈雨晗](https://github.com/Imsovegetable)、[强泽文](https://github.com/1278882181)、[陈健宇](https://github.com/JianyuChen01)、[李子健](https://github.com/FlowolfzzZ)、[范宇政](https://github.com/zheng-2001)、[刘佳彤](https://github.com/wdnnwza)完成,指导教师为[赵森栋](http://homepage.hit.edu.cn/stanzhao?lang=zh)副教授,秦兵教授以及刘挺教授。 287 | 288 | 289 | 290 | ## 致谢 291 | 292 | 293 | 294 | 本项目参考了以下开源项目,在此对相关项目和研究开发人员表示感谢。 295 | 296 | - 活字: https://github.com/HIT-SCIR/huozi 297 | - Facebook LLaMA: https://github.com/facebookresearch/llama 298 | - Stanford Alpaca: https://github.com/tatsu-lab/stanford_alpaca 299 | - alpaca-lora by @tloen: https://github.com/tloen/alpaca-lora 300 | - CMeKG https://github.com/king-yyf/CMeKG_tools 301 | - 文心一言 https://yiyan.baidu.com/welcome 本项目的logo由文心一言自动生成 302 | 303 | 304 | 305 | ## 免责声明 306 | 307 | 本项目相关资源仅供学术研究之用,严禁用于商业用途。使用涉及第三方代码的部分时,请严格遵循相应的开源协议。模型生成的内容受模型计算、随机性和量化精度损失等因素影响,本项目无法对其准确性作出保证。本项目数据集绝大部分由模型生成,即使符合某些医学事实,也不能被用作实际医学诊断的依据。对于模型输出的任何内容,本项目不承担任何法律责任,亦不对因使用相关资源和输出结果而可能产生的任何损失承担责任。 308 | 309 | 310 | ## Citation 311 | 312 | 如果您使用了本项目的数据或者代码,或是我们的工作对您有所帮助,请声明引用 313 | 314 | 315 | 316 | 知识微调:[Knowledge-tuning Large Language Models with Structured Medical Knowledge Bases for Trustworthy Response Generation in Chinese 317 | ](https://arxiv.org/pdf/2309.04175.pdf) 318 | 319 | ``` 320 | @article{10.1145/3686807, 321 | author = {Wang, Haochun and Zhao, Sendong and Qiang, Zewen and Li, Zijian and Liu, Chi and Xi, Nuwa and Du, Yanrui and Qin, Bing and Liu, Ting}, 322 | title = {Knowledge-tuning Large Language Models with Structured Medical Knowledge Bases for Trustworthy Response Generation in Chinese}, 323 | year = {2024}, 324 | publisher = {Association for Computing Machinery}, 325 | address = {New York, NY, USA}, 326 | issn = {1556-4681}, 327 | url = {https://doi.org/10.1145/3686807}, 328 | doi = {10.1145/3686807}, 329 | abstract = {Large Language Models (LLMs) have demonstrated remarkable success in diverse natural language processing (NLP) tasks in general domains. However, LLMs sometimes generate responses with the hallucination about medical facts due to limited domain knowledge. Such shortcomings pose potential risks in the utilization of LLMs within medical contexts. To address this challenge, we propose knowledge-tuning, which leverages structured medical knowledge bases for the LLMs to grasp domain knowledge efficiently and facilitate trustworthy response generation. We also release cMedKnowQA, a Chinese medical knowledge question-answering dataset constructed from medical knowledge bases to assess the medical knowledge proficiency of LLMs. Experimental results show that the LLMs which are knowledge-tuned with cMedKnowQA, can exhibit higher levels of accuracy in response generation compared with vanilla instruction-tuning and offer a new trustworthy way for the domain adaptation of LLMs. We release our code and data at .}, 330 | note = {Just Accepted}, 331 | journal = {ACM Trans. Knowl. Discov. Data}, 332 | month = aug, 333 | keywords = {Large Language Model, Medical Question Answering, Trustworthy Response Generation, Medical Knowledge Base} 334 | } 335 | ``` 336 | 337 | 首版技术报告: [Huatuo: Tuning llama model with chinese medical knowledge](https://arxiv.org/pdf/2304.06975) 338 | 339 | ``` 340 | @misc{wang2023huatuo, 341 |       title={HuaTuo: Tuning LLaMA Model with Chinese Medical Knowledge}, 342 |       author={Haochun Wang and Chi Liu and Nuwa Xi and Zewen Qiang and Sendong Zhao and Bing Qin and Ting Liu}, 343 |       year={2023}, 344 |       eprint={2304.06975}, 345 |       archivePrefix={arXiv}, 346 |       primaryClass={cs.CL} 347 | } 348 | ``` 349 | 350 | 医学文献知识获取:[The CALLA Dataset: Probing LLMs’ Interactive Knowledge Acquisition from Chinese Medical Literature](https://arxiv.org/pdf/2309.04198.pdf) 351 | 352 | ``` 353 | @misc{du2023calla, 354 | title={The CALLA Dataset: Probing LLMs' Interactive Knowledge Acquisition from Chinese Medical Literature}, 355 | author={Yanrui Du and Sendong Zhao and Muzhen Cai and Jianyu Chen and Haochun Wang and Yuhan Chen and Haoqiang Guo and Bing Qin}, 356 | year={2023}, 357 | eprint={2309.04198}, 358 | archivePrefix={arXiv}, 359 | primaryClass={cs.CL} 360 | } 361 | ``` 362 | 363 | -------------------------------------------------------------------------------- /README_EN.md: -------------------------------------------------------------------------------- 1 | [**中文**](./README.md) | [**English**](./README_EN.md) 2 |

3 | SCIR-HI-HuaTuo 4 |

5 | # BenTsao (original name: HuaTuo): Instruction-tuning Large Language Models With Chinese Medical Knowledge 6 | 7 | [![Code License](https://img.shields.io/badge/Code%20License-Apache_2.0-green.svg)](https://github.com/SCIR-HI/Huatuo-Llama-Med-Chinese/blob/main/LICENSE) [![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/release/python-390/) 8 | 9 | This repo open-sources a series of instruction-tuned large language models with Chinese medical instruction datasets, including LLaMA、Alpaca-Chinese、Bloom、Huozi. 10 | 11 | We constructed a Chinese medical instruct-tuning dataset using medical knowledge graphs, medical literatures and the GPT3.5 API, and performed instruction-tuning on various base models with this dataset, improving its question-answering performance in the medical field. 12 | 13 | 14 | ## News 15 | **[2023/08/07] 🔥🔥 Released a model instruction-tuned based on [Huozi](https://github.com/HIT-SCIR/huozi), resulting in a significant improvement in model performance. 🔥🔥** 16 | 17 | [2023/08/05] The "Bentsao" model was presented as a poster at the CCL 2023 Demo Track. 18 | 19 | [2023/08/03] SCIR Lab open-sourced the [Huozi](https://github.com/HIT-SCIR/huozi) general question-answering model. Everyone is welcome to check it out! 🎉🎉 20 | 21 | [2023/07/19] Released a model instruction-tuned based on [Bloom](https://huggingface.co/bigscience/bloom-7b1). 22 | 23 | [2023/05/12] The model was renamed from "Huatuo" to "Bentsao". 24 | 25 | [2023/04/28] Released a model instruction-tuned based on the [Chinese-Alpaca](https://github.com/ymcui/Chinese-LLaMA-Alpaca). 26 | 27 | [2023/04/24] Released a model instruction-tuned based on LLaMA with medical literature. 28 | 29 | [2023/03/31] Released a model instruction-tuned based on LLaMA with a medical knowledge base. 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | ## A Quick Start 38 | Firstly, install the required packages. It is recommended to use Python 3.9 or above 39 | 40 | ``` 41 | pip install -r requirements.txt 42 | ``` 43 | 44 | For all base models, we adopted the semi-precision base model LoRA fine-tuning method for instruction fine-tuning training, in order to strike a balance between computational resources and model performance. 45 | 46 | ### Base models 47 | - [Huozi1.0](https://github.com/HIT-SCIR/huozi), Bloom-7B-based Chinese QA model 48 | - [Bloom-7B](https://huggingface.co/bigscience/bloomz-7b1) 49 | - [Alpaca-Chinese-7B](https://github.com/ymcui/Chinese-LLaMA-Alpaca), LLaMA-based Chinese QA model 50 | - [LLaMA-7B](https://huggingface.co/decapoda-research/llama-7b-hf) 51 | 52 | 53 | ### LoRA weight download 54 | LORA weights can be downloaded through Baidu Netdisk or Huggingface. 55 | 56 | 1. 🔥LoRA for Huozi 1.0 57 | - with the medical knowledge base and medical QA dataset [BaiduDisk](https://pan.baidu.com/s/1BPnDNb1wQZTWy_Be6MfcnA?pwd=m21s) 58 | 2. LoRA for Bloom 59 | - with the medical knowledge base and medical QA dataset [BaiduDisk](https://pan.baidu.com/s/1jPcuEOhesFGYpzJ7U52Fag?pwd=scir) and [Hugging Face](https://huggingface.co/lovepon/lora-bloom-med-bloom) 60 | 3. LoRA for Chinese Alpaca 61 | - with the medical knowledge base [BaiduDisk](https://pan.baidu.com/s/16oxcjzXnXjDpL8SKihgNxw?pwd=scir) and [Hugging Face](https://huggingface.co/lovepon/lora-alpaca-med) 62 | - with the medical knowledge base and medical literature [BaiduDisk](https://pan.baidu.com/s/1HDdK84ASHmzOFlkmypBIJw?pwd=scir) and [Hugging Face](https://huggingface.co/lovepon/lora-alpaca-med-alldata) 63 | 4. LoRA for LLaMA 64 | - with the medical knowledge base [BaiduDisk](https://pan.baidu.com/s/1jih-pEr6jzEa6n2u6sUMOg?pwd=jjpf) and [Hugging Face](https://huggingface.co/thinksoso/lora-llama-med) 65 | - with medical literature [BaiduDisk](https://pan.baidu.com/s/1jADypClR2bLyXItuFfSjPA?pwd=odsk) and [Hugging Face](https://huggingface.co/lovepon/lora-llama-literature) 66 | 67 | 68 | Download the LORA weight file and extract it. The format of the extracted file should be as follows: 69 | 70 | ``` 71 | **lora-folder-name**/ 72 |   - adapter_config.json   # LoRA configuration 73 |   - adapter_model.bin   # LoRA weight 74 | ``` 75 | 76 | We also trained a medical version of ChatGLM: [ChatGLM-6B-Med](https://github.com/SCIR-HI/Med-ChatGLM) based on the same data. 77 | 78 | 79 | ### Infer 80 | We provided some test cases in `./data/infer.json`, which can be replaced with other datasets. Please make sure to keep the format consistent. 81 | 82 | Run the infer script 83 | 84 | ``` 85 | #Based on medical knowledge base 86 | bash ./scripts/infer.sh 87 | 88 | #Based on medical literature 89 | #single-epoch 90 | bash ./scripts/infer-literature-single.sh 91 | 92 | #multi-epoch 93 | bash ./scripts/infer-literature-multi.sh 94 | ``` 95 | 96 | The code for the infer.sh script is as follows. Please replace the base model base_model, the LoRA weights lora_weights, and the test dataset path instruct_dir before running 97 | 98 | python infer.py \ 99 | --base_model 'BASE_MODEL_PATH' \ 100 | --lora_weights 'LORA_WEIGHTS_PATH' \ 101 | --use_lora True \ 102 | --instruct_dir 'INFER_DATA_PATH' \ 103 | --prompt_template 'TEMPLATE_PATH' 104 | 105 | 106 | The prompt template is relevant to the model as follows: 107 | 108 | | Huozi&Bloom | LLaMA&Alpaca | 109 | |:------------------------------|:--------------------------------------------------------------------------------------| 110 | | `templates/bloom_deploy.json` | with the medical knowledge base`templates/med_template.json`
with the medical literature`templates/literature_template.json` | 111 | 112 | 113 | 114 | other reference in `./scripts/test.sh` 115 | 116 | 117 | ## Methodology 118 | The base model has limited effectiveness in medical question-answering scenarios. Instruction-tuning is an efficient method to give the base model the ability to answer human questions 119 | 120 | ### Dataset construction 121 | #### Medical knowledge base 122 | We used both publicly available and self-built Chinese medical knowledge bases, with a primary reference to [cMeKG](https://github.com/king-yyf/CMeKG_tools). The medical knowledge base is built around diseases, drugs, and diagnostic indicators, with fields including complications, risk factors, histological examinations, clinical symptoms, drug treatments, and adjuvant therapies. An example of the knowledge base is shown below: 123 | 124 | 125 | ``` 126 | {"中心词": "偏头痛", "相关疾病": ["妊娠合并偏头痛", "恶寒发热"], "相关症状": ["皮肤变硬", "头部及眼后部疼痛并能听到连续不断的隆隆声", "晨起头痛加重"], "所属科室": ["中西医结合科", "内科"], "发病部位": ["头部"]} 127 | ``` 128 | We used the GPT3.5 API to build a Question-Answer dataset around a medical knowledge base, with multiple prompts to fully leverage the knowledge. 129 | 130 | An example of the training dataset for instruct-tuning is shown below: 131 | 132 | 133 | ``` 134 | "问题:一位年轻男性长期使用可卡因,突然出现胸痛、呕吐、出汗等症状,经检查发现心电图反映心肌急性损伤,请问可能患的是什么疾病?治疗方式是什么?" 135 | 回答: 可能患的是心肌梗塞,需要进行维拉帕米、依普利酮、硝酸甘油、ß阻滞剂、吗啡等药物治疗,并进行溶栓治疗、低分子量肝素、钙通道阻滞剂等辅助治疗。此外需要及时停用可卡因等药物,以防止病情加重。" 136 | ``` 137 | 138 | We provided a training dataset for the model, consisting of more than eight thousand entries. It should be noted that although knowledge has been incorporated into the construction of the training set, there are still errors and imperfections. We will use better strategies to iteratively update the dataset in the future. 139 | 140 | The quality of the dataset for instruct-tuning is still limited. We will continue to iterate and improve it. Meanwhile, the medical knowledge base and dataset construction code are still being organized and will be released once completed. 141 | 142 | #### Medical literature 143 | In addition, we collected Chinese medical literature on liver cancer in 2023, and used the GPT3.5 interface to collect multiple rounds of question-and-answer data around the medical literature. We provide 1k training examples in `./data_literature/liver_cancer.json`. At present, the quality of training samples is still limited. In the future, we will further iterate the data and release it in the form of `public dataset`. An example of a training sample is as follows: 144 | 145 |

146 | 147 | SCIR-HI-HuaTuo-literature 148 | 149 |

150 | 151 | At present, we only open model parameters trained for the single disease "liver cancer". In the future, we plan to release a medical dialogue dataset incorporating medical literature conclusions, and plan to train models for 16 diseases related to "liver, gallbladder and pancreas". 152 | 153 | ### Finetune 154 | To fine-tune LLaMA with your own dataset, please construct your dataset following the format of `./data/llama_data.json` and run the finetune script. 155 | 156 | ``` 157 | bash ./scripts/finetune.sh 158 | ``` 159 | 160 | 161 | 162 | ## Training details 163 | ### GPU resource 164 | We trained the model on an A100-SXM-80GB for a total of 10 epochs, which took approximately 2 hours and 17 minutes. With a batch size of 128, the GPU memory usage was around 40GB. We expect that GPU with 24GB or more of memory, such as the 3090/4090, will be able to support the model well, and the batch size can be adjusted based on the size of the GPU memory. 165 | 166 | 167 | ### Experiments 168 | Parameter settings and details training process can be found in the wandb log: 169 | https://wandb.ai/thinksoso/llama_med/runs/a5wgcnzt/overview?workspace=user-thinksoso 170 | 171 | ## Comparison of models 172 | 173 | Note: test in March 2023. 174 | 175 | | Input入 | Llama output | Alpaca output | HuaTuo output | 176 | |:------------- |:---------------| :-------------|:-------------| 177 | |小张最近感觉身体不适,出现心悸、气促等症状。体检发现心脏扩大、搏动减弱。|问题:小张最近感觉身体不适,出现心悸、气促等症状。体检发现心脏扩大、搏动减弱。| 最近,小张一直感觉身体不舒服,有心烦意乱、呼吸急促等症状。体格检查显示心脏增大,肌肉无力。|**小张可能患有心肌炎,建议尽快到医院进行检查和治疗。治疗方案包括药物治疗和手术治疗。**| 178 | |麻风病和儿童哮喘的病因是否一致?|儿童哮喘和麻风病的病因是一致,但是儿童哮喘和麻风病的病因不一致|是的,哮喘和儿童喘息有相似的原因。| **不一致,麻风病的病因是麻风杆菌感染,而儿童哮喘的病因是喂养不良、吸烟等因素。**| 179 | |一位患者出现了持续性的胀痛和黄疸,经影像学检查后确诊为肝胆管结石病,应该采取哪些治疗方案?|肝胆管结石病患者应该采取哪些治疗方案?|患者应接受手术和化疗相结合的治疗。| **针对肝胆管结石病,治疗方案包括手术治疗、药物治疗和辅助治疗等。具体治疗方案需要根据患者的具体情况而定。**| 180 | 181 | ##FAQs 182 | 183 | 1. Q: Why was it renamed to "Bentsao"? 184 | 185 | A: For naming consistency with the SCIR large language model series, the name of the Chinese medical model has been renamed to "Bentsao". 186 | 187 | 2. Q: Why "Huatuo(华驼)" instead of "Huatuo(华佗)"? 188 | 189 | A: We chose the name "Huatuo(华驼)" because our base model, LLaMA(美洲驼), and Alpaca(羊驼). Inspired by their names and a pun on the name "tuo驼" & "tuo佗", we named our model "Huatuo(华驼)". 190 | 191 | 3. Q: Does it use Chinese traditional medicine theories or data? 192 | 193 | A: Not at the moment. 194 | 195 | 4. Q: The results from the model vary and are limited in effectiveness. 196 | 197 | A: Due to the diversity considerations of the generative model, results may vary with multiple runs. The current open-source model, owing to the limited Chinese corpus in LLaMA and Alpaca, and a rather coarse way of knowledge integration, may yield inconsistent results. Please try the bloom-based and character-based models. 198 | 5. Q: The model cannot run or the inferred content is completely unacceptable. 199 | 200 | A: Please ensure that you've installed the dependencies from the requirements, set up the CUDA environment and added the environment variables, and correctly input the downloaded model and Lora's storage location. Any repetition or partial mistakes in the inferred content are occasional issues with the llama-based model and relate to LLaMA's ability in Chinese, training data scale, and hyperparameter settings. Please try the new character-based model. If there are severe issues, please describe in detail the filename, model name, Lora configuration, etc., in an issue. Thank you all. 201 | Q: Among the released models, which one is the best? 202 | A: Based on our experience, the character-based model seems to perform relatively better. 203 | 204 | 205 | 206 | 207 | 208 | 209 | ## Contributors 210 | 211 | This project was founded by the Health Intelligence Group of the Research Center for Social Computing and Information Retrieval at Harbin Institute of Technology, including [Haochun Wang](https://github.com/s65b40), [Yanrui Du](https://github.com/DYR1), [Chi Liu](https://github.com/thinksoso), [Rui Bai](https://github.com/RuiBai1999), [Nuwa Xi](https://github.com/rootnx), [Yuhan Chen](https://github.com/Imsovegetable), [Zewen Qiang](https://github.com/1278882181), [Jianyu Chen](https://github.com/JianyuChen01), [Zijian Li](https://github.com/FlowolfzzZ) supervised by Associate Professor [Sendong Zhao](http://homepage.hit.edu.cn/stanzhao?lang=zh), Professor Bing Qin, and Professor Ting Liu. 212 | 213 | 214 | ## Acknowledgements 215 | 216 | This project has referred the following open-source projects. We would like to express our gratitude to the developers and researchers involved in those projects. 217 | 218 | - Facebook LLaMA: https://github.com/facebookresearch/llama 219 | - Stanford Alpaca: https://github.com/tatsu-lab/stanford_alpaca 220 | - alpaca-lora by @tloen: https://github.com/tloen/alpaca-lora 221 | - CMeKG https://github.com/king-yyf/CMeKG_tools 222 | - 文心一言 https://yiyan.baidu.com/welcome The logo of this project is automatically generated by Wenxin Yiyan. 223 | 224 | ## Disclaimer 225 | The resources related to this project are for academic research purposes only and strictly prohibited for commercial use. When using portions of third-party code, please strictly comply with the corresponding open source licenses. The content generated by the model is influenced by factors such as model computation, randomness, and quantization accuracy loss, and this project cannot guarantee its accuracy. The vast majority of the dataset used in this project is generated by the model, and even if it conforms to certain medical facts, it cannot be used as the basis for actual medical diagnosis. This project does not assume any legal liability for the content output by the model, nor is it responsible for any losses that may be incurred as a result of using the related resources and output results. 226 | 227 | 228 | ## Citation 229 | If you use the data or code from this project, please declare the reference: 230 | 231 | ``` 232 | @misc{wang2023huatuo, 233 | title={HuaTuo: Tuning LLaMA Model with Chinese Medical Knowledge}, 234 | author={Haochun Wang and Chi Liu and Nuwa Xi and Zewen Qiang and Sendong Zhao and Bing Qin and Ting Liu}, 235 | year={2023}, 236 | eprint={2304.06975}, 237 | archivePrefix={arXiv}, 238 | primaryClass={cs.CL} 239 | } 240 | ``` 241 | 242 | 243 | 244 | -------------------------------------------------------------------------------- /assets/benchmark.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SCIR-HI/Huatuo-Llama-Med-Chinese/13ab5ff8c351404c47e35c1a5e43f76c28c940a1/assets/benchmark.png -------------------------------------------------------------------------------- /assets/case.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SCIR-HI/Huatuo-Llama-Med-Chinese/13ab5ff8c351404c47e35c1a5e43f76c28c940a1/assets/case.png -------------------------------------------------------------------------------- /assets/logo/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SCIR-HI/Huatuo-Llama-Med-Chinese/13ab5ff8c351404c47e35c1a5e43f76c28c940a1/assets/logo/logo.png -------------------------------------------------------------------------------- /assets/logo/logo_new.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SCIR-HI/Huatuo-Llama-Med-Chinese/13ab5ff8c351404c47e35c1a5e43f76c28c940a1/assets/logo/logo_new.png -------------------------------------------------------------------------------- /assets/process_of_knowledge_tuning.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SCIR-HI/Huatuo-Llama-Med-Chinese/13ab5ff8c351404c47e35c1a5e43f76c28c940a1/assets/process_of_knowledge_tuning.png -------------------------------------------------------------------------------- /assets/wechat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SCIR-HI/Huatuo-Llama-Med-Chinese/13ab5ff8c351404c47e35c1a5e43f76c28c940a1/assets/wechat.jpg -------------------------------------------------------------------------------- /assets/wechat_group.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SCIR-HI/Huatuo-Llama-Med-Chinese/13ab5ff8c351404c47e35c1a5e43f76c28c940a1/assets/wechat_group.jpg -------------------------------------------------------------------------------- /benchmark/README.md: -------------------------------------------------------------------------------- 1 | ## 评价基准 2 | 我们提出了中文医学复杂开放式问题回答基准(CMCOQA),旨在通过在医疗场景中构建复杂的开放式问题来更准确、有效地评估大语言模型的医学水平。 3 | ### 基准构建 4 | 我们首先手动构建了100个复杂的医学问题,并通过GPT-4结合Self-Instruct方法扩展至1200个问题,形成CMCOQA基准。并依据ICD-10标准进行问题分类,对某些类别进行调整,以确保数据集涵盖了各种疾病,且分布相对均衡。 5 | - [CMCOQA基准数据集样例](question.json) 6 | ### 评价维度 7 | CMCOQA基准涉及三个评估维度:完整性(Completeness)、深刻性(Depth)和专业性(Professionalism)。在每个维度按照下表的评分标准,使用0到3分进行评分。 8 | 9 | 10 | | 维度 | 标准 | 描述 | 11 | |:----------:|:----------:|:------------------------------------:| 12 | | 完整性 | 覆盖度 | 涵盖问题的各个方面,例如病因、诊断、治疗和预防。 | 13 | | | 相关性 | 与问题直接相关,避免无关信息。 | 14 | | 深刻性 | 分析深度 | 提供深入的分析和讨论,而不仅仅是表面的描述。 | 15 | | | 见解 | 提供建设性的建议或观点。 | 16 | | 专业性 | 准确性 | 确保内容准确,避免错误或误导性内容。 | 17 | | | 简洁性 | 言简意赅,直奔主题,没有冗余信息。 | 18 | | | 术语使用 | 恰当使用专业术语,展现专业水平。 | 19 | 20 | ### 评价结果 21 | 我们使用人工和GPT-4o结合的方式,使用CMCOQA基准对一系列医学大模型以及通用领域大模型进行评估,可以看到,在医学大模型领域,基于知识微调的本草大模型表现优异,尤其在回复的深刻性方面,显著优于其他基线。 22 | 23 |

24 | SCIR-HI-HuaTuo-result 25 |

26 | -------------------------------------------------------------------------------- /benchmark/question.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "question": "描述传染性的甲型肝炎的传播途径及预防措施。", 4 | "ICD-10": "Certain infectious and parasitic diseases" 5 | }, 6 | { 7 | "question": "请说明腓骨神经病变的常见病因和治疗方法。", 8 | "ICD-10": "Diseases of the nervous system" 9 | }, 10 | { 11 | "question": "儿童多动症的核心症状是什么?有哪些常见的治疗方案?", 12 | "ICD-10": "Mental and behavioural disorders" 13 | }, 14 | { 15 | "question": "心律失常的常见类型有哪些,它们是如何被诊断的?", 16 | "ICD-10": "Diseases of the circulatory system" 17 | }, 18 | { 19 | "question": "葡萄膜炎的临床表现是什么,以及常见的治疗方法有哪些?", 20 | "ICD-10": "Diseases of the eye and adnexa" 21 | }, 22 | { 23 | "question": "脑卒中急性发作时的处理步骤是什么,及早期症状有哪些?", 24 | "ICD-10": "Diseases of the circulatory system" 25 | }, 26 | { 27 | "question": "癌症的放射治疗有何副作用?如何管理这些副作用?", 28 | "ICD-10": "Neoplasms" 29 | }, 30 | { 31 | "question": "阐述无创产前基因检测(NIPT)的工作原理及适用人群。", 32 | "ICD-10": "Pregnancy, childbirth and the puerperium" 33 | }, 34 | { 35 | "question": "动脉硬化的主要风险因素有哪些,预防措施应怎么做?", 36 | "ICD-10": "Diseases of the circulatory system" 37 | }, 38 | { 39 | "question": "什么是急性心肌梗死的主要症状,急救处理应包括哪些步骤?", 40 | "ICD-10": "Diseases of the circulatory system" 41 | }, 42 | { 43 | "question": "请解释吸烟对肺部健康的负面影响,以及戒烟有哪些科学有效的方法?", 44 | "ICD-10": "Diseases of the respiratory system" 45 | }, 46 | { 47 | "question": "详细描述常见的急性胃肠炎症状及处理方法。", 48 | "ICD-10": "Diseases of the digestive system" 49 | }, 50 | { 51 | "question": "你会如何向患者解释血浆置换疗法的步骤与注意事项?", 52 | "ICD-10": "Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism" 53 | }, 54 | { 55 | "question": "肩周炎的常见症状是什么,它通常如何被治疗?", 56 | "ICD-10": "Diseases of the musculoskeletal system and connective tissue" 57 | }, 58 | { 59 | "question": "白血病分为哪些不同的类型,每种类型的主要特点是什么?", 60 | "ICD-10": "Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism" 61 | }, 62 | { 63 | "question": "多发性硬化症的早期症状有哪些?这个病是如何确诊的?", 64 | "ICD-10": "Diseases of the nervous system" 65 | }, 66 | { 67 | "question": "请解释何谓多发性骨髓瘤,并概述其常见的诊断和治疗方法。", 68 | "ICD-10": "Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism" 69 | }, 70 | { 71 | "question": "请解释一下传染性单核细胞增多症的病因和预防方法。", 72 | "ICD-10": "Certain infectious and parasitic diseases" 73 | }, 74 | { 75 | "question": "解释梅毒的各个阶段及其在不同阶段的主要症状表现。", 76 | "ICD-10": "Certain infectious and parasitic diseases" 77 | }, 78 | { 79 | "question": "假设一个患者出现了黄疸,这可能提示哪些潜在的健康问题?请描述相关的病因和诊断流程。", 80 | "ICD-10": "Diseases of the digestive system" 81 | }, 82 | { 83 | "question": "请解释什么是RAST测试,它在过敏检测中的作用是什么?", 84 | "ICD-10": "Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism" 85 | }, 86 | { 87 | "question": "早期阿尔茨海默病有哪些典型表现?这种病在初期如何被诊断和治疗?", 88 | "ICD-10": "Diseases of the nervous system" 89 | }, 90 | { 91 | "question": "解释癌症免疫治疗的原理,并举例说明一些常见的免疫治疗方法。", 92 | "ICD-10": "Neoplasms" 93 | }, 94 | { 95 | "question": "乙肝病毒携带者需要定期进行哪些检查?这些检查对于病情监测有什么重要作用?", 96 | "ICD-10": "Certain infectious and parasitic diseases" 97 | }, 98 | { 99 | "question": "克罗恩病和溃疡性结肠炎有什么区别?", 100 | "ICD-10": "Diseases of the digestive system" 101 | }, 102 | { 103 | "question": "肌肉萎缩症的遗传背景及疾病进展机制是什么?", 104 | "ICD-10": "Diseases of the musculoskeletal system and connective tissue" 105 | }, 106 | { 107 | "question": "什么是系统性红斑狼疮?其常见症状及诊断标准是什么?", 108 | "ICD-10": "Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism" 109 | }, 110 | { 111 | "question": "什么是乳腺纤维腺瘤,它通常如何被处理?", 112 | "ICD-10": "Neoplasms" 113 | }, 114 | { 115 | "question": "请解释什么是偏头痛,以及一些常见的治疗方法。", 116 | "ICD-10": "Diseases of the nervous system" 117 | }, 118 | { 119 | "question": "什么是膝关节炎,患者通常会有哪些表现,如何缓解这些症状?", 120 | "ICD-10": "Diseases of the musculoskeletal system and connective tissue" 121 | }, 122 | { 123 | "question": "急性肾损伤的常见原因有哪些,如何进行预防和早期识别?", 124 | "ICD-10": "Diseases of the genitourinary system" 125 | }, 126 | { 127 | "question": "对于糖尿病患者,控制血糖的饮食建议是什么?推荐几种对控制血糖有益的食物。", 128 | "ICD-10": "Endocrine, nutritional and metabolic diseases" 129 | }, 130 | { 131 | "question": "什么是格林-巴利综合征,它的主要临床表现是什么?", 132 | "ICD-10": "Diseases of the nervous system" 133 | }, 134 | { 135 | "question": "描述心房纤颤的成因及其可能带来的健康风险。", 136 | "ICD-10": "Diseases of the circulatory system" 137 | }, 138 | { 139 | "question": "什么是呼吸机相关性肺炎,其预防措施有哪些?", 140 | "ICD-10": "Diseases of the respiratory system" 141 | }, 142 | { 143 | "question": "解释黄斑变性的病因及其治疗选择。", 144 | "ICD-10": "Diseases of the eye and adnexa" 145 | }, 146 | { 147 | "question": "什么是皮质类固醇,它们在医学上有何应用?请提供两个例子。", 148 | "ICD-10": "Endocrine, nutritional and metabolic diseases" 149 | }, 150 | { 151 | "question": "什么是双相情感障碍,其主要症状表现有哪些?", 152 | "ICD-10": "Mental and behavioural disorders" 153 | }, 154 | { 155 | "question": "过敏反应是如何发生的?请列举三种常见的致敏物质,并描述相应的防治措施。", 156 | "ICD-10": "Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism" 157 | }, 158 | { 159 | "question": "产后抑郁的症状是什么,有哪些有效的治疗或干预方法?", 160 | "ICD-10": "Mental and behavioural disorders" 161 | }, 162 | { 163 | "question": "乙肝病毒携带者能否进行体育锻炼?如果能,应该注意哪些事项?", 164 | "ICD-10": "Certain infectious and parasitic diseases" 165 | }, 166 | { 167 | "question": "胃溃疡的常见症状有哪些,它们通常如何被治疗?", 168 | "ICD-10": "Diseases of the digestive system" 169 | }, 170 | { 171 | "question": "请详细说明儿童注意力缺陷多动障碍(ADHD)的诊断标准。", 172 | "ICD-10": "Mental and behavioural disorders" 173 | }, 174 | { 175 | "question": "如何预防和控制流感的传播?如何做到自我保护?", 176 | "ICD-10": "Certain infectious and parasitic diseases" 177 | }, 178 | { 179 | "question": "请列出几种常见的抗抑郁药物及其可能的副作用。", 180 | "ICD-10": "Mental and behavioural disorders" 181 | }, 182 | { 183 | "question": "糖尿病性视网膜病变的主要病理改变是什么,它如何影响视力?", 184 | "ICD-10": "Endocrine, nutritional and metabolic diseases" 185 | }, 186 | { 187 | "question": "艾滋病的传播途径有哪些,如何进行有效的预防?", 188 | "ICD-10": "Certain infectious and parasitic diseases" 189 | }, 190 | { 191 | "question": "什么是代谢综合征,它由哪些疾病组成,检查时应主要关注哪些指标?", 192 | "ICD-10": "Endocrine, nutritional and metabolic diseases" 193 | }, 194 | { 195 | "question": "慢性肾病有哪些病因及常见的治疗方法?", 196 | "ICD-10": "Diseases of the genitourinary system" 197 | }, 198 | { 199 | "question": "如何诊断甲状腺功能减退症,以及它的常见症状有哪些?", 200 | "ICD-10": "Endocrine, nutritional and metabolic diseases" 201 | }, 202 | { 203 | "question": "在肝脏活检过程中,有哪些需要注意的事项和潜在风险?", 204 | "ICD-10": "Diseases of the digestive system" 205 | }, 206 | { 207 | "question": "骨梗死的病理特征是什么,如何进行诊断和治疗?", 208 | "ICD-10": "Diseases of the musculoskeletal system and connective tissue" 209 | }, 210 | { 211 | "question": "如何鉴别病毒性和细菌性咽喉炎?请描述两者在症状和治疗方法上的主要区别。", 212 | "ICD-10": "Certain infectious and parasitic diseases" 213 | }, 214 | { 215 | "question": "周期性麻痹的发生机制是什么,主要的诱发因素有哪些?", 216 | "ICD-10": "Diseases of the nervous system" 217 | }, 218 | { 219 | "question": "小儿肠痉挛的症状和原因是什么?如何缓解这种情况?", 220 | "ICD-10": "Diseases of the digestive system" 221 | }, 222 | { 223 | "question": "对于严重的腹腔出血,通常有哪些急救措施?", 224 | "ICD-10": "Diseases of the digestive system" 225 | }, 226 | { 227 | "question": "血液检查中,C反应蛋白升高通常代表什么,还可能暗示哪些健康问题?", 228 | "ICD-10": "Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism" 229 | }, 230 | { 231 | "question": "季节性皮炎的症状有哪些?为了减轻症状,最有效的预防措施是什么?", 232 | "ICD-10": "Diseases of the skin and subcutaneous tissue" 233 | }, 234 | { 235 | "question": "什么是完全性房室传导阻滞,患者可能会有哪些症状?", 236 | "ICD-10": "Diseases of the circulatory system" 237 | }, 238 | { 239 | "question": "口服异维A酸治疗重度痤疮的注意事项有哪些?", 240 | "ICD-10": "Diseases of the skin and subcutaneous tissue" 241 | }, 242 | { 243 | "question": "骨肉瘤是什么?它的病因、症状以及治疗方式是什么?", 244 | "ICD-10": "Neoplasms" 245 | }, 246 | { 247 | "question": "如何预防老年人髋部骨折,骨折后有什么有效的治疗方法?", 248 | "ICD-10": "Diseases of the musculoskeletal system and connective tissue" 249 | }, 250 | { 251 | "question": "对比急性肝衰竭与慢性肝病的病因、临床表现和治疗方法。", 252 | "ICD-10": "Diseases of the digestive system" 253 | }, 254 | { 255 | "question": "小儿哮鸣有哪些诱因,应该采取哪些预防措施?", 256 | "ICD-10": "Diseases of the respiratory system" 257 | }, 258 | { 259 | "question": "肾脏移植手术后,病人需要注意哪些事项来避免排斥反应?", 260 | "ICD-10": "Diseases of the genitourinary system" 261 | }, 262 | { 263 | "question": "请描述大肠息肉的危险因素及其去除手术的风险。", 264 | "ICD-10": "Diseases of the digestive system" 265 | }, 266 | { 267 | "question": "眼压高的危害有哪些?哪些方法可以有效控制眼压?", 268 | "ICD-10": "Diseases of the eye and adnexa" 269 | }, 270 | { 271 | "question": "视疲劳通常有哪些表现,应该如何缓解?", 272 | "ICD-10": "Diseases of the eye and adnexa" 273 | }, 274 | { 275 | "question": "成骨不全症(又称脆骨病)的病因是什么?有哪些常见的临床特征?", 276 | "ICD-10": "Congenital malformations, deformations and chromosomal abnormalities" 277 | }, 278 | { 279 | "question": "如何治疗胃酸反流?有哪些非药物的管理方式?", 280 | "ICD-10": "Diseases of the digestive system" 281 | }, 282 | { 283 | "question": "在冠心病急性发作时,患者应如何进行紧急处理?", 284 | "ICD-10": "Diseases of the circulatory system" 285 | }, 286 | { 287 | "question": "庖疹性皮肤病的主要症状和诊疗方法是什么?", 288 | "ICD-10": "Diseases of the skin and subcutaneous tissue" 289 | }, 290 | { 291 | "question": "什么是体外膜肺氧合(ECMO)?在什么情况下会使用这种技术?", 292 | "ICD-10": "Others" 293 | }, 294 | { 295 | "question": "银屑病的病因和病理学表现是什么,临床上有哪些治疗方法?", 296 | "ICD-10": "Diseases of the skin and subcutaneous tissue" 297 | }, 298 | { 299 | "question": "请详细说明髌骨软化症的症状及康复训练方法。", 300 | "ICD-10": "Diseases of the musculoskeletal system and connective tissue" 301 | }, 302 | { 303 | "question": "请描述进行骨密度测试的目的是什么,常见的测试方法有哪些?", 304 | "ICD-10": "Diseases of the musculoskeletal system and connective tissue" 305 | }, 306 | { 307 | "question": "抗焦虑药物氯硝西泮的用法和禁忌是什么?", 308 | "ICD-10": "Mental and behavioural disorders" 309 | }, 310 | { 311 | "question": "何为格雷夫斯病,它是如何诊断和治疗的?", 312 | "ICD-10": "Endocrine, nutritional and metabolic diseases" 313 | }, 314 | { 315 | "question": "请解释手足口病的传播途径及其主要预防措施。", 316 | "ICD-10": "Certain infectious and parasitic diseases" 317 | }, 318 | { 319 | "question": "请解析在医学检查中“红细胞沉降率”升高的可能原因及其临床意义。", 320 | "ICD-10": "Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism" 321 | }, 322 | { 323 | "question": "比较细菌性感冒和病毒性感冒的症状和治疗方法的不同。", 324 | "ICD-10": "Certain infectious and parasitic diseases" 325 | }, 326 | { 327 | "question": "请比较急性应激障碍和创伤后应激障碍(PTSD)的主要区别。", 328 | "ICD-10": "Mental and behavioural disorders" 329 | }, 330 | { 331 | "question": "适用于胃溃疡的药物类型都有哪几种?请详细描述其作用机制。", 332 | "ICD-10": "Diseases of the digestive system" 333 | }, 334 | { 335 | "question": "中枢性尿崩症的主要病因是什么,通常通过哪些方法进行诊断和治疗?", 336 | "ICD-10": "Endocrine, nutritional and metabolic diseases" 337 | }, 338 | { 339 | "question": "描述肾小球肾炎的病理机制以及常见的影像学特点。", 340 | "ICD-10": "Diseases of the genitourinary system" 341 | }, 342 | { 343 | "question": "对比川崎病和手足口病的临床表现和治疗方案。", 344 | "ICD-10": "Certain infectious and parasitic diseases" 345 | }, 346 | { 347 | "question": "详细说明急性脊髓损伤的紧急处理步骤和长期护理。", 348 | "ICD-10": "Injury, poisoning and certain other consequences of external causes" 349 | }, 350 | { 351 | "question": "如何识别脚癣的症状?推荐几种有效的治疗方法。", 352 | "ICD-10": "Diseases of the skin and subcutaneous tissue" 353 | }, 354 | { 355 | "question": "当一个人长期食欲不振时,可能是什么原因导致的?应如何进行诊断和治疗?", 356 | "ICD-10": "Endocrine, nutritional and metabolic diseases" 357 | }, 358 | { 359 | "question": "请解释一下什么是免疫复合物,哪些疾病与其相关?", 360 | "ICD-10": "Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism" 361 | }, 362 | { 363 | "question": "深部脑刺激(DBS)治疗帕金森病的机制是什么?", 364 | "ICD-10": "Diseases of the nervous system" 365 | }, 366 | { 367 | "question": "异位性皮炎的临床表现是什么?有哪些有效的治疗手段?", 368 | "ICD-10": "Diseases of the skin and subcutaneous tissue" 369 | }, 370 | { 371 | "question": "什么是风疹,它是如何传播的,主要症状是什么?", 372 | "ICD-10": "Certain infectious and parasitic diseases" 373 | }, 374 | { 375 | "question": "MSCI等血液学检查在诊断白血病中的作用是什么?", 376 | "ICD-10": "Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism" 377 | }, 378 | { 379 | "question": "肠梗阻一般分为哪几类?每类的主要特点是什么?", 380 | "ICD-10": "Diseases of the digestive system" 381 | }, 382 | { 383 | "question": "寡核苷酸疗法在治疗基因性疾病中有哪些应用实例?这种疗法如何发挥作用?", 384 | "ICD-10": "Others" 385 | }, 386 | { 387 | "question": "请提供流行性脑脊髓膜炎的流行病学特征和预防方法。", 388 | "ICD-10": "Certain infectious and parasitic diseases" 389 | }, 390 | { 391 | "question": "什么是脑出血的常见原因,紧急处理步骤是什么?", 392 | "ICD-10": "Diseases of the circulatory system" 393 | }, 394 | { 395 | "question": "什么是呼吸衰竭,它的分类和急救处理措施是什么?", 396 | "ICD-10": "Diseases of the respiratory system" 397 | }, 398 | { 399 | "question": "在治疗急性淋巴细胞白血病(ALL)中,化疗和靶向疗法各自的优缺点是什么?", 400 | "ICD-10": "Neoplasms" 401 | }, 402 | { 403 | "question": "如何治疗和预防风湿性心脏病?", 404 | "ICD-10": "Diseases of the circulatory system" 405 | }, 406 | { 407 | "question": "讲解肝硬化的不同阶段及其各阶段的管理措施。", 408 | "ICD-10": "Diseases of the digestive system" 409 | }, 410 | { 411 | "question": "什么是结直肠息肉,它们的存在会引发哪些健康问题?", 412 | "ICD-10": "Diseases of the digestive system" 413 | }, 414 | { 415 | "question": "诺卡菌病的临床表现有哪些?它是如何被诊断和治疗的?", 416 | "ICD-10": "Certain infectious and parasitic diseases" 417 | }, 418 | { 419 | "question": "输卵管阻塞会导致哪些健康问题?有哪些检测方法?", 420 | "ICD-10": "Diseases of the genitourinary system" 421 | }, 422 | { 423 | "question": "眼科检查中的“裂隙灯检查”是什么,它的作用有哪些?", 424 | "ICD-10": "Diseases of the eye and adnexa" 425 | }, 426 | { 427 | "question": "外阴阴道念珠菌感染有何症状及治疗方法?应注意哪些预防措施?", 428 | "ICD-10": "Certain infectious and parasitic diseases" 429 | }, 430 | { 431 | "question": "简述神经母细胞瘤的病因和早期发现方法。", 432 | "ICD-10": "Neoplasms" 433 | }, 434 | { 435 | "question": "近视眼和远视眼有什么不同?请描述它们的成因及矫正方式。", 436 | "ICD-10": "Diseases of the eye and adnexa" 437 | }, 438 | { 439 | "question": "什么是急性支气管炎,它的病因、症状和治疗方法有哪些?", 440 | "ICD-10": "Diseases of the respiratory system" 441 | }, 442 | { 443 | "question": "胸膜间皮瘤的早期症状是什么?有哪些风险因素?", 444 | "ICD-10": "Neoplasms" 445 | }, 446 | { 447 | "question": "描述曲霉菌病的肺部表现及其治疗方法。", 448 | "ICD-10": "Certain infectious and parasitic diseases" 449 | }, 450 | { 451 | "question": "慢性胰腺炎患者需采取哪些生活方式改变来减轻病情?", 452 | "ICD-10": "Diseases of the digestive system" 453 | }, 454 | { 455 | "question": "阐述酒精使用障碍的危害以及其治疗方案。", 456 | "ICD-10": "Mental and behavioural disorders" 457 | }, 458 | { 459 | "question": "什么是肺淀粉样变性,哪些临床表现是常见的?", 460 | "ICD-10": "Diseases of the respiratory system" 461 | }, 462 | { 463 | "question": "鼻咽癌的早期症状是什么,如何确诊?", 464 | "ICD-10": "Neoplasms" 465 | }, 466 | { 467 | "question": "描述胃穿孔的形成机制、急性症状及其应急处理方法。", 468 | "ICD-10": "Diseases of the digestive system" 469 | }, 470 | { 471 | "question": "了解骨结核的传播途径、临床表现及治疗方案。", 472 | "ICD-10": "Certain infectious and parasitic diseases" 473 | }, 474 | { 475 | "question": "肠瘘是什么,讨论其管理和治疗策略。", 476 | "ICD-10": "Diseases of the digestive system" 477 | }, 478 | { 479 | "question": "耳石症(良性阵发性位置性眩晕)有哪些症状及诊断方法?", 480 | "ICD-10": "Diseases of the ear and mastoid process" 481 | }, 482 | { 483 | "question": "描述下疤痕修复的常见方法和作用机制。", 484 | "ICD-10": "Diseases of the skin and subcutaneous tissue" 485 | }, 486 | { 487 | "question": "请解释什么是过敏性支气管肺曲霉病。有哪些诊断方法?", 488 | "ICD-10": "Diseases of the respiratory system" 489 | }, 490 | { 491 | "question": "如何诊断和治疗外阴白斑病?", 492 | "ICD-10": "Diseases of the skin and subcutaneous tissue" 493 | }, 494 | { 495 | "question": "拇外翻的非手术治疗方法有哪些,这些方法的有效性如何评估?", 496 | "ICD-10": "Diseases of the musculoskeletal system and connective tissue" 497 | }, 498 | { 499 | "question": "铁尘肺病的主要成因是什么,如何预防和治疗?", 500 | "ICD-10": "Diseases of the respiratory system" 501 | }, 502 | { 503 | "question": "心理治疗有哪些主要流派,每种流派在治疗上的侧重方向是什么?", 504 | "ICD-10": "Mental and behavioural disorders" 505 | }, 506 | { 507 | "question": "针对乳腺增生,医生会做哪些检查?病因和治疗方法是什么?", 508 | "ICD-10": "Diseases of the genitourinary system" 509 | }, 510 | { 511 | "question": "描述大隐静脉曲张的手术治疗方法,以及术前患者需要注意什么。", 512 | "ICD-10": "Diseases of the circulatory system" 513 | }, 514 | { 515 | "question": "详述心包炎的病因、诊断方法及其可能的治疗途径。", 516 | "ICD-10": "Diseases of the circulatory system" 517 | }, 518 | { 519 | "question": "腰椎滑脱症是怎样一种病变,临床上如何处理?", 520 | "ICD-10": "Diseases of the musculoskeletal system and connective tissue" 521 | }, 522 | { 523 | "question": "妇科炎症的常见症状有哪些?如何进行诊断和治疗?", 524 | "ICD-10": "Diseases of the genitourinary system" 525 | }, 526 | { 527 | "question": "什么是扩张型心肌病?其病因和治疗方法是什么?", 528 | "ICD-10": "Diseases of the circulatory system" 529 | }, 530 | { 531 | "question": "沙眼的病因和流行情况是怎样的,应采用哪些防治策略?", 532 | "ICD-10": "Diseases of the eye and adnexa" 533 | }, 534 | { 535 | "question": "什么是急性坏死性脑病,哪类人群易患?", 536 | "ICD-10": "Diseases of the nervous system" 537 | }, 538 | { 539 | "question": "探讨细菌性腹膜炎的病因、症状及治疗策略。", 540 | "ICD-10": "Certain infectious and parasitic diseases" 541 | }, 542 | { 543 | "question": "骨软骨炎通常发生在什么样的人群中,这种疾病如何进行诊断?", 544 | "ICD-10": "Diseases of the musculoskeletal system and connective tissue" 545 | }, 546 | { 547 | "question": "请解释不孕症的主要原因以及可能的治疗方案。", 548 | "ICD-10": "Diseases of the genitourinary system" 549 | }, 550 | { 551 | "question": "描述躁郁症与单纯性抑郁症的主要区别。", 552 | "ICD-10": "Mental and behavioural disorders" 553 | }, 554 | { 555 | "question": "什么是食道裂孔疝?手术是唯一的治疗方法吗?", 556 | "ICD-10": "Diseases of the digestive system" 557 | }, 558 | { 559 | "question": "在怀孕初期,哪些症状表明可能存在宫外孕的风险?", 560 | "ICD-10": "Pregnancy, childbirth and the puerperium" 561 | }, 562 | { 563 | "question": "宫颈糜烂是如何形成的?其治疗方法有哪些?", 564 | "ICD-10": "Diseases of the genitourinary system" 565 | }, 566 | { 567 | "question": "什么是三叉神经痛,患者常常会有怎样的表现?", 568 | "ICD-10": "Diseases of the nervous system" 569 | }, 570 | { 571 | "question": "请描述子宫肌瘤的分类及其对女性健康的影响。", 572 | "ICD-10": "Neoplasms" 573 | }, 574 | { 575 | "question": "骨盆骨折后通常需要多久才能康复?康复过程中应注意哪些事项?", 576 | "ICD-10": "Injury, poisoning and certain other consequences of external causes" 577 | }, 578 | { 579 | "question": "描述间质性肺病的典型表现及其常见的病因。", 580 | "ICD-10": "Diseases of the respiratory system" 581 | }, 582 | { 583 | "question": "请描述一下椎间孔狭窄是什么?它的临床表现和治疗方法是什么?", 584 | "ICD-10": "Diseases of the musculoskeletal system and connective tissue" 585 | }, 586 | { 587 | "question": "视网膜中央动脉阻塞的急救和后续治疗措施是什么?", 588 | "ICD-10": "Diseases of the eye and adnexa" 589 | }, 590 | { 591 | "question": "白内障的手术治疗包括哪些步骤?有哪些常见的术后护理措施?", 592 | "ICD-10": "Diseases of the eye and adnexa" 593 | }, 594 | { 595 | "question": "什么是坠积性肺炎?描述其病理生理、临床表现和治疗措施。", 596 | "ICD-10": "Diseases of the respiratory system" 597 | }, 598 | { 599 | "question": "一位患者抱怨看到视野中有浮游物或闪光,这可能是哪些眼部疾病的症状?", 600 | "ICD-10": "Diseases of the eye and adnexa" 601 | }, 602 | { 603 | "question": "请解释什么是骨纤维结构不良及其主要表现。", 604 | "ICD-10": "Diseases of the musculoskeletal system and connective tissue" 605 | }, 606 | { 607 | "question": "什么是石骨症,它的原因、症状和治疗方法有哪些?", 608 | "ICD-10": "Diseases of the musculoskeletal system and connective tissue" 609 | }, 610 | { 611 | "question": "请解释腱鞘囊肿的发展原因和主要治疗方法。", 612 | "ICD-10": "Diseases of the musculoskeletal system and connective tissue" 613 | }, 614 | { 615 | "question": "探讨克雅二氏病的病因及其对大脑的影响。", 616 | "ICD-10": "Diseases of the nervous system" 617 | }, 618 | { 619 | "question": "如何诊断及治疗糖原累积病?", 620 | "ICD-10": "Endocrine, nutritional and metabolic diseases" 621 | }, 622 | { 623 | "question": "请解释一下坏死性筋膜炎的病理机制及治疗方案。", 624 | "ICD-10": "Certain infectious and parasitic diseases" 625 | }, 626 | { 627 | "question": "请介绍脊髓空洞症的诊断过程及其治疗选项。", 628 | "ICD-10": "Diseases of the nervous system" 629 | }, 630 | { 631 | "question": "你能解释一下何谓尺神经麻痹吗?这种疾病的常见诱因和治疗方案是什么?", 632 | "ICD-10": "Diseases of the nervous system" 633 | }, 634 | { 635 | "question": "阐述象皮腿症状以及其可能的并发症。", 636 | "ICD-10": "Certain infectious and parasitic diseases" 637 | }, 638 | { 639 | "question": "怀疑有心肌桥的患者应该做哪些检查?如何进行治疗?", 640 | "ICD-10": "Diseases of the circulatory system" 641 | }, 642 | { 643 | "question": "您能解释一下婴儿支气管肺发育不良的成因及主要治疗措施吗?", 644 | "ICD-10": "Certain conditions originating in the perinatal period" 645 | }, 646 | { 647 | "question": "请解释心脏横纹肌肉瘤的临床表现和治疗选项。", 648 | "ICD-10": "Neoplasms" 649 | }, 650 | { 651 | "question": "请解释一下汉坦病毒肺综合征的症状和传播途径。", 652 | "ICD-10": "Certain infectious and parasitic diseases" 653 | }, 654 | { 655 | "question": "阐述皮样囊肿是什么,它通常在人体的哪些部位发生?", 656 | "ICD-10": "Diseases of the skin and subcutaneous tissue" 657 | }, 658 | { 659 | "question": "眼睑炎通常由哪些因素引起,有哪些常见的治疗策略?", 660 | "ICD-10": "Diseases of the eye and adnexa" 661 | }, 662 | { 663 | "question": "什么是利什曼病,它是如何传播的?", 664 | "ICD-10": "Certain infectious and parasitic diseases" 665 | }, 666 | { 667 | "question": "解释土拉热的病原体、传播方式及常见临床症状。", 668 | "ICD-10": "Certain infectious and parasitic diseases" 669 | }, 670 | { 671 | "question": "缺乏镁对人体的影响是什么样的?哪些食物富含镁?", 672 | "ICD-10": "Endocrine, nutritional and metabolic diseases" 673 | }, 674 | { 675 | "question": "什么是脊髓性肌肉萎缩症(SMA),以及它的主要分类和症状是什么?", 676 | "ICD-10": "Diseases of the nervous system" 677 | }, 678 | { 679 | "question": "脾脏肿大常见的原因是什么,应该如何进行诊断和处理?", 680 | "ICD-10": "Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism" 681 | }, 682 | { 683 | "question": "妥瑞氏症在儿童中有哪些常见表现,当前有什么主要的治疗手段?", 684 | "ICD-10": "Mental and behavioural disorders" 685 | }, 686 | { 687 | "question": "阿狄森病是什么?患者常见表现及应对措施有哪些?", 688 | "ICD-10": "Endocrine, nutritional and metabolic diseases" 689 | }, 690 | { 691 | "question": "阐述毛囊角化症的病因、症状和治疗方法。", 692 | "ICD-10": "Diseases of the skin and subcutaneous tissue" 693 | }, 694 | { 695 | "question": "具述韦格纳肉芽肿的临床表现以及它的诊断标准。", 696 | "ICD-10": "Diseases of the respiratory system" 697 | }, 698 | { 699 | "question": "男性乳腺发育症是什么,这种情况需要手术治疗吗?", 700 | "ICD-10": "Endocrine, nutritional and metabolic diseases" 701 | }, 702 | { 703 | "question": "解释眼眶假瘤的病因和治疗手段。", 704 | "ICD-10": "Diseases of the eye and adnexa" 705 | }, 706 | { 707 | "question": "库鲁病主要通过什么途径传播?其主要表现是什么?", 708 | "ICD-10": "Certain infectious and parasitic diseases" 709 | }, 710 | { 711 | "question": "鼻出血(鼻衄)常见的诱因有哪些,怎样进行紧急处理?", 712 | "ICD-10": "Diseases of the respiratory system" 713 | }, 714 | { 715 | "question": "请解释何为天疱疮,其主要病理机制和常见治疗方案有哪些?", 716 | "ICD-10": "Diseases of the skin and subcutaneous tissue" 717 | }, 718 | { 719 | "question": "房间隔缺损会引发哪些并发症?", 720 | "ICD-10": "Congenital malformations, deformations and chromosomal abnormalities" 721 | }, 722 | { 723 | "question": "描述一下抗磷脂综合征的病理基础和诊断标准。", 724 | "ICD-10": "Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism" 725 | }, 726 | { 727 | "question": "请解释一下寒冷性荨麻疹的原因及其治疗方法。", 728 | "ICD-10": "Diseases of the skin and subcutaneous tissue" 729 | }, 730 | { 731 | "question": "舒尔曼病通常有哪些临床表现,治疗的方法有哪些?", 732 | "ICD-10": "Diseases of the skin and subcutaneous tissue" 733 | }, 734 | { 735 | "question": "探讨佩梅病的成因、临床特征和诊断策略。", 736 | "ICD-10": "Diseases of the musculoskeletal system and connective tissue" 737 | }, 738 | { 739 | "question": "罕见的骨骺发育异常症有哪些症状和治疗方案?", 740 | "ICD-10": "Congenital malformations, deformations and chromosomal abnormalities" 741 | }, 742 | { 743 | "question": "解释一下缩窄性心包炎的病理机制及其症状。", 744 | "ICD-10": "Diseases of the circulatory system" 745 | }, 746 | { 747 | "question": "何谓黑棘皮症?描述该病在不同患者中的常见表现形式。", 748 | "ICD-10": "Endocrine, nutritional and metabolic diseases" 749 | }, 750 | { 751 | "question": "拇囊炎有哪些临床表现?应如何进行诊断和处理?", 752 | "ICD-10": "Diseases of the skin and subcutaneous tissue" 753 | }, 754 | { 755 | "question": "糠秕孢子菌毛囊炎的诊断挑战和治疗方式有哪些?", 756 | "ICD-10": "Diseases of the skin and subcutaneous tissue" 757 | }, 758 | { 759 | "question": "讲述吸脂手术在塑形美容中的应用及其潜在风险。", 760 | "ICD-10": "Others" 761 | }, 762 | { 763 | "question": "上颌窦炎有哪些典型的临床表现?如何治疗?", 764 | "ICD-10": "Diseases of the respiratory system" 765 | }, 766 | { 767 | "question": "有哪些原因会导致马蹄足的形成?它的治疗方法有哪些?", 768 | "ICD-10": "Congenital malformations, deformations and chromosomal abnormalities" 769 | }, 770 | { 771 | "question": "伯-韦综合征的主要病因是什么,如何进行科学检查和诊断?", 772 | "ICD-10": "Diseases of the circulatory system" 773 | }, 774 | { 775 | "question": "对新生儿胎粪吸入综合征及其常见治疗措施进行详细说明。", 776 | "ICD-10": "Certain conditions originating in the perinatal period" 777 | }, 778 | { 779 | "question": "中毒性表皮坏死松解症是什么,它的发病机制有哪些?", 780 | "ICD-10": "Diseases of the skin and subcutaneous tissue" 781 | }, 782 | { 783 | "question": "针对低颅压综合征,临床医生应如何进行治疗和管理?", 784 | "ICD-10": "Diseases of the nervous system" 785 | }, 786 | { 787 | "question": "什么是干燥综合征,它的主要症状和治疗方式是什么?", 788 | "ICD-10": "Diseases of the musculoskeletal system and connective tissue" 789 | }, 790 | { 791 | "question": "解释何为膀胱炎,及其典型症状和预防措施。", 792 | "ICD-10": "Diseases of the genitourinary system" 793 | }, 794 | { 795 | "question": "请描述小儿噬血细胞综合征(HLH)的病理机制及常见的治疗方案。", 796 | "ICD-10": "Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism" 797 | }, 798 | { 799 | "question": "请解释什么是脂膜炎及其常见症状。", 800 | "ICD-10": "Diseases of the skin and subcutaneous tissue" 801 | } 802 | ] -------------------------------------------------------------------------------- /data/infer.json: -------------------------------------------------------------------------------- 1 | {"instruction": "小张最近感觉身体不适,出现心悸、气促等症状。体检发现心脏扩大、搏动减弱。", "input": "", "output": "小张可能患有心肌炎,建议进行心电图和心脏超声等检查来确定诊断。治疗方案包括使用泼尼松、生脉饮和三磷酸腺苷等药物,同时建议适当控制体温,保持良好的营养状况。"} 2 | {"instruction": "小王被确诊为肝炎双重感染,最可能的并发症是什么?", "input": "", "output": "最可能的并发症是肝癌和肝硬化。"} 3 | {"instruction": "麻风病和儿童哮喘的病因是否一致?", "input": "", "output": "不一致,麻风病的病因是麻风分枝杆菌,而儿童哮喘的病因是气候、药物、吸入过敏原等。"} 4 | {"instruction": "一位患者出现了持续性的胀痛和黄疸,经影像学检查后确诊为肝胆管结石病,应该采取哪些治疗方案?", "input": "", "output": "可以考虑采用残石的处理、微创保肝取石术或手术治疗等治疗方案。"} 5 | {"instruction": "一位35岁女性患者出现了关节疼痛和糖耐量异常,请问她可能患了什么疾病?", "input": "", "output": "该患者可能患上了慢性自身免疫性胰腺炎,伴有慢性风湿性关节炎和糖耐量异常的症状。建议到消化内科进行检查和治疗。"} -------------------------------------------------------------------------------- /data/knowledge_tuning_data_sample.txt: -------------------------------------------------------------------------------- 1 | input 2 | 多发性大动脉炎要去哪个科室就诊? 3 | 请问地尔硫的用法用量是怎么样的? 4 | 双氯芬酸钠贴片有哪些副作用? 5 | 当存在哪些症状时需怀疑瘢痕性幽门梗阻? 6 | 味禾的主要成分是什么? 7 | 传染性单核细胞增多症怎样才算治愈? 8 | 米非司酮的用途是什么? 9 | 利巴韦林可以用于哪些疾病? 10 | 梅毒是如何传染的? 11 | 奥拉西坦的用法用量是怎样的? 12 | 胃食管反流病有什么症状? 13 | 复方胆通胶囊有哪些主要的成分? 14 | 使用尿素维e乳膏时可能出现哪些不良反应? 15 | 风疹会传染吗?是怎么传染的? 16 | 抽动障碍还会出现什么症状? 17 | Gilbert综合征应该如何治疗? 18 | 更昔洛韦属于哪种药物分类? 19 | 肠息肉患者出现哪些症状需警惕? 20 | 三合钙咀嚼片的用途是什么? 21 | 21金维他属于哪个分类? 22 | 前列腺增生患者都有哪些症状? 23 | 尿崩症应该去哪个科室就诊? 24 | 哌拉西林他唑巴坦有哪些不良反应? 25 | 如何治疗流行性腮腺炎(痄腮)? 26 | 扑尔伪麻片主要成分是什么? 27 | 维铁缓释片属于哪个分类? 28 | 口角炎会传染吗? 29 | 得了腺垂体功能减退症会有哪些症状表现? 30 | 非霍奇金淋巴瘤的首发临床症状是什么? 31 | 氨酚那敏片维b1那敏片复合包装的剂量和使用时间是什么? 32 | 维拉帕米通常用于治疗哪些疾病? 33 | 优必达属于哪一类药品? 34 | 桂利嗪片的使用方法是什么?用量是多少? 35 | 阴茎癌一般去看哪个科? 36 | 如何治疗间质性肾炎? 37 | 成人口服坦洛新的用法用量是多少? 38 | 面肌痉挛有哪些治疗方法? 39 | 做了扁桃体和腺样体切割术之后会有后遗症吗? 40 | 高渗氯化钠羟乙基淀粉40注射液会引起哪些不良反应? 41 | 寻常疣会传染吗? 42 | 支原体感染有什么表现? 43 | 小儿膀胱结石患者常见的临床表现有哪些? 44 | 复方维生素u胶囊的用法用量是什么? 45 | 如何治疗宫颈妊娠? 46 | 温胃舒颗粒的用途是什么? 47 | 胎儿生长受限要去看哪个科? 48 | 茵栀黄颗粒的使用方法是什么? 49 | 复方苯佐卡因软膏的用法用量是什么? 50 | 手腕部肌腱血管神经手术治疗后,恢复一般需要多长时间? 51 | 丁细牙痛胶囊的用法用量是什么? 52 | 室性早搏怎么治疗? 53 | 念珠菌性龟头炎治疗后有没有后遗症? 54 | 铅中毒如何治疗? 55 | 尼可刹米有哪些不良反应? 56 | 咪唑斯汀的常见不良反应有哪些? 57 | 沙格雷酯有哪些不良反应? 58 | 冰毒、摇头丸急性中毒有哪些表现? 59 | 外阴炎有什么表现? 60 | 安谱诺欣有哪些注意事项? 61 | 三苯双脒主要用于哪些感染治疗? 62 | 眼睑痉挛有哪些表现? 63 | 盐酸氯丙那林片属于哪个分类? 64 | 地屈孕酮主要用途是什么? 65 | 低镁血症怎么治疗?要治疗多久? 66 | 风湿性关节炎怎么治疗?要治疗多久? 67 | 长期使用多力康应该注意哪些方面? 68 | 味禾属于哪一类药物? 69 | 为什么糜烂性胃炎的症状总是反复发生? 70 | 急性淋巴细胞性白血病有什么表现? 71 | 不同部位的脑出血症状都一样吗? 72 | 氨咖黄敏胶囊的注意事项有哪些? 73 | 硫酸长春碱有哪些用途? 74 | 老年性黄斑变性发生前有什么征兆? 75 | 如果怎么治疗? 76 | 吉法酯的使用方法和剂量是什么? 77 | 如何早期发现无症状性血尿或(和)蛋白尿? 78 | 血管瘤的药物治疗具体包括哪些?(以下问题只针对婴幼儿血管瘤,卡波西型血管内皮瘤另有治疗方法) 79 | 继发性三叉神经痛与原发性三叉神经痛的表现有什么不同? 80 | 复方氨酚烷胺片有哪些禁用人群? 81 | 脑萎缩怎么治疗? 82 | 酮洛酸可以用于哪些疾病的治疗? 83 | 人凝血因子Ⅶ用于治疗哪些疾病? 84 | 低镁血症一般去看哪个科? 85 | 滴虫性阴道炎会传染吗?是怎么传染的? 86 | 轻度急性荨麻疹有什么表现? 87 | 类风湿关节炎关节表现有哪些? 88 | 有头痛的病人该如何注意症状,更有利于医生诊断? 89 | 典型的自闭症主要有哪些表现? 90 | 扁平足要去看哪个科? 91 | 生殖器疱疹怎么治疗? 92 | 发育迟缓需要手术治疗吗? 93 | 胃癌有哪些常见的症状? 94 | 茵栀黄颗粒的注意事项是什么? 95 | 下肢静脉血栓哪些必须手术治疗? 96 | 盐酸舒托必利注射液有哪些不良反应? 97 | 使用可泰舒时,用法用量应该怎么样? 98 | 曲美布汀可能会引起哪些不良反应? 99 | 新生儿溶血应该怎么治疗? 100 | 得了肢端雀斑痣样黑素瘤会有些什么样的表现?指甲盖下出现黑点是怎么回事? 101 | 头孢丙烯对哪些疾病有治疗作用? 102 | 乳酸钙咀嚼片的使用方法是什么? 103 | 脊髓炎有哪些症状? 104 | 协达利可用于治疗哪些疾病? 105 | 多奈哌齐属于什么分类? 106 | 抑肽酶用于哪些病症?如何使用? 107 | 哪些人群不宜使用利巴韦林? 108 | 富马酸亚铁颗粒主要治疗哪些疾病? 109 | 肠息肉有哪些表现? 110 | 维生素d滴剂胶囊型属于哪一类药物? 111 | 胃下垂出现症状容易和哪些病混淆? 112 | 精原细胞瘤有什么表现? 113 | 脑卒中会留下哪些后遗症? 114 | 什么人群不适合使用桂利嗪片? 115 | 脑性瘫痪的症状有哪些? 116 | 荨麻疹有哪些症状? 117 | 如何避免在使用西曲溴铵后出现暂时性视线模糊? 118 | 念珠菌性龟头炎有哪些常见表现? 119 | 弘旭明属于哪一类药物? 120 | 跟骨骨折常见的症状、体征是什么? 121 | 滴虫性阴道炎一般去看哪个科? 122 | 哪些食管癌患者不能行手术治疗? 123 | 胆管结石有什么临床表现? 124 | 儿童单纯性肥胖的主要治疗方式都有哪些? 125 | 氨氯地平有哪些不良反应? 126 | 得了甲型病毒性肝炎会有些什么样的表现? 127 | 胃十二指肠溃疡穿孔应如何治疗? 128 | 轻度牙龈脓肿有什么表现? 129 | 肝癌会传染吗? 130 | 鼻中隔偏曲的症状有哪些? 131 | 海绵状血管瘤手术治疗后有没有后遗症? 132 | 抑郁症有哪些表现? 133 | 脾功能亢进应就诊于哪个科室? 134 | 梨状肌综合征怎么治疗? 135 | 稳定型心绞痛的典型症状是什么? 136 | 布洛芬特非那定伪麻黄碱属于哪个分类? 137 | 糖尿病性视网膜病变有什么症状? 138 | 慢性肾盂肾炎怎么治疗? 139 | 泰脂安胶囊的不良反应有哪些? 140 | 二尖瓣关闭不全如何治疗? 141 | 药物性痤疮怎么治疗? 142 | 扁平足有哪些常见表现? 143 | 水杨酸咪唑属于哪一类药物? 144 | 利拉萘酯乳膏属于哪些药物分类? 145 | 对乙酰氨基酚双氢可待因的不良反应多为哪些? 146 | 基多托是什么分类的药物? 147 | 骨髓增生异常综合征的首发症状是什么? 148 | 丙哌维林有哪些注意事项? 149 | 胸主动脉瘤有哪些常见表现? 150 | 奥洛他定的用法用量是什么? 151 | 甘精胰岛素的用法用量是什么? 152 | 白塞病如何治疗? 153 | 曲克芦丁氯化钠注射液的用法用量是什么? 154 | 黄连上清丸属于哪种药物分类? 155 | 乳酶生片适用于哪些人群?用法用量是怎么样的? 156 | 阿维a是哪些类型的疾病用药? 157 | 骨肉瘤的症状有哪些? 158 | 未足月的胎膜早破怎么治疗? 159 | 使用白云山会导致哪些不良反应? 160 | 新凝灵属于哪种药物分类? 161 | 水解蛋白注射液可否用于孕妇或哺乳期妇女? 162 | 肛裂患者应该去哪个科就诊? 163 | 乐佰欣需要饭前服用吗?有什么需要注意的事项吗? 164 | 人们在亚硝酸盐中毒时会有哪些表现? 165 | 阿苯达唑适用于哪些疾病?如何使用? 166 | 结肠炎怎么治疗? 167 | 丙酸氯倍他索软膏可以用于眼部吗? 168 | 怎么治疗腺样体肥大? 169 | 如何治疗大网膜炎? 170 | 登革热怎么治疗? 171 | 亨特氏综合征如何治疗? 172 | 硫酸长春碱属于哪一类药物? 173 | 血液滤过置换液属于哪些分类? 174 | 对乙酰氨基酚伪麻黄碱的不良反应有哪些? 175 | 典型面肌痉挛有什么表现? 176 | 氯雷他定伪麻黄碱属于哪个分类? 177 | 新生儿窒息有哪些表现? 178 | 布洛芬锌是什么药?有什么用途? 179 | 复方倍氯米松樟脑乳膏有哪些注意事项? 180 | 胆囊切除术后综合征一般去看哪个科? 181 | ANCA相关性血管炎患者一般去看哪个科? 182 | 血脂异常有哪些表现和危害? 183 | 支气管炎有哪些治疗方法? 184 | 倾倒综合征会有哪些表现? 185 | 上睑下垂如何治疗? 186 | 甘草酸二铵有哪些用途? 187 | 学习障碍如何治疗? 188 | 维生素ec颗粒可能引发哪些不良反应? 189 | 什么是环乙米特?有哪些不良反应? 190 | 苯丙酮尿症会有后遗症吗? 191 | 拉贝洛尔的用法用量是什么? 192 | 念珠菌性龟头炎手术治疗后多久能恢复? 193 | 小牛血去蛋白提取物在治疗哪些疾病上有明显效果? 194 | 治疗什么疾病使用复方柳安咖注射液时常见哪些不良反应? 195 | 急性呼吸窘迫综合征治好后会有后遗症吗? 196 | 普罗帕酮属于哪一类药物? 197 | 产褥感染怎么治疗? 198 | 脑震荡后遗症病需要住院吗? 199 | 盐酸羟苄唑滴眼液属于哪一类药物? 200 | 可否使用醋酸氢化可的松眼膏对治疗青光眼? 201 | 醋酸曲安奈德硫酸新霉素麝香草酚属于哪个分类? 202 | -------------------------------------------------------------------------------- /doc/Tuning_Methods_for_LLMs_towards_Health_Intelligence.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SCIR-HI/Huatuo-Llama-Med-Chinese/13ab5ff8c351404c47e35c1a5e43f76c28c940a1/doc/Tuning_Methods_for_LLMs_towards_Health_Intelligence.pdf -------------------------------------------------------------------------------- /export_hf_checkpoint.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import torch 4 | import transformers 5 | from peft import PeftModel 6 | from transformers import LlamaForCausalLM, LlamaTokenizer # noqa: F402 7 | 8 | BASE_MODEL = os.environ.get("BASE_MODEL", None) 9 | assert ( 10 | BASE_MODEL 11 | ), "Please specify a value for BASE_MODEL environment variable, e.g. `export BASE_MODEL=decapoda-research/llama-7b-hf`" # noqa: E501 12 | 13 | tokenizer = LlamaTokenizer.from_pretrained(BASE_MODEL) 14 | 15 | base_model = LlamaForCausalLM.from_pretrained( 16 | BASE_MODEL, 17 | load_in_8bit=False, 18 | torch_dtype=torch.float16, 19 | device_map={"": "cpu"}, 20 | ) 21 | 22 | first_weight = base_model.model.layers[0].self_attn.q_proj.weight 23 | first_weight_old = first_weight.clone() 24 | 25 | lora_model = PeftModel.from_pretrained( 26 | base_model, 27 | "tloen/alpaca-lora-7b", 28 | device_map={"": "cpu"}, 29 | torch_dtype=torch.float16, 30 | ) 31 | 32 | lora_weight = lora_model.base_model.model.model.layers[ 33 | 0 34 | ].self_attn.q_proj.weight 35 | 36 | assert torch.allclose(first_weight_old, first_weight) 37 | 38 | # merge weights 39 | for layer in lora_model.base_model.model.model.layers: 40 | layer.self_attn.q_proj.merge_weights = True 41 | layer.self_attn.v_proj.merge_weights = True 42 | 43 | lora_model.train(False) 44 | 45 | # did we do anything? 46 | assert not torch.allclose(first_weight_old, first_weight) 47 | 48 | lora_model_sd = lora_model.state_dict() 49 | deloreanized_sd = { 50 | k.replace("base_model.model.", ""): v 51 | for k, v in lora_model_sd.items() 52 | if "lora" not in k 53 | } 54 | 55 | LlamaForCausalLM.save_pretrained( 56 | base_model, "./hf_ckpt", state_dict=deloreanized_sd, max_shard_size="400MB" 57 | ) 58 | -------------------------------------------------------------------------------- /export_state_dict_checkpoint.py: -------------------------------------------------------------------------------- 1 | import json 2 | import os 3 | 4 | import torch 5 | import transformers 6 | from peft import PeftModel 7 | from transformers import LlamaForCausalLM, LlamaTokenizer # noqa: E402 8 | 9 | BASE_MODEL = os.environ.get("BASE_MODEL", None) 10 | assert ( 11 | BASE_MODEL 12 | ), "Please specify a value for BASE_MODEL environment variable, e.g. `export BASE_MODEL=decapoda-research/llama-7b-hf`" # noqa: E501 13 | 14 | tokenizer = LlamaTokenizer.from_pretrained(BASE_MODEL) 15 | 16 | base_model = LlamaForCausalLM.from_pretrained( 17 | BASE_MODEL, 18 | load_in_8bit=False, 19 | torch_dtype=torch.float16, 20 | device_map={"": "cpu"}, 21 | ) 22 | 23 | lora_model = PeftModel.from_pretrained( 24 | base_model, 25 | "tloen/alpaca-lora-7b", 26 | device_map={"": "cpu"}, 27 | torch_dtype=torch.float16, 28 | ) 29 | 30 | # merge weights 31 | for layer in lora_model.base_model.model.model.layers: 32 | layer.self_attn.q_proj.merge_weights = True 33 | layer.self_attn.v_proj.merge_weights = True 34 | 35 | lora_model.train(False) 36 | 37 | lora_model_sd = lora_model.state_dict() 38 | 39 | params = { 40 | "dim": 4096, 41 | "multiple_of": 256, 42 | "n_heads": 32, 43 | "n_layers": 32, 44 | "norm_eps": 1e-06, 45 | "vocab_size": -1, 46 | } 47 | n_layers = params["n_layers"] 48 | n_heads = params["n_heads"] 49 | dim = params["dim"] 50 | dims_per_head = dim // n_heads 51 | base = 10000.0 52 | inv_freq = 1.0 / ( 53 | base ** (torch.arange(0, dims_per_head, 2).float() / dims_per_head) 54 | ) 55 | 56 | 57 | def permute(w): 58 | return ( 59 | w.view(n_heads, dim // n_heads // 2, 2, dim) 60 | .transpose(1, 2) 61 | .reshape(dim, dim) 62 | ) 63 | 64 | 65 | def unpermute(w): 66 | return ( 67 | w.view(n_heads, 2, dim // n_heads // 2, dim) 68 | .transpose(1, 2) 69 | .reshape(dim, dim) 70 | ) 71 | 72 | 73 | def translate_state_dict_key(k): # noqa: C901 74 | k = k.replace("base_model.model.", "") 75 | if k == "model.embed_tokens.weight": 76 | return "tok_embeddings.weight" 77 | elif k == "model.norm.weight": 78 | return "norm.weight" 79 | elif k == "lm_head.weight": 80 | return "output.weight" 81 | elif k.startswith("model.layers."): 82 | layer = k.split(".")[2] 83 | if k.endswith(".self_attn.q_proj.weight"): 84 | return f"layers.{layer}.attention.wq.weight" 85 | elif k.endswith(".self_attn.k_proj.weight"): 86 | return f"layers.{layer}.attention.wk.weight" 87 | elif k.endswith(".self_attn.v_proj.weight"): 88 | return f"layers.{layer}.attention.wv.weight" 89 | elif k.endswith(".self_attn.o_proj.weight"): 90 | return f"layers.{layer}.attention.wo.weight" 91 | elif k.endswith(".mlp.gate_proj.weight"): 92 | return f"layers.{layer}.feed_forward.w1.weight" 93 | elif k.endswith(".mlp.down_proj.weight"): 94 | return f"layers.{layer}.feed_forward.w2.weight" 95 | elif k.endswith(".mlp.up_proj.weight"): 96 | return f"layers.{layer}.feed_forward.w3.weight" 97 | elif k.endswith(".input_layernorm.weight"): 98 | return f"layers.{layer}.attention_norm.weight" 99 | elif k.endswith(".post_attention_layernorm.weight"): 100 | return f"layers.{layer}.ffn_norm.weight" 101 | elif k.endswith("rotary_emb.inv_freq") or "lora" in k: 102 | return None 103 | else: 104 | print(layer, k) 105 | raise NotImplementedError 106 | else: 107 | print(k) 108 | raise NotImplementedError 109 | 110 | 111 | new_state_dict = {} 112 | for k, v in lora_model_sd.items(): 113 | new_k = translate_state_dict_key(k) 114 | if new_k is not None: 115 | if "wq" in new_k or "wk" in new_k: 116 | new_state_dict[new_k] = unpermute(v) 117 | else: 118 | new_state_dict[new_k] = v 119 | 120 | os.makedirs("./ckpt", exist_ok=True) 121 | 122 | torch.save(new_state_dict, "./ckpt/consolidated.00.pth") 123 | 124 | with open("./ckpt/params.json", "w") as f: 125 | json.dump(params, f) 126 | -------------------------------------------------------------------------------- /finetune.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | from typing import List 4 | 5 | import fire 6 | import wandb 7 | import torch 8 | import transformers 9 | from datasets import load_dataset 10 | 11 | """ 12 | Unused imports: 13 | import torch.nn as nn 14 | import bitsandbytes as bnb 15 | """ 16 | 17 | from peft import ( 18 | LoraConfig, 19 | get_peft_model, 20 | get_peft_model_state_dict, 21 | prepare_model_for_int8_training, 22 | set_peft_model_state_dict, 23 | ) 24 | from transformers import AutoModelForCausalLM, AutoTokenizer 25 | 26 | from utils.prompter import Prompter 27 | 28 | from transformers import Seq2SeqTrainer, TrainerCallback, TrainingArguments, TrainerState, TrainerControl 29 | from transformers.trainer_utils import PREFIX_CHECKPOINT_DIR 30 | 31 | 32 | def train( 33 | # model/data params 34 | base_model: str = "", # the only required argument 35 | data_path: str = "yahma/alpaca-cleaned", 36 | output_dir: str = "./lora-alpaca", 37 | # training hyperparams 38 | batch_size: int = 128, 39 | micro_batch_size: int = 8, 40 | num_epochs: int = 10, 41 | learning_rate: float = 3e-4, 42 | cutoff_len: int = 256, 43 | val_set_size: int = 500, 44 | # lora hyperparams 45 | lora_r: int = 8, 46 | lora_alpha: int = 16, 47 | lora_dropout: float = 0.05, 48 | lora_target_modules: List[str] = [ 49 | "q_proj", 50 | "v_proj", 51 | ], 52 | # llm hyperparams 53 | train_on_inputs: bool = False, # if False, masks out inputs in loss 54 | group_by_length: bool = False, # faster, but produces an odd training loss curve 55 | # wandb params 56 | wandb_project: str = "llama_med", 57 | wandb_run_name: str = "", 58 | wandb_watch: str = "", # options: false | gradients | all 59 | wandb_log_model: str = "", # options: false | true 60 | resume_from_checkpoint: str = None, # either training checkpoint or final adapter 61 | prompt_template_name: str = "alpaca", # The prompt template to use, will default to alpaca. 62 | ): 63 | if int(os.environ.get("LOCAL_RANK", 0)) == 0: 64 | print( 65 | f"Training Alpaca-LoRA model with params:\n" 66 | f"base_model: {base_model}\n" 67 | f"data_path: {data_path}\n" 68 | f"output_dir: {output_dir}\n" 69 | f"batch_size: {batch_size}\n" 70 | f"micro_batch_size: {micro_batch_size}\n" 71 | f"num_epochs: {num_epochs}\n" 72 | f"learning_rate: {learning_rate}\n" 73 | f"cutoff_len: {cutoff_len}\n" 74 | f"val_set_size: {val_set_size}\n" 75 | f"lora_r: {lora_r}\n" 76 | f"lora_alpha: {lora_alpha}\n" 77 | f"lora_dropout: {lora_dropout}\n" 78 | f"lora_target_modules: {lora_target_modules}\n" 79 | f"train_on_inputs: {train_on_inputs}\n" 80 | f"group_by_length: {group_by_length}\n" 81 | f"wandb_project: {wandb_project}\n" 82 | f"wandb_run_name: {wandb_run_name}\n" 83 | f"wandb_watch: {wandb_watch}\n" 84 | f"wandb_log_model: {wandb_log_model}\n" 85 | f"resume_from_checkpoint: {resume_from_checkpoint or False}\n" 86 | f"prompt template: {prompt_template_name}\n" 87 | ) 88 | assert ( 89 | base_model 90 | ), "Please specify a --base_model, e.g. --base_model='decapoda-research/llama-7b-hf'" 91 | gradient_accumulation_steps = batch_size // micro_batch_size 92 | 93 | prompter = Prompter(prompt_template_name) 94 | 95 | device_map = "auto" 96 | world_size = int(os.environ.get("WORLD_SIZE", 1)) 97 | ddp = world_size != 1 98 | if ddp: 99 | device_map = {"": int(os.environ.get("LOCAL_RANK") or 0)} 100 | gradient_accumulation_steps = gradient_accumulation_steps // world_size 101 | 102 | # Check if parameter passed or if set within environ 103 | use_wandb = len(wandb_project) > 0 or ( 104 | "WANDB_PROJECT" in os.environ and len(os.environ["WANDB_PROJECT"]) > 0 105 | ) 106 | # Only overwrite environ if wandb param passed 107 | if len(wandb_project) > 0: 108 | os.environ["WANDB_PROJECT"] = wandb_project 109 | if len(wandb_watch) > 0: 110 | os.environ["WANDB_WATCH"] = wandb_watch 111 | if len(wandb_log_model) > 0: 112 | os.environ["WANDB_LOG_MODEL"] = wandb_log_model 113 | 114 | model = AutoModelForCausalLM.from_pretrained( 115 | base_model, 116 | load_in_8bit=True, 117 | torch_dtype=torch.float16, 118 | device_map=device_map, 119 | ) 120 | 121 | tokenizer = AutoTokenizer.from_pretrained(base_model) 122 | 123 | tokenizer.pad_token_id = ( 124 | 0 # unk. we want this to be different from the eos token 125 | ) 126 | tokenizer.padding_side = "left" # Allow batched inference 127 | 128 | def tokenize(prompt, add_eos_token=True): 129 | # there's probably a way to do this with the tokenizer settings 130 | # but again, gotta move fast 131 | result = tokenizer( 132 | prompt, 133 | truncation=True, 134 | max_length=cutoff_len, 135 | padding=False, 136 | return_tensors=None, 137 | ) 138 | if ( 139 | result["input_ids"][-1] != tokenizer.eos_token_id 140 | and len(result["input_ids"]) < cutoff_len 141 | and add_eos_token 142 | ): 143 | result["input_ids"].append(tokenizer.eos_token_id) 144 | result["attention_mask"].append(1) 145 | 146 | result["labels"] = result["input_ids"].copy() 147 | 148 | return result 149 | 150 | def generate_and_tokenize_prompt(data_point): 151 | full_prompt = prompter.generate_prompt( 152 | data_point["instruction"], 153 | data_point["input"], 154 | data_point["output"], 155 | ) 156 | tokenized_full_prompt = tokenize(full_prompt) 157 | if not train_on_inputs: 158 | user_prompt = prompter.generate_prompt( 159 | data_point["instruction"], data_point["input"] 160 | ) 161 | tokenized_user_prompt = tokenize(user_prompt, add_eos_token=False) 162 | user_prompt_len = len(tokenized_user_prompt["input_ids"]) 163 | 164 | tokenized_full_prompt["labels"] = [ 165 | -100 166 | ] * user_prompt_len + tokenized_full_prompt["labels"][ 167 | user_prompt_len: 168 | ] # could be sped up, probably 169 | return tokenized_full_prompt 170 | 171 | model = prepare_model_for_int8_training(model) 172 | 173 | config = LoraConfig( 174 | r=lora_r, 175 | lora_alpha=lora_alpha, 176 | target_modules=lora_target_modules, 177 | lora_dropout=lora_dropout, 178 | bias="none", 179 | task_type="CAUSAL_LM", 180 | ) 181 | model = get_peft_model(model, config) 182 | 183 | if data_path.endswith(".json") or data_path.endswith(".jsonl"): 184 | data = load_dataset("json", data_files=data_path) 185 | else: 186 | data = load_dataset(data_path) 187 | 188 | if resume_from_checkpoint: 189 | # Check the available weights and load them 190 | checkpoint_name = os.path.join( 191 | resume_from_checkpoint, "pytorch_model.bin" 192 | ) # Full checkpoint 193 | if not os.path.exists(checkpoint_name): 194 | checkpoint_name = os.path.join( 195 | resume_from_checkpoint, "adapter_model.bin" 196 | ) # only LoRA model - LoRA config above has to fit 197 | resume_from_checkpoint = ( 198 | False # So the trainer won't try loading its state 199 | ) 200 | # The two files above have a different name depending on how they were saved, but are actually the same. 201 | if os.path.exists(checkpoint_name): 202 | print(f"Restarting from {checkpoint_name}") 203 | adapters_weights = torch.load(checkpoint_name) 204 | set_peft_model_state_dict(model, adapters_weights) 205 | else: 206 | print(f"Checkpoint {checkpoint_name} not found") 207 | 208 | model.print_trainable_parameters() # Be more transparent about the % of trainable params. 209 | 210 | if val_set_size > 0: 211 | train_val = data["train"].train_test_split( 212 | test_size=val_set_size, shuffle=True, seed=2023 213 | ) 214 | train_data = ( 215 | train_val["train"].shuffle().map(generate_and_tokenize_prompt) 216 | ) 217 | val_data = ( 218 | train_val["test"].shuffle().map(generate_and_tokenize_prompt) 219 | ) 220 | else: 221 | train_data = data["train"].shuffle().map(generate_and_tokenize_prompt) 222 | val_data = None 223 | 224 | if not ddp and torch.cuda.device_count() > 1: 225 | # keeps Trainer from trying its own DataParallelism when more than 1 gpu is available 226 | model.is_parallelizable = True 227 | model.model_parallel = True 228 | 229 | class SavePeftModelCallback(TrainerCallback): 230 | def on_save( 231 | self, 232 | args: TrainingArguments, 233 | state: TrainerState, 234 | control: TrainerControl, 235 | **kwargs, 236 | ): 237 | checkpoint_folder = os.path.join(args.output_dir, f"{PREFIX_CHECKPOINT_DIR}-{state.global_step}") 238 | kwargs["model"].save_pretrained(checkpoint_folder) 239 | pytorch_model_path = os.path.join(checkpoint_folder, "pytorch_model.bin") 240 | if os.path.exists(pytorch_model_path): 241 | os.remove(pytorch_model_path) 242 | return control 243 | 244 | trainer = transformers.Trainer( 245 | model=model, 246 | train_dataset=train_data, 247 | eval_dataset=val_data, 248 | args=transformers.TrainingArguments( 249 | per_device_train_batch_size=micro_batch_size, 250 | gradient_accumulation_steps=gradient_accumulation_steps, 251 | warmup_ratio=0.1, 252 | num_train_epochs=num_epochs, 253 | learning_rate=learning_rate, 254 | fp16=True, 255 | logging_steps=8, 256 | optim="adamw_torch", 257 | evaluation_strategy="steps" if val_set_size > 0 else "no", 258 | save_strategy="steps", 259 | eval_steps=32 if val_set_size > 0 else None, 260 | save_steps=32, 261 | output_dir=output_dir, 262 | save_total_limit=5, 263 | load_best_model_at_end=True if val_set_size > 0 else False, 264 | ddp_find_unused_parameters=False if ddp else None, 265 | group_by_length=group_by_length, 266 | report_to="wandb" if use_wandb else None, 267 | run_name=wandb_run_name if use_wandb else None, 268 | ), 269 | data_collator=transformers.DataCollatorForSeq2Seq( 270 | tokenizer, pad_to_multiple_of=8, return_tensors="pt", padding=True 271 | ), 272 | callbacks=[SavePeftModelCallback], 273 | ) 274 | model.config.use_cache = False 275 | 276 | if torch.__version__ >= "2" and sys.platform != "win32": 277 | model = torch.compile(model) 278 | 279 | trainer.train(resume_from_checkpoint=resume_from_checkpoint) 280 | 281 | model.save_pretrained(output_dir) 282 | 283 | print( 284 | "\n If there's a warning about missing keys above, please disregard :)" 285 | ) 286 | 287 | 288 | if __name__ == "__main__": 289 | fire.Fire(train) 290 | -------------------------------------------------------------------------------- /generate.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | import fire 4 | import gradio as gr 5 | import torch 6 | import transformers 7 | from peft import PeftModel 8 | from transformers import GenerationConfig, AutoModelForCausalLM, AutoTokenizer 9 | 10 | from utils.prompter import Prompter 11 | 12 | if torch.cuda.is_available(): 13 | device = "cuda" 14 | else: 15 | device = "cpu" 16 | 17 | try: 18 | if torch.backends.mps.is_available(): 19 | device = "mps" 20 | except: # noqa: E722 21 | pass 22 | 23 | 24 | def main( 25 | load_8bit: bool = False, 26 | base_model: str = "", 27 | lora_weights: str = "tloen/alpaca-lora-7b", 28 | prompt_template: str = "med_template", # The prompt template to use, will default to alpaca. 29 | server_name: str = "0.0.0.0", # Allows to listen on all interfaces by providing '0.0.0.0' 30 | share_gradio: bool = True, 31 | ): 32 | assert ( 33 | base_model 34 | ), "Please specify a --base_model, e.g. --base_model='decapoda-research/llama-7b-hf'" 35 | 36 | prompter = Prompter(prompt_template) 37 | tokenizer = AutoTokenizer.from_pretrained(base_model) 38 | if device == "cuda": 39 | model = AutoModelForCausalLM.from_pretrained( 40 | base_model, 41 | load_in_8bit=load_8bit, 42 | torch_dtype=torch.float16, 43 | device_map="auto", 44 | ) 45 | model = PeftModel.from_pretrained( 46 | model, 47 | lora_weights, 48 | torch_dtype=torch.float16, 49 | ) 50 | elif device == "mps": 51 | model = AutoModelForCausalLM.from_pretrained( 52 | base_model, 53 | device_map={"": device}, 54 | torch_dtype=torch.float16, 55 | ) 56 | model = PeftModel.from_pretrained( 57 | model, 58 | lora_weights, 59 | device_map={"": device}, 60 | torch_dtype=torch.float16, 61 | ) 62 | else: 63 | model = AutoModelForCausalLM.from_pretrained( 64 | base_model, device_map={"": device}, low_cpu_mem_usage=True 65 | ) 66 | model = PeftModel.from_pretrained( 67 | model, 68 | lora_weights, 69 | device_map={"": device}, 70 | ) 71 | 72 | # unwind broken decapoda-research config 73 | model.config.pad_token_id = tokenizer.pad_token_id = 0 # unk 74 | model.config.bos_token_id = 1 75 | model.config.eos_token_id = 2 76 | 77 | if not load_8bit: 78 | model.half() # seems to fix bugs for some users. 79 | 80 | model.eval() 81 | if torch.__version__ >= "2" and sys.platform != "win32": 82 | model = torch.compile(model) 83 | 84 | def evaluate( 85 | instruction, 86 | input=None, 87 | temperature=0.1, 88 | top_p=0.75, 89 | top_k=40, 90 | num_beams=4, 91 | max_new_tokens=128, 92 | **kwargs, 93 | ): 94 | prompt = prompter.generate_prompt(instruction, input) 95 | inputs = tokenizer(prompt, return_tensors="pt") 96 | input_ids = inputs["input_ids"].to(device) 97 | generation_config = GenerationConfig( 98 | temperature=temperature, 99 | top_p=top_p, 100 | top_k=top_k, 101 | num_beams=num_beams, 102 | **kwargs, 103 | ) 104 | with torch.no_grad(): 105 | generation_output = model.generate( 106 | input_ids=input_ids, 107 | generation_config=generation_config, 108 | return_dict_in_generate=True, 109 | output_scores=True, 110 | max_new_tokens=max_new_tokens, 111 | ) 112 | s = generation_output.sequences[0] 113 | output = tokenizer.decode(s) 114 | return prompter.get_response(output) 115 | 116 | gr.Interface( 117 | fn=evaluate, 118 | inputs=[ 119 | gr.components.Textbox( 120 | lines=2, 121 | label="Instruction", 122 | placeholder="Tell me about alpacas.", 123 | ), 124 | gr.components.Textbox(lines=2, label="Input", placeholder="none"), 125 | gr.components.Slider( 126 | minimum=0, maximum=1, value=0.1, label="Temperature" 127 | ), 128 | gr.components.Slider( 129 | minimum=0, maximum=1, value=0.75, label="Top p" 130 | ), 131 | gr.components.Slider( 132 | minimum=0, maximum=100, step=1, value=40, label="Top k" 133 | ), 134 | gr.components.Slider( 135 | minimum=1, maximum=4, step=1, value=4, label="Beams" 136 | ), 137 | gr.components.Slider( 138 | minimum=1, maximum=2000, step=1, value=128, label="Max tokens" 139 | ), 140 | ], 141 | outputs=[ 142 | gr.inputs.Textbox( 143 | lines=5, 144 | label="Output", 145 | ) 146 | ], 147 | title="BenTsao", 148 | description="", # noqa: E501 149 | ).launch(server_name=server_name, share=share_gradio) 150 | # Old testing code follows. 151 | 152 | """ 153 | # testing code for readme 154 | for instruction in [ 155 | "Tell me about alpacas.", 156 | "Tell me about the president of Mexico in 2019.", 157 | "Tell me about the king of France in 2019.", 158 | "List all Canadian provinces in alphabetical order.", 159 | "Write a Python program that prints the first 10 Fibonacci numbers.", 160 | "Write a program that prints the numbers from 1 to 100. But for multiples of three print 'Fizz' instead of the number and for the multiples of five print 'Buzz'. For numbers which are multiples of both three and five print 'FizzBuzz'.", # noqa: E501 161 | "Tell me five words that rhyme with 'shock'.", 162 | "Translate the sentence 'I have no mouth but I must scream' into Spanish.", 163 | "Count up from 1 to 500.", 164 | ]: 165 | print("Instruction:", instruction) 166 | print("Response:", evaluate(instruction)) 167 | print() 168 | """ 169 | 170 | 171 | if __name__ == "__main__": 172 | fire.Fire(main) 173 | -------------------------------------------------------------------------------- /infer.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import json 3 | import fire 4 | import gradio as gr 5 | import torch 6 | import transformers 7 | from peft import PeftModel 8 | from transformers import GenerationConfig, AutoModelForCausalLM, AutoTokenizer 9 | 10 | from utils.prompter import Prompter 11 | 12 | if torch.cuda.is_available(): 13 | device = "cuda" 14 | 15 | def load_instruction(instruct_dir): 16 | input_data = [] 17 | with open(instruct_dir, "r") as f: 18 | lines = f.readlines() 19 | for line in lines: 20 | line = line.strip() 21 | d = json.loads(line) 22 | input_data.append(d) 23 | return input_data 24 | 25 | 26 | def main( 27 | load_8bit: bool = False, 28 | base_model: str = "", 29 | # the infer data, if not exists, infer the default instructions in code 30 | instruct_dir: str = "", 31 | use_lora: bool = True, 32 | lora_weights: str = "tloen/alpaca-lora-7b", 33 | # The prompt template to use, will default to med_template. 34 | prompt_template: str = "med_template", 35 | ): 36 | prompter = Prompter(prompt_template) 37 | tokenizer = AutoTokenizer.from_pretrained(base_model) 38 | model = AutoModelForCausalLM.from_pretrained( 39 | base_model, 40 | load_in_8bit=load_8bit, 41 | torch_dtype=torch.float16, 42 | device_map="auto", 43 | ) 44 | if use_lora: 45 | print(f"using lora {lora_weights}") 46 | model = PeftModel.from_pretrained( 47 | model, 48 | lora_weights, 49 | torch_dtype=torch.float16, 50 | ) 51 | # unwind broken decapoda-research config 52 | model.config.pad_token_id = tokenizer.pad_token_id = 0 # unk 53 | model.config.bos_token_id = 1 54 | model.config.eos_token_id = 2 55 | if not load_8bit: 56 | model.half() # seems to fix bugs for some users. 57 | 58 | model.eval() 59 | 60 | if torch.__version__ >= "2" and sys.platform != "win32": 61 | model = torch.compile(model) 62 | 63 | def evaluate( 64 | instruction, 65 | input=None, 66 | temperature=0.1, 67 | top_p=0.75, 68 | top_k=40, 69 | num_beams=4, 70 | max_new_tokens=256, 71 | **kwargs, 72 | ): 73 | prompt = prompter.generate_prompt(instruction, input) 74 | inputs = tokenizer(prompt, return_tensors="pt") 75 | input_ids = inputs["input_ids"].to(device) 76 | generation_config = GenerationConfig( 77 | temperature=temperature, 78 | top_p=top_p, 79 | top_k=top_k, 80 | num_beams=num_beams, 81 | **kwargs, 82 | ) 83 | with torch.no_grad(): 84 | generation_output = model.generate( 85 | input_ids=input_ids, 86 | generation_config=generation_config, 87 | return_dict_in_generate=True, 88 | output_scores=True, 89 | max_new_tokens=max_new_tokens, 90 | ) 91 | s = generation_output.sequences[0] 92 | output = tokenizer.decode(s) 93 | return prompter.get_response(output) 94 | 95 | def infer_from_json(instruct_dir): 96 | input_data = load_instruction(instruct_dir) 97 | for d in input_data: 98 | instruction = d["instruction"] 99 | output = d["output"] 100 | print("###infering###") 101 | model_output = evaluate(instruction) 102 | print("###instruction###") 103 | print(instruction) 104 | print("###golden output###") 105 | print(output) 106 | print("###model output###") 107 | print(model_output) 108 | 109 | if instruct_dir != "": 110 | infer_from_json(instruct_dir) 111 | else: 112 | for instruction in [ 113 | "我感冒了,怎么治疗", 114 | "一个患有肝衰竭综合征的病人,除了常见的临床表现外,还有哪些特殊的体征?", 115 | "急性阑尾炎和缺血性心脏病的多发群体有何不同?", 116 | "小李最近出现了心动过速的症状,伴有轻度胸痛。体检发现P-R间期延长,伴有T波低平和ST段异常", 117 | ]: 118 | print("Instruction:", instruction) 119 | print("Response:", evaluate(instruction)) 120 | print() 121 | 122 | 123 | if __name__ == "__main__": 124 | fire.Fire(main) 125 | -------------------------------------------------------------------------------- /infer_literature.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import json 3 | 4 | import fire 5 | import gradio as gr 6 | import torch 7 | import transformers 8 | from peft import PeftModel 9 | from transformers import GenerationConfig, AutoModelForCausalLM, AutoTokenizer 10 | 11 | from utils.prompter import Prompter 12 | 13 | if torch.cuda.is_available(): 14 | device = "cuda" 15 | 16 | def load_instruction(instruct_dir): 17 | input_data = [] 18 | with open(instruct_dir, "r") as f: 19 | lines = f.readlines() 20 | for line in lines: 21 | line = line.strip() 22 | d = json.loads(line) 23 | input_data.append(d) 24 | return input_data 25 | 26 | 27 | def main( 28 | load_8bit: bool = False, 29 | base_model: str = "", 30 | # the infer data, if not exists, infer the default instructions in code 31 | single_or_multi: str = "", 32 | use_lora: bool = True, 33 | lora_weights: str = "tloen/alpaca-lora-7b", 34 | # The prompt template to use, will default to med_template. 35 | prompt_template: str = "med_template", 36 | ): 37 | prompter = Prompter(prompt_template) 38 | tokenizer = AutoTokenizer.from_pretrained(base_model) 39 | model = AutoModelForCausalLM.from_pretrained( 40 | base_model, 41 | load_in_8bit=load_8bit, 42 | torch_dtype=torch.float16, 43 | device_map="auto", 44 | ) 45 | if use_lora: 46 | print(f"using lora {lora_weights}") 47 | model = PeftModel.from_pretrained( 48 | model, 49 | lora_weights, 50 | torch_dtype=torch.float16, 51 | ) 52 | # unwind broken decapoda-research config 53 | model.config.pad_token_id = tokenizer.pad_token_id = 0 # unk 54 | model.config.bos_token_id = 1 55 | model.config.eos_token_id = 2 56 | if not load_8bit: 57 | model.half() # seems to fix bugs for some users. 58 | 59 | model.eval() 60 | 61 | if torch.__version__ >= "2" and sys.platform != "win32": 62 | model = torch.compile(model) 63 | 64 | def evaluate( 65 | instruction, 66 | input=None, 67 | temperature=0.1, 68 | top_p=0.75, 69 | top_k=40, 70 | num_beams=4, 71 | max_new_tokens=256, 72 | **kwargs, 73 | ): 74 | prompt = prompter.generate_prompt(instruction, input) 75 | inputs = tokenizer(prompt, return_tensors="pt") 76 | input_ids = inputs["input_ids"].to(device) 77 | generation_config = GenerationConfig( 78 | temperature=temperature, 79 | top_p=top_p, 80 | top_k=top_k, 81 | num_beams=num_beams, 82 | **kwargs, 83 | ) 84 | with torch.no_grad(): 85 | generation_output = model.generate( 86 | input_ids=input_ids, 87 | generation_config=generation_config, 88 | return_dict_in_generate=True, 89 | output_scores=True, 90 | max_new_tokens=max_new_tokens, 91 | ) 92 | s = generation_output.sequences[0] 93 | output = tokenizer.decode(s) 94 | return prompter.get_response(output) 95 | 96 | if single_or_multi == "multi": 97 | response="" 98 | instruction="" 99 | for _ in range(0,5): 100 | inp=input("请输入:") 101 | inp=": " + inp 102 | instruction=instruction+inp 103 | response=evaluate(instruction) 104 | response=response.replace('\n','') 105 | print("Response:", response) 106 | instruction= instruction + " : " + response 107 | 108 | 109 | elif single_or_multi == "single": 110 | for instruction in [ 111 | "肝癌是什么?有哪些症状和迹象?", 112 | "肝癌是如何诊断的?有哪些检查和测试可以帮助诊断?", 113 | "Sorafenib是一种口服的多靶点酪氨酸激酶抑制剂,它的作用机制是什么?", 114 | "Regorafenib是一种口服的多靶点酪氨酸激酶抑制剂,它的作用机制是什么?它和Sorafenib有什么不同?", 115 | "肝癌药物治疗的副作用有哪些?如何缓解这些副作用?", 116 | "肝癌药物治疗的费用高昂,如何降低治疗的经济负担?", 117 | "我想了解一下β-谷甾醇是否可作为肝癌的治疗药物", 118 | "能介绍一下最近Hsa_circ_0008583在肝细胞癌治疗中的潜在应用的研究么?" 119 | ]: 120 | print("instruction:",instruction) 121 | instruction=": "+instruction 122 | print("Response:", evaluate(instruction)) 123 | 124 | 125 | 126 | if __name__ == "__main__": 127 | fire.Fire(main) 128 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | accelerate==0.20.1 2 | appdirs==1.4.4 3 | bitsandbytes==0.37.2 4 | black==23.3.0 5 | black[jupyter]==23.3.0 6 | datasets==2.11.0 7 | fire==0.5.0 8 | peft==0.3.0 9 | transformers==4.30.1 10 | gradio==3.33.1 11 | sentencepiece==0.1.97 12 | scipy==1.10.1 13 | wandb 14 | -------------------------------------------------------------------------------- /scripts/finetune.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | exp_tag="e1" 4 | python finetune.py \ 5 | --base_model 'decapoda-research/llama-7b-hf' \ 6 | --data_path './data/llama_data.json' \ 7 | --output_dir './lora-llama-med-'$exp_tag \ 8 | --prompt_template_name 'med_template' \ 9 | --micro_batch_size 128 \ 10 | --batch_size 128 \ 11 | --wandb_run_name $exp_tag -------------------------------------------------------------------------------- /scripts/infer-literature-multi.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # If inferring with the llama model, set 'use_lora' to 'False' and 'prompt_template' to 'ori_template'. 4 | # If inferring with the default alpaca model, set 'use_lora' to 'True', 'lora_weights' to 'tloen/alpaca-lora-7b', and 'prompt_template' to 'alpaca'. 5 | # If inferring with the llama-med model, download the LORA weights and set 'lora_weights' to './lora-llama-med' (or the exact directory of LORA weights) and 'prompt_template' to 'med_template'. 6 | 7 | """多轮交互""" 8 | python infer_literature.py \ 9 | --base_model 'decapoda-research/llama-7b-hf' \ 10 | --lora_weights './lora-llama-literature' \ 11 | --single_or_multi 'multi' \ 12 | --use_lora True \ 13 | --prompt_template 'literature_template' -------------------------------------------------------------------------------- /scripts/infer-literature-single.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # If inferring with the llama model, set 'use_lora' to 'False' and 'prompt_template' to 'ori_template'. 4 | # If inferring with the default alpaca model, set 'use_lora' to 'True', 'lora_weights' to 'tloen/alpaca-lora-7b', and 'prompt_template' to 'alpaca'. 5 | # If inferring with the llama-med model, download the LORA weights and set 'lora_weights' to './lora-llama-med' (or the exact directory of LORA weights) and 'prompt_template' to 'med_template'. 6 | 7 | """单轮""" 8 | python infer_literature.py \ 9 | --base_model 'decapoda-research/llama-7b-hf' \ 10 | --lora_weights './lora-llama-literature' \ 11 | --single_or_multi 'single' \ 12 | --use_lora True \ 13 | --prompt_template 'literature_template' 14 | -------------------------------------------------------------------------------- /scripts/infer.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # If inferring with the llama model, set 'use_lora' to 'False' and 'prompt_template' to 'ori_template'. 4 | # If inferring with the default alpaca model, set 'use_lora' to 'True', 'lora_weights' to 'tloen/alpaca-lora-7b', and 'prompt_template' to 'alpaca'. 5 | # If inferring with the llama-med model, download the LORA weights and set 'lora_weights' to './lora-llama-med' (or the exact directory of LORA weights) and 'prompt_template' to 'med_template'. 6 | 7 | python infer.py \ 8 | --base_model 'decapoda-research/llama-7b-hf' \ 9 | --lora_weights './lora-llama-med' \ 10 | --use_lora True \ 11 | --instruct_dir './data/infer.json' \ 12 | --prompt_template 'med_template' 13 | -------------------------------------------------------------------------------- /scripts/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # If inferring with the llama model, set 'use_lora' to 'False' and 'prompt_template' to 'ori_template'. 4 | # If inferring with the default alpaca model, set 'use_lora' to 'True', 'lora_weights' to 'tloen/alpaca-lora-7b', and 'prompt_template' to 'alpaca'. 5 | # If inferring with the llama-med model, download the LORA weights and set 'lora_weights' to './lora-llama-med' (or the exact directory of LORA weights) and 'prompt_template' to 'med_template'. 6 | 7 | BASE_MODEL="decapoda-research/llama-7b-hf" 8 | # 原始llama 9 | o_cmd="python infer.py \ 10 | --base_model ${BASE_MODEL} \ 11 | --use_lora False \ 12 | --prompt_template 'ori_template'" 13 | 14 | # Alpaca 15 | a_cmd="python infer.py \ 16 | --base_model ${BASE_MODEL} \ 17 | --use_lora True \ 18 | --lora_weights "tloen/alpaca-lora-7b" \ 19 | --prompt_template 'alpaca'" 20 | 21 | # llama-med 22 | m_cmd="python infer.py \ 23 | --base_model ${BASE_MODEL} \ 24 | --use_lora True \ 25 | --lora_weights "lora-llama-med" \ 26 | --prompt_template 'med_template'" 27 | 28 | echo "ori" 29 | eval $o_cmd > infer_result/o_tmp.txt 30 | echo "alpaca" 31 | eval $a_cmd > infer_result/a_tmp.txt 32 | echo "med" 33 | eval $m_cmd > infer_result/m_tmp.txt -------------------------------------------------------------------------------- /templates/bloom_deploy.json: -------------------------------------------------------------------------------- 1 | { 2 | "description": "Template used by Med Instruction Tuning", 3 | "prompt_input": "下面是一个问题,运用医学知识来正确回答提问.\n{instruction}\n\n\n### 回答:\n", 4 | "prompt_no_input": "下面是一个问题,运用医学知识来正确回答提问.\n{instruction}\n### 回答:\n", 5 | "response_split": "### 回答:" 6 | } 7 | 8 | 9 | -------------------------------------------------------------------------------- /templates/literature_template.json: -------------------------------------------------------------------------------- 1 | { 2 | "description": "Template used by Alpaca-LoRA.", 3 | "prompt_no_input": "以下是描述任务的说明,编写适当地回复完成请求的响应。\n\n ### 说明:\n{instruction}\n\n### 回复:\n", 4 | "response_split": "### 回复:" 5 | } 6 | -------------------------------------------------------------------------------- /templates/med_template.json: -------------------------------------------------------------------------------- 1 | { 2 | "description": "Template used by Med Instruction Tuning", 3 | "prompt_input": "下面是一个问题,运用医学知识来正确回答提问.\n### 问题:\n{instruction}\n### 回答:\n", 4 | "prompt_no_input": "下面是一个问题,运用医学知识来正确回答提问.\n### 问题:\n{instruction}\n### 回答:\n", 5 | "response_split": "### 回答:" 6 | } -------------------------------------------------------------------------------- /utils/README.md: -------------------------------------------------------------------------------- 1 | # Directory for helpers modules 2 | 3 | ## prompter.py 4 | 5 | Prompter class, a template manager. 6 | 7 | `from utils.prompter import Prompter` -------------------------------------------------------------------------------- /utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SCIR-HI/Huatuo-Llama-Med-Chinese/13ab5ff8c351404c47e35c1a5e43f76c28c940a1/utils/__init__.py -------------------------------------------------------------------------------- /utils/prompter.py: -------------------------------------------------------------------------------- 1 | """ 2 | A dedicated helper to manage templates and prompt building. 3 | """ 4 | 5 | import json 6 | import os.path as osp 7 | from typing import Union 8 | 9 | 10 | class Prompter(object): 11 | __slots__ = ("template", "_verbose") 12 | 13 | def __init__(self, template_name: str = "", verbose: bool = False): 14 | self._verbose = verbose 15 | if not template_name: 16 | # Enforce the default here, so the constructor can be called with '' and will not break. 17 | template_name = "alpaca" 18 | file_name = osp.join("templates", f"{template_name}.json") 19 | if not osp.exists(file_name): 20 | raise ValueError(f"Can't read {file_name}") 21 | with open(file_name, encoding='utf-8') as fp: 22 | self.template = json.load(fp) 23 | if self._verbose: 24 | print( 25 | f"Using prompt template {template_name}: {self.template['description']}" 26 | ) 27 | 28 | def generate_prompt( 29 | self, 30 | instruction: str, 31 | input: Union[None, str] = None, 32 | label: Union[None, str] = None, 33 | ) -> str: 34 | # returns the full prompt from instruction and optional input 35 | # if a label (=response, =output) is provided, it's also appended. 36 | if input: 37 | res = self.template["prompt_input"].format( 38 | instruction=instruction, input=input 39 | ) 40 | else: 41 | res = self.template["prompt_no_input"].format( 42 | instruction=instruction 43 | ) 44 | if label: 45 | res = f"{res}{label}" 46 | if self._verbose: 47 | print(res) 48 | return res 49 | 50 | def get_response(self, output: str) -> str: 51 | return output.split(self.template["response_split"])[1].strip() 52 | --------------------------------------------------------------------------------