# 扁仓中医大模型(BianCang: A Traditional Chinese Medicine Large Language Model)
## 💡介绍
你好,欢迎来到扁仓中医大模型的开源仓库。
为推动大语言模型在传统中医领域的落地应用,辅助医生进行疾病诊断,辅助患者进行自我评估,推动大模型赋能传统中医,我们在该仓库推出了**扁仓**系列中医大模型。扁仓是古代名医扁鹊、仓公的并称,泛指名医。我们期待扁仓中医大模型能够在延续中医传承和提升我国人民医疗健康水平方面做出一定的贡献。
扁仓以Qwen2/2.5作为基座,采用先注入领域知识再进行知识激活和对齐的两阶段训练方法而得到。扁仓在中医辨病辨证等中医特色任务上取得了最先进的性能,并且在各种医学执照考试中表现优异。
我们在该仓库中开源以下资源:
- 扁仓基座模型权重:包括BianCang-Qwen2-7B、BianCang-Qwen2.5-7B。
- 扁仓指令精调模型权重:包括BianCang-Qwen2-7B-Instruct、BianCang-Qwen2.5-7B-Instruct。
更多信息请查看[GitHub]([QLU-NLP/BianCang](https://github.com/QLU-NLP/BianCang))
## 🚀推理
### 使用SWIFT
#### 环境安装
在[Release v2.4.2 · modelscope/ms-swift](https://github.com/modelscope/ms-swift/releases/tag/v2.4.2)处下载SWIFT源码,切换到对应目录,然后执行安装命令:
```shell
cd swift
pip install -e .
```
你可以根据自己的GPU驱动版本去选择合适的torch版本进行替换,SWIFT至少需要torch >= 1.13,推荐torch >= 2.0.0。
注意:由于我们进行SFT训练时使用的Chat Template为*qwen*,因此如果你使用的SWIFT版本大于我们提供的版本,可能会遇到Qwen2.5 Chat Template不对应的问题,请手动将Chat Template指定为*qwen*而不是*qwen2_5*。具体原因参考:[fix qwen2.5 template by Jintao-Huang · Pull Request #2081 · modelscope/ms-swift](https://github.com/modelscope/ms-swift/pull/2081)
#### 推理方式1-代码推理
```python
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
from swift.llm import (
get_model_tokenizer, get_template, inference, ModelType
)
from swift.utils import seed_everything
model_type = ModelType.qwen2_5_7b_instruct
template_type = 'qwen'
model_id_or_path = 'QLU-NLP/BianCang-Qwen2.5-7B-Instruct'
model, tokenizer = get_model_tokenizer(model_type, model_id_or_path=model_id_or_path, model_kwargs={'device_map': 'auto'})
model.generation_config.max_new_tokens = 256
template = get_template(template_type, tokenizer)
seed_everything(42)
query = '你好,你是谁?'
response, history = inference(model, template, query)
print(f'query: {query}')
print(f'response: {response}')
query = '下面是一名患者的基本情况。年龄:78岁,性别:女。主 诉:活动后胸痛一周。现病史:患者一周前活动后出现胸口隐隐作痛,如针刺样乏力气短,活动后汗出,偏头痛。中医望闻切诊:表情自然,面色红润,形体正常,语气清,气息平;无异常气味,舌暗红,苔少。请你根据上述患者的主诉、病史和中医望闻切诊情况,判断该患者的主要中医疾病和中医证型,并给出中医辨病辨证的依据。'
response, history = inference(model, template, query, history)
print(f'query: {query}')
print(f'response: {response}')
print(f'history: {history}')
```
输出:
```
query: 你好,你是谁?
response: 你好!我是一个名为扁仓中医大模型的人工智能,由齐鲁工业大学(山东省科学院)计算机科学与技术学部(国家超级计算济南中心)自然语言处理与认知计算团队研发。我被设计成能够理解和生成自然语言文本,以便与人类进行中医辩证、中医处方推荐、中医知识问答、中医问题咨询等方面的对话交流,辅助人们完成疾病诊断相关的任务。请问有什么我可以帮助您的吗?
query: 下面是一名患者的基本情况。年龄:78岁,性别:女。主 诉:活动后胸痛一周。现病史:患者一周前活动后出现胸口隐隐作痛,如针刺样乏力气短,活动后汗出,偏头痛。中医望闻切诊:表情自然,面色红润,形体正常,语气清,气息平;无异常气味,舌暗红,苔少。请你根据上述患者的主诉、病史和中医望闻切诊情况,判断该患者的主要中医疾病和中医证型,并给出中医辨病辨证的依据。
response: 根据中医的诊断方法,患者患有胸痹心痛,中医证型属于气虚血瘀证。综合脉证,四诊合参,本病当属祖国医学“胸痹心痛病”范畴,证属“气虚血瘀”。患者素体虚弱,久病伤正,伤及心气,心气衰微,机能不健,致阴邪易于上乘阳位,况心脉为宗气之所,百脉朝会之枢,宗气的鼓动形成了心气推动血液运行全身,心气不足则血行无力瘀滞,发为本病,舌脉俱为佐证。
history: [['你好,你是谁?', '你好!我是一个名为扁仓中医大模型的人工智能,由齐鲁工业大学(山东省科学院)计算机科学与技术学部(国家超级计算济南中心)自然语言处理与认知计算团队研发。我被设计成能够理解和生成自然语言文本,以便与人类进行中医辩证、中医处方推荐、中医知识问答、中医问题咨询等方面的对话交流,辅助人们完成疾病诊断相关的任务。请问有什么我可以帮助您的吗?'], ['下面是一名患者的基本情况。年龄:78岁,性别:女。主 诉:活动后胸痛一周。现病史:患者一周前活动后出现胸口隐隐作痛,如针刺样乏力气短,活动后汗出,偏头痛。中医望闻切诊:表情自然,面色红润,形体正常,语气清,气息平;无异常气味,舌暗红,苔少。请你根据上述患者的主诉、病史和中医望闻切诊情况,判断该患者的主要中医疾病和中医证型,并给出中医辨病辨证的依据。', '根据中医的诊断方法,患者患有胸痹心痛,中医证型属于气虚血瘀证。综合脉证,四诊合参,本病当属祖国医学“胸痹心痛病”范畴,证属“气虚血瘀”。患者素体虚弱,久病伤正,伤及心气,心气衰微,机能不健,致阴邪易于上乘阳位,况心脉为宗气之所,百脉朝会之枢,宗气的鼓动形成了心气推动血液运行全身,心气不足则血行无力瘀滞,发为本病,舌脉俱为佐证。']]
```
#### 推理方式2-部署API
使用以下命令部署API:
```shell
CUDA_VISIBLE_DEVICES=0 swift deploy --model_type qwen2_5-7b-instruct --model_id_or_path QLU-NLP/BianCang-Qwen2.5-7B-Instruct --port 8090 --template_type qwen
```
使用curl进行测试:
```shell
curl http://localhost:8090/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "qwen2_5-7b-instruct",
"messages": [{"role": "user", "content": "你好,你是谁?"}],
"max_tokens": 256,
"temperature": 0.3
}'
```
响应如下:
```json
{"model":"qwen2_5-7b-instruct",
"choices":[{"index":0,"message":{"role":"assistant","content":"你好!我是一个名为扁仓中医大模型的人工智能,由齐鲁工业大学(山东省科学院)计算机科学与技术学部(国家超级计算济南中心)自然语言处理与认知计算团队研发。我被设计成能够理解和生成自然语言文本,以便与人类进行中医辩证、中医处方推荐、中医知识问答、中医问题咨询等方面的对话交流,辅助人们完成疾病诊断相关的任务。请问有什么我可以帮助您的吗?",
"tool_calls":null},"finish_reason":null,"logprobs":null}],
"usage":{"prompt_tokens":24,"completion_tokens":92,"total_tokens":116},
"id":"chatcmpl-6b4a02dee57a42238b27b5c40085df16",
"object":"chat.completion","created":1730209011}
```
使用代码进行测试:
```python
from swift.llm import get_model_list_client, XRequestConfig, inference_client
model_list = get_model_list_client(port=8090)
model_type = model_list.data[0].id
print(f'model_type: {model_type}')
query = "你好,你是谁?"
request_config = XRequestConfig(seed=42)
resp = inference_client(model_type, query, request_config=request_config, port=8090)
response = resp.choices[0].message.content
print(f'query: {query}')
print(f'response: {response}')
history = [(query, response)]
query = '下面是一名患者的基本情况。年龄:78岁,性别:女。主 诉:活动后胸痛一周。现病史:患者一周前活动后出现胸口隐隐作痛,如针刺样乏力气短,活动后汗出,偏头痛。中医望闻切诊:表情自然,面色红润,形体正常,语气清,气息平;无异常气味,舌暗红,苔少。请你根据上述患者的主诉、病史和中医望闻切诊情况,判断该患者的主要中医疾病和中医证型,并给出中医辨病辨证的依据。'
request_config = XRequestConfig(stream=True, seed=42)
stream_resp = inference_client(model_type, query, history, request_config=request_config, port=8090)
print(f'query: {query}')
print('response: ', end='')
for chunk in stream_resp:
print(chunk.choices[0].delta.content, end='', flush=True)
print()
```
输出如下:
```
model_type: qwen2_5-7b-instruct
query: 你好,你是谁?
response: 你好!我是一个名为扁仓中医大模型的人工智能,由齐鲁工业大学(山东省科学院)计算机科学与技术学部(国家超级计算济南中心)自然语言处理与认知计算团队研发。我被设计成能够理解和生成自然语言文本,以便与人类进行中医辩证、中医处方推荐、中医知识问答、中医问题咨询等方面的对话交流,辅助人们完成疾病诊断相关的任务。请问有什么我可以帮助您的吗?
query: 下面是一名患者的基本情况。年龄:78岁,性别:女。主 诉:活动后胸痛一周。现病史:患者一周前活动后出现胸口隐隐作痛,如针刺样乏力气短,活动后汗出,偏头痛。中医望闻切诊:表情自然,面色红润,形体正常,语气清,气息平;无异常气味,舌暗红,苔少。请你根据上述患者的主诉、病史和中医望闻切诊情况,判断该患者的主要中医疾病和中医证型,并给出中医辨病辨证的依据。
response: 根据中医的诊断方法,患者患有胸痹心痛,中医证型属于气虚血瘀证。综合脉证,四诊合参,本病当属祖国医学“胸痹心痛病”范畴,证属“气虚血瘀”。患者素体虚弱,久病伤正,伤及心气,心气衰微,机能不健,致阴邪易于上乘阳位,况心脉为宗气之所,百脉朝会之枢,宗气的鼓动形成了心气推动血液运行全身,心气不足则血行无力瘀滞,发为本病,舌脉俱为佐证。
```
### 使用Transformers
你也可以使用transformers包进行推理:
```python
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "QLU-NLP/BianCang-Qwen2.5-7B-Instruct"
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto",
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
prompt = "你好,你是谁?"
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
generated_ids = model.generate(
**model_inputs,
max_new_tokens=256
)
generated_ids = [
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(response)
```
### 使用Web UI
我们提供了一个简单的演示Web UI。
安装streamlit:
```shell
pip install streamlit
```
使用SWIFT部署API:
```shell
CUDA_VISIBLE_DEVICES=0 swift deploy --model_type qwen2_5-7b-instruct --model_id_or_path QLU-NLP/BianCang-Qwen2.5-7B-Instruct --port 8090 --template_type qwen
```
启动streamlit:
```shell
streamlit run web_ui.py
```
![image-20241029215908096](assets/webui.png)
## 🥇中医能力测试
Model |
TCM Syndrome Differentiation |
TCM Disease Diagnosis |
TCM Exam |
TCMSD Acc.(%) |
TCMSD-BC Acc.(%) |
TCMDD Acc.(%) |
TCMDD-BC Acc.(%) |
MLEC-TCM Acc.(%) |
MLEC-CWM Acc.(%) |
| DI | CoT | DI | CoT | DI | CoT | DI | CoT | ZS | FS | ZS | FS |
GPT-4 | 24.53 | 45.21 | 16.67 | 70.73 | 27.83 | 54.54 | 41.80 | 68.33 | 74.70 | 76.35 | 76.26 | 76.37 |
Qwen2-7B | 31.74 | 27.18 | 32.73 | 28.40 | 41.60 | 54.59 | 74.87 | 77.93 | 86.01 | 89.18 | 84.45 | 87.89 |
Qwen2-7B-Instruct | 25.70 | 33.41 | 14.27 | 57.00 | 32.87 | 52.92 | 60.40 | 60.13 | 83.61 | 84.22 | 79.89 | 82.99 |
Qwen2.5-7B | 30.44 | 21.29 | 17.87 | 35.73 | 23.71 | 43.88 | 63.87 | 71.27 | 83.32 | 85.52 | 82.02 | 84.04 |
Qwen2.5-7B-Instruct | 24.30 | 32.19 | 9.93 | 57.07 | 36.29 | 51.51 | 62.93 | 55.53 | 78.72 | 79.88 | 77.27 | 78.43 |
Qwen2.5-14B | 35.62 | 25.21 | 33.93 | 30.13 | 24.33 | 36.64 | 33.33 | 32.80 | 86.59 | 89.93 | 87.10 | 90.06 |
Qwen2.5-14B-Instruct | 25.94 | 35.03 | 16.07 | 60.00 | 38.30 | 49.31 | 46.27 | 53.67 | 82.25 | 84.81 | 81.79 | 85.68 |
BianCang-Qwen2-7B | 42.14 | 30.30 | 57.80 | 48.00 | 43.73 | 54.67 | 74.73 | 80.67 | 90.86 | 91.87 | 89.08 | 90.36 |
BianCang-Qwen2-7B-Instruct | 68.88 | 75.96 | 57.33 | 75.40 | 64.42 | 77.71 | 89.07 | 85.67 | 92.39 | 92.39 | 91.14 | 91.48 |
BianCang-Qwen2.5-7B | 46.57 | 26.72 | 52.93 | 45.47 | 49.80 | 53.15 | 68.13 | 61.73 | 86.46 | 86.30 | 83.93 | 85.35 |
BianCang-Qwen2.5-7B-Instruct | 78.90 | 82.10 | 66.73 | 77.73 | 73.73 | 82.65 | 87.87 | 89.40 | 90.22 | 90.57 | 90.32 | 90.62 |
BianCang-Qwen2.5-14B | 43.77 | 33.96 | 61.93 | 53.47 | 66.61 | 60.39 | 82.93 | 77.07 | 89.28 | 90.86 | 89.42 | 90.58 |
BianCang-Qwen2.5-14B-Instruct | 79.38 | 75.54 | 62.27 | 70.73 | 77.63 | 82.05 | 86.33 | 88.73 | 92.29 | 92.29 | 92.75 | 92.86 |
Model |
CMB Acc.(%) |
MLEC-Clinic Acc.(%) |
MLEC-PublicHealth Acc.(%) |
MLEC-Stomatology Acc.(%) |
|
ZS/FS |
ZS |
FS |
ZS |
FS |
ZS |
FS |
GPT-4 |
59.46* |
82.63 |
82.69 |
81.55 |
82.58 |
72.97 |
75.43 |
Qwen2-7B |
81.63 |
87.63 |
90.63 |
82.63 |
86.79 |
80.34 |
84.65 |
Qwen2-7B-Instruct |
83.45 |
85.16 |
83.35 |
81.61 |
81.07 |
76.29 |
75.88 |
Qwen2.5-7B |
79.60 |
86.65 |
88.55 |
83.39 |
85.17 |
78.03 |
80.79 |
Qwen2.5-7B-Instruct |
79.51 |
82.81 |
83.73 |
80.96 |
80.85 |
72.93 |
74.40 |
Qwen2.5-14B |
84.07 |
90.40 |
93.13 |
86.46 |
89.54 |
84.31 |
88.20 |
Qwen2.5-14B-Instruct |
83.69 |
86.47 |
88.02 |
83.17 |
86.14 |
78.94 |
82.57 |
BianCang-Qwen2-7B (Ours) |
83.27 |
91.88 |
93.31 |
88.57 |
90.72 |
85.29 |
88.47 |
BianCang-Qwen2-7B-Instruct (Ours) |
84.08 |
94.35 |
94.35 |
91.37 |
91.64 |
89.19 |
90.02 |
BianCang-Qwen2.5-7B (Ours) |
80.13 |
90.43 |
91.32 |
85.65 |
87.22 |
82.19 |
82.65 |
BianCang-Qwen2.5-7B-Instruct (Ours) |
80.71 |
93.40 |
93.43 |
89.91 |
89.91 |
86.43 |
86.77 |
BianCang-Qwen2.5-14B (Ours) |
84.34 |
91.70 |
93.37 |
87.92 |
89.97 |
86.16 |
87.94 |
BianCang-Qwen2.5-14B-Instruct (Ours) |
83.80 |
94.74 |
94.97 |
91.86 |
91.53 |
90.43 |
90.51 |
更多测评结果请关注我们的技术报告。
## 🧡致谢
本项目基于开源项目进行开发,在此对相关项目和研究开发人员表示感谢。
- [Qwen2](https://github.com/vitanova/Qwen2)
- [Qwen2.5](https://github.com/QwenLM/Qwen2.5)
- [SWIFT](https://github.com/modelscope/ms-swift)
- [ModelScope](https://github.com/modelscope/modelscope)
- [ShenNong-TCM-LLM](https://github.com/michael-wzhu/ShenNong-TCM-LLM?tab=readme-ov-file)
- [HuatuoGPT-II](https://github.com/FreedomIntelligence/HuatuoGPT-II)
- [DISC-MedLLM](https://github.com/FudanDISC/DISC-MedLLM)
- [MLEC-QA](https://github.com/Judenpech/MLEC-QA)
- [CMB](https://github.com/FreedomIntelligence/CMB?tab=readme-ov-file)
- [ZY-BERT](https://github.com/Borororo/ZY-BERT)
- [COIG](https://github.com/BAAI-Zlab/COIG)
- [APE210k](https://github.com/Chenny0808/ape210k)
- [Evol-Instruction-66K](https://github.com/Continuum-Labs-HQ/EvolInstruct)
## ❔关于我们
本项目由齐鲁工业大学(山东省科学院)计算学部(国家超级计算济南中心)自然语言处理与认知计算团队、山东~~省~~中医药大学附属医院临床研究中心合作完成。
## ❕免责声明
- 本项目相关资源仅供学术研究之用。
- 扁仓中医大模型作为基于语言模型的智能助手,具有局限性,无法保证所有响应的准确性,其不能代替中医/西医进行医学诊断和给出医学建议。如有需要,请咨询专业医生或前往医院就诊。
- 由于医疗领域的数据不准确可能造成严重后果,我们强烈建议用户在处理生成的信息时要小心谨慎,并向专家寻求建议。
## 📖引用
```
@article{Wei2024BianCang,
title={BianCang: A Traditional Chinese Medicine Large Language Model},
author={Sibo, Wei and Xueping, Peng and Yi-fei, Wang and Jiasheng, Si and Weiyu, Zhang and Wenpeng, Lu and Xiaoming, Wu and Yinglong, Wang},
journal={arXiv preprint arXiv:2411.11027},
year={2024}
}
```