在线问诊 Python、FastAPI、Neo4j — 问题咨询


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

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

image

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

posted @ 2023-10-10 12:37  VipSoft  阅读(488)  评论(0编辑  收藏  举报