線上問診 Python、FastAPI、Neo4j — 提供諮詢介面服務

2023-10-11 15:02:00

採用 Fast API 搭建服務介面: https://www.cnblogs.com/vipsoft/p/17684079.html
Fast API 檔案:https://fastapi.tiangolo.com/zh/

構建服務層

qa_service.py

from service.question_classifier import *
from service.question_parser import *
from service.answer_search import *


class QAService:
    def __init__(self):
        self.classifier = QuestionClassifier()
        self.parser = QuestionPaser()
        self.searcher = AnswerSearcher()

    def chat_main(self, sent):
        answer = '您的問題,我還沒有學習到。祝您身體健康!'
        res_classify = self.classifier.classify(sent)
        if not res_classify:
            return answer
        res_sql = self.parser.parser_main(res_classify)
        final_answers = self.searcher.search_main(res_sql)
        if not final_answers:
            return answer
        else:
            return '\n'.join(final_answers)

同時將 answer_search.pyquestion_classifier.pyquestion_parser.py 從test 目錄中,移到 service 包中

QuestionClassifier 中的 路徑獲取方式進行修改 ../dic/xxxx 替換為 dic/xxx

介面路由層

FastAPI 請求體:https://fastapi.tiangolo.com/zh/tutorial/body/
建立路由介面檔案
qa_router.py

#!/usr/bin/python3

import logging
from fastapi import APIRouter, status
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from service.qa_service import QAService
import json

router = APIRouter()
qa = QAService() #實類化 QAService 服務


class Item(BaseModel):
    name: str = None
    question: str


@router.post("/consult")
async def get_search(param: Item):
    answer = qa.chat_main(param.question)
    return JSONResponse(content=answer, status_code=status.HTTP_200_OK)

PostMan 呼叫

URL: http://127.0.0.1:8000/api/qa/consult

{"question": "請問最近看東西有時候清楚有時候不清楚是怎麼回事"}

返回值:
"可能是:乾眼"


源代:https://gitee.com/VipSoft/VipQA

參考:https://github.com/liuhuanyong/QASystemOnMedicalKG