首页 > 编程语言 >在线问诊 Python、FastAPI、Neo4j — 问题反馈

在线问诊 Python、FastAPI、Neo4j — 问题反馈

时间:2023-09-26 11:00:49浏览次数:64  
标签:name Python FastAPI question disease 干眼 answer Neo4j type

目录
通过节点关系,找出对应的节点,获取节点属性值,并拼接成想要的结果。

接上节生成的CQL

# 输入
question_class = {'args': {'看东西有时候清楚有时候不清楚': ['symptom']}, 'question_types': ['symptom_disease']}
# 输出
[{'question_type': 'symptom_disease', 'sql': ["MATCH (m:Disease)-[r:has_symptom]->(n:Symptom) where n.name = '看东西有时候清楚有时候不清楚' return m.name, r.name, n.name"]}]

# 输入:
question_class = {'args': {'干眼': ['disease']}, 'question_types': ['disease_drug']}
# 输出: 
[{'question_type': 'disease_drug', 'sql': ["MATCH (m:Disease)-[r:used_drugs]->(n:Drug) where m.name = '干眼' return m.name, r.name, n.name,n.usage_dosage,n.generic_name,n.contraindications"]}]

# 输入:
question_class = {'args': {'干眼': ['disease']}, 'question_types': ['disease_not_food']}
# 输出:
[{'question_type': 'disease_not_food', 'sql': ["MATCH (m:Disease)-[r:noteat_foods]->(n:Foods) where m.name = '干眼' return m.name, r.name, n.name"]}]

查出节点

def search_main(self, sqls):
    """执行cypher查询,并返回相应结果"""
    final_answers = []
    for sql_ in sqls:
        question_type = sql_['question_type']  # 'question_type': 'symptom_disease'
        queries = sql_['sql']  # 'sql': ["MATCH (m:Disease)-[r:has_symptom]->(n:Symptom) where n.name = '看东西有时候清楚有时候不清楚' return m.name, r.name, n.name"]
        answers = []
        for cql in queries:
            ress = neo4j.execute_query(cql)  # logging.info("%s, %s", record["p"]["name"], record["p"]["generation"])
            answers += ress
        final_answer = self.answer_prettify(question_type, answers)
        if final_answer:
            final_answers.append(final_answer)
    return final_answers

拼接节点属性

def answer_prettify(self, question_type, answers):
    if question_type == 'symptom_disease':
        desc = [i['m.name'] for i in answers]
        subject = answers[0]['n.name']
        final_answer = '{0}可能是:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

    if question_type == 'disease_drug':
        desc = []
        for i in answers:
            desc.append(i['n.name'] + "(" + i['n.generic_name'] + ")" + " 【用法用量】:" + i['n.usage_dosage'] + " 【禁忌】:" + i['n.contraindications'])
        subject = answers[0]['m.name']
        final_answer = '{0}一般可以用:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

    if question_type == 'disease_not_food':
        desc = [i['n.name'] for i in answers]
        subject = answers[0]['m.name']
        final_answer = '{0}忌食的食物包括有:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

测试结果

searcher = AnswerSearcher()
# 根据 症状描述 查出 对应的疾病
sqls = [{'question_type': 'symptom_disease',
         'sql': ["MATCH (m:Disease)-[r:has_symptom]->(n:Symptom) where n.name = '看东西有时候清楚有时候不清楚' return m.name, r.name, n.name"]}]
answer = searcher.search_main(sqls)
print(answer)

# 根据 疾病 查出 常用药品
sqls = [{'question_type': 'disease_drug', 'sql': [
    "MATCH (m:Disease)-[r:used_drugs]->(n:Drug) where m.name = '干眼' return m.name, r.name, n.name,n.usage_dosage,n.generic_name,n.contraindications"]}]
answer = searcher.search_main(sqls)
print(answer)

# 根据 药品 查出 给出饮食建议
sqls = [{'question_type': 'disease_not_food', 'sql': ["MATCH (m:Disease)-[r:noteat_foods]->(n:Foods) where m.name = '干眼' return m.name, r.name, n.name"]}]
answer = searcher.search_main(sqls)
print(answer)
['看东西有时候清楚有时候不清楚可能是:干眼']
['干眼一般可以用:施图伦(七叶洋地黄双苷滴眼液) 【用法用量】:黄斑变性:每日3次,每次1滴,滴入眼结膜囊内(近耳侧外眼角)。\n眼疲劳:每日3次,每次1滴,滴入眼结膜囊内(近耳侧外眼角),延续1周或至病情好转,建议每日2次,每次1滴。 【禁忌】:对制剂中活性成份或其它任一成份过敏者禁用。']
['干眼,下列食物不要吃:芥末;海鲜']

问答演示

if __name__ == '__main__':
    print("VipQA:您好,我是人工智能助理,希望可以帮到您")
    handler = ChatBotGraph()
    while 1:
        question = input('用户:')  # 请问最近看东西有时候清楚有时候不清楚是怎么回事,干眼常用药有哪些,干眼哪些不能吃
        answer = handler.chat_main(question)
        print('VipQA:', answer)
VipQA:您好,我是人工智能助理,希望可以帮到您
model init finished ......
用户:请问最近看东西有时候清楚有时候不清楚是怎么回事
VipQA: 可能是:干眼
用户:干眼常用药有哪些
VipQA: 干眼一般可以用:施图伦(七叶洋地黄双苷滴眼液) 【用法用量】:黄斑变性:每日3次,每次1滴,滴入眼结膜囊内(近耳侧外眼角)。
眼疲劳:每日3次,每次1滴,滴入眼结膜囊内(近耳侧外眼角),延续1周或至病情好转,建议每日2次,每次1滴。 【禁忌】:对制剂中活性成份或其它任一成份过敏者禁用。
用户:干眼哪些不能吃
VipQA: 干眼,下列食物不要吃:海鲜;芥末
用户:

image

源代码地址:https://gitee.com/VipSoft/VipQA

标签:name,Python,FastAPI,question,disease,干眼,answer,Neo4j,type
From: https://www.cnblogs.com/vipsoft/p/17729644.html

相关文章

  • 如何在Python中实现高效的数据处理与分析
    在当今信息爆炸的时代,我们面对的数据量越来越大,如何高效地处理和分析数据成为了一种迫切的需求。Python作为一种强大的编程语言,提供了丰富的数据处理和分析库,帮助我们轻松应对这个挑战。本文将为您介绍如何在Python中实现高效的数据处理与分析,以提升工作效率和数据洞察力。1、数据......
  • Python之html2text:将HTML转换为Markdown文档示例详解
    From: https://mp.weixin.qq.com/s/Pa3NDXOseyg0mIn869mbhQ-----------------------------------------------------------------------------------------hello大家好我是Monday,本文将详细介绍如何使用Python库中的html2text模块来实现将HTML转换为Markdown的操作,并提供示例......
  • 在线问诊 Python、FastAPI、Neo4j — Cypher 生成
    目录构建节点字典构建CypherCQL语句Test这边只是为了测试,演示效果和思路,实际应用中,可以通过NLP构建CQL接上一篇的问题分类question="请问最近看东西有时候清楚有时候不清楚是怎么回事"#最终输出data={'args':{'看东西有时候清楚有时候不清楚':['symptom']},'questio......
  • python读写excel
     pipinstallpandasopenpyxl 读取Excel文件importpandasaspd#读取Excel文件df=pd.read_excel('your_file.xlsx')#查看数据print(df) 写入Excel文件importpandasaspd#创建一个数据框(dataframe)data={'Name':['Tom','Nick&#......
  • 利用Python对当前目录下xlsx文件数据绘制拆线图
    importpandasaspdimportxlwtimportosimportglobimportmatplotlib.pyplotaspltfromopenpyxlimportWorkbookfromopenpyxlimportload_workbookfrommatplotlibimportrcParams ##获取当前路径path=os.getcwd()##获取当前路径下(不包含子目录)的所有xlsx文件zx_w......
  • [888] How to get the directory of the current Python file
    TogetthedirectoryofthecurrentPythonfile,youcanusetheos.pathmoduleincombinationwiththe__file__attribute.Here'showyoucandoit:importos#GetthedirectoryofthecurrentPythonfilecurrent_directory=os.path.dirname(os.pat......
  • # yyds干货盘点 # 盘点一个使用Python自动化处理GPS、北斗经纬度数据实战(下篇)
    大家好,我是皮皮。一、前言上一篇文章我们使用了Python来实现数据的导入和分列处理,最终可以得到符合预期的结果,不过还可以继续深挖优化下,这一篇文章一起来看看吧。优化的背景如下图所示:二、实现过程这里【瑜亮老师】继续给了一个优化指导,如下图所示:并且给出的代码如下:withopen("./G......
  • 盘点一个使用Python自动化处理GPS、北斗经纬度数据实战(下篇)
    大家好,我是皮皮。一、前言上一篇文章我们使用了Python来实现数据的导入和分列处理,最终可以得到符合预期的结果,不过还可以继续深挖优化下,这一篇文章一起来看看吧。优化的背景如下图所示:二、实现过程这里【瑜亮老师】继续给了一个优化指导,如下图所示:并且给出的代码如下:with......
  • Python RuntimeError: dictionary changed size during iteration
    运行下面代码,报如下错误fornameinglobals():print(name) 解决办法是:将待遍历的对象转换成列表fornameinlist(globals()):print(name) ......
  • poython文件运行模式和python控制台运行模式和Juputer运行模式
    今天学pytorch,我知道了python文件是整个代码运行,,而python控制台是一行一行的运行。而Juputer是任意代码块运行的。 ......