From 4f6a4f244c0dd797a2d5c38eaad5752fa20ff78b Mon Sep 17 00:00:00 2001 From: LeanDeR <34670582+auxpd@users.noreply.github.com> Date: Wed, 5 Mar 2025 14:35:08 +0800 Subject: [PATCH] fix(llm/nodes.py): Ensure that the output returns without any exceptions (#14880) --- api/core/workflow/nodes/llm/node.py | 33 ++++++++++++++--------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/api/core/workflow/nodes/llm/node.py b/api/core/workflow/nodes/llm/node.py index f717a2a877..b61b5b5cb5 100644 --- a/api/core/workflow/nodes/llm/node.py +++ b/api/core/workflow/nodes/llm/node.py @@ -191,6 +191,22 @@ class LLMNode(BaseNode[LLMNodeData]): # deduct quota self.deduct_llm_quota(tenant_id=self.tenant_id, model_instance=model_instance, usage=usage) break + outputs = {"text": result_text, "usage": jsonable_encoder(usage), "finish_reason": finish_reason} + + yield RunCompletedEvent( + run_result=NodeRunResult( + status=WorkflowNodeExecutionStatus.SUCCEEDED, + inputs=node_inputs, + process_data=process_data, + outputs=outputs, + metadata={ + NodeRunMetadataKey.TOTAL_TOKENS: usage.total_tokens, + NodeRunMetadataKey.TOTAL_PRICE: usage.total_price, + NodeRunMetadataKey.CURRENCY: usage.currency, + }, + llm_usage=usage, + ) + ) except LLMNodeError as e: yield RunCompletedEvent( run_result=NodeRunResult( @@ -211,23 +227,6 @@ class LLMNode(BaseNode[LLMNodeData]): ) ) - outputs = {"text": result_text, "usage": jsonable_encoder(usage), "finish_reason": finish_reason} - - yield RunCompletedEvent( - run_result=NodeRunResult( - status=WorkflowNodeExecutionStatus.SUCCEEDED, - inputs=node_inputs, - process_data=process_data, - outputs=outputs, - metadata={ - NodeRunMetadataKey.TOTAL_TOKENS: usage.total_tokens, - NodeRunMetadataKey.TOTAL_PRICE: usage.total_price, - NodeRunMetadataKey.CURRENCY: usage.currency, - }, - llm_usage=usage, - ) - ) - def _invoke_llm( self, node_data_model: ModelConfig,