chat.py 992 B

12345678910111213141516171819202122232425262728
  1. from fastapi import FastAPI, Depends, HTTPException
  2. from api import swl_jwt
  3. from grpc_m.proto import vector_service_pb2, vector_service_pb2_grpc
  4. import grpc
  5. app = FastAPI()
  6. # gRPC 通道设置
  7. channel = grpc.insecure_channel('localhost:18600')
  8. stub = vector_service_pb2_grpc.VectorServiceStub(channel)
  9. @app.post("/chat")
  10. async def chat(user_id: str, doc_id: str, message: str, token: str = Depends(swl_jwt.get_token)):
  11. # 鉴权逻辑(根据实际情况进行修改)
  12. # ...
  13. # 调用 LangChain 微服务进行对话处理
  14. response = stub.ChatWithDoc(vector_service_pb2.ChatWithDocRequest(
  15. user_id=user_id,
  16. doc_id=doc_id,
  17. message=message
  18. ))
  19. # 处理响应结果并返回给前端用户
  20. if response.status == vector_service_pb2.ErrorCode.SUCCESS:
  21. return {"reply": response.reply}
  22. else:
  23. raise HTTPException(status_code=500, detail="Internal server error")