From e6310c806a19c90a4eca0e3b3452154eb0ea9a01 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Fri, 19 Apr 2024 19:05:25 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20fix:=20Minor=20Assistants=20Endp?= =?UTF-8?q?oint=20Fixes=20(#2472)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(useCreateAssistantMutation): force re-render of assistants map by avoiding use shallow reference of listRes.data * fix(AppService): regression by not including azure assistant defaults when no assistant endpoint values are set --- api/server/services/AppService.js | 11 +++++++++-- api/server/services/AppService.spec.js | 25 +++++++++++++++++++++--- api/server/services/start/assistants.js | 26 +++++++++++++++---------- client/src/data-provider/mutations.ts | 3 +-- 4 files changed, 48 insertions(+), 17 deletions(-) diff --git a/api/server/services/AppService.js b/api/server/services/AppService.js index c9c42a2a1a..87c4a2394f 100644 --- a/api/server/services/AppService.js +++ b/api/server/services/AppService.js @@ -5,8 +5,8 @@ const { defaultSocialLogins, } = require('librechat-data-provider'); const { checkVariables, checkHealth, checkConfig, checkAzureVariables } = require('./start/checks'); +const { azureAssistantsDefaults, assistantsConfigSetup } = require('./start/assistants'); const { initializeFirebase } = require('./Files/Firebase/initialize'); -const { assistantsConfigSetup } = require('./start/assistants'); const loadCustomConfig = require('./Config/loadCustomConfig'); const handleRateLimits = require('./Config/handleRateLimits'); const { azureConfigSetup } = require('./start/azureOpenAI'); @@ -70,8 +70,15 @@ const AppService = async (app) => { checkAzureVariables(); } + if (config?.endpoints?.[EModelEndpoint.azureOpenAI]?.assistants) { + endpointLocals[EModelEndpoint.assistants] = azureAssistantsDefaults(); + } + if (config?.endpoints?.[EModelEndpoint.assistants]) { - endpointLocals[EModelEndpoint.assistants] = assistantsConfigSetup(config); + endpointLocals[EModelEndpoint.assistants] = assistantsConfigSetup( + config, + endpointLocals[EModelEndpoint.assistants], + ); } app.locals = { diff --git a/api/server/services/AppService.spec.js b/api/server/services/AppService.spec.js index dd8e0c0068..bf71ece6ed 100644 --- a/api/server/services/AppService.spec.js +++ b/api/server/services/AppService.spec.js @@ -154,9 +154,7 @@ describe('AppService', () => { }); it('should default to `PNG` `imageOutputType` with no provided config', async () => { - require('./Config/loadCustomConfig').mockImplementationOnce(() => - Promise.resolve(undefined), - ); + require('./Config/loadCustomConfig').mockImplementationOnce(() => Promise.resolve(undefined)); await AppService(app); expect(app.locals.imageOutputType).toEqual(EImageOutputType.PNG); @@ -230,6 +228,27 @@ describe('AppService', () => { ); }); + it('should correctly configure minimum Azure OpenAI Assistant values', async () => { + const assistantGroups = [azureGroups[0], { ...azureGroups[1], assistants: true }]; + require('./Config/loadCustomConfig').mockImplementationOnce(() => + Promise.resolve({ + endpoints: { + [EModelEndpoint.azureOpenAI]: { + groups: assistantGroups, + assistants: true, + }, + }, + }), + ); + + process.env.WESTUS_API_KEY = 'westus-key'; + process.env.EASTUS_API_KEY = 'eastus-key'; + + await AppService(app); + expect(app.locals).toHaveProperty(EModelEndpoint.assistants); + expect(app.locals[EModelEndpoint.assistants].capabilities.length).toEqual(3); + }); + it('should correctly configure Azure OpenAI endpoint based on custom config', async () => { require('./Config/loadCustomConfig').mockImplementationOnce(() => Promise.resolve({ diff --git a/api/server/services/start/assistants.js b/api/server/services/start/assistants.js index c092318ce8..dfef99e591 100644 --- a/api/server/services/start/assistants.js +++ b/api/server/services/start/assistants.js @@ -6,11 +6,23 @@ const { const { logger } = require('~/config'); /** - * Sets up the Assistants configuration from the config (`librechat.yaml`) file. - * @param {TCustomConfig} config - The loaded custom configuration. + * Sets up the minimum, default Assistants configuration if Azure OpenAI Assistants option is enabled. * @returns {Partial} The Assistants endpoint configuration. */ -function assistantsConfigSetup(config) { +function azureAssistantsDefaults() { + return { + capabilities: [Capabilities.tools, Capabilities.actions, Capabilities.code_interpreter], + }; +} + +/** + * Sets up the Assistants configuration from the config (`librechat.yaml`) file. + * @param {TCustomConfig} config - The loaded custom configuration. + * @param {Partial} [prevConfig] + * - The previously loaded assistants configuration from Azure OpenAI Assistants option. + * @returns {Partial} The Assistants endpoint configuration. + */ +function assistantsConfigSetup(config, prevConfig = {}) { const assistantsConfig = config.endpoints[EModelEndpoint.assistants]; const parsedConfig = assistantEndpointSchema.parse(assistantsConfig); if (assistantsConfig.supportedIds?.length && assistantsConfig.excludedIds?.length) { @@ -19,12 +31,6 @@ function assistantsConfigSetup(config) { ); } - const prevConfig = config.endpoints[EModelEndpoint.azureOpenAI]?.assistants - ? { - capabilities: [Capabilities.tools, Capabilities.actions, Capabilities.code_interpreter], - } - : {}; - return { ...prevConfig, retrievalModels: parsedConfig.retrievalModels, @@ -37,4 +43,4 @@ function assistantsConfigSetup(config) { }; } -module.exports = { assistantsConfigSetup }; +module.exports = { azureAssistantsDefaults, assistantsConfigSetup }; diff --git a/client/src/data-provider/mutations.ts b/client/src/data-provider/mutations.ts index a38ff09a33..bada848247 100644 --- a/client/src/data-provider/mutations.ts +++ b/client/src/data-provider/mutations.ts @@ -315,8 +315,7 @@ export const useCreateAssistantMutation = ( return options?.onSuccess?.(newAssistant, variables, context); } - const currentAssistants = listRes.data; - currentAssistants.push(newAssistant); + const currentAssistants = [newAssistant, ...JSON.parse(JSON.stringify(listRes.data))]; queryClient.setQueryData([QueryKeys.assistants, defaultOrderQuery], { ...listRes,