|
| 1 | +/* |
| 2 | + * Copyright 2025 New Relic Corporation. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict' |
| 7 | + |
| 8 | +/** |
| 9 | + * Determines if the provided token count is valid. |
| 10 | + * A valid token count is greater than 0 and not null. |
| 11 | + * @param {number} tokenCount The token count obtained from the token callback |
| 12 | + * @returns {boolean} Whether the token count is valid |
| 13 | + */ |
| 14 | +function validCallbackTokenCount(tokenCount) { |
| 15 | + return tokenCount !== null && tokenCount > 0 |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Calculates the total token count from the prompt tokens and completion tokens |
| 20 | + * set in the context. |
| 21 | + * @param {LlmEvent} context The context object containing token counts |
| 22 | + * @returns {number} The total token count |
| 23 | + */ |
| 24 | +function getTotalTokenCount(context) { |
| 25 | + return Number(context['response.usage.prompt_tokens']) + Number(context['response.usage.completion_tokens']) |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Sets the provided tokens counts on the LLM event. |
| 30 | + * @param {LlmChatCompletionMessage} context The context object to set token usage counts on. |
| 31 | + * @param {object} tokens The object contains the token prompt, completion and total counts. |
| 32 | + */ |
| 33 | +function setTokensInResponse(context, tokens) { |
| 34 | + context['response.usage.prompt_tokens'] = tokens.promptTokens |
| 35 | + context['response.usage.completion_tokens'] = tokens.completionTokens |
| 36 | + context['response.usage.total_tokens'] = tokens.totalTokens || getTotalTokenCount(context) |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Calculates prompt and completion token counts using the provided callback and models. |
| 41 | + * If both counts are valid, sets context.token_count to 0. |
| 42 | + * |
| 43 | + * @param {object} options - The params object. |
| 44 | + * @param {LlmChatCompletionMessage} options.context - The context object to set token count on. |
| 45 | + * @param {Function} options.tokenCB - The token counting callback function. |
| 46 | + * @param {string} options.reqModel - The model used for the prompt. |
| 47 | + * @param {string} options.resModel - The model used for the completion. |
| 48 | + * @param {string} options.promptContent - The prompt content to count tokens for. |
| 49 | + * @param {string} options.completionContent - The completion content to count tokens for. |
| 50 | + * @returns {void} |
| 51 | + */ |
| 52 | +function setTokenFromCallback({ context, tokenCB, reqModel, resModel, promptContent, completionContent }) { |
| 53 | + const promptToken = calculateCallbackTokens(tokenCB, reqModel, promptContent) |
| 54 | + const completionToken = calculateCallbackTokens(tokenCB, resModel, completionContent) |
| 55 | + |
| 56 | + const hasValidCallbackCounts = |
| 57 | + validCallbackTokenCount(promptToken) && validCallbackTokenCount(completionToken) |
| 58 | + |
| 59 | + if (hasValidCallbackCounts) { |
| 60 | + context.token_count = 0 |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Calculates prompt and completion token counts using the provided callback and models. |
| 66 | + * If both counts are valid, sets token prompt, completion and total counts on the context. |
| 67 | + * |
| 68 | + * @param {object} options - The params object. |
| 69 | + * @param {LlmEvent} options.context - The context object (llm summary or llm embedding) to set token count on. |
| 70 | + * @param {Function} options.tokenCB - The token counting callback function. |
| 71 | + * @param {string} options.reqModel - The model used for the prompt. |
| 72 | + * @param {string} options.resModel - The model used for the completion. |
| 73 | + * @param {string} options.promptContent - The prompt content to count tokens for. |
| 74 | + * @param {string} options.completionContent - The completion content to count tokens for. |
| 75 | + * @returns {void} |
| 76 | + */ |
| 77 | +function setTokenUsageFromCallback({ context, tokenCB, reqModel, resModel, promptContent, completionContent }) { |
| 78 | + const promptTokens = calculateCallbackTokens(tokenCB, reqModel, promptContent) |
| 79 | + const completionTokens = calculateCallbackTokens(tokenCB, resModel, completionContent) |
| 80 | + |
| 81 | + const hasValidCallbackCounts = |
| 82 | + validCallbackTokenCount(promptTokens) && validCallbackTokenCount(completionTokens) |
| 83 | + |
| 84 | + if (hasValidCallbackCounts) { |
| 85 | + setTokensInResponse(context, { promptTokens, completionTokens, totalTokens: promptTokens + completionTokens }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Calculate the token counts using the provided callback. |
| 91 | + * @param {Function} tokenCB - The token count callback function. |
| 92 | + * @param {string} model - The model. |
| 93 | + * @param {string} content - The content to calculate tokens for, such as prompt or completion response. |
| 94 | + * @returns {number|undefined} - The calculated token count or undefined if callback is not a function. |
| 95 | + */ |
| 96 | +function calculateCallbackTokens(tokenCB, model, content) { |
| 97 | + if (typeof tokenCB === 'function') { |
| 98 | + return tokenCB(model, content) |
| 99 | + } |
| 100 | + return undefined |
| 101 | +} |
| 102 | + |
| 103 | +module.exports = { |
| 104 | + validCallbackTokenCount, |
| 105 | + getTotalTokenCount, |
| 106 | + setTokensInResponse, |
| 107 | + setTokenFromCallback, |
| 108 | + setTokenUsageFromCallback, |
| 109 | + calculateCallbackTokens |
| 110 | +} |
0 commit comments