UNPKG

openlit

Version:

OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications, facilitating the integration of observability into your GenAI-driven projects

118 lines 4.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const js_tiktoken_1 = require("js-tiktoken"); const api_1 = require("@opentelemetry/api"); class OpenLitHelper { static openaiTokens(text, model) { try { const encoding = (0, js_tiktoken_1.encodingForModel)(model); return encoding.encode(text).length; } catch (error) { console.error(`Error in openaiTokens: ${error}`); throw error; } } static generalTokens(text) { const encoding = (0, js_tiktoken_1.encodingForModel)('gpt2'); return encoding.encode(text).length; } static getChatModelCost(model, pricingInfo, promptTokens, completionTokens) { try { return ((promptTokens / OpenLitHelper.PROMPT_TOKEN_FACTOR) * pricingInfo.chat[model].promptPrice + (completionTokens / OpenLitHelper.PROMPT_TOKEN_FACTOR) * pricingInfo.chat[model].completionPrice); } catch (error) { console.error(`Error in getChatModelCost: ${error}`); return 0; } } static getEmbedModelCost(model, pricingInfo, promptTokens) { try { return (promptTokens / OpenLitHelper.PROMPT_TOKEN_FACTOR) * pricingInfo.embeddings[model]; } catch (error) { console.error(`Error in getEmbedModelCost: ${error}`); return 0; } } static getImageModelCost(model, pricingInfo, size, quality) { try { return pricingInfo.images[model][quality][size]; } catch (error) { console.error(`Error in getImageModelCost: ${error}`); return 0; } } static getAudioModelCost(model, pricingInfo, prompt) { try { return (prompt.length / OpenLitHelper.PROMPT_TOKEN_FACTOR) * pricingInfo.audio[model]; } catch (error) { console.error(`Error in getAudioModelCost: ${error}`); return 0; } } static async fetchPricingInfo(pricingJson) { let pricingUrl = 'https://raw.githubusercontent.com/openlit/openlit/main/assets/pricing.json'; if (pricingJson) { let isUrl = false; try { isUrl = !!new URL(pricingJson); } catch { isUrl = false; } if (isUrl) { pricingUrl = pricingJson; } else { try { if (typeof pricingJson === 'string') { const json = JSON.parse(pricingJson); return json; } else { const json = JSON.parse(JSON.stringify(pricingJson)); return json; } } catch { return {}; } } } try { const response = await fetch(pricingUrl); if (response.ok) { return response.json(); } else { throw new Error(`HTTP error occurred while fetching pricing info: ${response.status}`); } } catch (error) { console.error(`Unexpected error occurred while fetching pricing info: ${error}`); return {}; } } static handleException(span, error) { span.recordException(error); span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: error.message }); } static async createStreamProxy(stream, generatorFuncResponse) { return new Proxy(stream, { get(target, prop, receiver) { if (prop === Symbol.asyncIterator) { return () => generatorFuncResponse; } return Reflect.get(target, prop, receiver); } }); } } OpenLitHelper.PROMPT_TOKEN_FACTOR = 1000; exports.default = OpenLitHelper; //# sourceMappingURL=helpers.js.map