在线问诊 Python、FastAPI、Neo4j — 提供咨询接口服务
采用 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.py
、question_classifier.py
、question_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": "请问最近看东西有时候清楚有时候不清楚是怎么回事"}
返回值:
"可能是:干眼"
本文来自博客园,作者:VipSoft 转载请注明原文链接:https://www.cnblogs.com/vipsoft/p/17729756.html