mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 17:00:15 +01:00
feat(azureOpenAI): Allow Switching Deployment Name by Model Name (#1137)
* feat(azureOpenAI): allow switching deployment name by model name * ci: add unit tests and throw error on no api key provided to avoid API call * fix(gptPlugins/initializeClient): check if azure is enabled; ci: add unit tests for gptPlugins/initializeClient * fix(ci): fix expected error message for partial regex match: unexpected token
This commit is contained in:
parent
a7b5639da1
commit
0886441461
8 changed files with 562 additions and 38 deletions
|
|
@ -1,6 +1,6 @@
|
|||
const { OpenAIClient } = require('../../../../app');
|
||||
const { isEnabled } = require('../../../utils');
|
||||
const { getAzureCredentials } = require('../../../../utils');
|
||||
const { getAzureCredentials, sanitizeModelName } = require('../../../../utils');
|
||||
const { getUserKey, checkUserKeyExpiry } = require('../../../services/UserService');
|
||||
|
||||
const initializeClient = async ({ req, res, endpointOption }) => {
|
||||
|
|
@ -24,29 +24,40 @@ const initializeClient = async ({ req, res, endpointOption }) => {
|
|||
...endpointOption,
|
||||
};
|
||||
|
||||
const isUserProvided =
|
||||
endpoint === 'openAI' ? OPENAI_API_KEY === 'user_provided' : AZURE_API_KEY === 'user_provided';
|
||||
const credentials = {
|
||||
openAI: OPENAI_API_KEY,
|
||||
azureOpenAI: AZURE_API_KEY,
|
||||
};
|
||||
|
||||
let key = null;
|
||||
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.',
|
||||
);
|
||||
key = await getUserKey({ userId: req.user.id, name: endpoint });
|
||||
userKey = await getUserKey({ userId: req.user.id, name: endpoint });
|
||||
}
|
||||
|
||||
let openAIApiKey = isUserProvided ? key : OPENAI_API_KEY;
|
||||
let apiKey = isUserProvided ? userKey : credentials[endpoint];
|
||||
|
||||
if (process.env.AZURE_API_KEY && endpoint === 'azureOpenAI') {
|
||||
clientOptions.azure = isUserProvided ? JSON.parse(key) : getAzureCredentials();
|
||||
openAIApiKey = clientOptions.azure.azureOpenAIApiKey;
|
||||
if (endpoint === 'azureOpenAI') {
|
||||
clientOptions.azure = isUserProvided ? JSON.parse(userKey) : getAzureCredentials();
|
||||
clientOptions.azure.azureOpenAIApiDeploymentName = sanitizeModelName(
|
||||
clientOptions.modelOptions.model,
|
||||
);
|
||||
apiKey = clientOptions.azure.azureOpenAIApiKey;
|
||||
}
|
||||
|
||||
const client = new OpenAIClient(openAIApiKey, clientOptions);
|
||||
if (!apiKey) {
|
||||
throw new Error('API key not provided.');
|
||||
}
|
||||
|
||||
const client = new OpenAIClient(apiKey, clientOptions);
|
||||
return {
|
||||
client,
|
||||
openAIApiKey,
|
||||
openAIApiKey: apiKey,
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue