mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-16 16:30:15 +01:00
🔑 fix: Azure Serverless Support for API Key Header & Version (#4791)
* 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
This commit is contained in:
parent
07511b3db8
commit
e0a5f879b6
11 changed files with 100 additions and 42 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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 () => {
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ function getLLMConfig(apiKey, options = {}) {
|
|||
modelOptions = {},
|
||||
reverseProxyUrl,
|
||||
useOpenRouter,
|
||||
defaultQuery,
|
||||
headers,
|
||||
proxy,
|
||||
azure,
|
||||
|
|
@ -74,6 +75,10 @@ function getLLMConfig(apiKey, options = {}) {
|
|||
}
|
||||
}
|
||||
|
||||
if (defaultQuery) {
|
||||
configOptions.baseOptions.defaultQuery = defaultQuery;
|
||||
}
|
||||
|
||||
if (proxy) {
|
||||
const proxyAgent = new HttpsProxyAgent(proxy);
|
||||
Object.assign(configOptions, {
|
||||
|
|
|
|||
2
package-lock.json
generated
2
package-lock.json
generated
|
|
@ -36137,7 +36137,7 @@
|
|||
},
|
||||
"packages/data-provider": {
|
||||
"name": "librechat-data-provider",
|
||||
"version": "0.7.55",
|
||||
"version": "0.7.56",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@types/js-yaml": "^4.0.9",
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
@ -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);
|
||||
|
|
@ -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,
|
||||
|
|
@ -765,6 +766,7 @@ describe('validateAzureGroups with modelGroupMap and groupMap', () => {
|
|||
);
|
||||
expect(azureOptions7).toEqual({
|
||||
azureOpenAIApiKey: 'mistral-key',
|
||||
azureOpenAIApiVersion: '',
|
||||
});
|
||||
|
||||
const {
|
||||
|
|
@ -782,6 +784,7 @@ describe('validateAzureGroups with modelGroupMap and groupMap', () => {
|
|||
);
|
||||
expect(azureOptions8).toEqual({
|
||||
azureOpenAIApiKey: 'llama-key',
|
||||
azureOpenAIApiVersion: '',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
@ -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.`,
|
||||
);
|
||||
|
|
@ -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.`,
|
||||
);
|
||||
|
|
@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -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,
|
||||
};
|
||||
|
||||
|
|
@ -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,
|
||||
|
|
@ -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) {
|
||||
|
|
@ -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.`,
|
||||
);
|
||||
|
|
@ -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,
|
||||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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 = {
|
||||
|
|
@ -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 */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue