UNPKG

gpt-research

Version:

Autonomous AI research agent that conducts comprehensive research on any topic and generates detailed reports with citations

95 lines 3.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LLMProviderFactory = exports.LLMProvider = void 0; const events_1 = require("events"); class LLMProvider extends events_1.EventEmitter { config; modelCosts; constructor(config) { super(); this.config = config; this.modelCosts = new Map(); this.initializeModelCosts(); } estimateCost(input, output, model) { const costs = this.modelCosts.get(model) || { input: 0, output: 0 }; // Rough token estimation (1 token ≈ 4 characters) const inputTokens = Math.ceil(input.length / 4); const outputTokens = Math.ceil(output.length / 4); const inputCost = (inputTokens / 1000) * costs.input; const outputCost = (outputTokens / 1000) * costs.output; return { inputTokens, outputTokens, totalCost: inputCost + outputCost, model }; } async validateApiKey() { try { const models = await this.getAvailableModels(); return models.length > 0; } catch (error) { return false; } } handleError(error) { const errorMessage = error?.response?.data?.error?.message || error?.message || 'Unknown error occurred'; const statusCode = error?.response?.status || 500; const customError = new Error(errorMessage); customError.statusCode = statusCode; customError.provider = this.constructor.name; this.emit('error', customError); throw customError; } async retry(fn, retries = 3, delay = 1000) { try { return await fn(); } catch (error) { if (retries === 0 || error?.statusCode === 401) { throw error; } await new Promise(resolve => setTimeout(resolve, delay)); return this.retry(fn, retries - 1, delay * 2); } } // Token counting utilities countTokens(text) { // This is a rough approximation // For accurate counting, use tiktoken or the provider's tokenizer return Math.ceil(text.length / 4); } truncateToTokenLimit(text, maxTokens) { const estimatedTokens = this.countTokens(text); if (estimatedTokens <= maxTokens) { return text; } // Rough truncation based on character count const maxChars = maxTokens * 4; return text.substring(0, maxChars); } } exports.LLMProvider = LLMProvider; // Factory for creating LLM providers class LLMProviderFactory { static providers = new Map(); static register(name, provider) { this.providers.set(name.toLowerCase(), provider); } static create(name, config) { const Provider = this.providers.get(name.toLowerCase()); if (!Provider) { throw new Error(`Unknown LLM provider: ${name}`); } return new Provider(config); } static getAvailableProviders() { return Array.from(this.providers.keys()); } } exports.LLMProviderFactory = LLMProviderFactory; //# sourceMappingURL=LLMProvider.js.map