Skip to content

Commit

Permalink
Audio cost calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
abrenneke committed Feb 2, 2025
1 parent e6fd847 commit 0de1731
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
49 changes: 49 additions & 0 deletions packages/core/src/model/nodes/ChatNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,55 @@ export class ChatNodeImpl extends NodeImpl<ChatNode> {

output['duration' as PortId] = { type: 'number', value: Date.now() - startTime };

if (response.usage) {
output['usage' as PortId] = {
type: 'object',
value: response.usage,
};

const costs =
finalModel in openaiModels ? openaiModels[finalModel as keyof typeof openaiModels].cost : undefined;

const promptCostPerThousand = costs?.prompt ?? 0;
const completionCostPerThousand = costs?.completion ?? 0;
const audioPromptCostPerThousand = costs
? 'audioPrompt' in costs
? (costs.audioPrompt as number)
: 0
: 0;
const audioCompletionCostPerThousand = costs
? 'audioCompletion' in costs
? (costs.audioCompletion as number)
: 0
: 0;

const promptCost = getCostForTokens(
response.usage.prompt_tokens_details.text_tokens,
'prompt',
promptCostPerThousand,
);
const completionCost = getCostForTokens(
response.usage.completion_tokens_details.text_tokens,
'completion',
completionCostPerThousand,
);
const audioPromptCost = getCostForTokens(
response.usage.prompt_tokens_details.audio_tokens,
'prompt',
audioPromptCostPerThousand,
);
const audioCompletionCost = getCostForTokens(
response.usage.completion_tokens_details.audio_tokens,
'completion',
audioCompletionCostPerThousand,
);

output['cost' as PortId] = {
type: 'number',
value: promptCost + completionCost + audioPromptCost + audioCompletionCost,
};
}

Object.freeze(output);
cache.set(cacheKey, output);

Expand Down
33 changes: 33 additions & 0 deletions packages/core/src/utils/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export type OpenAIModel = {
cost: {
prompt: number;
completion: number;

audioPrompt?: number;
audioCompletion?: number;
};
displayName: string;
};
Expand Down Expand Up @@ -171,6 +174,8 @@ export const openaiModels = {
cost: {
prompt: 0.0025,
completion: 0.01,
audioPrompt: 0.04,
audioCompletion: 0.08,
},
displayName: 'GPT-4o Audio (Preview)',
},
Expand Down Expand Up @@ -365,6 +370,34 @@ export type ChatCompletionResponse = {

/** Total number of tokens used in the request (prompt + completion). */
total_tokens: number;

prompt_tokens_details: {
/** Number of tokens used from the cache. */
cached_tokens: number;

/** Number of tokens used for audio. */
audio_tokens: number;

text_tokens: number;

image_tokens: number;
};

completion_tokens_details: {
/** Number of tokens used for reasoning. */
reasoning_tokens: number;

/** Number of tokens used for audio. */
audio_tokens: number;

/** Number of tokens used for accepted predictions. */
accepted_prediction_tokens: number;

/** Number of tokens used for rejected predictions. */
rejected_prediction_tokens: number;

text_tokens: number;
};
};

/** A list of chat completion choices. Can be more than one if n is greater than 1. */
Expand Down

0 comments on commit 0de1731

Please sign in to comment.