UNPKG

ggai

Version:

OpenAI LLM Agent Interface

43 lines (40 loc) 1 kB
const pricing = { 'gpt-4o-mini': { inputPrice: 0.00015, outputPrice: 0.0006 }, 'gpt-4': { inputPrice: 0.03, outputPrice: 0.06 }, 'gpt-4o': { inputPrice: 0.005, outputPrice: 0.015 }, }; /** * Calculate the cost of a prompt and completion. * @param {object} param0 * @param {number} param0.promptTokens * @param {number} param0.completionTokens * @param {string} model * @returns {number} * @throws {Error} Invalid model */ function calculateCost({ promptTokens, completionTokens }, model) { if (model in pricing) { const { inputPrice, outputPrice } = pricing[model]; return (promptTokens * inputPrice + completionTokens * outputPrice) / 1000; } else { throw new Error('Invalid model: ' + model); } } /** * Estimate the number of tokens in a string. * @param {string} str * @returns {number} */ function estimateTokenLength(str) { return Math.ceil(str.length / 3); } module.exports = { pricing, calculateCost, estimateTokenLength };