mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 17:00:15 +01:00
* refactor(useUpdateUserKeysMutation): only invalidate the endpoint whose key is being updated by user * fix(assistants): await `getUserKeyExpiry` call * chore: fix spinner loading color * refactor(initializeClient): make known which endpoint api Key is missing * fix: prevent an `endpointType` mismatch by making it impossible to assign when the `endpointsConfig` doesn't have a `type` defined, also prefer `getQueryData` call to useQuery in useChatHelpers
71 lines
1.9 KiB
JavaScript
71 lines
1.9 KiB
JavaScript
const { EModelEndpoint } = require('librechat-data-provider');
|
|
const { getUserKey, checkUserKeyExpiry } = require('~/server/services/UserService');
|
|
const { getAzureCredentials } = require('~/utils');
|
|
const { isEnabled } = require('~/server/utils');
|
|
const { OpenAIClient } = require('~/app');
|
|
|
|
const initializeClient = async ({ req, res, endpointOption }) => {
|
|
const {
|
|
PROXY,
|
|
OPENAI_API_KEY,
|
|
AZURE_API_KEY,
|
|
OPENAI_REVERSE_PROXY,
|
|
AZURE_OPENAI_BASEURL,
|
|
OPENAI_SUMMARIZE,
|
|
DEBUG_OPENAI,
|
|
} = process.env;
|
|
const { key: expiresAt, endpoint } = req.body;
|
|
const contextStrategy = isEnabled(OPENAI_SUMMARIZE) ? 'summarize' : null;
|
|
|
|
const baseURLOptions = {
|
|
[EModelEndpoint.openAI]: OPENAI_REVERSE_PROXY,
|
|
[EModelEndpoint.azureOpenAI]: AZURE_OPENAI_BASEURL,
|
|
};
|
|
|
|
const reverseProxyUrl = baseURLOptions[endpoint] ?? null;
|
|
|
|
const clientOptions = {
|
|
debug: isEnabled(DEBUG_OPENAI),
|
|
contextStrategy,
|
|
reverseProxyUrl,
|
|
proxy: PROXY ?? null,
|
|
req,
|
|
res,
|
|
...endpointOption,
|
|
};
|
|
|
|
const credentials = {
|
|
[EModelEndpoint.openAI]: OPENAI_API_KEY,
|
|
[EModelEndpoint.azureOpenAI]: AZURE_API_KEY,
|
|
};
|
|
|
|
const isUserProvided = credentials[endpoint] === 'user_provided';
|
|
|
|
let userKey = null;
|
|
if (expiresAt && isUserProvided) {
|
|
checkUserKeyExpiry(
|
|
expiresAt,
|
|
'Your OpenAI API key has expired. Please provide your API key again.',
|
|
);
|
|
userKey = await getUserKey({ userId: req.user.id, name: endpoint });
|
|
}
|
|
|
|
let apiKey = isUserProvided ? userKey : credentials[endpoint];
|
|
|
|
if (endpoint === EModelEndpoint.azureOpenAI) {
|
|
clientOptions.azure = isUserProvided ? JSON.parse(userKey) : getAzureCredentials();
|
|
apiKey = clientOptions.azure.azureOpenAIApiKey;
|
|
}
|
|
|
|
if (!apiKey) {
|
|
throw new Error(`${endpoint} API key not provided.`);
|
|
}
|
|
|
|
const client = new OpenAIClient(apiKey, clientOptions);
|
|
return {
|
|
client,
|
|
openAIApiKey: apiKey,
|
|
};
|
|
};
|
|
|
|
module.exports = initializeClient;
|