線上問診 Python、FastAPI、Neo4j — 問題諮詢

VipSoft發表於2023-10-10


透過節點關係,找出對應的節點,獲取節點屬性值,並拼接成想要的結果。

接上節生成的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

相關文章