✨ feat(chat.py): improve SSE response by adding node id and valid flag

The changes in this commit handle exceptions that may occur when creating a streaming response. The try-except block ensures that any exceptions are caught and an appropriate response is returned. Additionally, the SSE response has been improved by adding a valid flag and node id to the response. This provides more information to the client about the status of the node build and allows for better error handling.
🐛 fix(chat.py): handle exceptions when creating a streaming response
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-06-12 13:03:46 -03:00
commit baeadf018d

View file

@ -1,7 +1,6 @@
import json import json
from fastapi import ( from fastapi import (
APIRouter, APIRouter,
HTTPException,
WebSocket, WebSocket,
WebSocketDisconnect, WebSocketDisconnect,
WebSocketException, WebSocketException,
@ -63,18 +62,25 @@ async def stream_build(flow_id: str):
yield f"data: {json.dumps({'error': error_message})}\n\n" yield f"data: {json.dumps({'error': error_message})}\n\n"
return return
try:
logger.debug("Building langchain object") logger.debug("Building langchain object")
graph = Graph.from_payload(graph_data) graph = Graph.from_payload(graph_data)
for node_repr, node_id in graph.generator_build(): for node in graph.generator_build():
try:
node.build()
params = node._built_object_repr()
valid = True
logger.debug( logger.debug(
f"Building node {node_repr[:50]}{'...' if len(node_repr) > 50 else ''}" f"Building node {params[:50]}{'...' if len(params) > 50 else ''}"
) )
except Exception as exc:
params = str(exc)
valid = False
response = json.dumps( response = json.dumps(
{ {
"valid": True, "valid": valid,
"params": node_repr, "params": params,
"id": node_id, "id": node.id,
} }
) )
yield f"data: {response}\n\n" # SSE format yield f"data: {response}\n\n" # SSE format
@ -83,9 +89,8 @@ async def stream_build(flow_id: str):
final_response = json.dumps({"end_of_stream": True}) final_response = json.dumps({"end_of_stream": True})
yield f"data: {final_response}\n\n" # SSE format yield f"data: {final_response}\n\n" # SSE format
except Exception as exc: try:
logger.exception(exc)
error_response = json.dumps({"valid": False, "params": str(exc)})
yield f"data: {error_response}\n\n" # SSE format
return StreamingResponse(event_stream(flow_id), media_type="text/event-stream") return StreamingResponse(event_stream(flow_id), media_type="text/event-stream")
except Exception as exc:
logger.error(exc)
return JSONResponse(content={"error": str(exc)})