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.
This commit is contained in:
ogabrielluiz 2024-06-12 17:35:39 -03:00
commit 0f31e5517e

View file

@ -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: