UNPKG

@topgroup/diginext

Version:

A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.

79 lines (78 loc) 3.35 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.aiApi = exports.aiModels = exports.OPENROUTER_BASE_API_URL = void 0; const axios_1 = __importDefault(require("axios")); const lodash_1 = require("lodash"); const app_config_1 = require("../../app.config"); exports.OPENROUTER_BASE_API_URL = "https://openrouter.ai/api/v1"; exports.aiModels = [ "deepseek/deepseek-coder", "google/gemini-flash-1.5", "google/gemini-pro-1.5", "openai/gpt-4o", "openai/gpt-3.5-turbo", "openai/gpt-3.5-turbo-16k", "openai/gpt-4", "openai/gpt-4-32k", "anthropic/claude-3.5-sonnet", "anthropic/claude-2", "anthropic/claude-instant-v1", "meta-llama/llama-2-13b-chat", "meta-llama/llama-2-70b-chat", "qwen/qwen-2.5-coder-32b-instruct", // 2024-11-14 ]; async function aiApi(options) { var _a, _b; if (!options.method) options.method = "POST"; const { method } = options; options.baseURL = options.baseUrl || exports.OPENROUTER_BASE_API_URL; if (!options.url) options.url = "/chat/completions"; // default headers let headers = { "HTTP-Referer": app_config_1.Config.BASE_URL, "X-Title": `DIGINEXT (${app_config_1.Config.ENV})`, }; if (!(0, lodash_1.isEmpty)(options.headers)) headers = { ...headers, ...options.headers }; // Authentication const apiKey = options.apiKey || app_config_1.Config.grab("OPENROUTER_KEY"); if (apiKey) headers.Authorization = `Bearer ${apiKey}`; if (["POST", "PATCH", "DELETE"].includes(method === null || method === void 0 ? void 0 : method.toUpperCase())) { if ((0, lodash_1.isEmpty)(headers["content-type"])) headers["content-type"] = "application/json"; } // if (options.data) options.data = new URLSearchParams(options.data); options.headers = headers; if (options.isDebugging) console.log(`aiApi: ${options.url} > headers :>>`, options.headers); if (options.isDebugging) console.log("aiApi: ${options.url} > options.data :>> ", options.data); if (options.isDebugging) console.log("aiApi: ${options.url} > options.method :>> ", options.method); try { const res = await (0, axios_1.default)(options); const { data: responseData } = res; if (options.isDebugging) console.log("aiApi: ${options.url} > data :>> ", responseData); return responseData; } catch (e) { if (options.isDebugging) console.log("aiApi: ${options.url} > e :>> ", e); if (options.isDebugging) console.log("aiApi: ${options.url} > e.response :>> ", e.response); if (options.isDebugging) console.log("aiApi: ${options.url} > e.data :>> ", e.data); if (options.isDebugging) console.log("aiApi: ${options.url} > e.message :>> ", e.message); const err = e.response || ((_a = e.data) === null || _a === void 0 ? void 0 : _a.message) === "UNAUTHORIZED" || ((_b = e.data) === null || _b === void 0 ? void 0 : _b.status) === 401 ? "Invalid API Key." : e.message; return { status: 0, messages: [`${err}`] }; } } exports.aiApi = aiApi;