Views
No views yet

1cd swift
2pip install -e .1import os
2os.environ['CUDA_VISIBLE_DEVICES'] = '0'
3
4from swift.llm import (
5 get_model_tokenizer, get_template, inference, ModelType
6)
7from swift.utils import seed_everything
8
9model_type = ModelType.qwen2_5_7b_instruct
10template_type = 'qwen'
11
12model_id_or_path = 'QLU-NLP/BianCang-Qwen2.5-7B-Instruct'
13model, tokenizer = get_model_tokenizer(model_type, model_id_or_path=model_id_or_path, model_kwargs={'device_map': 'auto'})
14model.generation_config.max_new_tokens = 256
15
16template = get_template(template_type, tokenizer)
17seed_everything(42)
18query = '你好,你是谁?'
19response, history = inference(model, template, query)
20print(f'query: {query}')
21print(f'response: {response}')
22query = '下面是一名患者的基本情况。年龄:78岁,性别:女。主 诉:活动后胸痛一周。现病史:患者一周前活动后出现胸口隐隐作痛,如针刺样乏力气短,活动后汗出,偏头痛。中医望闻切诊:表情自然,面色红润,形体正常,语气清,气息平;无异常气味,舌暗红,苔少。请你根据上述患者的主诉、病史和中医望闻切诊情况,判断该患者的主要中医疾病和中医证型,并给出中医辨病辨证的依据。'
23response, history = inference(model, template, query, history)
24print(f'query: {query}')
25print(f'response: {response}')
26print(f'history: {history}')query: 你好,你是谁?
response: 你好!我是一个名为扁仓中医大模型的人工智能,由齐鲁工业大学(山东省科学院)计算机科学与技术学部(国家超级计算济南中心)自然语言处理与认知计算团队研发。我被设计成能够理解和生成自然语言文本,以便与人类进行中医辩证、中医处方推荐、中医知识问答、中医问题咨询等方面的对话交流,辅助人们完成疾病诊断相关的任务。请问有什么我可以帮助您的吗?
query: 下面是一名患者的基本情况。年龄:78岁,性别:女。主 诉:活动后胸痛一周。现病史:患者一周前活动后出现胸口隐隐作痛,如针刺样乏力气短,活动后汗出,偏头痛。中医望闻切诊:表情自然,面色红润,形体正常,语气清,气息平;无异常气味,舌暗红,苔少。请你根据上述患者的主诉、病史和中医望闻切诊情况,判断该患者的主要中医疾病和中医证型,并给出中医辨病辨证的依据。
response: 根据中医的诊断方法,患者患有胸痹心痛,中医证型属于气虚血瘀证。综合脉证,四诊合参,本病当属祖国医学“胸痹心痛病”范畴,证属“气虚血瘀”。患者素体虚弱,久病伤正,伤及心气,心气衰微,机能不健,致阴邪易于上乘阳位,况心脉为宗气之所,百脉朝会之枢,宗气的鼓动形成了心气推动血液运行全身,心气不足则血行无力瘀滞,发为本病,舌脉俱为佐证。
history: [['你好,你是谁?', '你好!我是一个名为扁仓中医大模型的人工智能,由齐鲁工业大学(山东省科学院)计算机科学与技术学部(国家超级计算济南中心)自然语言处理与认知计算团队研发。我被设计成能够理解和生成自然语言文本,以便与人类进行中医辩证、中医处方推荐、中医知识问答、中医问题咨询等方面的对话交流,辅助人们完成疾病诊断相关的任务。请问有什么我可以帮助您的吗?'], ['下面是一名患者的基本情况。年龄:78岁,性别:女。主 诉:活动后胸痛一周。现病史:患者一周前活动后出现胸口隐隐作痛,如针刺样乏力气短,活动后汗出,偏头痛。中医望闻切诊:表情自然,面色红润,形体正常,语气清,气息平;无异常气味,舌暗红,苔少。请你根据上述患者的主诉、病史和中医望闻切诊情况,判断该患者的主要中医疾病和中医证型,并给出中医辨病辨证的依据。', '根据中医的诊断方法,患者患有胸痹心痛,中医证型属于气虚血瘀证。综合脉证,四诊合参,本病当属祖国医学“胸痹心痛病”范畴,证属“气虚血瘀”。患者素体虚弱,久病伤正,伤及心气,心气衰微,机能不健,致阴邪易于上乘阳位,况心脉为宗气之所,百脉朝会之枢,宗气的鼓动形成了心气推动血液运行全身,心气不足则血行无力瘀滞,发为本病,舌脉俱为佐证。']]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 qwen1curl http://localhost:8090/v1/chat/completions \
2-H "Content-Type: application/json" \
3-d '{
4"model": "qwen2_5-7b-instruct",
5"messages": [{"role": "user", "content": "你好,你是谁?"}],
6"max_tokens": 256,
7"temperature": 0.3
8}'1{"model":"qwen2_5-7b-instruct",
2"choices":[{"index":0,"message":{"role":"assistant","content":"你好!我是一个名为扁仓中医大模型的人工智能,由齐鲁工业大学(山东省科学院)计算机科学与技术学部(国家超级计算济南中心)自然语言处理与认知计算团队研发。我被设计成能够理解和生成自然语言文本,以便与人类进行中医辩证、中医处方推荐、中医知识问答、中医问题咨询等方面的对话交流,辅助人们完成疾病诊断相关的任务。请问有什么我可以帮助您的吗?",
3"tool_calls":null},"finish_reason":null,"logprobs":null}],
4"usage":{"prompt_tokens":24,"completion_tokens":92,"total_tokens":116},
5"id":"chatcmpl-6b4a02dee57a42238b27b5c40085df16",
6"object":"chat.completion","created":1730209011}1from swift.llm import get_model_list_client, XRequestConfig, inference_client
2
3model_list = get_model_list_client(port=8090)
4model_type = model_list.data[0].id
5print(f'model_type: {model_type}')
6
7query = "你好,你是谁?"
8request_config = XRequestConfig(seed=42)
9resp = inference_client(model_type, query, request_config=request_config, port=8090)
10response = resp.choices[0].message.content
11print(f'query: {query}')
12print(f'response: {response}')
13
14history = [(query, response)]
15query = '下面是一名患者的基本情况。年龄:78岁,性别:女。主 诉:活动后胸痛一周。现病史:患者一周前活动后出现胸口隐隐作痛,如针刺样乏力气短,活动后汗出,偏头痛。中医望闻切诊:表情自然,面色红润,形体正常,语气清,气息平;无异常气味,舌暗红,苔少。请你根据上述患者的主诉、病史和中医望闻切诊情况,判断该患者的主要中医疾病和中医证型,并给出中医辨病辨证的依据。'
16request_config = XRequestConfig(stream=True, seed=42)
17stream_resp = inference_client(model_type, query, history, request_config=request_config, port=8090)
18print(f'query: {query}')
19print('response: ', end='')
20for chunk in stream_resp:
21 print(chunk.choices[0].delta.content, end='', flush=True)
22print()model_type: qwen2_5-7b-instruct
query: 你好,你是谁?
response: 你好!我是一个名为扁仓中医大模型的人工智能,由齐鲁工业大学(山东省科学院)计算机科学与技术学部(国家超级计算济南中心)自然语言处理与认知计算团队研发。我被设计成能够理解和生成自然语言文本,以便与人类进行中医辩证、中医处方推荐、中医知识问答、中医问题咨询等方面的对话交流,辅助人们完成疾病诊断相关的任务。请问有什么我可以帮助您的吗?
query: 下面是一名患者的基本情况。年龄:78岁,性别:女。主 诉:活动后胸痛一周。现病史:患者一周前活动后出现胸口隐隐作痛,如针刺样乏力气短,活动后汗出,偏头痛。中医望闻切诊:表情自然,面色红润,形体正常,语气清,气息平;无异常气味,舌暗红,苔少。请你根据上述患者的主诉、病史和中医望闻切诊情况,判断该患者的主要中医疾病和中医证型,并给出中医辨病辨证的依据。
response: 根据中医的诊断方法,患者患有胸痹心痛,中医证型属于气虚血瘀证。综合脉证,四诊合参,本病当属祖国医学“胸痹心痛病”范畴,证属“气虚血瘀”。患者素体虚弱,久病伤正,伤及心气,心气衰微,机能不健,致阴邪易于上乘阳位,况心脉为宗气之所,百脉朝会之枢,宗气的鼓动形成了心气推动血液运行全身,心气不足则血行无力瘀滞,发为本病,舌脉俱为佐证。1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "QLU-NLP/BianCang-Qwen2.5-7B-Instruct"
4
5model = AutoModelForCausalLM.from_pretrained(
6 model_name,
7 torch_dtype="auto",
8 device_map="auto"
9)
10tokenizer = AutoTokenizer.from_pretrained(model_name)
11
12prompt = "你好,你是谁?"
13messages = [
14 {"role": "system", "content": "You are a helpful assistant."},
15 {"role": "user", "content": prompt}
16]
17text = tokenizer.apply_chat_template(
18 messages,
19 tokenize=False,
20 add_generation_prompt=True
21)
22model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
23
24generated_ids = model.generate(
25 **model_inputs,
26 max_new_tokens=256
27)
28generated_ids = [
29 output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
30]
31
32response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
33print(response)
34pip install streamlitCUDA_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 qwenstreamlit run web_ui.py
| 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 |
| DeepSeek-V3 | 34.62 | 40.74 | 24.53 | 72.00 | 46.97 | 59.08 | 82.67 | 72.93 | 84.97 | 88.56 | 85.05 | 87.81 |
| DeepSeek-R1 | 37.17 | 55.67 | 25.53 | 76.07 | 50.66 | 80.75 | 79.27 | 94.53 | 92.68 | 93.10 | 90.92 | 90.77 |
| 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 |
| DeepSeek-V3 | 82.33 | 86.83 | 89.41 | 85.38 | 87.59 | 79.09 | 81.97 |
| DeepSeek-R1 | 86.38 | 92.51 | 92.36 | 91.42 | 90.40 | 87.03 | 86.16 |
| 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 |



@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}
}