Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve merge token logic to account for nested dicts #3825

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/promptflow-tracing/promptflow/tracing/_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,20 @@ def collect_openai_tokens_for_streaming(self, span, inputs, output, is_chat):
tokens = calculator.get_openai_metrics_for_completion_api(inputs, output)
with self._lock:
self._span_id_to_tokens[span_id] = tokens


def merge_tokens(self, child_tokens, parent_tokens):
merged_tokens = {}
for key in set(child_tokens) | set(parent_tokens):
child_value = child_tokens.get(key, 0) or 0
parent_value = parent_tokens.get(key, 0) or 0
if isinstance(child_value, dict) and isinstance(parent_value, dict):
merged_tokens[key] = self.merge_tokens(child_tokens[key], parent_tokens[key])
elif isinstance(child_value, dict) or isinstance(parent_value, dict):
merged_tokens[key] = parent_value if isinstance(child_value, dict) else child_value
else:
merged_tokens[key] = child_value + parent_value
return merged_tokens

def collect_openai_tokens_for_parent_span(self, span):
tokens = self.try_get_openai_tokens(span.get_span_context().span_id)
if tokens:
Expand All @@ -140,12 +153,7 @@ def collect_openai_tokens_for_parent_span(self, span):
parent_span_id = span.parent.span_id
with self._lock:
if parent_span_id in self._span_id_to_tokens:
merged_tokens = {
# When token count is None for some reason, we should default to 0.
key: (self._span_id_to_tokens[parent_span_id].get(key, 0) or 0) + (tokens.get(key, 0) or 0)
for key in set(self._span_id_to_tokens[parent_span_id]) | set(tokens)
}
self._span_id_to_tokens[parent_span_id] = merged_tokens
self._span_id_to_tokens[parent_span_id] = self.merge_tokens(tokens, self._span_id_to_tokens[parent_span_id])
else:
self._span_id_to_tokens[parent_span_id] = tokens

Expand Down
Loading