UNPKG

blackboxify

Version:

BlackboxAI API client with OpenAI-compatible interface, streaming support, multiple auth accounts, and token pricing

64 lines (55 loc) 1.52 kB
import { makeRequest, createStream } from './requests.js'; import { validateModel, updateResponseWithTokens } from './models.js'; import { AuthManager } from './auth.js'; import { BlackboxError } from './errors.js'; class BlackboxAI { constructor(options = {}) { this.models = options.models || {}; this.authManager = new AuthManager(options.auth || []); this.chat = { completions: { create: this.createChatCompletion.bind(this) } }; } async createChatCompletion(params) { const { model, messages, max_tokens = 4096, temperature = 1, stream = false, ...rest } = params; // Validate model and get pricing info const modelConfig = validateModel(model, this.models); if (stream) { return createStream({ messages, model, temperature, max_tokens, authManager: this.authManager, ...rest }); } try { const response = await makeRequest({ messages, model, temperature, max_tokens, authManager: this.authManager, ...rest }); // Add token and pricing information return updateResponseWithTokens(response, messages, modelConfig); } catch (error) { if (error instanceof BlackboxError) { throw error; } throw new BlackboxError('Request failed', error); } } } export default BlackboxAI;