From 34d578b813b7ef9822f125e7d1163aa96227c23a Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 7 Mar 2024 16:55:29 -0300 Subject: [PATCH] Refactor build_vertex function and improve code readability --- src/backend/langflow/api/v1/chat.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/backend/langflow/api/v1/chat.py b/src/backend/langflow/api/v1/chat.py index ecec8f200..208007394 100644 --- a/src/backend/langflow/api/v1/chat.py +++ b/src/backend/langflow/api/v1/chat.py @@ -93,7 +93,7 @@ async def build_vertex( current_user=Depends(get_current_active_user), ): """Build a vertex instead of the entire graph.""" - {"inputs": {"input_value": "some value"}} + start_time = time.perf_counter() next_vertices_ids = [] try: @@ -101,8 +101,12 @@ async def build_vertex( cache = chat_service.get_cache(flow_id) if not cache: # If there's no cache - logger.warning(f"No cache found for {flow_id}. Building graph starting at {vertex_id}") - graph = build_and_cache_graph(flow_id=flow_id, session=next(get_session()), chat_service=chat_service) + logger.warning( + f"No cache found for {flow_id}. Building graph starting at {vertex_id}" + ) + graph = build_and_cache_graph( + flow_id=flow_id, session=next(get_session()), chat_service=chat_service + ) else: graph = cache.get("result") result_data_response = ResultDataResponse(results={}) @@ -122,7 +126,9 @@ async def build_vertex( else: raise ValueError(f"No result found for vertex {vertex_id}") next_vertices_ids = vertex.successors_ids - next_vertices_ids = [v for v in next_vertices_ids if graph.should_run_vertex(v)] + next_vertices_ids = [ + v for v in next_vertices_ids if graph.should_run_vertex(v) + ] result_data_response = ResultDataResponse(**result_dict.model_dump()) @@ -205,7 +211,9 @@ async def build_vertex_stream( else: graph = cache.get("result") else: - session_data = await session_service.load_session(session_id, flow_id=flow_id) + session_data = await session_service.load_session( + session_id, flow_id=flow_id + ) graph, artifacts = session_data if session_data else (None, None) if not graph: raise ValueError(f"No graph found for {flow_id}.")