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:
Danny Avila 2023-11-04 15:03:31 -04:00 committed by GitHub
parent a7b5639da1
commit 0886441461
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 562 additions and 38 deletions

View file

@ -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,
};
};