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 { PluginsClient } = 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 }) => {
@ -25,38 +25,43 @@ const initializeClient = async ({ req, res, endpointOption }) => {
...endpointOption,
};
const isUserProvided = PLUGINS_USE_AZURE
const useAzure = isEnabled(PLUGINS_USE_AZURE);
const isUserProvided = useAzure
? AZURE_API_KEY === 'user_provided'
: OPENAI_API_KEY === 'user_provided';
let key = null;
let userKey = null;
if (expiresAt && isUserProvided) {
checkUserKeyExpiry(
expiresAt,
'Your OpenAI API key has expired. Please provide your API key again.',
);
key = await getUserKey({
userKey = await getUserKey({
userId: req.user.id,
name: PLUGINS_USE_AZURE ? 'azureOpenAI' : 'openAI',
name: useAzure ? 'azureOpenAI' : 'openAI',
});
}
let openAIApiKey = isUserProvided ? key : OPENAI_API_KEY;
let apiKey = isUserProvided ? userKey : OPENAI_API_KEY;
if (PLUGINS_USE_AZURE) {
clientOptions.azure = isUserProvided ? JSON.parse(key) : getAzureCredentials();
openAIApiKey = clientOptions.azure.azureOpenAIApiKey;
if (useAzure || (apiKey && apiKey.includes('azure') && !clientOptions.azure)) {
clientOptions.azure = isUserProvided ? JSON.parse(userKey) : getAzureCredentials();
clientOptions.azure.azureOpenAIApiDeploymentName = sanitizeModelName(
clientOptions.modelOptions.model,
);
apiKey = clientOptions.azure.azureOpenAIApiKey;
}
if (openAIApiKey && openAIApiKey.includes('azure') && !clientOptions.azure) {
clientOptions.azure = isUserProvided ? JSON.parse(key) : getAzureCredentials();
openAIApiKey = clientOptions.azure.azureOpenAIApiKey;
if (!apiKey) {
throw new Error('API key not provided.');
}
const client = new PluginsClient(openAIApiKey, clientOptions);
const client = new PluginsClient(apiKey, clientOptions);
return {
client,
azure: clientOptions.azure,
openAIApiKey,
openAIApiKey: apiKey,
};
};