UNPKG

multi-llm

Version:

A unified TypeScript/JavaScript package to use LLMs across ALL platforms with support for 17 major providers, streaming, MCP tools, and intelligent response parsing

154 lines 6.14 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OpenAIProvider = void 0; const axios_1 = __importDefault(require("axios")); const provider_1 = require("../provider"); const llm_1 = require("../llm"); const parser_1 = require("../utils/parser"); class OpenAIProvider extends provider_1.Provider { constructor(apiKey, baseUrl) { super(apiKey, baseUrl); this.baseUrl = baseUrl || 'https://api.openai.com/v1'; } async getModels() { try { const response = await axios_1.default.get(`${this.baseUrl}/models`, { headers: { 'Authorization': `Bearer ${this.apiKey}`, 'Content-Type': 'application/json' } }); return response.data.data.map((model) => ({ id: model.id, name: model.id, contextWindow: this.getContextWindow(model.id), maxOutputTokens: this.getMaxOutputTokens(model.id), pricing: this.getPricing(model.id) })); } catch (error) { throw new Error(`Failed to fetch OpenAI models: ${error}`); } } createLLM(modelId) { return new llm_1.LLM(this, modelId); } async chat(modelId, messages, options, streamCallback) { try { const payload = { model: modelId, messages: messages.map(msg => ({ role: msg.role, content: msg.content })), temperature: options.temperature, max_tokens: options.maxTokens, top_p: options.topP, stream: !!streamCallback }; if (streamCallback) { return this.streamChat(payload, streamCallback); } else { const response = await axios_1.default.post(`${this.baseUrl}/chat/completions`, payload, { headers: { 'Authorization': `Bearer ${this.apiKey}`, 'Content-Type': 'application/json' } }); const content = response.data.choices[0].message.content; const parsed = parser_1.ResponseParser.parseResponse(content); return { raw: response.data, parsed, usage: { inputTokens: response.data.usage?.prompt_tokens || 0, outputTokens: response.data.usage?.completion_tokens || 0, totalTokens: response.data.usage?.total_tokens || 0 } }; } } catch (error) { throw new Error(`OpenAI chat failed: ${error}`); } } async streamChat(payload, streamCallback) { return new Promise((resolve, reject) => { let fullContent = ''; let rawResponse = null; const source = axios_1.default.post(`${this.baseUrl}/chat/completions`, payload, { headers: { 'Authorization': `Bearer ${this.apiKey}`, 'Content-Type': 'application/json' }, responseType: 'stream' }); source.then(response => { response.data.on('data', (chunk) => { const lines = chunk.toString().split('\n'); for (const line of lines) { if (line.startsWith('data: ')) { const data = line.slice(6); if (data === '[DONE]') { const parsed = parser_1.ResponseParser.parseResponse(fullContent); resolve({ raw: rawResponse, parsed, usage: undefined }); return; } try { const parsed = JSON.parse(data); rawResponse = parsed; const content = parsed.choices[0]?.delta?.content || ''; if (content) { fullContent += content; streamCallback(content); } } catch (e) { // Ignore parsing errors for partial chunks } } } }); response.data.on('error', reject); }).catch(reject); }); } getContextWindow(modelId) { const contextWindows = { 'gpt-4o': 128000, 'gpt-4o-mini': 128000, 'gpt-4-turbo': 128000, 'gpt-4': 8192, 'gpt-3.5-turbo': 16385 }; return contextWindows[modelId] || 8192; } getMaxOutputTokens(modelId) { const maxOutputTokens = { 'gpt-4o': 4096, 'gpt-4o-mini': 16384, 'gpt-4-turbo': 4096, 'gpt-4': 4096, 'gpt-3.5-turbo': 4096 }; return maxOutputTokens[modelId] || 4096; } getPricing(modelId) { const pricing = { 'gpt-4o': { input: 0.005, output: 0.015 }, 'gpt-4o-mini': { input: 0.00015, output: 0.0006 }, 'gpt-4-turbo': { input: 0.01, output: 0.03 }, 'gpt-4': { input: 0.03, output: 0.06 }, 'gpt-3.5-turbo': { input: 0.001, output: 0.002 } }; const modelPricing = pricing[modelId]; return modelPricing ? { ...modelPricing, currency: 'USD' } : undefined; } } exports.OpenAIProvider = OpenAIProvider; //# sourceMappingURL=openai.js.map