Skip to content

Commit

Permalink
🔑 fix: Azure Serverless Support for API Key Header & Version (#4791)
Browse files Browse the repository at this point in the history
* fix: azure validation/extraction types

* fix: typing, add optional chaining for modelGroup and groupMap properties; expect azureOpenAIApiVersion in serverless tests

* fix: add support for azureOpenAIApiVersion and api-key in serverless mode across clients

* chore: update CONFIG_VERSION to 1.1.8, data-provider bump
  • Loading branch information
danny-avila authored Nov 25, 2024
1 parent 07511b3 commit e0a5f87
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 42 deletions.
10 changes: 10 additions & 0 deletions api/app/clients/ChatGPTClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ class ChatGPTClient extends BaseClient {
this.azure = !serverless && azureOptions;
this.azureEndpoint =
!serverless && genAzureChatCompletion(this.azure, modelOptions.model, this);
if (serverless === true) {
this.options.defaultQuery = azureOptions.azureOpenAIApiVersion
? { 'api-version': azureOptions.azureOpenAIApiVersion }
: undefined;
this.options.headers['api-key'] = this.apiKey;
}
}

if (this.options.defaultQuery) {
opts.defaultQuery = this.options.defaultQuery;
}

if (this.options.headers) {
Expand Down
16 changes: 16 additions & 0 deletions api/app/clients/OpenAIClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,12 @@ class OpenAIClient extends BaseClient {
this.options.dropParams = azureConfig.groupMap[groupName].dropParams;
this.options.forcePrompt = azureConfig.groupMap[groupName].forcePrompt;
this.azure = !serverless && azureOptions;
if (serverless === true) {
this.options.defaultQuery = azureOptions.azureOpenAIApiVersion
? { 'api-version': azureOptions.azureOpenAIApiVersion }
: undefined;
this.options.headers['api-key'] = this.apiKey;
}
}

const titleChatCompletion = async () => {
Expand Down Expand Up @@ -1169,6 +1175,10 @@ ${convo}
opts.defaultHeaders = { ...opts.defaultHeaders, ...this.options.headers };
}

if (this.options.defaultQuery) {
opts.defaultQuery = this.options.defaultQuery;
}

if (this.options.proxy) {
opts.httpAgent = new HttpsProxyAgent(this.options.proxy);
}
Expand Down Expand Up @@ -1207,6 +1217,12 @@ ${convo}
this.azure = !serverless && azureOptions;
this.azureEndpoint =
!serverless && genAzureChatCompletion(this.azure, modelOptions.model, this);
if (serverless === true) {
this.options.defaultQuery = azureOptions.azureOpenAIApiVersion
? { 'api-version': azureOptions.azureOpenAIApiVersion }
: undefined;
this.options.headers['api-key'] = this.apiKey;
}
}

if (this.azure || this.options.azure) {
Expand Down
6 changes: 6 additions & 0 deletions api/server/services/Endpoints/azureAssistants/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ const initializeClient = async ({ req, res, version, endpointOption, initAppClie
clientOptions.reverseProxyUrl = baseURL ?? clientOptions.reverseProxyUrl;
clientOptions.headers = opts.defaultHeaders;
clientOptions.azure = !serverless && azureOptions;
if (serverless === true) {
clientOptions.defaultQuery = azureOptions.azureOpenAIApiVersion
? { 'api-version': azureOptions.azureOpenAIApiVersion }
: undefined;
clientOptions.headers['api-key'] = apiKey;
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions api/server/services/Endpoints/gptPlugins/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ const initializeClient = async ({ req, res, endpointOption }) => {

apiKey = azureOptions.azureOpenAIApiKey;
clientOptions.azure = !serverless && azureOptions;
if (serverless === true) {
clientOptions.defaultQuery = azureOptions.azureOpenAIApiVersion
? { 'api-version': azureOptions.azureOpenAIApiVersion }
: undefined;
clientOptions.headers['api-key'] = apiKey;
}
} else if (useAzure || (apiKey && apiKey.includes('{"azure') && !clientOptions.azure)) {
clientOptions.azure = userProvidesKey ? JSON.parse(userValues.apiKey) : getAzureCredentials();
apiKey = clientOptions.azure.azureOpenAIApiKey;
Expand Down
6 changes: 6 additions & 0 deletions api/server/services/Endpoints/openAI/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ const initializeClient = async ({

apiKey = azureOptions.azureOpenAIApiKey;
clientOptions.azure = !serverless && azureOptions;
if (serverless === true) {
clientOptions.defaultQuery = azureOptions.azureOpenAIApiVersion
? { 'api-version': azureOptions.azureOpenAIApiVersion }
: undefined;
clientOptions.headers['api-key'] = apiKey;
}
} else if (isAzureOpenAI) {
clientOptions.azure = userProvidesKey ? JSON.parse(userValues.apiKey) : getAzureCredentials();
apiKey = clientOptions.azure.azureOpenAIApiKey;
Expand Down
5 changes: 5 additions & 0 deletions api/server/services/Endpoints/openAI/llm.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function getLLMConfig(apiKey, options = {}) {
modelOptions = {},
reverseProxyUrl,
useOpenRouter,
defaultQuery,
headers,
proxy,
azure,
Expand Down Expand Up @@ -74,6 +75,10 @@ function getLLMConfig(apiKey, options = {}) {
}
}

if (defaultQuery) {
configOptions.baseOptions.defaultQuery = defaultQuery;
}

if (proxy) {
const proxyAgent = new HttpsProxyAgent(proxy);
Object.assign(configOptions, {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/data-provider/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "librechat-data-provider",
"version": "0.7.55",
"version": "0.7.56",
"description": "data services for librechat apps",
"main": "dist/index.js",
"module": "dist/index.es.js",
Expand Down
13 changes: 8 additions & 5 deletions packages/data-provider/specs/azure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ describe('validateAzureGroups', () => {
expect(isValid).toBe(true);
const modelGroup = modelGroupMap['gpt-5-turbo'];
expect(modelGroup).toBeDefined();
expect(modelGroup.group).toBe('japan-east');
expect(groupMap[modelGroup.group]).toBeDefined();
expect(modelGroup?.group).toBe('japan-east');
expect(groupMap[modelGroup?.group ?? '']).toBeDefined();
expect(modelNames).toContain('gpt-5-turbo');
const { azureOptions } = mapModelToAzureConfig({
modelName: 'gpt-5-turbo',
Expand Down Expand Up @@ -323,6 +323,7 @@ describe('validateAzureGroups for Serverless Configurations', () => {

expect(azureOptions).toEqual({
azureOpenAIApiKey: 'def456',
azureOpenAIApiVersion: '',
});
expect(baseURL).toEqual('https://new-serverless.example.com/v1/completions');
expect(serverless).toBe(true);
Expand Down Expand Up @@ -381,10 +382,10 @@ describe('validateAzureGroups with modelGroupMap and groupMap', () => {
const { isValid, modelGroupMap, groupMap } = validateAzureGroups(validConfigs);
expect(isValid).toBe(true);
expect(modelGroupMap['gpt-4-turbo']).toBeDefined();
expect(modelGroupMap['gpt-4-turbo'].group).toBe('us-east');
expect(modelGroupMap['gpt-4-turbo']?.group).toBe('us-east');
expect(groupMap['us-east']).toBeDefined();
expect(groupMap['us-east'].apiKey).toBe('prod-1234');
expect(groupMap['us-east'].models['gpt-4-turbo']).toBeDefined();
expect(groupMap['us-east']?.apiKey).toBe('prod-1234');
expect(groupMap['us-east']?.models['gpt-4-turbo']).toBeDefined();
const { azureOptions, baseURL, headers } = mapModelToAzureConfig({
modelName: 'gpt-4-turbo',
modelGroupMap,
Expand Down Expand Up @@ -765,6 +766,7 @@ describe('validateAzureGroups with modelGroupMap and groupMap', () => {
);
expect(azureOptions7).toEqual({
azureOpenAIApiKey: 'mistral-key',
azureOpenAIApiVersion: '',
});

const {
Expand All @@ -782,6 +784,7 @@ describe('validateAzureGroups with modelGroupMap and groupMap', () => {
);
expect(azureOptions8).toEqual({
azureOpenAIApiKey: 'llama-key',
azureOpenAIApiVersion: '',
});
});
});
Expand Down
70 changes: 38 additions & 32 deletions packages/data-provider/src/azure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ export function validateAzureGroups(configs: TAzureGroups): TAzureConfigValidati
const {
group: groupName,
apiKey,
instanceName,
deploymentName,
version,
baseURL,
instanceName = '',
deploymentName = '',
version = '',
baseURL = '',
additionalHeaders,
models,
serverless,
serverless = false,
...rest
} = group;

Expand Down Expand Up @@ -120,9 +120,11 @@ export function validateAzureGroups(configs: TAzureGroups): TAzureConfigValidati
continue;
}

const groupDeploymentName = group.deploymentName ?? '';
const groupVersion = group.version ?? '';
if (typeof model === 'boolean') {
// For boolean models, check if group-level deploymentName and version are present.
if (!group.deploymentName || !group.version) {
if (!groupDeploymentName || !groupVersion) {
errors.push(
`Model "${modelName}" in group "${groupName}" is missing a deploymentName or version.`,
);
Expand All @@ -133,11 +135,10 @@ export function validateAzureGroups(configs: TAzureGroups): TAzureConfigValidati
group: groupName,
};
} else {
const modelDeploymentName = model.deploymentName ?? '';
const modelVersion = model.version ?? '';
// For object models, check if deploymentName and version are required but missing.
if (
(!model.deploymentName && !group.deploymentName) ||
(!model.version && !group.version)
) {
if ((!modelDeploymentName && !groupDeploymentName) || (!modelVersion && !groupVersion)) {
errors.push(
`Model "${modelName}" in group "${groupName}" is missing a required deploymentName or version.`,
);
Expand All @@ -146,8 +147,8 @@ export function validateAzureGroups(configs: TAzureGroups): TAzureConfigValidati

modelGroupMap[modelName] = {
group: groupName,
// deploymentName: model.deploymentName || group.deploymentName,
// version: model.version || group.version,
// deploymentName: modelDeploymentName || groupDeploymentName,
// version: modelVersion || groupVersion,
};
}
}
Expand Down Expand Up @@ -190,26 +191,28 @@ export function mapModelToAzureConfig({
);
}

const instanceName = groupConfig.instanceName;
const instanceName = groupConfig.instanceName ?? '';

if (!instanceName && !groupConfig.serverless) {
if (!instanceName && groupConfig.serverless !== true) {
throw new Error(
`Group "${modelConfig.group}" is missing an instanceName for non-serverless configuration.`,
);
}

if (groupConfig.serverless && !groupConfig.baseURL) {
const baseURL = groupConfig.baseURL ?? '';
if (groupConfig.serverless === true && !baseURL) {
throw new Error(
`Group "${modelConfig.group}" is missing the required base URL for serverless configuration.`,
);
}

if (groupConfig.serverless) {
if (groupConfig.serverless === true) {
const result: MappedAzureConfig = {
azureOptions: {
azureOpenAIApiVersion: extractEnvVariable(groupConfig.version ?? ''),
azureOpenAIApiKey: extractEnvVariable(groupConfig.apiKey),
},
baseURL: extractEnvVariable(groupConfig.baseURL as string),
baseURL: extractEnvVariable(baseURL),
serverless: true,
};

Expand All @@ -232,11 +235,11 @@ export function mapModelToAzureConfig({
}

const modelDetails = groupConfig.models[modelName];
const { deploymentName, version } =
const { deploymentName = '', version = '' } =
typeof modelDetails === 'object'
? {
deploymentName: modelDetails.deploymentName || groupConfig.deploymentName,
version: modelDetails.version || groupConfig.version,
deploymentName: modelDetails.deploymentName ?? groupConfig.deploymentName,
version: modelDetails.version ?? groupConfig.version,
}
: {
deploymentName: groupConfig.deploymentName,
Expand Down Expand Up @@ -264,8 +267,8 @@ export function mapModelToAzureConfig({

const result: MappedAzureConfig = { azureOptions };

if (groupConfig.baseURL) {
result.baseURL = extractEnvVariable(groupConfig.baseURL);
if (baseURL) {
result.baseURL = extractEnvVariable(baseURL);
}

if (groupConfig.additionalHeaders) {
Expand All @@ -287,15 +290,17 @@ export function mapGroupToAzureConfig({
throw new Error(`Group named "${groupName}" not found in configuration.`);
}

const instanceName = groupConfig.instanceName as string;
const instanceName = groupConfig.instanceName ?? '';
const serverless = groupConfig.serverless ?? false;
const baseURL = groupConfig.baseURL ?? '';

if (!instanceName && !groupConfig.serverless) {
if (!instanceName && !serverless) {
throw new Error(
`Group "${groupName}" is missing an instanceName for non-serverless configuration.`,
);
}

if (groupConfig.serverless && !groupConfig.baseURL) {
if (serverless && !baseURL) {
throw new Error(
`Group "${groupName}" is missing the required base URL for serverless configuration.`,
);
Expand All @@ -311,25 +316,26 @@ export function mapGroupToAzureConfig({
const modelDetails = groupConfig.models[firstModelName];

const azureOptions: AzureOptions = {
azureOpenAIApiVersion: extractEnvVariable(groupConfig.version ?? ''),
azureOpenAIApiKey: extractEnvVariable(groupConfig.apiKey),
azureOpenAIApiInstanceName: extractEnvVariable(instanceName),
// DeploymentName and Version set below
};

if (groupConfig.serverless) {
if (serverless) {
return {
azureOptions,
baseURL: extractEnvVariable(groupConfig.baseURL ?? ''),
baseURL: extractEnvVariable(baseURL),
serverless: true,
...(groupConfig.additionalHeaders && { headers: groupConfig.additionalHeaders }),
};
}

const { deploymentName, version } =
const { deploymentName = '', version = '' } =
typeof modelDetails === 'object'
? {
deploymentName: modelDetails.deploymentName || groupConfig.deploymentName,
version: modelDetails.version || groupConfig.version,
deploymentName: modelDetails.deploymentName ?? groupConfig.deploymentName,
version: modelDetails.version ?? groupConfig.version,
}
: {
deploymentName: groupConfig.deploymentName,
Expand All @@ -347,8 +353,8 @@ export function mapGroupToAzureConfig({

const result: MappedAzureConfig = { azureOptions };

if (groupConfig.baseURL) {
result.baseURL = extractEnvVariable(groupConfig.baseURL);
if (baseURL) {
result.baseURL = extractEnvVariable(baseURL);
}

if (groupConfig.additionalHeaders) {
Expand Down
6 changes: 3 additions & 3 deletions packages/data-provider/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ export type TAzureModelMapSchema = {
group: string;
};

export type TAzureModelGroupMap = Record<string, TAzureModelMapSchema>;
export type TAzureModelGroupMap = Record<string, TAzureModelMapSchema | undefined>;
export type TAzureGroupMap = Record<
string,
TAzureBaseSchema & { models: Record<string, TAzureModelConfig> }
(TAzureBaseSchema & { models: Record<string, TAzureModelConfig | undefined> }) | undefined
>;

export type TValidatedAzureConfig = {
Expand Down Expand Up @@ -1080,7 +1080,7 @@ export enum Constants {
/** Key for the app's version. */
VERSION = 'v0.7.5',
/** Key for the Custom Config's version (librechat.yaml). */
CONFIG_VERSION = '1.1.7',
CONFIG_VERSION = '1.1.8',
/** Standard value for the first message's `parentMessageId` value, to indicate no parent exists. */
NO_PARENT = '00000000-0000-0000-0000-000000000000',
/** Standard value for the initial conversationId before a request is sent */
Expand Down

0 comments on commit e0a5f87

Please sign in to comment.