🛠️ refactor(loadConfigModels): make apiKey and baseURL pairings more versatile (#1985)

This commit is contained in:
Danny Avila 2024-03-05 15:42:19 -05:00 committed by GitHub
parent a33db54b81
commit b023c5683d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 277 additions and 10 deletions

View file

@ -1,6 +1,6 @@
const { EModelEndpoint, extractEnvVariable } = require('librechat-data-provider');
const { isUserProvided } = require('~/server/utils');
const { fetchModels } = require('~/server/services/ModelService');
const { isUserProvided } = require('~/server/utils');
const getCustomConfig = require('./getCustomConfig');
/**
@ -41,8 +41,8 @@ async function loadConfigModels(req) {
(endpoint.models.fetch || endpoint.models.default),
);
const fetchPromisesMap = {}; // Map for promises keyed by baseURL
const baseUrlToNameMap = {}; // Map to associate baseURLs with names
const fetchPromisesMap = {}; // Map for promises keyed by unique combination of baseURL and apiKey
const uniqueKeyToNameMap = {}; // Map to associate unique keys with endpoint names
for (let i = 0; i < customEndpoints.length; i++) {
const endpoint = customEndpoints[i];
@ -51,11 +51,13 @@ async function loadConfigModels(req) {
const API_KEY = extractEnvVariable(apiKey);
const BASE_URL = extractEnvVariable(baseURL);
const uniqueKey = `${BASE_URL}__${API_KEY}`;
modelsConfig[name] = [];
if (models.fetch && !isUserProvided(API_KEY) && !isUserProvided(BASE_URL)) {
fetchPromisesMap[BASE_URL] =
fetchPromisesMap[BASE_URL] ||
fetchPromisesMap[uniqueKey] =
fetchPromisesMap[uniqueKey] ||
fetchModels({
user: req.user.id,
baseURL: BASE_URL,
@ -63,8 +65,8 @@ async function loadConfigModels(req) {
name,
userIdQuery: models.userIdQuery,
});
baseUrlToNameMap[BASE_URL] = baseUrlToNameMap[BASE_URL] || [];
baseUrlToNameMap[BASE_URL].push(name);
uniqueKeyToNameMap[uniqueKey] = uniqueKeyToNameMap[uniqueKey] || [];
uniqueKeyToNameMap[uniqueKey].push(name);
continue;
}
@ -74,12 +76,12 @@ async function loadConfigModels(req) {
}
const fetchedData = await Promise.all(Object.values(fetchPromisesMap));
const baseUrls = Object.keys(fetchPromisesMap);
const uniqueKeys = Object.keys(fetchPromisesMap);
for (let i = 0; i < fetchedData.length; i++) {
const currentBaseUrl = baseUrls[i];
const currentKey = uniqueKeys[i];
const modelData = fetchedData[i];
const associatedNames = baseUrlToNameMap[currentBaseUrl];
const associatedNames = uniqueKeyToNameMap[currentKey];
for (const name of associatedNames) {
modelsConfig[name] = modelData;