UNPKG

ai.libx.js

Version:

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

163 lines 8.55 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.AnthropicAdapter = 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 AnthropicAdapter extends BaseAdapter_1.BaseAdapter { get name() { return 'anthropic'; } chat(options) { return __awaiter(this, void 0, void 0, function* () { try { const apiKey = this.getApiKey(options); const baseUrl = this.getBaseUrl('https://api.anthropic.com/v1'); const model = options.model.replace(/^anthropic\//, ''); const systemMessage = options.messages.find((m) => m.role === 'system'); const nonSystemMessages = options.messages.filter((m) => m.role !== 'system'); const request = { model, messages: this.transformMessages(nonSystemMessages), max_tokens: options.maxTokens || 4096, stream: options.stream || false, }; if (systemMessage) { request.system = (0, content_helpers_1.contentToString)(systemMessage.content); } if (options.temperature !== undefined) request.temperature = options.temperature; if (options.topP !== undefined) request.top_p = options.topP; if (options.topK !== undefined) request.top_k = options.topK; if (options.stop && Array.isArray(options.stop)) { request.stop_sequences = options.stop; } if (options.providerOptions) { Object.assign(request, options.providerOptions); } const response = yield this.fetchWithErrorHandling(`${baseUrl}/messages`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-api-key': apiKey, 'anthropic-version': '2023-06-01', }, 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); } }); } transformMessages(messages) { return messages.map((msg) => ({ role: msg.role === 'user' ? 'user' : 'assistant', content: (0, content_helpers_1.contentToString)(msg.content), })); } handleNonStreamResponse(data, model) { var _a, _b; const content = ((_b = (_a = data.content) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.text) || ''; return { content, finishReason: data.stop_reason, usage: data.usage ? { promptTokens: data.usage.input_tokens, completionTokens: data.usage.output_tokens, totalTokens: data.usage.input_tokens + data.usage.output_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.streamLines)(response.body)), _h; _h = yield __await(_g.next()), _a = _h.done, !_a; _f = true) { _c = _h.value; _f = false; const line = _c; if (!line.startsWith('data: ')) continue; const data = line.slice(6).trim(); if (data === '[DONE]') break; try { const chunk = JSON.parse(data); if (chunk.type === 'content_block_delta') { const content = ((_d = chunk.delta) === null || _d === void 0 ? void 0 : _d.text) || ''; if (content) { yield yield __await({ content, index: chunk.index, }); } } else if (chunk.type === 'message_delta') { if ((_e = chunk.delta) === null || _e === void 0 ? void 0 : _e.stop_reason) { yield yield __await({ content: '', finishReason: chunk.delta.stop_reason, }); } } } catch (e) { continue; } } } 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.AnthropicAdapter = AnthropicAdapter; //# sourceMappingURL=anthropic.js.map