mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-23 11:50:14 +01:00
🅰️ feat: Azure OpenAI Assistants API Support (#1992)
* chore: rename dir from `assistant` to plural * feat: `assistants` field for azure config, spread options in AppService * refactor: rename constructAzureURL param for azure as `azureOptions` * chore: bump openai and bun * chore(loadDefaultModels): change naming of assistant -> assistants * feat: load azure settings with currect baseURL for assistants' initializeClient * refactor: add `assistants` flags to groups and model configs, add mapGroupToAzureConfig * feat(loadConfigEndpoints): initialize assistants endpoint if azure flag `assistants` is enabled * feat(AppService): determine assistant models on startup, throw Error if none * refactor(useDeleteAssistantMutation): send model along with assistant id for delete mutations * feat: support listing and deleting assistants with azure * feat: add model query to assistant avatar upload * feat: add azure support for retrieveRun method * refactor: update OpenAIClient initialization * chore: update README * fix(ci): tests passing * refactor(uploadOpenAIFile): improve logging and use more efficient REST API method * refactor(useFileHandling): add model to metadata to target Azure region compatible with current model * chore(files): add azure naming pattern for valid file id recognition * fix(assistants): initialize openai with first available assistant model if none provided * refactor(uploadOpenAIFile): add content type for azure, initialize formdata before azure options * refactor(sleep): move sleep function out of Runs and into `~/server/utils` * fix(azureOpenAI/assistants): make sure to only overwrite models with assistant models if `assistants` flag is enabled * refactor(uploadOpenAIFile): revert to old method * chore(uploadOpenAIFile): use enum for file purpose * docs: azureOpenAI update guide with more info, examples * feat: enable/disable assistant capabilities and specify retrieval models * refactor: optional chain conditional statement in loadConfigModels.js * docs: add assistants examples * chore: update librechat.example.yaml * docs(azure): update note of file upload behavior in Azure OpenAI Assistants * chore: update docs and add descriptive message about assistant errors * fix: prevent message submission with invalid assistant or if files loading * style: update Landing icon & text when assistant is not selected * chore: bump librechat-data-provider to 0.4.8 * fix(assistants/azure): assign req.body.model for proper azure init to abort runs
This commit is contained in:
parent
1b243c6f8c
commit
5cd5c3bef8
60 changed files with 1044 additions and 300 deletions
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "librechat-data-provider",
|
||||
"version": "0.4.7",
|
||||
"version": "0.4.8",
|
||||
"description": "data services for librechat apps",
|
||||
"main": "dist/index.js",
|
||||
"module": "dist/index.es.js",
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import type { TAzureGroups } from '../src/config';
|
||||
import { validateAzureGroups, mapModelToAzureConfig } from '../src/azure';
|
||||
import { validateAzureGroups, mapModelToAzureConfig, mapGroupToAzureConfig } from '../src/azure';
|
||||
|
||||
describe('validateAzureGroups', () => {
|
||||
it('should validate a correct configuration', () => {
|
||||
|
|
@ -785,3 +785,57 @@ describe('validateAzureGroups with modelGroupMap and groupMap', () => {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('mapGroupToAzureConfig', () => {
|
||||
// Test setup for a basic config with 2 groups
|
||||
const groupMap = {
|
||||
group1: {
|
||||
apiKey: 'key-for-group1',
|
||||
instanceName: 'instance-group1',
|
||||
models: {
|
||||
model1: { deploymentName: 'deployment1', version: '1.0' },
|
||||
},
|
||||
},
|
||||
group2: {
|
||||
apiKey: 'key-for-group2',
|
||||
instanceName: 'instance-group2',
|
||||
serverless: true,
|
||||
baseURL: 'https://group2.example.com',
|
||||
models: {
|
||||
model2: true, // demonstrating a boolean style model configuration
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
it('should successfully map non-serverless group configuration', () => {
|
||||
const groupName = 'group1';
|
||||
const result = mapGroupToAzureConfig({ groupName, groupMap });
|
||||
expect(result).toEqual({
|
||||
azureOptions: expect.objectContaining({
|
||||
azureOpenAIApiKey: 'key-for-group1',
|
||||
azureOpenAIApiInstanceName: 'instance-group1',
|
||||
azureOpenAIApiDeploymentName: expect.any(String),
|
||||
azureOpenAIApiVersion: expect.any(String),
|
||||
}),
|
||||
});
|
||||
});
|
||||
|
||||
it('should successfully map serverless group configuration', () => {
|
||||
const groupName = 'group2';
|
||||
const result = mapGroupToAzureConfig({ groupName, groupMap });
|
||||
expect(result).toEqual({
|
||||
azureOptions: expect.objectContaining({
|
||||
azureOpenAIApiKey: 'key-for-group2',
|
||||
}),
|
||||
baseURL: 'https://group2.example.com',
|
||||
serverless: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw error for nonexistent group name', () => {
|
||||
const groupName = 'nonexistent-group';
|
||||
expect(() => {
|
||||
mapGroupToAzureConfig({ groupName, groupMap });
|
||||
}).toThrow(`Group named "${groupName}" not found in configuration.`);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -66,7 +66,20 @@ export const plugins = () => '/api/plugins';
|
|||
|
||||
export const config = () => '/api/config';
|
||||
|
||||
export const assistants = (id?: string) => `/api/assistants${id ? `/${id}` : ''}`;
|
||||
export const assistants = (id?: string, options?: Record<string, string>) => {
|
||||
let url = '/api/assistants';
|
||||
|
||||
if (id) {
|
||||
url += `/${id}`;
|
||||
}
|
||||
|
||||
if (options && Object.keys(options).length > 0) {
|
||||
const queryParams = new URLSearchParams(options).toString();
|
||||
url += `?${queryParams}`;
|
||||
}
|
||||
|
||||
return url;
|
||||
};
|
||||
|
||||
export const files = () => '/api/files';
|
||||
|
||||
|
|
|
|||
|
|
@ -234,14 +234,16 @@ export function mapModelToAzureConfig({
|
|||
}
|
||||
|
||||
const modelDetails = groupConfig.models[modelName];
|
||||
const deploymentName =
|
||||
const { deploymentName, version } =
|
||||
typeof modelDetails === 'object'
|
||||
? modelDetails.deploymentName || groupConfig.deploymentName
|
||||
: groupConfig.deploymentName;
|
||||
const version =
|
||||
typeof modelDetails === 'object'
|
||||
? modelDetails.version || groupConfig.version
|
||||
: groupConfig.version;
|
||||
? {
|
||||
deploymentName: modelDetails.deploymentName || groupConfig.deploymentName,
|
||||
version: modelDetails.version || groupConfig.version,
|
||||
}
|
||||
: {
|
||||
deploymentName: groupConfig.deploymentName,
|
||||
version: groupConfig.version,
|
||||
};
|
||||
|
||||
if (!deploymentName || !version) {
|
||||
throw new Error(
|
||||
|
|
@ -274,3 +276,86 @@ export function mapModelToAzureConfig({
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
export function mapGroupToAzureConfig({
|
||||
groupName,
|
||||
groupMap,
|
||||
}: {
|
||||
groupName: string;
|
||||
groupMap: TAzureGroupMap;
|
||||
}): MappedAzureConfig {
|
||||
const groupConfig = groupMap[groupName];
|
||||
if (!groupConfig) {
|
||||
throw new Error(`Group named "${groupName}" not found in configuration.`);
|
||||
}
|
||||
|
||||
const instanceName = groupConfig.instanceName as string;
|
||||
|
||||
if (!instanceName && !groupConfig.serverless) {
|
||||
throw new Error(
|
||||
`Group "${groupName}" is missing an instanceName for non-serverless configuration.`,
|
||||
);
|
||||
}
|
||||
|
||||
if (groupConfig.serverless && !groupConfig.baseURL) {
|
||||
throw new Error(
|
||||
`Group "${groupName}" is missing the required base URL for serverless configuration.`,
|
||||
);
|
||||
}
|
||||
|
||||
const models = Object.keys(groupConfig.models);
|
||||
if (models.length === 0) {
|
||||
throw new Error(`Group "${groupName}" does not have any models configured.`);
|
||||
}
|
||||
|
||||
// Use the first available model in the group
|
||||
const firstModelName = models[0];
|
||||
const modelDetails = groupConfig.models[firstModelName];
|
||||
|
||||
const azureOptions: AzureOptions = {
|
||||
azureOpenAIApiKey: extractEnvVariable(groupConfig.apiKey),
|
||||
azureOpenAIApiInstanceName: extractEnvVariable(instanceName),
|
||||
// DeploymentName and Version set below
|
||||
};
|
||||
|
||||
if (groupConfig.serverless) {
|
||||
return {
|
||||
azureOptions,
|
||||
baseURL: extractEnvVariable(groupConfig.baseURL ?? ''),
|
||||
serverless: true,
|
||||
...(groupConfig.additionalHeaders && { headers: groupConfig.additionalHeaders }),
|
||||
};
|
||||
}
|
||||
|
||||
const { deploymentName, version } =
|
||||
typeof modelDetails === 'object'
|
||||
? {
|
||||
deploymentName: modelDetails.deploymentName || groupConfig.deploymentName,
|
||||
version: modelDetails.version || groupConfig.version,
|
||||
}
|
||||
: {
|
||||
deploymentName: groupConfig.deploymentName,
|
||||
version: groupConfig.version,
|
||||
};
|
||||
|
||||
if (!deploymentName || !version) {
|
||||
throw new Error(
|
||||
`Model "${firstModelName}" in group "${groupName}" or the group itself is missing a deploymentName ("${deploymentName}") or version ("${version}").`,
|
||||
);
|
||||
}
|
||||
|
||||
azureOptions.azureOpenAIApiDeploymentName = extractEnvVariable(deploymentName);
|
||||
azureOptions.azureOpenAIApiVersion = extractEnvVariable(version);
|
||||
|
||||
const result: MappedAzureConfig = { azureOptions };
|
||||
|
||||
if (groupConfig.baseURL) {
|
||||
result.baseURL = extractEnvVariable(groupConfig.baseURL);
|
||||
}
|
||||
|
||||
if (groupConfig.additionalHeaders) {
|
||||
result.headers = groupConfig.additionalHeaders;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,12 +6,25 @@ import { FileSources } from './types/files';
|
|||
|
||||
export const defaultSocialLogins = ['google', 'facebook', 'openid', 'github', 'discord'];
|
||||
|
||||
export const defaultRetrievalModels = [
|
||||
'gpt-4-turbo-preview',
|
||||
'gpt-3.5-turbo-0125',
|
||||
'gpt-4-0125-preview',
|
||||
'gpt-4-1106-preview',
|
||||
'gpt-3.5-turbo-1106',
|
||||
'gpt-3.5-turbo-0125',
|
||||
'gpt-4-turbo',
|
||||
'gpt-4-0125',
|
||||
'gpt-4-1106',
|
||||
];
|
||||
|
||||
export const fileSourceSchema = z.nativeEnum(FileSources);
|
||||
|
||||
export const modelConfigSchema = z
|
||||
.object({
|
||||
deploymentName: z.string().optional(),
|
||||
version: z.string().optional(),
|
||||
assistants: z.boolean().optional(),
|
||||
})
|
||||
.or(z.boolean());
|
||||
|
||||
|
|
@ -22,6 +35,7 @@ export const azureBaseSchema = z.object({
|
|||
serverless: z.boolean().optional(),
|
||||
instanceName: z.string().optional(),
|
||||
deploymentName: z.string().optional(),
|
||||
assistants: z.boolean().optional(),
|
||||
addParams: z.record(z.any()).optional(),
|
||||
dropParams: z.array(z.string()).optional(),
|
||||
forcePrompt: z.boolean().optional(),
|
||||
|
|
@ -61,6 +75,13 @@ export type TValidatedAzureConfig = {
|
|||
groupMap: TAzureGroupMap;
|
||||
};
|
||||
|
||||
export enum Capabilities {
|
||||
code_interpreter = 'code_interpreter',
|
||||
retrieval = 'retrieval',
|
||||
actions = 'actions',
|
||||
tools = 'tools',
|
||||
}
|
||||
|
||||
export const assistantEndpointSchema = z.object({
|
||||
/* assistants specific */
|
||||
disableBuilder: z.boolean().optional(),
|
||||
|
|
@ -68,6 +89,16 @@ export const assistantEndpointSchema = z.object({
|
|||
timeoutMs: z.number().optional(),
|
||||
supportedIds: z.array(z.string()).min(1).optional(),
|
||||
excludedIds: z.array(z.string()).min(1).optional(),
|
||||
retrievalModels: z.array(z.string()).min(1).optional().default(defaultRetrievalModels),
|
||||
capabilities: z
|
||||
.array(z.nativeEnum(Capabilities))
|
||||
.optional()
|
||||
.default([
|
||||
Capabilities.code_interpreter,
|
||||
Capabilities.retrieval,
|
||||
Capabilities.actions,
|
||||
Capabilities.tools,
|
||||
]),
|
||||
/* general */
|
||||
apiKey: z.string().optional(),
|
||||
baseURL: z.string().optional(),
|
||||
|
|
@ -116,6 +147,7 @@ export const azureEndpointSchema = z
|
|||
.object({
|
||||
groups: azureGroupConfigsSchema,
|
||||
plugins: z.boolean().optional(),
|
||||
assistants: z.boolean().optional(),
|
||||
})
|
||||
.and(
|
||||
endpointSchema
|
||||
|
|
@ -288,14 +320,6 @@ export const defaultModels = {
|
|||
],
|
||||
};
|
||||
|
||||
export const supportsRetrieval = new Set([
|
||||
'gpt-3.5-turbo-0125',
|
||||
'gpt-4-0125-preview',
|
||||
'gpt-4-turbo-preview',
|
||||
'gpt-4-1106-preview',
|
||||
'gpt-3.5-turbo-1106',
|
||||
]);
|
||||
|
||||
export const EndpointURLs: { [key in EModelEndpoint]: string } = {
|
||||
[EModelEndpoint.openAI]: `/api/ask/${EModelEndpoint.openAI}`,
|
||||
[EModelEndpoint.bingAI]: `/api/ask/${EModelEndpoint.bingAI}`,
|
||||
|
|
@ -485,7 +509,7 @@ export enum Constants {
|
|||
/**
|
||||
* Key for the Custom Config's version (librechat.yaml).
|
||||
*/
|
||||
CONFIG_VERSION = '1.0.4',
|
||||
CONFIG_VERSION = '1.0.5',
|
||||
/**
|
||||
* Standard value for the first message's `parentMessageId` value, to indicate no parent exists.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -186,8 +186,8 @@ export const updateAssistant = (
|
|||
return request.patch(endpoints.assistants(assistant_id), data);
|
||||
};
|
||||
|
||||
export const deleteAssistant = (assistant_id: string): Promise<void> => {
|
||||
return request.delete(endpoints.assistants(assistant_id));
|
||||
export const deleteAssistant = (assistant_id: string, model: string): Promise<void> => {
|
||||
return request.delete(endpoints.assistants(assistant_id, { model }));
|
||||
};
|
||||
|
||||
export const listAssistants = (
|
||||
|
|
@ -225,7 +225,10 @@ export const uploadAvatar = (data: FormData): Promise<f.AvatarUploadResponse> =>
|
|||
};
|
||||
|
||||
export const uploadAssistantAvatar = (data: m.AssistantAvatarVariables): Promise<a.Assistant> => {
|
||||
return request.postMultiPart(endpoints.assistants(`avatar/${data.assistant_id}`), data.formData);
|
||||
return request.postMultiPart(
|
||||
endpoints.assistants(`avatar/${data.assistant_id}`, { model: data.model }),
|
||||
data.formData,
|
||||
);
|
||||
};
|
||||
|
||||
export const updateAction = (data: m.UpdateActionVariables): Promise<m.UpdateActionResponse> => {
|
||||
|
|
|
|||
|
|
@ -146,6 +146,8 @@ export type TConfig = {
|
|||
userProvide?: boolean | null;
|
||||
userProvideURL?: boolean | null;
|
||||
disableBuilder?: boolean;
|
||||
retrievalModels?: string[];
|
||||
capabilities?: string[];
|
||||
};
|
||||
|
||||
export type TEndpointsConfig =
|
||||
|
|
|
|||
|
|
@ -46,6 +46,7 @@ export type LogoutOptions = {
|
|||
|
||||
export type AssistantAvatarVariables = {
|
||||
assistant_id: string;
|
||||
model: string;
|
||||
formData: FormData;
|
||||
postCreation?: boolean;
|
||||
};
|
||||
|
|
@ -86,6 +87,8 @@ export type UpdateAssistantMutationOptions = {
|
|||
) => void;
|
||||
};
|
||||
|
||||
export type DeleteAssistantBody = { assistant_id: string; model: string };
|
||||
|
||||
export type DeleteAssistantMutationOptions = {
|
||||
onSuccess?: (data: void, variables: { assistant_id: string }, context?: unknown) => void;
|
||||
onMutate?: (variables: { assistant_id: string }) => void | Promise<unknown>;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue