From 9f5591639efdad29f5d6f94fded93d69a24330bf Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 13 Jun 2023 20:41:58 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(chat.py):=20handle=20excepti?= =?UTF-8?q?ons=20while=20building=20the=20flow=20and=20add=20a=20final=20r?= =?UTF-8?q?esponse=20to=20the=20event=20stream=20The=20code=20now=20handle?= =?UTF-8?q?s=20exceptions=20that=20may=20occur=20while=20building=20the=20?= =?UTF-8?q?flow.=20A=20final=20response=20is=20added=20to=20the=20event=20?= =?UTF-8?q?stream=20to=20indicate=20the=20end=20of=20the=20stream.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/v1/chat.py | 78 +++++++++++++++-------------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/src/backend/langflow/api/v1/chat.py b/src/backend/langflow/api/v1/chat.py index 04b93bd9e..a8f6a2101 100644 --- a/src/backend/langflow/api/v1/chat.py +++ b/src/backend/langflow/api/v1/chat.py @@ -69,44 +69,48 @@ async def stream_build(flow_id: str): """Stream the build process based on stored flow data.""" async def event_stream(flow_id): - if flow_id not in flow_data_store: - error_message = "Invalid session ID" - yield f"data: {json.dumps({'error': error_message})}\n\n" - return - - graph_data = flow_data_store[flow_id].get("data") - - if not graph_data: - error_message = "No data provided" - yield f"data: {json.dumps({'error': error_message})}\n\n" - return - - 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 {params[:50]}{'...' if len(params) > 50 else ''}" - ) - except Exception as exc: - params = str(exc) - valid = False - - response = json.dumps( - { - "valid": valid, - "params": params, - "id": node.id, - } - ) - yield f"data: {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 + try: + if flow_id not in flow_data_store: + error_message = "Invalid session ID" + yield f"data: {json.dumps({'error': error_message})}\n\n" + return + + graph_data = flow_data_store[flow_id].get("data") + + if not graph_data: + error_message = "No data provided" + yield f"data: {json.dumps({'error': error_message})}\n\n" + return + + 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 {params[:50]}{'...' if len(params) > 50 else ''}" + ) + except Exception as exc: + params = str(exc) + valid = False + + response = json.dumps( + { + "valid": valid, + "params": params, + "id": node.id, + } + ) + yield f"data: {response}\n\n" + + chat_manager.set_cache(flow_id, graph.build()) + except Exception: + logger.error("Error while building the flow") + finally: + yield f"data: {final_response}\n\n" try: return StreamingResponse(event_stream(flow_id), media_type="text/event-stream")