From 0f31e5517ecb4ead59f77ea401fe5441365066d3 Mon Sep 17 00:00:00 2001 From: ogabrielluiz Date: Wed, 12 Jun 2024 17:35:39 -0300 Subject: [PATCH] refactor: Update build_vertex function to handle logs and result data response Refactor the build_vertex function in chat.py to handle logs and result data response more efficiently. Instead of using a conditional statement to check the type of vertex.artifacts_raw, the code now directly assigns the logs variable based on the type. Additionally, the logs variable is only assigned to result_data_response if it is not empty. This change improves the clarity and maintainability of the code. --- src/backend/base/langflow/api/v1/chat.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/backend/base/langflow/api/v1/chat.py b/src/backend/base/langflow/api/v1/chat.py index 35d1f4406..ad4b222da 100644 --- a/src/backend/base/langflow/api/v1/chat.py +++ b/src/backend/base/langflow/api/v1/chat.py @@ -149,6 +149,7 @@ async def build_vertex( next_runnable_vertices = [] top_level_vertices = [] + logs = {} try: start_time = time.perf_counter() cache = await chat_service.get_cache(flow_id_str) @@ -180,21 +181,20 @@ async def build_vertex( inputs_dict=inputs.model_dump() if inputs else {}, files=files, ) - if isinstance(vertex.artifacts_raw, dict): - logs = {} - for key in vertex.artifacts_raw: - log_obj = Log(message=vertex.artifacts_raw[key], type=vertex.artifacts_type[key]) - logs[key] = log_obj - else: - logs = [Log(message=vertex.artifacts_raw, type=vertex.artifacts_type)] - - result_data_response = ResultDataResponse(**result_dict.model_dump()) + # logs = {} + # if isinstance(vertex.artifacts_raw, dict): + # for key in vertex.artifacts_raw: + # log_obj = Log(message=vertex.artifacts_raw[key], type=vertex.artifacts_type[key]) + # logs[key] = log_obj + # else: + # logs = [Log(message=vertex.artifacts_raw, type=vertex.artifacts_type)] + result_data_response = ResultDataResponse.model_validate(result_dict, from_attributes=True) except Exception as exc: logger.exception(f"Error building vertex: {exc}") params = format_exception_message(exc) valid = False - logs = [Log(message=params, type="error")] + logs = {vertex.outputs[0]["name"]: [Log(message=params, type="error")]} result_data_response = ResultDataResponse(results={}) artifacts = {} # If there's an error building the vertex @@ -202,7 +202,8 @@ async def build_vertex( await chat_service.clear_cache(flow_id_str) result_data_response.message = artifacts - result_data_response.logs = logs + if logs: + result_data_response.logs = logs # Log the vertex build if not vertex.will_stream: