✨ 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:
parent
806d815a10
commit
baeadf018d
1 changed files with 27 additions and 22 deletions
|
|
@ -1,7 +1,6 @@
|
||||||
import json
|
import json
|
||||||
from fastapi import (
|
from fastapi import (
|
||||||
APIRouter,
|
APIRouter,
|
||||||
HTTPException,
|
|
||||||
WebSocket,
|
WebSocket,
|
||||||
WebSocketDisconnect,
|
WebSocketDisconnect,
|
||||||
WebSocketException,
|
WebSocketException,
|
||||||
|
|
@ -63,29 +62,35 @@ 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 in graph.generator_build():
|
||||||
for node_repr, node_id 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 ''}"
|
||||||
)
|
)
|
||||||
response = json.dumps(
|
except Exception as exc:
|
||||||
{
|
params = str(exc)
|
||||||
"valid": True,
|
valid = False
|
||||||
"params": node_repr,
|
|
||||||
"id": node_id,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
yield f"data: {response}\n\n" # SSE format
|
|
||||||
|
|
||||||
chat_manager.set_cache(flow_id, graph.build())
|
response = json.dumps(
|
||||||
final_response = json.dumps({"end_of_stream": True})
|
{
|
||||||
yield f"data: {final_response}\n\n" # SSE format
|
"valid": valid,
|
||||||
|
"params": params,
|
||||||
|
"id": node.id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
yield f"data: {response}\n\n" # SSE format
|
||||||
|
|
||||||
except Exception as exc:
|
chat_manager.set_cache(flow_id, graph.build())
|
||||||
logger.exception(exc)
|
final_response = json.dumps({"end_of_stream": True})
|
||||||
error_response = json.dumps({"valid": False, "params": str(exc)})
|
yield f"data: {final_response}\n\n" # SSE format
|
||||||
yield f"data: {error_response}\n\n" # SSE format
|
|
||||||
|
|
||||||
return StreamingResponse(event_stream(flow_id), media_type="text/event-stream")
|
try:
|
||||||
|
return StreamingResponse(event_stream(flow_id), media_type="text/event-stream")
|
||||||
|
except Exception as exc:
|
||||||
|
logger.error(exc)
|
||||||
|
return JSONResponse(content={"error": str(exc)})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue