mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-18 01:10:14 +01:00
🅰️ feat: Azure Config to Allow Different Deployments per Model (#1863)
* wip: first pass for azure endpoint schema * refactor: azure config to return groupMap and modelConfigMap * wip: naming and schema changes * refactor(errorsToString): move to data-provider * feat: rename to azureGroups, add additional tests, tests all expected outcomes, return errors * feat(AppService): load Azure groups * refactor(azure): use imported types, write `mapModelToAzureConfig` * refactor: move `extractEnvVariable` to data-provider * refactor(validateAzureGroups): throw on duplicate groups or models; feat(mapModelToAzureConfig): throw if env vars not present, add tests * refactor(AppService): ensure each model is properly configured on startup * refactor: deprecate azureOpenAI environment variables in favor of librechat.yaml config * feat: use helper functions to handle and order enabled/default endpoints; initialize azureOpenAI from config file * refactor: redefine types as well as load azureOpenAI models from config file * chore(ci): fix test description naming * feat(azureOpenAI): use validated model grouping for request authentication * chore: bump data-provider following rebase * chore: bump config file version noting significant changes * feat: add title options and switch azure configs for titling and vision requests * feat: enable azure plugins from config file * fix(ci): pass tests * chore(.env.example): mark `PLUGINS_USE_AZURE` as deprecated * fix(fetchModels): early return if apiKey not passed * chore: fix azure config typing * refactor(mapModelToAzureConfig): return baseURL and headers as well as azureOptions * feat(createLLM): use `azureOpenAIBasePath` * feat(parsers): resolveHeaders * refactor(extractBaseURL): handle invalid input * feat(OpenAIClient): handle headers and baseURL for azureConfig * fix(ci): pass `OpenAIClient` tests * chore: extract env var for azureOpenAI group config, baseURL * docs: azureOpenAI config setup docs * feat: safe check of potential conflicting env vars that map to unique placeholders * fix: reset apiKey when model switches from originally requested model (vision or title) * chore: linting * docs: CONFIG_PATH notes in custom_config.md
This commit is contained in:
parent
7a55132e42
commit
097a978e5b
37 changed files with 2066 additions and 394 deletions
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "librechat-data-provider",
|
||||
"version": "0.4.3",
|
||||
"version": "0.4.4",
|
||||
"description": "data services for librechat apps",
|
||||
"main": "dist/index.js",
|
||||
"module": "dist/index.es.js",
|
||||
|
|
|
|||
567
packages/data-provider/specs/azure.spec.ts
Normal file
567
packages/data-provider/specs/azure.spec.ts
Normal file
|
|
@ -0,0 +1,567 @@
|
|||
import type { TAzureGroups } from '../src/config';
|
||||
import { validateAzureGroups, mapModelToAzureConfig } from '../src/azure';
|
||||
|
||||
describe('validateAzureGroups', () => {
|
||||
it('should validate a correct configuration', () => {
|
||||
const configs = [
|
||||
{
|
||||
group: 'us-east',
|
||||
apiKey: 'prod-1234',
|
||||
instanceName: 'prod-instance',
|
||||
deploymentName: 'v1-deployment',
|
||||
version: '2023-12-31',
|
||||
baseURL: 'https://prod.example.com',
|
||||
additionalHeaders: {
|
||||
'X-Custom-Header': 'value',
|
||||
},
|
||||
models: {
|
||||
'gpt-4-turbo': {
|
||||
deploymentName: 'gpt-4-turbo-deployment',
|
||||
version: '2023-11-06',
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
const { isValid, modelNames, modelGroupMap, groupMap } = validateAzureGroups(configs);
|
||||
expect(isValid).toBe(true);
|
||||
expect(modelNames).toEqual(['gpt-4-turbo']);
|
||||
|
||||
const { azureOptions, baseURL, headers } = mapModelToAzureConfig({
|
||||
modelName: 'gpt-4-turbo',
|
||||
modelGroupMap,
|
||||
groupMap,
|
||||
});
|
||||
expect(azureOptions).toEqual({
|
||||
azureOpenAIApiKey: 'prod-1234',
|
||||
azureOpenAIApiInstanceName: 'prod-instance',
|
||||
azureOpenAIApiDeploymentName: 'gpt-4-turbo-deployment',
|
||||
azureOpenAIApiVersion: '2023-11-06',
|
||||
});
|
||||
expect(baseURL).toBe('https://prod.example.com');
|
||||
expect(headers).toEqual({
|
||||
'X-Custom-Header': 'value',
|
||||
});
|
||||
});
|
||||
|
||||
it('should return invalid for a configuration missing deploymentName at the model level where required', () => {
|
||||
const configs = [
|
||||
{
|
||||
group: 'us-west',
|
||||
apiKey: 'us-west-key-5678',
|
||||
instanceName: 'us-west-instance',
|
||||
models: {
|
||||
'gpt-5': {
|
||||
version: '2023-12-01', // Missing deploymentName
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
const { isValid, errors } = validateAzureGroups(configs);
|
||||
expect(isValid).toBe(false);
|
||||
expect(errors.length).toBe(1);
|
||||
});
|
||||
|
||||
it('should return invalid for a configuration with a boolean model where group lacks deploymentName and version', () => {
|
||||
const configs = [
|
||||
{
|
||||
group: 'sweden-central',
|
||||
apiKey: 'sweden-central-9012',
|
||||
instanceName: 'sweden-central-instance',
|
||||
models: {
|
||||
'gpt-35-turbo': true, // The group lacks deploymentName and version
|
||||
},
|
||||
},
|
||||
];
|
||||
const { isValid, errors } = validateAzureGroups(configs);
|
||||
expect(isValid).toBe(false);
|
||||
expect(errors.length).toBe(1);
|
||||
});
|
||||
|
||||
it('should allow a boolean model when group has both deploymentName and version', () => {
|
||||
const configs = [
|
||||
{
|
||||
group: 'japan-east',
|
||||
apiKey: 'japan-east-3456',
|
||||
instanceName: 'japan-east-instance',
|
||||
deploymentName: 'default-deployment',
|
||||
version: '2023-04-01',
|
||||
models: {
|
||||
'gpt-5-turbo': true,
|
||||
},
|
||||
},
|
||||
];
|
||||
const { isValid, modelNames, modelGroupMap, groupMap } = validateAzureGroups(configs);
|
||||
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(modelNames).toContain('gpt-5-turbo');
|
||||
const { azureOptions } = mapModelToAzureConfig({
|
||||
modelName: 'gpt-5-turbo',
|
||||
modelGroupMap,
|
||||
groupMap,
|
||||
});
|
||||
expect(azureOptions).toEqual({
|
||||
azureOpenAIApiKey: 'japan-east-3456',
|
||||
azureOpenAIApiInstanceName: 'japan-east-instance',
|
||||
azureOpenAIApiDeploymentName: 'default-deployment',
|
||||
azureOpenAIApiVersion: '2023-04-01',
|
||||
});
|
||||
});
|
||||
|
||||
it('should validate correctly when optional fields are missing', () => {
|
||||
const configs = [
|
||||
{
|
||||
group: 'canada-central',
|
||||
apiKey: 'canada-key',
|
||||
instanceName: 'canada-instance',
|
||||
models: {
|
||||
'gpt-6': {
|
||||
deploymentName: 'gpt-6-deployment',
|
||||
version: '2023-01-01',
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
const { isValid, modelNames, modelGroupMap, groupMap } = validateAzureGroups(configs);
|
||||
expect(isValid).toBe(true);
|
||||
expect(modelNames).toEqual(['gpt-6']);
|
||||
const { azureOptions } = mapModelToAzureConfig({ modelName: 'gpt-6', modelGroupMap, groupMap });
|
||||
expect(azureOptions).toEqual({
|
||||
azureOpenAIApiKey: 'canada-key',
|
||||
azureOpenAIApiInstanceName: 'canada-instance',
|
||||
azureOpenAIApiDeploymentName: 'gpt-6-deployment',
|
||||
azureOpenAIApiVersion: '2023-01-01',
|
||||
});
|
||||
});
|
||||
|
||||
it('should return invalid for configurations with incorrect types', () => {
|
||||
const configs = [
|
||||
{
|
||||
group: 123, // incorrect type
|
||||
apiKey: 'key123',
|
||||
instanceName: 'instance123',
|
||||
models: {
|
||||
'gpt-7': true,
|
||||
},
|
||||
},
|
||||
];
|
||||
// @ts-expect-error This error is expected because the 'group' property should be a string.
|
||||
const { isValid, errors } = validateAzureGroups(configs);
|
||||
expect(isValid).toBe(false);
|
||||
expect(errors.length).toBe(1);
|
||||
});
|
||||
|
||||
it('should correctly handle a mix of valid and invalid model configurations', () => {
|
||||
const configs = [
|
||||
{
|
||||
group: 'australia-southeast',
|
||||
apiKey: 'australia-key',
|
||||
instanceName: 'australia-instance',
|
||||
models: {
|
||||
'valid-model': {
|
||||
deploymentName: 'valid-deployment',
|
||||
version: '2023-02-02',
|
||||
},
|
||||
'invalid-model': true, // Invalid because the group lacks deploymentName and version
|
||||
},
|
||||
},
|
||||
];
|
||||
const { isValid, modelNames, errors } = validateAzureGroups(configs);
|
||||
expect(isValid).toBe(false);
|
||||
expect(modelNames).toEqual(expect.arrayContaining(['valid-model', 'invalid-model']));
|
||||
expect(errors.length).toBe(1);
|
||||
});
|
||||
|
||||
it('should return invalid for configuration missing required fields at the group level', () => {
|
||||
const configs = [
|
||||
{
|
||||
group: 'brazil-south',
|
||||
apiKey: 'brazil-key',
|
||||
// Missing instanceName
|
||||
models: {
|
||||
'gpt-8': {
|
||||
deploymentName: 'gpt-8-deployment',
|
||||
version: '2023-03-03',
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
// @ts-expect-error This error is expected because the 'instanceName' property is intentionally left out.
|
||||
const { isValid, errors } = validateAzureGroups(configs);
|
||||
expect(isValid).toBe(false);
|
||||
expect(errors.length).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('validateAzureGroups with modelGroupMap and groupMap', () => {
|
||||
const originalEnv = process.env;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
process.env = { ...originalEnv };
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
process.env = originalEnv;
|
||||
});
|
||||
|
||||
it('should provide a valid modelGroupMap and groupMap for a correct configuration', () => {
|
||||
const validConfigs: TAzureGroups = [
|
||||
{
|
||||
group: 'us-east',
|
||||
apiKey: 'prod-1234',
|
||||
instanceName: 'prod-instance',
|
||||
deploymentName: 'v1-deployment',
|
||||
version: '2023-12-31',
|
||||
baseURL: 'https://prod.example.com',
|
||||
additionalHeaders: {
|
||||
'X-Custom-Header': 'value',
|
||||
},
|
||||
models: {
|
||||
'gpt-4-turbo': {
|
||||
deploymentName: 'gpt-4-turbo-deployment',
|
||||
version: '2023-11-06',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
group: 'us-west',
|
||||
apiKey: 'prod-12345',
|
||||
instanceName: 'prod-instance',
|
||||
deploymentName: 'v1-deployment',
|
||||
version: '2023-12-31',
|
||||
baseURL: 'https://prod.example.com',
|
||||
additionalHeaders: {
|
||||
'X-Custom-Header': 'value',
|
||||
},
|
||||
models: {
|
||||
'gpt-5-turbo': {
|
||||
deploymentName: 'gpt-5-turbo-deployment',
|
||||
version: '2023-11-06',
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
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(groupMap['us-east']).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,
|
||||
groupMap,
|
||||
});
|
||||
expect(azureOptions).toEqual({
|
||||
azureOpenAIApiKey: 'prod-1234',
|
||||
azureOpenAIApiInstanceName: 'prod-instance',
|
||||
azureOpenAIApiDeploymentName: 'gpt-4-turbo-deployment',
|
||||
azureOpenAIApiVersion: '2023-11-06',
|
||||
});
|
||||
expect(baseURL).toBe('https://prod.example.com');
|
||||
expect(headers).toEqual({
|
||||
'X-Custom-Header': 'value',
|
||||
});
|
||||
});
|
||||
|
||||
it('should not allow duplicate group names', () => {
|
||||
const duplicateGroups: TAzureGroups = [
|
||||
{
|
||||
group: 'us-east',
|
||||
apiKey: 'prod-1234',
|
||||
instanceName: 'prod-instance',
|
||||
deploymentName: 'v1-deployment',
|
||||
version: '2023-12-31',
|
||||
baseURL: 'https://prod.example.com',
|
||||
additionalHeaders: {
|
||||
'X-Custom-Header': 'value',
|
||||
},
|
||||
models: {
|
||||
'gpt-4-turbo': {
|
||||
deploymentName: 'gpt-4-turbo-deployment',
|
||||
version: '2023-11-06',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
group: 'us-east',
|
||||
apiKey: 'prod-1234',
|
||||
instanceName: 'prod-instance',
|
||||
deploymentName: 'v1-deployment',
|
||||
version: '2023-12-31',
|
||||
baseURL: 'https://prod.example.com',
|
||||
additionalHeaders: {
|
||||
'X-Custom-Header': 'value',
|
||||
},
|
||||
models: {
|
||||
'gpt-5-turbo': {
|
||||
deploymentName: 'gpt-4-turbo-deployment',
|
||||
version: '2023-11-06',
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
const { isValid } = validateAzureGroups(duplicateGroups);
|
||||
expect(isValid).toBe(false);
|
||||
});
|
||||
it('should not allow duplicate models across groups', () => {
|
||||
const duplicateGroups: TAzureGroups = [
|
||||
{
|
||||
group: 'us-east',
|
||||
apiKey: 'prod-1234',
|
||||
instanceName: 'prod-instance',
|
||||
deploymentName: 'v1-deployment',
|
||||
version: '2023-12-31',
|
||||
baseURL: 'https://prod.example.com',
|
||||
additionalHeaders: {
|
||||
'X-Custom-Header': 'value',
|
||||
},
|
||||
models: {
|
||||
'gpt-4-turbo': {
|
||||
deploymentName: 'gpt-4-turbo-deployment',
|
||||
version: '2023-11-06',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
group: 'us-west',
|
||||
apiKey: 'prod-1234',
|
||||
instanceName: 'prod-instance',
|
||||
deploymentName: 'v1-deployment',
|
||||
version: '2023-12-31',
|
||||
baseURL: 'https://prod.example.com',
|
||||
additionalHeaders: {
|
||||
'X-Custom-Header': 'value',
|
||||
},
|
||||
models: {
|
||||
'gpt-4-turbo': {
|
||||
deploymentName: 'gpt-4-turbo-deployment',
|
||||
version: '2023-11-06',
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
const { isValid } = validateAzureGroups(duplicateGroups);
|
||||
expect(isValid).toBe(false);
|
||||
});
|
||||
|
||||
it('should throw an error if environment variables are set but not configured', () => {
|
||||
const validConfigs: TAzureGroups = [
|
||||
{
|
||||
group: 'librechat-westus',
|
||||
apiKey: '${WESTUS_API_KEY}',
|
||||
instanceName: 'librechat-westus',
|
||||
version: '2023-12-01-preview',
|
||||
models: {
|
||||
'gpt-4-vision-preview': {
|
||||
deploymentName: 'gpt-4-vision-preview',
|
||||
version: '2024-02-15-preview',
|
||||
},
|
||||
'gpt-3.5-turbo': {
|
||||
deploymentName: 'gpt-35-turbo',
|
||||
},
|
||||
'gpt-3.5-turbo-1106': {
|
||||
deploymentName: 'gpt-35-turbo-1106',
|
||||
},
|
||||
'gpt-4': {
|
||||
deploymentName: 'gpt-4',
|
||||
},
|
||||
'gpt-4-1106-preview': {
|
||||
deploymentName: 'gpt-4-1106-preview',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
group: 'librechat-eastus',
|
||||
apiKey: '${EASTUS_API_KEY}',
|
||||
instanceName: 'librechat-eastus',
|
||||
deploymentName: 'gpt-4-turbo',
|
||||
version: '2024-02-15-preview',
|
||||
models: {
|
||||
'gpt-4-turbo': true,
|
||||
},
|
||||
},
|
||||
];
|
||||
const { isValid, modelGroupMap, groupMap } = validateAzureGroups(validConfigs);
|
||||
expect(isValid).toBe(true);
|
||||
expect(() =>
|
||||
mapModelToAzureConfig({ modelName: 'gpt-4-turbo', modelGroupMap, groupMap }),
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it('should list all expected models in both modelGroupMap and groupMap', () => {
|
||||
process.env.WESTUS_API_KEY = 'westus-key';
|
||||
process.env.EASTUS_API_KEY = 'eastus-key';
|
||||
|
||||
const validConfigs: TAzureGroups = [
|
||||
{
|
||||
group: 'librechat-westus',
|
||||
apiKey: '${WESTUS_API_KEY}',
|
||||
instanceName: 'librechat-westus',
|
||||
version: '2023-12-01-preview',
|
||||
models: {
|
||||
'gpt-4-vision-preview': {
|
||||
deploymentName: 'gpt-4-vision-preview',
|
||||
version: '2024-02-15-preview',
|
||||
},
|
||||
'gpt-3.5-turbo': {
|
||||
deploymentName: 'gpt-35-turbo',
|
||||
},
|
||||
'gpt-3.5-turbo-1106': {
|
||||
deploymentName: 'gpt-35-turbo-1106',
|
||||
},
|
||||
'gpt-4': {
|
||||
deploymentName: 'gpt-4',
|
||||
},
|
||||
'gpt-4-1106-preview': {
|
||||
deploymentName: 'gpt-4-1106-preview',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
group: 'librechat-eastus',
|
||||
apiKey: '${EASTUS_API_KEY}',
|
||||
instanceName: 'librechat-eastus',
|
||||
deploymentName: 'gpt-4-turbo',
|
||||
version: '2024-02-15-preview',
|
||||
models: {
|
||||
'gpt-4-turbo': true,
|
||||
},
|
||||
baseURL: 'https://eastus.example.com',
|
||||
additionalHeaders: {
|
||||
'x-api-key': 'x-api-key-value',
|
||||
},
|
||||
},
|
||||
];
|
||||
const { isValid, modelGroupMap, groupMap, modelNames } = validateAzureGroups(validConfigs);
|
||||
expect(isValid).toBe(true);
|
||||
expect(modelNames).toEqual([
|
||||
'gpt-4-vision-preview',
|
||||
'gpt-3.5-turbo',
|
||||
'gpt-3.5-turbo-1106',
|
||||
'gpt-4',
|
||||
'gpt-4-1106-preview',
|
||||
'gpt-4-turbo',
|
||||
]);
|
||||
|
||||
// Check modelGroupMap
|
||||
modelNames.forEach((modelName) => {
|
||||
expect(modelGroupMap[modelName]).toBeDefined();
|
||||
});
|
||||
|
||||
// Check groupMap for 'librechat-westus'
|
||||
expect(groupMap).toHaveProperty('librechat-westus');
|
||||
expect(groupMap['librechat-westus']).toEqual(
|
||||
expect.objectContaining({
|
||||
apiKey: '${WESTUS_API_KEY}',
|
||||
instanceName: 'librechat-westus',
|
||||
version: '2023-12-01-preview',
|
||||
models: expect.objectContaining({
|
||||
'gpt-4-vision-preview': expect.any(Object),
|
||||
'gpt-3.5-turbo': expect.any(Object),
|
||||
'gpt-3.5-turbo-1106': expect.any(Object),
|
||||
'gpt-4': expect.any(Object),
|
||||
'gpt-4-1106-preview': expect.any(Object),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
// Check groupMap for 'librechat-eastus'
|
||||
expect(groupMap).toHaveProperty('librechat-eastus');
|
||||
expect(groupMap['librechat-eastus']).toEqual(
|
||||
expect.objectContaining({
|
||||
apiKey: '${EASTUS_API_KEY}',
|
||||
instanceName: 'librechat-eastus',
|
||||
deploymentName: 'gpt-4-turbo',
|
||||
version: '2024-02-15-preview',
|
||||
models: expect.objectContaining({
|
||||
'gpt-4-turbo': true,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
const { azureOptions: azureOptions1 } = mapModelToAzureConfig({
|
||||
modelName: 'gpt-4-vision-preview',
|
||||
modelGroupMap,
|
||||
groupMap,
|
||||
});
|
||||
expect(azureOptions1).toEqual({
|
||||
azureOpenAIApiKey: 'westus-key',
|
||||
azureOpenAIApiInstanceName: 'librechat-westus',
|
||||
azureOpenAIApiDeploymentName: 'gpt-4-vision-preview',
|
||||
azureOpenAIApiVersion: '2024-02-15-preview',
|
||||
});
|
||||
|
||||
const {
|
||||
azureOptions: azureOptions2,
|
||||
baseURL,
|
||||
headers,
|
||||
} = mapModelToAzureConfig({
|
||||
modelName: 'gpt-4-turbo',
|
||||
modelGroupMap,
|
||||
groupMap,
|
||||
});
|
||||
expect(azureOptions2).toEqual({
|
||||
azureOpenAIApiKey: 'eastus-key',
|
||||
azureOpenAIApiInstanceName: 'librechat-eastus',
|
||||
azureOpenAIApiDeploymentName: 'gpt-4-turbo',
|
||||
azureOpenAIApiVersion: '2024-02-15-preview',
|
||||
});
|
||||
expect(baseURL).toBe('https://eastus.example.com');
|
||||
expect(headers).toEqual({
|
||||
'x-api-key': 'x-api-key-value',
|
||||
});
|
||||
|
||||
const { azureOptions: azureOptions3 } = mapModelToAzureConfig({
|
||||
modelName: 'gpt-4',
|
||||
modelGroupMap,
|
||||
groupMap,
|
||||
});
|
||||
expect(azureOptions3).toEqual({
|
||||
azureOpenAIApiKey: 'westus-key',
|
||||
azureOpenAIApiInstanceName: 'librechat-westus',
|
||||
azureOpenAIApiDeploymentName: 'gpt-4',
|
||||
azureOpenAIApiVersion: '2023-12-01-preview',
|
||||
});
|
||||
|
||||
const { azureOptions: azureOptions4 } = mapModelToAzureConfig({
|
||||
modelName: 'gpt-3.5-turbo',
|
||||
modelGroupMap,
|
||||
groupMap,
|
||||
});
|
||||
expect(azureOptions4).toEqual({
|
||||
azureOpenAIApiKey: 'westus-key',
|
||||
azureOpenAIApiInstanceName: 'librechat-westus',
|
||||
azureOpenAIApiDeploymentName: 'gpt-35-turbo',
|
||||
azureOpenAIApiVersion: '2023-12-01-preview',
|
||||
});
|
||||
|
||||
const { azureOptions: azureOptions5 } = mapModelToAzureConfig({
|
||||
modelName: 'gpt-3.5-turbo-1106',
|
||||
modelGroupMap,
|
||||
groupMap,
|
||||
});
|
||||
expect(azureOptions5).toEqual({
|
||||
azureOpenAIApiKey: 'westus-key',
|
||||
azureOpenAIApiInstanceName: 'librechat-westus',
|
||||
azureOpenAIApiDeploymentName: 'gpt-35-turbo-1106',
|
||||
azureOpenAIApiVersion: '2023-12-01-preview',
|
||||
});
|
||||
|
||||
const { azureOptions: azureOptions6 } = mapModelToAzureConfig({
|
||||
modelName: 'gpt-4-1106-preview',
|
||||
modelGroupMap,
|
||||
groupMap,
|
||||
});
|
||||
expect(azureOptions6).toEqual({
|
||||
azureOpenAIApiKey: 'westus-key',
|
||||
azureOpenAIApiInstanceName: 'librechat-westus',
|
||||
azureOpenAIApiDeploymentName: 'gpt-4-1106-preview',
|
||||
azureOpenAIApiVersion: '2023-12-01-preview',
|
||||
});
|
||||
});
|
||||
});
|
||||
48
packages/data-provider/specs/parsers.spec.ts
Normal file
48
packages/data-provider/specs/parsers.spec.ts
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import { extractEnvVariable } from '../src/parsers';
|
||||
|
||||
describe('extractEnvVariable', () => {
|
||||
const originalEnv = process.env;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
process.env = { ...originalEnv };
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
process.env = originalEnv;
|
||||
});
|
||||
|
||||
test('should return the value of the environment variable', () => {
|
||||
process.env.TEST_VAR = 'test_value';
|
||||
expect(extractEnvVariable('${TEST_VAR}')).toBe('test_value');
|
||||
});
|
||||
|
||||
test('should return the original string if the envrionment variable is not defined correctly', () => {
|
||||
process.env.TEST_VAR = 'test_value';
|
||||
expect(extractEnvVariable('${ TEST_VAR }')).toBe('${ TEST_VAR }');
|
||||
});
|
||||
|
||||
test('should return the original string if environment variable is not set', () => {
|
||||
expect(extractEnvVariable('${NON_EXISTENT_VAR}')).toBe('${NON_EXISTENT_VAR}');
|
||||
});
|
||||
|
||||
test('should return the original string if it does not contain an environment variable', () => {
|
||||
expect(extractEnvVariable('some_string')).toBe('some_string');
|
||||
});
|
||||
|
||||
test('should handle empty strings', () => {
|
||||
expect(extractEnvVariable('')).toBe('');
|
||||
});
|
||||
|
||||
test('should handle strings without variable format', () => {
|
||||
expect(extractEnvVariable('no_var_here')).toBe('no_var_here');
|
||||
});
|
||||
|
||||
test('should not process multiple variable formats', () => {
|
||||
process.env.FIRST_VAR = 'first';
|
||||
process.env.SECOND_VAR = 'second';
|
||||
expect(extractEnvVariable('${FIRST_VAR} and ${SECOND_VAR}')).toBe(
|
||||
'${FIRST_VAR} and ${SECOND_VAR}',
|
||||
);
|
||||
});
|
||||
});
|
||||
211
packages/data-provider/src/azure.ts
Normal file
211
packages/data-provider/src/azure.ts
Normal file
|
|
@ -0,0 +1,211 @@
|
|||
import type { ZodError } from 'zod';
|
||||
import type {
|
||||
TAzureGroups,
|
||||
TAzureGroupMap,
|
||||
TAzureModelGroupMap,
|
||||
TValidatedAzureConfig,
|
||||
} from '../src/config';
|
||||
import { errorsToString, extractEnvVariable, envVarRegex } from '../src/parsers';
|
||||
import { azureGroupConfigsSchema } from '../src/config';
|
||||
|
||||
export const deprecatedAzureVariables = [
|
||||
/* "related to" precedes description text */
|
||||
{ key: 'AZURE_OPENAI_DEFAULT_MODEL', description: 'setting a default model' },
|
||||
{ key: 'AZURE_OPENAI_MODELS', description: 'setting models' },
|
||||
{
|
||||
key: 'AZURE_USE_MODEL_AS_DEPLOYMENT_NAME',
|
||||
description: 'using model names as deployment names',
|
||||
},
|
||||
{ key: 'AZURE_API_KEY', description: 'setting a single Azure API key' },
|
||||
{ key: 'AZURE_OPENAI_API_INSTANCE_NAME', description: 'setting a single Azure instance name' },
|
||||
{
|
||||
key: 'AZURE_OPENAI_API_DEPLOYMENT_NAME',
|
||||
description: 'setting a single Azure deployment name',
|
||||
},
|
||||
{ key: 'AZURE_OPENAI_API_VERSION', description: 'setting a single Azure API version' },
|
||||
{
|
||||
key: 'AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME',
|
||||
description: 'setting a single Azure completions deployment name',
|
||||
},
|
||||
{
|
||||
key: 'AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME',
|
||||
description: 'setting a single Azure embeddings deployment name',
|
||||
},
|
||||
{
|
||||
key: 'PLUGINS_USE_AZURE',
|
||||
description: 'using Azure for Plugins',
|
||||
},
|
||||
];
|
||||
|
||||
export const conflictingAzureVariables = [
|
||||
{
|
||||
key: 'INSTANCE_NAME',
|
||||
},
|
||||
{
|
||||
key: 'DEPLOYMENT_NAME',
|
||||
},
|
||||
];
|
||||
|
||||
export function validateAzureGroups(configs: TAzureGroups): TValidatedAzureConfig & {
|
||||
isValid: boolean;
|
||||
errors: (ZodError | string)[];
|
||||
} {
|
||||
let isValid = true;
|
||||
const modelNames: string[] = [];
|
||||
const modelGroupMap: TAzureModelGroupMap = {};
|
||||
const groupMap: TAzureGroupMap = {};
|
||||
const errors: (ZodError | string)[] = [];
|
||||
|
||||
const result = azureGroupConfigsSchema.safeParse(configs);
|
||||
if (!result.success) {
|
||||
isValid = false;
|
||||
errors.push(errorsToString(result.error.errors));
|
||||
} else {
|
||||
for (const group of result.data) {
|
||||
const {
|
||||
group: groupName,
|
||||
apiKey,
|
||||
instanceName,
|
||||
deploymentName,
|
||||
version,
|
||||
baseURL,
|
||||
additionalHeaders,
|
||||
models,
|
||||
} = group;
|
||||
|
||||
if (groupMap[groupName]) {
|
||||
errors.push(`Duplicate group name detected: "${groupName}". Group names must be unique.`);
|
||||
return { isValid: false, modelNames, modelGroupMap, groupMap, errors };
|
||||
}
|
||||
|
||||
groupMap[groupName] = {
|
||||
apiKey,
|
||||
instanceName,
|
||||
deploymentName,
|
||||
version,
|
||||
baseURL,
|
||||
additionalHeaders,
|
||||
models,
|
||||
};
|
||||
|
||||
for (const modelName in group.models) {
|
||||
modelNames.push(modelName);
|
||||
const model = group.models[modelName];
|
||||
|
||||
if (modelGroupMap[modelName]) {
|
||||
errors.push(
|
||||
`Duplicate model name detected: "${modelName}". Model names must be unique across groups.`,
|
||||
);
|
||||
return { isValid: false, modelNames, modelGroupMap, groupMap, errors };
|
||||
}
|
||||
|
||||
if (typeof model === 'boolean') {
|
||||
// For boolean models, check if group-level deploymentName and version are present.
|
||||
if (!group.deploymentName || !group.version) {
|
||||
errors.push(
|
||||
`Model "${modelName}" in group "${groupName}" is missing a deploymentName or version.`,
|
||||
);
|
||||
return { isValid: false, modelNames, modelGroupMap, groupMap, errors };
|
||||
}
|
||||
|
||||
modelGroupMap[modelName] = {
|
||||
group: groupName,
|
||||
};
|
||||
} else {
|
||||
// For object models, check if deploymentName and version are required but missing.
|
||||
if (
|
||||
(!model.deploymentName && !group.deploymentName) ||
|
||||
(!model.version && !group.version)
|
||||
) {
|
||||
errors.push(
|
||||
`Model "${modelName}" in group "${groupName}" is missing a required deploymentName or version.`,
|
||||
);
|
||||
return { isValid: false, modelNames, modelGroupMap, groupMap, errors };
|
||||
}
|
||||
|
||||
modelGroupMap[modelName] = {
|
||||
group: groupName,
|
||||
// deploymentName: model.deploymentName || group.deploymentName,
|
||||
// version: model.version || group.version,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { isValid, modelNames, modelGroupMap, groupMap, errors };
|
||||
}
|
||||
|
||||
type AzureOptions = {
|
||||
azureOpenAIApiKey: string;
|
||||
azureOpenAIApiInstanceName: string;
|
||||
azureOpenAIApiDeploymentName: string;
|
||||
azureOpenAIApiVersion: string;
|
||||
};
|
||||
|
||||
type MappedAzureConfig = {
|
||||
azureOptions: AzureOptions;
|
||||
baseURL?: string;
|
||||
headers?: Record<string, string>;
|
||||
};
|
||||
|
||||
export function mapModelToAzureConfig({
|
||||
modelName,
|
||||
modelGroupMap,
|
||||
groupMap,
|
||||
}: Omit<TValidatedAzureConfig, 'modelNames'> & {
|
||||
modelName: string;
|
||||
}): MappedAzureConfig {
|
||||
const modelConfig = modelGroupMap[modelName];
|
||||
if (!modelConfig) {
|
||||
throw new Error(`Model named "${modelName}" not found in configuration.`);
|
||||
}
|
||||
|
||||
const groupConfig = groupMap[modelConfig.group];
|
||||
if (!groupConfig) {
|
||||
throw new Error(
|
||||
`Group "${modelConfig.group}" for model "${modelName}" not found in configuration.`,
|
||||
);
|
||||
}
|
||||
|
||||
const modelDetails = groupConfig.models[modelName];
|
||||
const deploymentName =
|
||||
typeof modelDetails === 'object'
|
||||
? modelDetails.deploymentName || groupConfig.deploymentName
|
||||
: groupConfig.deploymentName;
|
||||
const version =
|
||||
typeof modelDetails === 'object'
|
||||
? modelDetails.version || groupConfig.version
|
||||
: groupConfig.version;
|
||||
|
||||
if (!deploymentName || !version) {
|
||||
throw new Error(
|
||||
`Model "${modelName}" in group "${modelConfig.group}" is missing a deploymentName ("${deploymentName}") or version ("${version}").`,
|
||||
);
|
||||
}
|
||||
|
||||
const azureOptions: AzureOptions = {
|
||||
azureOpenAIApiKey: extractEnvVariable(groupConfig.apiKey),
|
||||
azureOpenAIApiInstanceName: extractEnvVariable(groupConfig.instanceName),
|
||||
azureOpenAIApiDeploymentName: extractEnvVariable(deploymentName),
|
||||
azureOpenAIApiVersion: extractEnvVariable(version),
|
||||
};
|
||||
|
||||
for (const value of Object.values(azureOptions)) {
|
||||
if (typeof value === 'string' && envVarRegex.test(value)) {
|
||||
throw new Error(`Azure configuration environment variable "${value}" was not found.`);
|
||||
}
|
||||
}
|
||||
|
||||
const result: MappedAzureConfig = { azureOptions };
|
||||
|
||||
if (groupConfig.baseURL) {
|
||||
result.baseURL = extractEnvVariable(groupConfig.baseURL);
|
||||
}
|
||||
|
||||
if (groupConfig.additionalHeaders) {
|
||||
result.headers = groupConfig.additionalHeaders;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
@ -8,6 +8,55 @@ export const defaultSocialLogins = ['google', 'facebook', 'openid', 'github', 'd
|
|||
|
||||
export const fileSourceSchema = z.nativeEnum(FileSources);
|
||||
|
||||
export const modelConfigSchema = z
|
||||
.object({
|
||||
deploymentName: z.string().optional(),
|
||||
version: z.string().optional(),
|
||||
})
|
||||
.or(z.boolean());
|
||||
|
||||
export type TAzureModelConfig = z.infer<typeof modelConfigSchema>;
|
||||
|
||||
export const azureBaseSchema = z.object({
|
||||
apiKey: z.string(),
|
||||
instanceName: z.string(),
|
||||
deploymentName: z.string().optional(),
|
||||
version: z.string().optional(),
|
||||
baseURL: z.string().optional(),
|
||||
additionalHeaders: z.record(z.any()).optional(),
|
||||
});
|
||||
|
||||
export type TAzureBaseSchema = z.infer<typeof azureBaseSchema>;
|
||||
|
||||
export const azureGroupSchema = z
|
||||
.object({
|
||||
group: z.string(),
|
||||
models: z.record(z.string(), modelConfigSchema),
|
||||
})
|
||||
.required()
|
||||
.and(azureBaseSchema);
|
||||
|
||||
export const azureGroupConfigsSchema = z.array(azureGroupSchema).min(1);
|
||||
export type TAzureGroups = z.infer<typeof azureGroupConfigsSchema>;
|
||||
|
||||
export type TAzureModelMapSchema = {
|
||||
// deploymentName?: string;
|
||||
// version?: string;
|
||||
group: string;
|
||||
};
|
||||
|
||||
export type TAzureModelGroupMap = Record<string, TAzureModelMapSchema>;
|
||||
export type TAzureGroupMap = Record<
|
||||
string,
|
||||
TAzureBaseSchema & { models: Record<string, TAzureModelConfig> }
|
||||
>;
|
||||
|
||||
export type TValidatedAzureConfig = {
|
||||
modelNames: string[];
|
||||
modelGroupMap: TAzureModelGroupMap;
|
||||
groupMap: TAzureGroupMap;
|
||||
};
|
||||
|
||||
export const assistantEndpointSchema = z.object({
|
||||
/* assistants specific */
|
||||
disableBuilder: z.boolean().optional(),
|
||||
|
|
@ -56,8 +105,30 @@ export const endpointSchema = z.object({
|
|||
headers: z.record(z.any()).optional(),
|
||||
addParams: z.record(z.any()).optional(),
|
||||
dropParams: z.array(z.string()).optional(),
|
||||
customOrder: z.number().optional(),
|
||||
});
|
||||
|
||||
export const azureEndpointSchema = z
|
||||
.object({
|
||||
groups: azureGroupConfigsSchema,
|
||||
plugins: z.boolean().optional(),
|
||||
})
|
||||
.and(
|
||||
endpointSchema
|
||||
.pick({
|
||||
titleConvo: true,
|
||||
titleMethod: true,
|
||||
titleModel: true,
|
||||
summarize: true,
|
||||
summaryModel: true,
|
||||
customOrder: true,
|
||||
})
|
||||
.partial(),
|
||||
);
|
||||
|
||||
export type TAzureConfig = Omit<z.infer<typeof azureEndpointSchema>, 'groups'> &
|
||||
TValidatedAzureConfig;
|
||||
|
||||
export const rateLimitSchema = z.object({
|
||||
fileUploads: z
|
||||
.object({
|
||||
|
|
@ -83,6 +154,7 @@ export const configSchema = z.object({
|
|||
fileConfig: fileConfigSchema.optional(),
|
||||
endpoints: z
|
||||
.object({
|
||||
[EModelEndpoint.azureOpenAI]: azureEndpointSchema.optional(),
|
||||
[EModelEndpoint.assistants]: assistantEndpointSchema.optional(),
|
||||
custom: z.array(endpointSchema.partial()).optional(),
|
||||
})
|
||||
|
|
@ -371,7 +443,7 @@ export enum Constants {
|
|||
/**
|
||||
* Key for the Custom Config's version (librechat.yaml).
|
||||
*/
|
||||
CONFIG_VERSION = '1.0.3',
|
||||
CONFIG_VERSION = '1.0.4',
|
||||
/**
|
||||
* Standard value for the first message's `parentMessageId` value, to indicate no parent exists.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
/* config */
|
||||
export * from './azure';
|
||||
export * from './config';
|
||||
export * from './file-config';
|
||||
/* schema helpers */
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import type { ZodIssue } from 'zod';
|
||||
import type { TConversation, TPreset } from './schemas';
|
||||
import type { TEndpointOption } from './types';
|
||||
import type { TConfig, TEndpointOption, TEndpointsConfig } from './types';
|
||||
import {
|
||||
EModelEndpoint,
|
||||
openAISchema,
|
||||
|
|
@ -42,6 +43,101 @@ const endpointSchemas: Record<EModelEndpoint, EndpointSchema> = {
|
|||
// [EModelEndpoint.google]: createGoogleSchema,
|
||||
// };
|
||||
|
||||
/** Get the enabled endpoints from the `ENDPOINTS` environment variable */
|
||||
export function getEnabledEndpoints() {
|
||||
const defaultEndpoints: string[] = [
|
||||
EModelEndpoint.openAI,
|
||||
EModelEndpoint.assistants,
|
||||
EModelEndpoint.azureOpenAI,
|
||||
EModelEndpoint.google,
|
||||
EModelEndpoint.bingAI,
|
||||
EModelEndpoint.chatGPTBrowser,
|
||||
EModelEndpoint.gptPlugins,
|
||||
EModelEndpoint.anthropic,
|
||||
];
|
||||
|
||||
const endpointsEnv = process.env.ENDPOINTS || '';
|
||||
let enabledEndpoints = defaultEndpoints;
|
||||
if (endpointsEnv) {
|
||||
enabledEndpoints = endpointsEnv
|
||||
.split(',')
|
||||
.filter((endpoint) => endpoint?.trim())
|
||||
.map((endpoint) => endpoint.trim());
|
||||
}
|
||||
return enabledEndpoints;
|
||||
}
|
||||
|
||||
/** Orders an existing EndpointsConfig object based on enabled endpoint/custom ordering */
|
||||
export function orderEndpointsConfig(endpointsConfig: TEndpointsConfig) {
|
||||
if (!endpointsConfig) {
|
||||
return {};
|
||||
}
|
||||
const enabledEndpoints = getEnabledEndpoints();
|
||||
const endpointKeys = Object.keys(endpointsConfig);
|
||||
const defaultCustomIndex = enabledEndpoints.indexOf(EModelEndpoint.custom);
|
||||
return endpointKeys.reduce(
|
||||
(accumulatedConfig: Record<string, TConfig | null | undefined>, currentEndpointKey) => {
|
||||
const isCustom = !(currentEndpointKey in EModelEndpoint);
|
||||
const isEnabled = enabledEndpoints.includes(currentEndpointKey);
|
||||
if (!isEnabled && !isCustom) {
|
||||
return accumulatedConfig;
|
||||
}
|
||||
|
||||
const index = enabledEndpoints.indexOf(currentEndpointKey);
|
||||
|
||||
if (isCustom) {
|
||||
accumulatedConfig[currentEndpointKey] = {
|
||||
order: defaultCustomIndex >= 0 ? defaultCustomIndex : 9999,
|
||||
...(endpointsConfig[currentEndpointKey] as Omit<TConfig, 'order'> & { order?: number }),
|
||||
};
|
||||
} else if (endpointsConfig[currentEndpointKey]) {
|
||||
accumulatedConfig[currentEndpointKey] = {
|
||||
...endpointsConfig[currentEndpointKey],
|
||||
order: index,
|
||||
};
|
||||
}
|
||||
return accumulatedConfig;
|
||||
},
|
||||
{},
|
||||
);
|
||||
}
|
||||
|
||||
/** Converts an array of Zod issues into a string. */
|
||||
export function errorsToString(errors: ZodIssue[]) {
|
||||
return errors
|
||||
.map((error) => {
|
||||
const field = error.path.join('.');
|
||||
const message = error.message;
|
||||
|
||||
return `${field}: ${message}`;
|
||||
})
|
||||
.join(' ');
|
||||
}
|
||||
|
||||
export const envVarRegex = /^\${(.+)}$/;
|
||||
|
||||
/** Extracts the value of an environment variable from a string. */
|
||||
export function extractEnvVariable(value: string) {
|
||||
const envVarMatch = value.match(envVarRegex);
|
||||
if (envVarMatch) {
|
||||
return process.env[envVarMatch[1]] || value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/** Resolves header values to env variables if detected */
|
||||
export function resolveHeaders(headers: Record<string, string> | undefined) {
|
||||
const resolvedHeaders = { ...(headers ?? {}) };
|
||||
|
||||
if (headers && typeof headers === 'object' && !Array.isArray(headers)) {
|
||||
Object.keys(headers).forEach((key) => {
|
||||
resolvedHeaders[key] = extractEnvVariable(headers[key]);
|
||||
});
|
||||
}
|
||||
|
||||
return resolvedHeaders;
|
||||
}
|
||||
|
||||
export function getFirstDefinedValue(possibleValues: string[]) {
|
||||
let returnValue;
|
||||
for (const value of possibleValues) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue