UNPKG

openlit

Version:

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

200 lines 9.71 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const api_1 = require("@opentelemetry/api"); const config_1 = __importDefault(require("../../config")); const helpers_1 = __importDefault(require("../../helpers")); const semantic_convention_1 = __importDefault(require("../../semantic-convention")); const constant_1 = require("../../constant"); const base_wrapper_1 = __importDefault(require("../base-wrapper")); class CohereWrapper extends base_wrapper_1.default { static _patchEmbed(tracer) { const genAIEndpoint = 'cohere.embed'; const traceContent = config_1.default.traceContent; return (originalMethod) => { return async function (...args) { const span = tracer.startSpan(genAIEndpoint, { kind: api_1.SpanKind.CLIENT }); return api_1.context.with(api_1.trace.setSpan(api_1.context.active(), span), async () => { try { const response = await originalMethod.apply(this, args); span.setAttributes({ [constant_1.TELEMETRY_SDK_NAME]: constant_1.SDK_NAME, }); const model = response.model || 'embed-english-v2.0'; const pricingInfo = await config_1.default.updatePricingJson(config_1.default.pricing_json); const cost = helpers_1.default.getEmbedModelCost(model, pricingInfo, response.meta.billedUnits.inputTokens); span.setAttribute(semantic_convention_1.default.GEN_AI_OPERATION, semantic_convention_1.default.GEN_AI_OPERATION_TYPE_EMBEDDING); const { dimensions, encoding_format = 'float', input, user, texts = [] } = args[0]; // Set base span attribues CohereWrapper.setBaseSpanAttributes(span, { genAIEndpoint, model, user, cost, aiSystem: CohereWrapper.aiSystem, }); // Request Params attributes : Start span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_ENCODING_FORMATS, encoding_format); span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_EMBEDDING_DIMENSION, dimensions); if (traceContent) { span.setAttribute(semantic_convention_1.default.GEN_AI_CONTENT_PROMPT, JSON.stringify(texts)); } // Request Params attributes : End span.setAttribute(semantic_convention_1.default.GEN_AI_RESPONSE_ID, response.id); span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_INPUT_TOKENS, response.meta.billedUnits.inputTokens); span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_TOTAL_TOKENS, response.meta.billedUnits.inputTokens); return response; } catch (e) { helpers_1.default.handleException(span, e); } finally { span.end(); } }); }; }; } static _patchChat(tracer) { const genAIEndpoint = 'cohere.chat'; return (originalMethod) => { return async function (...args) { const span = tracer.startSpan(genAIEndpoint, { kind: api_1.SpanKind.CLIENT }); return api_1.context .with(api_1.trace.setSpan(api_1.context.active(), span), async () => { return originalMethod.apply(this, args); }) .then((response) => { return CohereWrapper._chat({ args, genAIEndpoint, response, span }); }) .catch((e) => { helpers_1.default.handleException(span, e); span.end(); }); }; }; } static _patchChatStream(tracer) { const genAIEndpoint = 'cohere.chat'; return (originalMethod) => { return async function (...args) { const span = tracer.startSpan(genAIEndpoint, { kind: api_1.SpanKind.CLIENT }); return api_1.context .with(api_1.trace.setSpan(api_1.context.active(), span), async () => { return originalMethod.apply(this, args); }) .then((response) => { return helpers_1.default.createStreamProxy(response, CohereWrapper._chatGenerator({ args, genAIEndpoint, response, span, })); }) .catch((e) => { helpers_1.default.handleException(span, e); span.end(); }); }; }; } static async _chat({ args, genAIEndpoint, response, span, }) { try { await CohereWrapper._chatCommonSetter({ args, genAIEndpoint, result: response, span, stream: false, }); return response; } catch (e) { helpers_1.default.handleException(span, e); } finally { span.end(); } } static async *_chatGenerator({ args, genAIEndpoint, response, span, }) { try { let result = { response_id: '', text: '', generationId: '', chatHistory: [], finishReason: '', meta: { apiVersion: { version: '1' }, billedUnits: { inputTokens: 0, outputTokens: 0 }, }, }; for await (const chunk of response) { if (chunk.eventType === 'stream-end') { result = chunk.response; } yield chunk; } await CohereWrapper._chatCommonSetter({ args, genAIEndpoint, result, span, stream: true, }); return result; } catch (e) { helpers_1.default.handleException(span, e); } finally { span.end(); } } static async _chatCommonSetter({ args, genAIEndpoint, result, span, stream, }) { const traceContent = config_1.default.traceContent; const { message, model = 'command-r-plus-08-2024', frequency_penalty = 0, max_tokens = null, presence_penalty = 0, seed = null, temperature = 1, user, tools, } = args[0]; // Request Params attributes : Start span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_MAX_TOKENS, max_tokens); span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_TEMPERATURE, temperature); span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_PRESENCE_PENALTY, presence_penalty); span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_FREQUENCY_PENALTY, frequency_penalty); span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_SEED, seed); span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_IS_STREAM, stream); if (traceContent) { span.setAttribute(semantic_convention_1.default.GEN_AI_CONTENT_PROMPT, message); } // Request Params attributes : End span.setAttribute(semantic_convention_1.default.GEN_AI_OPERATION, semantic_convention_1.default.GEN_AI_OPERATION_TYPE_CHAT); span.setAttribute(semantic_convention_1.default.GEN_AI_RESPONSE_ID, result.response_id); const pricingInfo = await config_1.default.updatePricingJson(config_1.default.pricing_json); // Calculate cost of the operation const cost = helpers_1.default.getChatModelCost(model, pricingInfo, result.meta.billedUnits.inputTokens, result.meta.billedUnits.outputTokens); CohereWrapper.setBaseSpanAttributes(span, { genAIEndpoint, model, user, cost, aiSystem: CohereWrapper.aiSystem, }); span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_INPUT_TOKENS, result.meta.billedUnits.inputTokens); span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_OUTPUT_TOKENS, result.meta.billedUnits.outputTokens); span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_TOTAL_TOKENS, result.meta.billedUnits.inputTokens + result.meta.billedUnits.outputTokens); if (result.finishReason) { span.setAttribute(semantic_convention_1.default.GEN_AI_RESPONSE_FINISH_REASON, result.finishReason); } if (tools) { span.setAttribute(semantic_convention_1.default.GEN_AI_CONTENT_COMPLETION, 'Function called with tools'); } else { if (traceContent) { span.setAttribute(semantic_convention_1.default.GEN_AI_CONTENT_COMPLETION, result.text); } } } } CohereWrapper.aiSystem = semantic_convention_1.default.GEN_AI_SYSTEM_COHERE; exports.default = CohereWrapper; //# sourceMappingURL=wrapper.js.map