LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
feat: Minor log updates in token usage related code (#654)
Chandrasekharan M825d63270ab9
diff --git a/backend/usage/helper.py b/backend/usage/helper.pyindex 0bfab7556..fd217cd6f 100644--- a/backend/usage/helper.py+++ b/backend/usage/helper.py@@ -39,7 +39,7 @@ class UsageHelper:cost_in_dollars=Sum(UsageKeys.COST_IN_DOLLARS),)- logger.info(f"Token counts aggregated successfully for run_id: {run_id}")+ logger.debug(f"Token counts aggregated successfully for run_id: {run_id}")# Prepare the result dictionary with None as the default valueresult = {@@ -61,4 +61,4 @@ class UsageHelper:except Exception as e:# Handle any other exceptions that might occur during the executionlogger.error(f"An unexpected error occurred for run_id {run_id}: {str(e)}")- raise APIException("An unexpected error occurred")+ raise APIException("Error while aggregating token counts")diff --git a/backend/usage/views.py b/backend/usage/views.pyindex 1fcebfaa0..66438fd4e 100644--- a/backend/usage/views.py+++ b/backend/usage/views.py@@ -3,7 +3,6 @@ import loggingfrom django.http import HttpRequestfrom rest_framework import status, viewsetsfrom rest_framework.decorators import action-from rest_framework.exceptions import APIException, ValidationErrorfrom rest_framework.response import Responsefrom .constants import UsageKeys@@ -31,33 +30,19 @@ class UsageView(viewsets.ModelViewSet):Response: A Response object containing the aggregated token usage datawith HTTP 200 OK status if successful, or an error message andappropriate HTTP status if an error occurs.-- Raises:- ValidationError: If the 'run_id' query parameter is missing or invalid.- APIException: If an unexpected error occurs during the execution."""- try:- # Validate the query parameters using the serializer- # This ensures that 'run_id' is present and valid- serializer = GetUsageSerializer(data=self.request.query_params)- serializer.is_valid(raise_exception=True)- run_id = serializer.validated_data.get(UsageKeys.RUN_ID)-- # Retrieve aggregated token count for the given run_id.- result: dict = UsageHelper.get_aggregated_token_count(run_id=run_id)-- # Log the successful completion of the operation- logger.info(f"Token usage retrieved successfully for run_id: {run_id}")-- # Return the result- return Response(status=status.HTTP_200_OK, data=result)- except ValidationError as e:- # Handle validation errors specifically- logger.error(f"Validation error: {e.detail}")- raise ValidationError(detail=f"Validation error: {str(e)}")- except Exception as e:- # Handle any other exceptions that might occur during the execution- error_msg = "An unexpected error occurred while fetching the token usage"- logger.error(f"{error_msg}: {e}")- raise APIException(detail=error_msg)+ # Validate the query parameters using the serializer+ # This ensures that 'run_id' is present and valid+ serializer = GetUsageSerializer(data=self.request.query_params)+ serializer.is_valid(raise_exception=True)+ run_id = serializer.validated_data.get(UsageKeys.RUN_ID)++ # Retrieve aggregated token count for the given run_id.+ result: dict = UsageHelper.get_aggregated_token_count(run_id=run_id)++ # Log the successful completion of the operation+ logger.debug(f"Token usage retrieved successfully for run_id: {run_id}")++ # Return the result+ return Response(status=status.HTTP_200_OK, data=result)diff --git a/backend/usage_v2/helper.py b/backend/usage_v2/helper.pyindex 0bfab7556..20182fc3d 100644--- a/backend/usage_v2/helper.py+++ b/backend/usage_v2/helper.py@@ -39,7 +39,7 @@ class UsageHelper:cost_in_dollars=Sum(UsageKeys.COST_IN_DOLLARS),)- logger.info(f"Token counts aggregated successfully for run_id: {run_id}")+ logger.debug(f"Token counts aggregated successfully for run_id: {run_id}")# Prepare the result dictionary with None as the default valueresult = {diff --git a/backend/usage_v2/views.py b/backend/usage_v2/views.pyindex 1fcebfaa0..ce964abb8 100644--- a/backend/usage_v2/views.py+++ b/backend/usage_v2/views.py@@ -3,7 +3,6 @@ import loggingfrom django.http import HttpRequestfrom rest_framework import status, viewsetsfrom rest_framework.decorators import action-from rest_framework.exceptions import APIException, ValidationErrorfrom rest_framework.response import Responsefrom .constants import UsageKeys@@ -31,33 +30,19 @@ class UsageView(viewsets.ModelViewSet):Response: A Response object containing the aggregated token usage datawith HTTP 200 OK status if successful, or an error message andappropriate HTTP status if an error occurs.-- Raises:- ValidationError: If the 'run_id' query parameter is missing or invalid.- APIException: If an unexpected error occurs during the execution."""- try:- # Validate the query parameters using the serializer- # This ensures that 'run_id' is present and valid- serializer = GetUsageSerializer(data=self.request.query_params)- serializer.is_valid(raise_exception=True)- run_id = serializer.validated_data.get(UsageKeys.RUN_ID)-- # Retrieve aggregated token count for the given run_id.- result: dict = UsageHelper.get_aggregated_token_count(run_id=run_id)-- # Log the successful completion of the operation- logger.info(f"Token usage retrieved successfully for run_id: {run_id}")-- # Return the result- return Response(status=status.HTTP_200_OK, data=result)- except ValidationError as e:- # Handle validation errors specifically- logger.error(f"Validation error: {e.detail}")- raise ValidationError(detail=f"Validation error: {str(e)}")- except Exception as e:- # Handle any other exceptions that might occur during the execution- error_msg = "An unexpected error occurred while fetching the token usage"- logger.error(f"{error_msg}: {e}")- raise APIException(detail=error_msg)+ # Validate the query parameters using the serializer+ # This ensures that 'run_id' is present and valid+ serializer = GetUsageSerializer(data=self.request.query_params)+ serializer.is_valid(raise_exception=True)+ run_id = serializer.validated_data.get(UsageKeys.RUN_ID)++ # Retrieve aggregated token count for the given run_id.+ result: dict = UsageHelper.get_aggregated_token_count(run_id=run_id)++ # Log the successful completion of the operation+ logger.info(f"Token usage retrieved successfully for run_id: {run_id}")++ # Return the result+ return Response(status=status.HTTP_200_OK, data=result)diff --git a/backend/workflow_manager/workflow/execution.py b/backend/workflow_manager/workflow/execution.pyindex 764f3ed4f..ef6a6a7c1 100644--- a/backend/workflow_manager/workflow/execution.py+++ b/backend/workflow_manager/workflow/execution.py@@ -244,7 +244,7 @@ class WorkflowExecutionServiceHelper(WorkflowExecutionService):end_time = time.time()execution_time = end_time - start_timemessage = str(exception)[:EXECUTION_ERROR_LENGTH]- logger.info(+ logger.error(f"Execution {self.execution_id} ran for {execution_time:.4f}s, "f" Error {exception}")diff --git a/backend/workflow_manager/workflow_v2/execution.py b/backend/workflow_manager/workflow_v2/execution.pyindex 58ada880f..84e2b51dc 100644--- a/backend/workflow_manager/workflow_v2/execution.py+++ b/backend/workflow_manager/workflow_v2/execution.py@@ -239,15 +239,14 @@ class WorkflowExecutionServiceHelper(WorkflowExecutionService):execution_time = end_time - start_timelogger.info(f"Execution {self.execution_id} stopped")raise exception- except Exception as exception:+ except Exception as exc:end_time = time.time()execution_time = end_time - start_time- message = str(exception)[:EXECUTION_ERROR_LENGTH]- logger.info(- f"Execution {self.execution_id} ran for {execution_time:.4f}s, "- f" Error {exception}"+ message = str(exc)[:EXECUTION_ERROR_LENGTH]+ logger.error(+ f"Execution {self.execution_id} ran for {execution_time:.4f}s, {exc}")- raise WorkflowExecutionError(message) from exception+ raise WorkflowExecutionError(message) from excdef publish_initial_workflow_logs(self, total_files: int) -> None:"""Publishes the initial logs for the workflow.diff --git a/prompt-service/src/unstract/prompt_service/main.py b/prompt-service/src/unstract/prompt_service/main.pyindex ed447dd56..9e1885ed1 100644--- a/prompt-service/src/unstract/prompt_service/main.py+++ b/prompt-service/src/unstract/prompt_service/main.py@@ -819,7 +819,10 @@ def _retrieve_context(output, doc_id, vector_index, answer) -> str:if node.score > 0:text += node.get_content() + "\f\n"else:- app.logger.info("Node score is less than 0. " f"Ignored: {node.score}")+ app.logger.info(+ "Node score is less than 0. "+ f"Ignored: {node.node_id} with score {node.score}"+ )return text