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