QAsystem.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #encoding:utf-8
  2. from fastapi import FastAPI
  3. from py2neo import Graph
  4. from chatbot_graph import ChatBotGraph
  5. app = FastAPI()
  6. @app.get("/question")
  7. async def read_item(question: str):
  8. handler = ChatBotGraph()
  9. answer = handler.chat_main(question)
  10. if(answer == "抱歉,我不知道"):
  11. return {
  12. "code": 404,
  13. "data": answer,
  14. "msg": "未找到答案"
  15. }
  16. return {
  17. "code": 200,
  18. "data": answer,
  19. "msg": None
  20. }
  21. if __name__ == '__main__':
  22. # 连接到Neo4j数据库
  23. graph = Graph(
  24. "bolt://127.0.0.1:7687",
  25. # host="127.0.0.1",
  26. # http_port=7474,
  27. # user="neo4j",
  28. # password="123456"
  29. # http_port=7687, # neo4j 服务器监听的端口号
  30. user="neo4j", # 数据库user name,如果没有更改过,应该是neo4j
  31. password="123456")
  32. # 查询实体信息
  33. query = "MATCH (n) RETURN n"
  34. results = graph.run(query)
  35. # 将实体信息写入到TXT文件中
  36. with open("./dict/entity.txt", "w",encoding = 'utf-8') as f:
  37. for record in results:
  38. node = record["n"]
  39. f.write(f"{node['name']}\n")
  40. import uvicorn
  41. uvicorn.run(app=app,
  42. host="0.0.0.0",
  43. port=9998,
  44. workers=1)