大模型相关目录
大模型,包括部署微调prompt/Agent应用开发、知识库增强、数据库增强、知识图谱增强、自然语言处理、多模态等大模型应用开发内容
从0起步,扬帆起航。
- 大模型应用向开发路径:AI代理工作流
- 大模型应用开发实用开源项目汇总
- 大模型问答项目问答性能评估方法
- 大模型数据侧总结
- 大模型token等基本概念及参数和内存的关系
- 大模型应用开发-华为大模型生态规划
- 从零开始的LLaMA-Factory的指令增量微调
- 基于实体抽取-SMC-语义向量的大模型能力评估通用算法(附代码)
- 基于Langchain-chatchat的向量库构建及检索(附代码)
- 一文教你成为合格的Prompt工程师
- 最简明的大模型agent教程
- 批量使用API调用langchain-chatchat知识库能力
- langchin-chatchat部分开发笔记(持续更新)
- 文心一言、讯飞星火、GPT、通义千问等线上API调用示例
- 大模型RAG性能提升路径
- langchain的基本使用
- 结合基础模型的大模型多源信息应用开发
- COT:大模型的强化利器
- 多角色大模型问答性能提升策略(附代码)
- 大模型接入外部在线信息提升应用性能
- 从零开始的Dify大模型应用开发指南
- 基于dify开发的多模态大模型应用(附代码)
- 基于零一万物多模态大模型通过外接数据方案优化图像文字抽取系统
- 快速接入stable diffusion的文生图能力
- 多模态大模型通过外接数据方案实现电力智能巡检(设计方案)
- 大模型prompt实例:知识库信息质量校验模块
- 基于Dify的LLM-RAG多轮对话需求解决方案(附代码)
- Dify大模型开发技巧:约束大模型回答范围
- 以API形式调用Dify项目应用(附代码)
- 基于Dify的QA数据集构建(附代码)
- Qwen-2-7B和GLM-4-9B:大模型届的比亚迪秦L
- 文擎毕昇和Dify:大模型开发平台模式对比
- Qwen-VL图文多模态大模型微调指南
- 从零开始的Ollama指南:部署私域大模型
- 基于Dify的智能分类方案:大模型结合KNN算法(附代码)
- OpenCompass:大模型测评工具
- 一文读懂多模态大模型基础架构
- 大模型管理平台:one-api使用指南
- 大模型RAG、ROG、RCG概念科普
- RAGOnMedicalKG:大模型结合知识图谱的RAG实现
文章目录
- 大模型相关目录
- 开源项目
- 简介
- 概况介绍
开源项目
https://github.com/liuhuanyong/RAGOnMedicalKG
https://github.com/liuhuanyong/QASystemOnMedicalKG
简介
该项目分两部分:
基于垂直网站数据的医药知识图谱构建
基于医药知识图谱的自动问答,基于LLM的方式
step1: linking entity,针对问题进行实体识别,本项目采用基于ac自动机通过加载图谱词表进行匹配获得;
step2:recall kg facts,通过上一步得到的多个实体,通过prompt的方式提示llm进行实体的意图识别,然后转换成cypher语句进行查询,并过滤兼枝,得到子图路径;
step3:generate answer,通过召回好的子图,拼接prompt,使用llm完成问答;
概况介绍
该项目开源了所用数据:
开源了其构建代码及效果:
#!/usr/bin/env python3
# coding: utf-8
# File: MedicalGraph.py
# Author: lhy<lhy_in_blcu@126.com,https://huangyong.github.io>
# Date: 18-10-3
import os
import json
from py2neo import Graph,Node
class MedicalGraph:
def __init__(self):
cur_dir = '/'.join(os.path.abspath(__file__).split('/')[:-1])
self.data_path = os.path.join(cur_dir, 'data/medical.json')
self.g = Graph(
host="127.0.0.1", # neo4j 搭载服务器的ip地址,ifconfig可获取到
http_port=7474, # neo4j 服务器监听的端口号
user="lhy", # 数据库user name,如果没有更改过,应该是neo4j
password="lhy123")
'''读取文件'''
def read_nodes(self):
# 共7类节点
drugs = [] # 药品
foods = [] # 食物
checks = [] # 检查
departments = [] #科室
producers = [] #药品大类
diseases = [] #疾病
symptoms = []#症状
disease_infos = []#疾病信息
# 构建节点实体关系
rels_department = [] # 科室-科室关系
rels_noteat = [] # 疾病-忌吃食物关系
rels_doeat = [] # 疾病-宜吃食物关系
rels_recommandeat = [] # 疾病-推荐吃食物关系
rels_commonddrug = [] # 疾病-通用药品关系
rels_recommanddrug = [] # 疾病-热门药品关系
rels_check = [] # 疾病-检查关系
rels_drug_producer = [] # 厂商-药物关系
rels_symptom = [] #疾病症状关系
rels_acompany = [] # 疾病并发关系
rels_category = [] # 疾病与科室之间的关系
count = 0
for data in open(self.data_path):
disease_dict = {}
count += 1
print(count)
data_json = json.loads(data)
disease = data_json['name']
disease_dict['name'] = disease
diseases.append(disease)
disease_dict['desc'] = ''
disease_dict['prevent'] = ''
disease_dict['cause'] = ''
disease_dict['easy_get'] = ''
disease_dict['cure_department'] = ''
disease_dict['cure_way'] = ''
disease_dict['cure_lasttime'] = ''
disease_dict['symptom'] = ''
disease_dict['cured_prob'] = ''
if 'symptom' in data_json:
symptoms += data_json['symptom']
for symptom in data_json['symptom']:
rels_symptom.append([disease, symptom])
if 'acompany' in data_json:
for acompany in data_json['acompany']:
rels_acompany.append([disease, acompany])
if 'desc' in data_json:
disease_dict['desc'] = data_json['desc']
if 'prevent' in data_json:
disease_dict['prevent'] = data_json['prevent']
if 'cause' in data_json:
disease_dict['cause'] = data_json['cause']
if 'get_prob' in data_json:
disease_dict['get_prob'] = data_json['get_prob']
if 'easy_get' in data_json:
disease_dict['easy_get'] = data_json['easy_get']
if 'cure_department' in data_json:
cure_department = data_json['cure_department']
if len(cure_department) == 1:
rels_category.append([disease, cure_department[0]])
if len(cure_department) == 2:
big = cure_department[0]
small = cure_department[1]
rels_department.append([small, big])
rels_category.append([disease, small])
disease_dict['cure_department'] = cure_department
departments += cure_department
if 'cure_way' in data_json:
disease_dict['cure_way'] = data_json['cure_way']
if 'cure_lasttime' in data_json:
disease_dict['cure_lasttime'] = data_json['cure_lasttime']
if 'cured_prob' in data_json:
disease_dict['cured_prob'] = data_json['cured_prob']
if 'common_drug' in data_json:
common_drug = data_json['common_drug']
for drug in common_drug:
rels_commonddrug.append([disease, drug])
drugs += common_drug
if 'recommand_drug' in data_json:
recommand_drug = data_json['recommand_drug']
drugs += recommand_drug
for drug in recommand_drug:
rels_recommanddrug.append([disease, drug])
if 'not_eat' in data_json:
not_eat = data_json['not_eat']
for _not in not_eat:
rels_noteat.append([disease, _not])
foods += not_eat
do_eat = data_json['do_eat']
for _do in do_eat:
rels_doeat.append([disease, _do])
foods += do_eat
recommand_eat = data_json['recommand_eat']
for _recommand in recommand_eat:
rels_recommandeat.append([disease, _recommand])
foods += recommand_eat
if 'check' in data_json:
check = data_json['check']
for _check in check:
rels_check.append([disease, _check])
checks += check
if 'drug_detail' in data_json:
drug_detail = data_json['drug_detail']
producer = [i.split('(')[0] for i in drug_detail]
rels_drug_producer += [[i.split('(')[0], i.split('(')[-1].replace(')', '')] for i in drug_detail]
producers += producer
disease_infos.append(disease_dict)
return set(drugs), set(foods), set(checks), set(departments), set(producers), set(symptoms), set(diseases), disease_infos,\
rels_check, rels_recommandeat, rels_noteat, rels_doeat, rels_department, rels_commonddrug, rels_drug_producer, rels_recommanddrug,\
rels_symptom, rels_acompany, rels_category
'''建立节点'''
def create_node(self, label, nodes):
count = 0
for node_name in nodes:
node = Node(label, name=node_name)
self.g.create(node)
count += 1
print(count, len(nodes))
return
'''创建知识图谱中心疾病的节点'''
def create_diseases_nodes(self, disease_infos):
count = 0
for disease_dict in disease_infos:
node = Node("Disease", name=disease_dict['name'], desc=disease_dict['desc'],
prevent=disease_dict['prevent'] ,cause=disease_dict['cause'],
easy_get=disease_dict['easy_get'],cure_lasttime=disease_dict['cure_lasttime'],
cure_department=disease_dict['cure_department']
,cure_way=disease_dict['cure_way'] , cured_prob=disease_dict['cured_prob'])
self.g.create(node)
count += 1
print(count)
return
'''创建知识图谱实体节点类型schema'''
def create_graphnodes(self):
Drugs, Foods, Checks, Departments, Producers, Symptoms, Diseases, disease_infos,rels_check, rels_recommandeat, rels_noteat, rels_doeat, rels_department, rels_commonddrug, rels_drug_producer, rels_recommanddrug,rels_symptom, rels_acompany, rels_category = self.read_nodes()
self.create_diseases_nodes(disease_infos)
self.create_node('Drug', Drugs)
print(len(Drugs))
self.create_node('Food', Foods)
print(len(Foods))
self.create_node('Check', Checks)
print(len(Checks))
self.create_node('Department', Departments)
print(len(Departments))
self.create_node('Producer', Producers)
print(len(Producers))
self.create_node('Symptom', Symptoms)
return
'''创建实体关系边'''
def create_graphrels(self):
Drugs, Foods, Checks, Departments, Producers, Symptoms, Diseases, disease_infos, rels_check, rels_recommandeat, rels_noteat, rels_doeat, rels_department, rels_commonddrug, rels_drug_producer, rels_recommanddrug,rels_symptom, rels_acompany, rels_category = self.read_nodes()
self.create_relationship('Disease', 'Food', rels_recommandeat, 'recommand_eat', '推荐食谱')
self.create_relationship('Disease', 'Food', rels_noteat, 'no_eat', '忌吃')
self.create_relationship('Disease', 'Food', rels_doeat, 'do_eat', '宜吃')
self.create_relationship('Department', 'Department', rels_department, 'belongs_to', '属于')
self.create_relationship('Disease', 'Drug', rels_commonddrug, 'common_drug', '常用药品')
self.create_relationship('Producer', 'Drug', rels_drug_producer, 'drugs_of', '生产药品')
self.create_relationship('Disease', 'Drug', rels_recommanddrug, 'recommand_drug', '好评药品')
self.create_relationship('Disease', 'Check', rels_check, 'need_check', '诊断检查')
self.create_relationship('Disease', 'Symptom', rels_symptom, 'has_symptom', '症状')
self.create_relationship('Disease', 'Disease', rels_acompany, 'acompany_with', '并发症')
self.create_relationship('Disease', 'Department', rels_category, 'belongs_to', '所属科室')
'''创建实体关联边'''
def create_relationship(self, start_node, end_node, edges, rel_type, rel_name):
count = 0
# 去重处理
set_edges = []
for edge in edges:
set_edges.append('###'.join(edge))
all = len(set(set_edges))
for edge in set(set_edges):
edge = edge.split('###')
p = edge[0]
q = edge[1]
query = "match(p:%s),(q:%s) where p.name='%s'and q.name='%s' create (p)-[rel:%s{name:'%s'}]->(q)" % (
start_node, end_node, p, q, rel_type, rel_name)
try:
self.g.run(query)
count += 1
print(rel_type, count, all)
except Exception as e:
print(e)
return
'''导出数据'''
def export_data(self):
Drugs, Foods, Checks, Departments, Producers, Symptoms, Diseases, disease_infos, rels_check, rels_recommandeat, rels_noteat, rels_doeat, rels_department, rels_commonddrug, rels_drug_producer, rels_recommanddrug, rels_symptom, rels_acompany, rels_category = self.read_nodes()
f_drug = open('drug.txt', 'w+')
f_food = open('food.txt', 'w+')
f_check = open('check.txt', 'w+')
f_department = open('department.txt', 'w+')
f_producer = open('producer.txt', 'w+')
f_symptom = open('symptoms.txt', 'w+')
f_disease = open('disease.txt', 'w+')
f_drug.write('\n'.join(list(Drugs)))
f_food.write('\n'.join(list(Foods)))
f_check.write('\n'.join(list(Checks)))
f_department.write('\n'.join(list(Departments)))
f_producer.write('\n'.join(list(Producers)))
f_symptom.write('\n'.join(list(Symptoms)))
f_disease.write('\n'.join(list(Diseases)))
f_drug.close()
f_food.close()
f_check.close()
f_department.close()
f_producer.close()
f_symptom.close()
f_disease.close()
return
if __name__ == '__main__':
handler = MedicalGraph()
print("step1:导入图谱节点中")
handler.create_graphnodes()
print("step2:导入图谱边中")
handler.create_graphrels()
以及其对图数据库的使用逻辑:
# ccoding = utf-8
import os
from question_classifier import *
from question_parser import *
from llm_server import *
from build_medicalgraph import *
import re
entity_parser = QuestionClassifier()
kg = MedicalGraph()
model = ModelAPI(MODEL_URL="http://你的IP:3001/generate")
class KGRAG():
def __init__(self):
self.cn_dict = {
"name":"名称",
"desc":"疾病简介",
"cause":"疾病病因",
"prevent":"预防措施",
"cure_department":"治疗科室",
"cure_lasttime":"治疗周期",
"cure_way":"治疗方式",
"cured_prob":"治愈概率",
"easy_get":"易感人群",
"belongs_to":"所属科室",
"common_drug":"常用药品",
"do_eat":"宜吃",
"drugs_of":"生产药品",
"need_check":"诊断检查",
"no_eat":"忌吃",
"recommand_drug":"好评药品",
"recommand_eat":"推荐食谱",
"has_symptom":"症状",
"acompany_with":"并发症",
"Check":"诊断检查项目",
"Department":"医疗科目",
"Disease":"疾病",
"Drug":"药品",
"Food":"食物",
"Producer":"在售药品",
"Symptom":"疾病症状"
}
self.entity_rel_dict = {
"check":["name", 'need_check'],
"department":["name", 'belongs_to'],
"disease":["prevent", "cure_way", "name", "cure_lasttime", "cured_prob", "cause", "cure_department", "desc", "easy_get", 'recommand_eat', 'no_eat', 'do_eat', "common_drug", 'drugs_of', 'recommand_drug', 'need_check', 'has_symptom', 'acompany_with', 'belongs_to'],
"drug":["name", "common_drug", 'drugs_of', 'recommand_drug'],
"food":["name"],
"producer":["name"],
"symptom":["name", 'has_symptom'],
}
return
def entity_linking(self, query):
return entity_parser.check_medical(query)
def link_entity_rel(self, query, entity, entity_type):
cate = [self.cn_dict.get(i) for i in self.entity_rel_dict.get(entity_type)]
prompt = "请判定问题:{query}所提及的是{entity}的哪几个信息,请从{cate}中进行选择,并以列表形式返回。".format(query=query, entity=entity, cate=cate)
answer, history = model.chat(query=prompt, history=[])
cls_rel = set([i for i in re.split(r"[\[。、, ;'\]]", answer)]).intersection(set(cate))
print([prompt, answer, cls_rel])
return cls_rel
def recall_facts(self, cls_rel, entity_type, entity_name, depth=1):
entity_dict = {
"check":"Check",
"department":"Department",
"disease":"Disease",
"drug":"Drug",
"food":"Food",
"producer":"Producer",
"symptom":"Symptom"
}
# "MATCH p=(m:Disease)-[r*..2]-(n) where m.name = '耳聋' return p "
sql = "MATCH p=(m:{entity_type})-[r*..{depth}]-(n) where m.name = '{entity_name}' return p".format(depth=depth, entity_type=entity_dict.get(entity_type), entity_name=entity_name)
print(sql)
ress = kg.g.run(sql).data()
triples = set()
for res in ress:
p_data = res["p"]
nodes = p_data.nodes
rels = p_data.relationships
for node in nodes:
node_name = node["name"]
for k,v in node.items():
# print(k)
if v == node_name:
continue
if self.cn_dict[k] not in cls_rel:
continue
triples.add("<" + ','.join([str(node_name), str(self.cn_dict[k]), str(v)]) + ">")
for rel in rels:
if rel.start_node["name"] == rel.end_node["name"]:
continue
# print(rel["name"])
if rel["name"] not in cls_rel:
continue
triples.add("<" + ','.join([str(rel.start_node["name"]), str(rel["name"]), str(rel.end_node["name"])]) + ">")
print(len(triples), list(triples)[:3])
return list(triples)
def format_prompt(self, query, context):
prompt = "这是一个关于医疗领域的问题。给定以下知识三元组集合,三元组形式为<subject, relation, object>,表示subject和object之间存在relation关系" \
"请先从这些三元组集合中找到能够支撑问题的部分,在这里叫做证据,并基于此回答问题。如果没有找到,那么直接回答没有找到证据,回答不知道,如果找到了,请先回答证据的内容,然后在给出最终答案" \
"知识三元组集合为:" + str(context) + "\n问题是:" + query + "\n请回答:"
return prompt
def chat(self, query):
"{'耳聋': ['disease', 'symptom']}"
print("step1: linking entity.....")
entity_dict = self.entity_linking(query)
depth = 1
facts = list()
answer = ""
default = "抱歉,我在知识库中没有找到对应的实体,无法回答。"
if not entity_dict:
print("no entity founded...finished...")
return default
print("step2:recall kg facts....")
for entity_name, types in entity_dict.items():
for entity_type in types:
rels = self.link_entity_rel(query, entity_name, entity_type)
entity_triples = self.recall_facts(rels, entity_type, entity_name, depth)
facts += entity_triples
fact_prompt = self.format_prompt(query, facts)
print("step3:generate answer...")
answer = model.chat(query=fact_prompt, history=[])
return answer
if __name__ == "__main__":
chatbot = KGRAG()
while 1:
query = input("USER:").strip()
answer = chatbot.chat(query)
print("KGRAG_BOT:", answer)