UNPKG

ai.libx.js

Version:

Unified API bridge for various AI models (LLMs, image/video generation, TTS, STT) - stateless, edge-compatible

143 lines 7.62 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i; function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; } function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GroqAdapter = void 0; const BaseAdapter_1 = require("./base/BaseAdapter"); const stream_1 = require("../utils/stream"); const errors_1 = require("../utils/errors"); const content_helpers_1 = require("../utils/content-helpers"); class GroqAdapter extends BaseAdapter_1.BaseAdapter { get name() { return 'groq'; } chat(options) { return __awaiter(this, void 0, void 0, function* () { try { const apiKey = this.getApiKey(options); const baseUrl = this.getBaseUrl('https://api.groq.com/openai/v1'); const model = options.model.replace(/^groq\//, ''); const request = { model, messages: options.messages.map((msg) => ({ role: msg.role, content: (0, content_helpers_1.contentToString)(msg.content), })), stream: options.stream || false, }; if (options.temperature !== undefined) request.temperature = options.temperature; if (options.maxTokens !== undefined) request.max_tokens = options.maxTokens; if (options.topP !== undefined) request.top_p = options.topP; if (options.frequencyPenalty !== undefined) request.frequency_penalty = options.frequencyPenalty; if (options.presencePenalty !== undefined) request.presence_penalty = options.presencePenalty; if (options.stop !== undefined) request.stop = options.stop; if (options.providerOptions) { Object.assign(request, options.providerOptions); } const response = yield this.fetchWithErrorHandling(`${baseUrl}/chat/completions`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${apiKey}`, }, body: JSON.stringify(request), }, this.name); if (options.stream) { return this.handleStreamResponse(response, model); } return this.handleNonStreamResponse(yield response.json(), model); } catch (error) { (0, errors_1.handleProviderError)(error, this.name); } }); } handleNonStreamResponse(data, model) { var _a, _b; const choice = (_a = data.choices) === null || _a === void 0 ? void 0 : _a[0]; if (!choice) { throw new Error('No choices in response'); } return { content: ((_b = choice.message) === null || _b === void 0 ? void 0 : _b.content) || '', finishReason: choice.finish_reason, usage: data.usage ? { promptTokens: data.usage.prompt_tokens, completionTokens: data.usage.completion_tokens, totalTokens: data.usage.total_tokens, } : undefined, model, raw: data, }; } handleStreamResponse(response, model) { return __asyncGenerator(this, arguments, function* handleStreamResponse_1() { var _a, e_1, _b, _c; var _d, _e; if (!response.body) { throw new Error('No response body for streaming'); } try { for (var _f = true, _g = __asyncValues((0, stream_1.parseSSEStream)(response.body)), _h; _h = yield __await(_g.next()), _a = _h.done, !_a; _f = true) { _c = _h.value; _f = false; const chunk = _c; const choice = (_d = chunk.choices) === null || _d === void 0 ? void 0 : _d[0]; if (!choice) continue; const content = ((_e = choice.delta) === null || _e === void 0 ? void 0 : _e.content) || ''; const finishReason = choice.finish_reason; if (content || finishReason) { yield yield __await({ content, finishReason, index: choice.index, }); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_f && !_a && (_b = _g.return)) yield __await(_b.call(_g)); } finally { if (e_1) throw e_1.error; } } }); } } exports.GroqAdapter = GroqAdapter; //# sourceMappingURL=groq.js.map