UNPKG

@convo-lang/convo-lang

Version:
95 lines 3.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseOpenAiConvoCompletionService = void 0; const common_1 = require("@iyio/common"); class BaseOpenAiConvoCompletionService { serviceId; inputType; outputType; secretManager; apiKey; apiBaseUrl; completionsEndpoint; secretsName; models; apiKeyHeader; apiKeyHeaderValuePrefix; headers; isFallback; logRequests; updateRequest; completeAsync; constructor({ apiKey, secretManager, secretsName, apiBaseUrl = 'https://api.openai.com', completionsEndpoint = '/v1/chat/completions', inputType, outputType, models, isFallback = false, apiKeyHeader = 'Authorization', apiKeyHeaderValuePrefix = 'Bearer ', headers = { 'Content-Type': 'application/json' }, serviceId, logRequests = false, completeAsync, updateRequest, }) { this.serviceId = serviceId; this.apiKey = apiKey; this.apiBaseUrl = apiBaseUrl; this.completionsEndpoint = completionsEndpoint; this.secretManager = secretManager; this.secretsName = secretsName; this.inputType = inputType; this.outputType = outputType; this.isFallback = isFallback; this.models = models; this.apiKeyHeader = apiKeyHeader; this.apiKeyHeaderValuePrefix = apiKeyHeaderValuePrefix ?? undefined; this.logRequests = logRequests; this.completeAsync = completeAsync; this.headers = headers; this.updateRequest = updateRequest; } canComplete(model, flat) { if (!model) { return this.isFallback; } return this.models.some(m => m.name === model); } clientPromises = {}; async getApiClientAsync(apiKeyOverride, endpoint) { const url = endpoint ?? (0, common_1.joinPaths)(this.apiBaseUrl, this.completionsEndpoint); const key = `${url}:::${apiKeyOverride ?? '.'}`; return await (this.clientPromises[key] ?? (this.clientPromises[key] = (async () => { let apiKey = apiKeyOverride ?? this.apiKey; if (!apiKey && this.secretManager && this.secretsName) { const { apiKey: key } = await this.secretManager.requireSecretTAsync(this.secretsName, true); apiKey = key; } return { apiKey, url, }; })())); } async completeConvoAsync(input, flat, ctx) { const client = await this.getApiClientAsync(flat.apiKey ?? undefined, flat.responseEndpoint); if (flat.apiKey && flat.apiKey === client.apiKey) { flat.apiKeyUsedForCompletion = true; } const headers = { [this.apiKeyHeader]: client.apiKey ? ((this.apiKeyHeaderValuePrefix ?? '') + client.apiKey) : undefined, ...this.headers }; if (this.updateRequest) { input = { ...input }; this.updateRequest(input, headers); } await ctx.beforeComplete?.(this, input, flat); const r = await (this.completeAsync ? this.completeAsync(input, flat, client.apiKey, client.url) : (0, common_1.httpClient)().postAsync(client.url, input, { headers, readErrors: true, log: this.logRequests })); if (!r) { throw new common_1.NotFoundError(); } return r; } getModelsAsync() { return Promise.resolve([...this.models]); } } exports.BaseOpenAiConvoCompletionService = BaseOpenAiConvoCompletionService; //# sourceMappingURL=BaseOpenAiConvoCompletionService.js.map