UNPKG

dokulabs

Version:

An NPM Package for tracking OpenAI API calls and sending usage metrics to Doku

63 lines (57 loc) 2.32 kB
import {sendData} from './helpers.js'; import {countTokens} from '@anthropic-ai/tokenizer'; /** * Initializes Anthropic functionality with performance tracking. * * @param {Object} func - The Anthropic function object. * @param {string} dokuUrl - The URL for logging data. * @param {string} token - The authentication token. * @param {string} environment - The environment. * @param {string} applicationName - The application name. * @param {boolean} skipResp - To skip waiting for API resopnse. * @return {void} * * @jsondoc * { * "description": "Initializes Anthropic function and performance tracking", * "params": [ * {"name": "dokuUrl", "type": "string", "description": "Doku URL"}, * {"name": "token", "type": "string", "description": "Auth Token"}, * {"name": "func", "type": "Object", "description": "The Anthropic object"}, * {"name": "environment", "type": "string", "description": "Environment"}, * {"name": "applicationName", "type": "string", "description": "Application Name"}, * {"name": "skipResp", "type": "boolean", "description": "To skip waiting for API resopnse."} * ], * "returns": {"type": "void"}, * "example": { * "description": "Example usage of init function.", * "code": "init('https://example.com/log', 'authToken', anthropicFunc);" * } * } */ export default function initAnthropic(func, {dokuUrl, token, environment, applicationName, skipResp}) { const originalCompletionsCreate = func.completions.create; // Define wrapped method func.completions.create = async function(params) { const start = performance.now(); const response = await originalCompletionsCreate.apply(this, params); const end = performance.now(); const duration = (end - start) / 1000; const data = { environment: environment, applicationName: applicationName, sourceLanguage: 'Javascript', endpoint: 'anthropic.completions', skipResp: skipResp, completionTokens: countTokens(response.completion), promptTokens: countTokens(prompt), requestDuration: duration, model: params.model, prompt: params.prompt, finishReason: response.stop_reason, response: response.completion, }; sendData(data, dokuUrl, token); return response; }; }