Skip to content

Commit 683b2cd

Browse files
authored
fix: [Application] Tokens consumed by AI dialogue nodes within a loop are not counted in the overall dialogue statistics. (#4626)
1 parent 632c12d commit 683b2cd

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

apps/application/flow/step_node/loop_node/impl/base_loop_node.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,17 @@ def loop(workflow_manage_new_instance, node: INode, generate_loop):
210210
node.context['run_time'] = time.time() - node.context.get("start_time")
211211

212212

213+
def get_tokens(loop_node_data):
214+
message_tokens = 0
215+
answer_tokens = 0
216+
for details in loop_node_data:
217+
message_tokens += sum([row.get('message_tokens') for row in details.values() if
218+
'message_tokens' in row and row.get('message_tokens') is not None])
219+
answer_tokens += sum([row.get('answer_tokens') for row in details.values() if
220+
'answer_tokens' in row and row.get('answer_tokens') is not None])
221+
return {'message_tokens': message_tokens, 'answer_tokens': answer_tokens}
222+
223+
213224
def get_write_context(loop_type, array, number, loop_body):
214225
def inner_write_context(node_variable: Dict, workflow_variable: Dict, node: INode, workflow):
215226
if loop_type == 'ARRAY':
@@ -286,7 +297,7 @@ def get_loop_context_data(self):
286297
self.context.get(f.get('value')) is not None}
287298

288299
def get_details(self, index: int, **kwargs):
289-
300+
tokens = get_tokens(self.context.get("loop_node_data"))
290301
return {
291302
'name': self.node.properties.get('stepName'),
292303
"index": index,
@@ -305,4 +316,6 @@ def get_details(self, index: int, **kwargs):
305316
'loop_answer_data': self.context.get("loop_answer_data"),
306317
'err_message': self.err_message,
307318
'enableException': self.node.properties.get('enableException'),
319+
'message_tokens': tokens.get('message_tokens') or 0,
320+
'answer_tokens': tokens.get('answer_tokens') or 0,
308321
}

0 commit comments

Comments
 (0)