From baeadf018d4332744f864b155c503272baa1a13b Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Mon, 12 Jun 2023 13:03:46 -0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(chat.py):=20improve=20SSE=20re?= =?UTF-8?q?sponse=20by=20adding=20node=20id=20and=20valid=20flag=20The=20c?= =?UTF-8?q?hanges=20in=20this=20commit=20handle=20exceptions=20that=20may?= =?UTF-8?q?=20occur=20when=20creating=20a=20streaming=20response.=20The=20?= =?UTF-8?q?try-except=20block=20ensures=20that=20any=20exceptions=20are=20?= =?UTF-8?q?caught=20and=20an=20appropriate=20response=20is=20returned.=20A?= =?UTF-8?q?dditionally,=20the=20SSE=20response=20has=20been=20improved=20b?= =?UTF-8?q?y=20adding=20a=20valid=20flag=20and=20node=20id=20to=20the=20re?= =?UTF-8?q?sponse.=20This=20provides=20more=20information=20to=20the=20cli?= =?UTF-8?q?ent=20about=20the=20status=20of=20the=20node=20build=20and=20al?= =?UTF-8?q?lows=20for=20better=20error=20handling.=20=F0=9F=90=9B=20fix(ch?= =?UTF-8?q?at.py):=20handle=20exceptions=20when=20creating=20a=20streaming?= =?UTF-8?q?=20response?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/v1/chat.py | 49 ++++++++++++++++------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/src/backend/langflow/api/v1/chat.py b/src/backend/langflow/api/v1/chat.py index 1353090d6..4d9d85a58 100644 --- a/src/backend/langflow/api/v1/chat.py +++ b/src/backend/langflow/api/v1/chat.py @@ -1,7 +1,6 @@ import json from fastapi import ( APIRouter, - HTTPException, WebSocket, WebSocketDisconnect, WebSocketException, @@ -63,29 +62,35 @@ async def stream_build(flow_id: str): yield f"data: {json.dumps({'error': error_message})}\n\n" return - try: - logger.debug("Building langchain object") - graph = Graph.from_payload(graph_data) - for node_repr, node_id in graph.generator_build(): + logger.debug("Building langchain object") + graph = Graph.from_payload(graph_data) + for node in graph.generator_build(): + try: + node.build() + params = node._built_object_repr() + valid = True 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( - { - "valid": True, - "params": node_repr, - "id": node_id, - } - ) - yield f"data: {response}\n\n" # SSE format + except Exception as exc: + params = str(exc) + valid = False - chat_manager.set_cache(flow_id, graph.build()) - final_response = json.dumps({"end_of_stream": True}) - yield f"data: {final_response}\n\n" # SSE format + response = json.dumps( + { + "valid": valid, + "params": params, + "id": node.id, + } + ) + yield f"data: {response}\n\n" # SSE format - except Exception as exc: - logger.exception(exc) - error_response = json.dumps({"valid": False, "params": str(exc)}) - yield f"data: {error_response}\n\n" # SSE format + chat_manager.set_cache(flow_id, graph.build()) + final_response = json.dumps({"end_of_stream": True}) + yield f"data: {final_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)})