2024-02-26 14:12:25 -05:00
|
|
|
const { EModelEndpoint } = require('librechat-data-provider');
|
2023-12-06 19:36:57 -05:00
|
|
|
const { addOpenAPISpecs } = require('~/app/clients/tools/util/addOpenAPISpecs');
|
2024-02-26 14:12:25 -05:00
|
|
|
const { availableTools } = require('~/app/clients/tools');
|
2024-02-28 14:27:19 -05:00
|
|
|
const { isUserProvided } = require('~/server/utils');
|
|
|
|
|
const { config } = require('./EndpointService');
|
|
|
|
|
|
|
|
|
|
const { openAIApiKey, azureOpenAIApiKey, useAzurePlugins, userProvidedOpenAI, googleKey } = config;
|
2023-12-06 19:36:57 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Load async endpoints and return a configuration object
|
2024-02-26 14:12:25 -05:00
|
|
|
* @param {Express.Request} req - The request object
|
2023-12-06 19:36:57 -05:00
|
|
|
*/
|
2024-02-26 14:12:25 -05:00
|
|
|
async function loadAsyncEndpoints(req) {
|
2023-12-06 19:36:57 -05:00
|
|
|
let i = 0;
|
2023-12-15 02:18:07 -05:00
|
|
|
let serviceKey, googleUserProvides;
|
2023-12-06 19:36:57 -05:00
|
|
|
try {
|
2023-12-15 02:18:07 -05:00
|
|
|
serviceKey = require('~/data/auth.json');
|
2023-12-06 19:36:57 -05:00
|
|
|
} catch (e) {
|
|
|
|
|
if (i === 0) {
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-28 14:27:19 -05:00
|
|
|
if (isUserProvided(googleKey)) {
|
2023-12-10 14:54:13 -05:00
|
|
|
googleUserProvides = true;
|
2023-12-06 19:36:57 -05:00
|
|
|
if (i <= 1) {
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const tools = await addOpenAPISpecs(availableTools);
|
|
|
|
|
function transformToolsToMap(tools) {
|
|
|
|
|
return tools.reduce((map, obj) => {
|
|
|
|
|
map[obj.pluginKey] = obj.name;
|
|
|
|
|
return map;
|
|
|
|
|
}, {});
|
|
|
|
|
}
|
|
|
|
|
const plugins = transformToolsToMap(tools);
|
|
|
|
|
|
2023-12-15 02:18:07 -05:00
|
|
|
const google = serviceKey || googleKey ? { userProvide: googleUserProvides } : false;
|
2023-12-06 19:36:57 -05:00
|
|
|
|
2024-02-26 14:12:25 -05:00
|
|
|
const useAzure = req.app.locals[EModelEndpoint.azureOpenAI]?.plugins;
|
2023-12-06 19:36:57 -05:00
|
|
|
const gptPlugins =
|
2024-02-26 14:12:25 -05:00
|
|
|
useAzure || openAIApiKey || azureOpenAIApiKey
|
2023-12-06 19:36:57 -05:00
|
|
|
? {
|
|
|
|
|
plugins,
|
|
|
|
|
availableAgents: ['classic', 'functions'],
|
2024-02-26 14:12:25 -05:00
|
|
|
userProvide: useAzure ? false : userProvidedOpenAI,
|
2024-02-28 14:27:19 -05:00
|
|
|
userProvideURL: useAzure
|
|
|
|
|
? false
|
|
|
|
|
: config[EModelEndpoint.openAI]?.userProvideURL ||
|
|
|
|
|
config[EModelEndpoint.azureOpenAI]?.userProvideURL,
|
2024-02-26 14:12:25 -05:00
|
|
|
azure: useAzurePlugins || useAzure,
|
2023-12-06 19:36:57 -05:00
|
|
|
}
|
|
|
|
|
: false;
|
|
|
|
|
|
|
|
|
|
return { google, gptPlugins };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = loadAsyncEndpoints;
|