From d63c81733ab87353dc7a02310309c54d3ee0c9fc Mon Sep 17 00:00:00 2001 From: "devin-ai-integration[bot]" <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Sun, 8 Dec 2024 00:42:09 +0000 Subject: [PATCH] fix: standardize token count properties and add Number conversion - Rename token count properties from prompt_tokens/completion_tokens to input_tokens/output_tokens - Add Number() conversion for token counts in trace utilities - Ensure consistent token property naming across all files - Replicate changes from langtrace-internal PR #267 --- .../project/traces/conversation-row.tsx | 16 +++---- lib/crewai_trace_util.ts | 48 +++++++++---------- lib/dspy_trace_util.ts | 48 +++++++++---------- lib/trace_util.ts | 18 +++---- 4 files changed, 65 insertions(+), 65 deletions(-) diff --git a/components/project/traces/conversation-row.tsx b/components/project/traces/conversation-row.tsx index 96c4bd01..48f78d3c 100644 --- a/components/project/traces/conversation-row.tsx +++ b/components/project/traces/conversation-row.tsx @@ -86,12 +86,12 @@ export const ConversationRow = ({ attributes["gen_ai.usage.completion_tokens"] ) { tokenCounts = { - input_tokens: tokenCounts.prompt_tokens - ? tokenCounts.prompt_tokens + + input_tokens: tokenCounts.input_tokens + ? tokenCounts.input_tokens + attributes["gen_ai.usage.prompt_tokens"] : attributes["gen_ai.usage.prompt_tokens"], - output_tokens: tokenCounts.completion_tokens - ? tokenCounts.completion_tokens + + output_tokens: tokenCounts.output_tokens + ? tokenCounts.output_tokens + attributes["gen_ai.usage.completion_tokens"] : attributes["gen_ai.usage.completion_tokens"], total_tokens: tokenCounts.total_tokens @@ -111,12 +111,12 @@ export const ConversationRow = ({ attributes["gen_ai.usage.output_tokens"] ) { tokenCounts = { - input_tokens: tokenCounts.prompt_tokens - ? tokenCounts.prompt_tokens + + input_tokens: tokenCounts.input_tokens + ? tokenCounts.input_tokens + attributes["gen_ai.usage.input_tokens"] : attributes["gen_ai.usage.input_tokens"], - output_tokens: tokenCounts.completion_tokens - ? tokenCounts.completion_tokens + + output_tokens: tokenCounts.output_tokens + ? tokenCounts.output_tokens + attributes["gen_ai.usage.output_tokens"] : attributes["gen_ai.usage.output_tokens"], total_tokens: tokenCounts.total_tokens diff --git a/lib/crewai_trace_util.ts b/lib/crewai_trace_util.ts index 81050723..44246c06 100644 --- a/lib/crewai_trace_util.ts +++ b/lib/crewai_trace_util.ts @@ -297,20 +297,20 @@ export function processCrewAITrace(trace: any): CrewAITrace { attributes["gen_ai.usage.completion_tokens"] ) { tokenCounts = { - input_tokens: tokenCounts.prompt_tokens - ? tokenCounts.prompt_tokens + - attributes["gen_ai.usage.prompt_tokens"] - : attributes["gen_ai.usage.prompt_tokens"], - output_tokens: tokenCounts.completion_tokens - ? tokenCounts.completion_tokens + - attributes["gen_ai.usage.completion_tokens"] - : attributes["gen_ai.usage.completion_tokens"], + input_tokens: tokenCounts.input_tokens + ? tokenCounts.input_tokens + + Number(attributes["gen_ai.usage.prompt_tokens"]) + : Number(attributes["gen_ai.usage.prompt_tokens"]), + output_tokens: tokenCounts.output_tokens + ? tokenCounts.output_tokens + + Number(attributes["gen_ai.usage.completion_tokens"]) + : Number(attributes["gen_ai.usage.completion_tokens"]), total_tokens: tokenCounts.total_tokens ? tokenCounts.total_tokens + - attributes["gen_ai.usage.prompt_tokens"] + - attributes["gen_ai.usage.completion_tokens"] - : attributes["gen_ai.usage.prompt_tokens"] + - attributes["gen_ai.usage.completion_tokens"], + Number(attributes["gen_ai.usage.prompt_tokens"]) + + Number(attributes["gen_ai.usage.completion_tokens"]) + : Number(attributes["gen_ai.usage.prompt_tokens"]) + + Number(attributes["gen_ai.usage.completion_tokens"]), }; // calculate the cost of the current span @@ -325,20 +325,20 @@ export function processCrewAITrace(trace: any): CrewAITrace { attributes["gen_ai.usage.output_tokens"] ) { tokenCounts = { - input_tokens: tokenCounts.prompt_tokens - ? tokenCounts.prompt_tokens + - attributes["gen_ai.usage.input_tokens"] - : attributes["gen_ai.usage.input_tokens"], - output_tokens: tokenCounts.completion_tokens - ? tokenCounts.completion_tokens + - attributes["gen_ai.usage.output_tokens"] - : attributes["gen_ai.usage.output_tokens"], + input_tokens: tokenCounts.input_tokens + ? tokenCounts.input_tokens + + Number(attributes["gen_ai.usage.input_tokens"]) + : Number(attributes["gen_ai.usage.input_tokens"]), + output_tokens: tokenCounts.output_tokens + ? tokenCounts.output_tokens + + Number(attributes["gen_ai.usage.output_tokens"]) + : Number(attributes["gen_ai.usage.output_tokens"]), total_tokens: tokenCounts.total_tokens ? tokenCounts.total_tokens + - attributes["gen_ai.usage.input_tokens"] + - attributes["gen_ai.usage.output_tokens"] - : attributes["gen_ai.usage.input_tokens"] + - attributes["gen_ai.usage.output_tokens"], + Number(attributes["gen_ai.usage.input_tokens"]) + + Number(attributes["gen_ai.usage.output_tokens"]) + : Number(attributes["gen_ai.usage.input_tokens"]) + + Number(attributes["gen_ai.usage.output_tokens"]), }; const currentcost = calculatePriceFromUsage(vendor, model, tokenCounts); // add the cost of the current span to the total cost diff --git a/lib/dspy_trace_util.ts b/lib/dspy_trace_util.ts index 48bd579c..56b19f22 100644 --- a/lib/dspy_trace_util.ts +++ b/lib/dspy_trace_util.ts @@ -158,20 +158,20 @@ export function processDspyTrace(trace: any): DspyTrace { attributes["gen_ai.usage.completion_tokens"] ) { tokenCounts = { - input_tokens: tokenCounts.prompt_tokens - ? tokenCounts.prompt_tokens + - attributes["gen_ai.usage.prompt_tokens"] - : attributes["gen_ai.usage.prompt_tokens"], - output_tokens: tokenCounts.completion_tokens - ? tokenCounts.completion_tokens + - attributes["gen_ai.usage.completion_tokens"] - : attributes["gen_ai.usage.completion_tokens"], + input_tokens: tokenCounts.input_tokens + ? tokenCounts.input_tokens + + Number(attributes["gen_ai.usage.prompt_tokens"]) + : Number(attributes["gen_ai.usage.prompt_tokens"]), + output_tokens: tokenCounts.output_tokens + ? tokenCounts.output_tokens + + Number(attributes["gen_ai.usage.completion_tokens"]) + : Number(attributes["gen_ai.usage.completion_tokens"]), total_tokens: tokenCounts.total_tokens ? tokenCounts.total_tokens + - attributes["gen_ai.usage.prompt_tokens"] + - attributes["gen_ai.usage.completion_tokens"] - : attributes["gen_ai.usage.prompt_tokens"] + - attributes["gen_ai.usage.completion_tokens"], + Number(attributes["gen_ai.usage.prompt_tokens"]) + + Number(attributes["gen_ai.usage.completion_tokens"]) + : Number(attributes["gen_ai.usage.prompt_tokens"]) + + Number(attributes["gen_ai.usage.completion_tokens"]), }; // calculate the cost of the current span @@ -186,20 +186,20 @@ export function processDspyTrace(trace: any): DspyTrace { attributes["gen_ai.usage.output_tokens"] ) { tokenCounts = { - input_tokens: tokenCounts.prompt_tokens - ? tokenCounts.prompt_tokens + - attributes["gen_ai.usage.input_tokens"] - : attributes["gen_ai.usage.input_tokens"], - output_tokens: tokenCounts.completion_tokens - ? tokenCounts.completion_tokens + - attributes["gen_ai.usage.output_tokens"] - : attributes["gen_ai.usage.output_tokens"], + input_tokens: tokenCounts.input_tokens + ? tokenCounts.input_tokens + + Number(attributes["gen_ai.usage.input_tokens"]) + : Number(attributes["gen_ai.usage.input_tokens"]), + output_tokens: tokenCounts.output_tokens + ? tokenCounts.output_tokens + + Number(attributes["gen_ai.usage.output_tokens"]) + : Number(attributes["gen_ai.usage.output_tokens"]), total_tokens: tokenCounts.total_tokens ? tokenCounts.total_tokens + - attributes["gen_ai.usage.input_tokens"] + - attributes["gen_ai.usage.output_tokens"] - : attributes["gen_ai.usage.input_tokens"] + - attributes["gen_ai.usage.output_tokens"], + Number(attributes["gen_ai.usage.input_tokens"]) + + Number(attributes["gen_ai.usage.output_tokens"]) + : Number(attributes["gen_ai.usage.input_tokens"]) + + Number(attributes["gen_ai.usage.output_tokens"]), }; const currentcost = calculatePriceFromUsage(vendor, model, tokenCounts); // add the cost of the current span to the total cost diff --git a/lib/trace_util.ts b/lib/trace_util.ts index 0eb42b17..e285f4e1 100644 --- a/lib/trace_util.ts +++ b/lib/trace_util.ts @@ -105,14 +105,14 @@ export function processTrace(trace: any): Trace { attributes["gen_ai.usage.completion_tokens"] ) { tokenCounts = { - input_tokens: tokenCounts.prompt_tokens - ? Number(tokenCounts.prompt_tokens) + + input_tokens: tokenCounts.input_tokens + ? Number(tokenCounts.input_tokens) + Number(attributes["gen_ai.usage.prompt_tokens"]) : Number(attributes["gen_ai.usage.prompt_tokens"]), - output_tokens: tokenCounts.completion_tokens - ? Number(tokenCounts.completion_tokens) + + output_tokens: tokenCounts.output_tokens + ? Number(tokenCounts.output_tokens) + Number(attributes["gen_ai.usage.completion_tokens"]) - : Number(attributes["gen_ai.usage.completion_tokens"]), + : Number(attributes["gen_ai.usage.completion_tokens"]), total_tokens: tokenCounts.total_tokens ? Number(tokenCounts.total_tokens) + Number(attributes["gen_ai.usage.prompt_tokens"]) + @@ -133,12 +133,12 @@ export function processTrace(trace: any): Trace { attributes["gen_ai.usage.output_tokens"] ) { tokenCounts = { - input_tokens: tokenCounts.prompt_tokens - ? Number(tokenCounts.prompt_tokens) + + input_tokens: tokenCounts.input_tokens + ? Number(tokenCounts.input_tokens) + Number(attributes["gen_ai.usage.input_tokens"]) : Number(attributes["gen_ai.usage.input_tokens"]), - output_tokens: tokenCounts.completion_tokens - ? Number(tokenCounts.completion_tokens) + + output_tokens: tokenCounts.output_tokens + ? Number(tokenCounts.output_tokens) + Number(attributes["gen_ai.usage.output_tokens"]) : Number(attributes["gen_ai.usage.output_tokens"]), total_tokens: tokenCounts.total_tokens