在线问诊 Python、FastAPI、Neo4j — 提供接口服务

发布时间 2023-09-26 11:50:30作者: VipSoft

采用 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 包中
image

QuestionClassifier 中的 路径获取方式进行修改 ../dic/xxxx 替换为 dic/xxx
image

接口路由层

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": "请问最近看东西有时候清楚有时候不清楚是怎么回事"}

返回值:
"可能是:干眼"
image

image
image

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

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