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:
parent
dc37f279e0
commit
0f31e5517e
1 changed files with 12 additions and 11 deletions
|
|
@ -149,6 +149,7 @@ async def build_vertex(
|
||||||
|
|
||||||
next_runnable_vertices = []
|
next_runnable_vertices = []
|
||||||
top_level_vertices = []
|
top_level_vertices = []
|
||||||
|
logs = {}
|
||||||
try:
|
try:
|
||||||
start_time = time.perf_counter()
|
start_time = time.perf_counter()
|
||||||
cache = await chat_service.get_cache(flow_id_str)
|
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 {},
|
inputs_dict=inputs.model_dump() if inputs else {},
|
||||||
files=files,
|
files=files,
|
||||||
)
|
)
|
||||||
if isinstance(vertex.artifacts_raw, dict):
|
# logs = {}
|
||||||
logs = {}
|
# if isinstance(vertex.artifacts_raw, dict):
|
||||||
for key in vertex.artifacts_raw:
|
# for key in vertex.artifacts_raw:
|
||||||
log_obj = Log(message=vertex.artifacts_raw[key], type=vertex.artifacts_type[key])
|
# log_obj = Log(message=vertex.artifacts_raw[key], type=vertex.artifacts_type[key])
|
||||||
logs[key] = log_obj
|
# logs[key] = log_obj
|
||||||
else:
|
# else:
|
||||||
logs = [Log(message=vertex.artifacts_raw, type=vertex.artifacts_type)]
|
# logs = [Log(message=vertex.artifacts_raw, type=vertex.artifacts_type)]
|
||||||
|
|
||||||
result_data_response = ResultDataResponse(**result_dict.model_dump())
|
|
||||||
|
|
||||||
|
result_data_response = ResultDataResponse.model_validate(result_dict, from_attributes=True)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.exception(f"Error building vertex: {exc}")
|
logger.exception(f"Error building vertex: {exc}")
|
||||||
params = format_exception_message(exc)
|
params = format_exception_message(exc)
|
||||||
valid = False
|
valid = False
|
||||||
logs = [Log(message=params, type="error")]
|
logs = {vertex.outputs[0]["name"]: [Log(message=params, type="error")]}
|
||||||
result_data_response = ResultDataResponse(results={})
|
result_data_response = ResultDataResponse(results={})
|
||||||
artifacts = {}
|
artifacts = {}
|
||||||
# If there's an error building the vertex
|
# If there's an error building the vertex
|
||||||
|
|
@ -202,7 +202,8 @@ async def build_vertex(
|
||||||
await chat_service.clear_cache(flow_id_str)
|
await chat_service.clear_cache(flow_id_str)
|
||||||
|
|
||||||
result_data_response.message = artifacts
|
result_data_response.message = artifacts
|
||||||
result_data_response.logs = logs
|
if logs:
|
||||||
|
result_data_response.logs = logs
|
||||||
|
|
||||||
# Log the vertex build
|
# Log the vertex build
|
||||||
if not vertex.will_stream:
|
if not vertex.will_stream:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue