Skip to content

Commit

Permalink
fix: standardize token count properties and add Number conversion (#367)
Browse files Browse the repository at this point in the history
- 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

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
  • Loading branch information
devin-ai-integration[bot] authored Dec 12, 2024
1 parent 3caf605 commit b58d90c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 65 deletions.
16 changes: 8 additions & 8 deletions components/project/traces/conversation-row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
48 changes: 24 additions & 24 deletions lib/crewai_trace_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
48 changes: 24 additions & 24 deletions lib/dspy_trace_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 9 additions & 9 deletions lib/trace_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]) +
Expand All @@ -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
Expand Down

0 comments on commit b58d90c

Please sign in to comment.