Skip to content

Commit

Permalink
💽 refactor(client): Optimize ModelsConfig Query Cache (#2330)
Browse files Browse the repository at this point in the history
* refactor(client): remove double caching of models via recoil to rely exclusively on react-query

* chore(useConversation): add modelsQuery.data dep to callback
  • Loading branch information
danny-avila authored Apr 5, 2024
1 parent fb80af0 commit f6a8488
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 94 deletions.
5 changes: 3 additions & 2 deletions client/src/components/Endpoints/EndpointSettings.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useRecoilValue } from 'recoil';
import { SettingsViews } from 'librechat-data-provider';
import { useGetModelsQuery } from 'librechat-data-provider/react-query';
import type { TSettingsProps } from '~/common';
import { getSettings } from './Settings';
import { cn } from '~/utils';
Expand All @@ -12,15 +13,15 @@ export default function Settings({
className = '',
isMultiChat = false,
}: TSettingsProps & { isMultiChat?: boolean }) {
const modelsConfig = useRecoilValue(store.modelsConfig);
const modelsQuery = useGetModelsQuery();
const currentSettingsView = useRecoilValue(store.currentSettingsView);
if (!conversation?.endpoint || currentSettingsView !== SettingsViews.default) {
return null;
}

const { settings, multiViewSettings } = getSettings(isMultiChat);
const { endpoint: _endpoint, endpointType } = conversation;
const models = modelsConfig?.[_endpoint] ?? [];
const models = modelsQuery?.data?.[_endpoint] ?? [];
const endpoint = endpointType ?? _endpoint;
const OptionComponent = settings[endpoint];

Expand Down
8 changes: 4 additions & 4 deletions client/src/components/Input/ModelSelect/ModelSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useRecoilValue } from 'recoil';
import type { TConversation } from 'librechat-data-provider';
import type { TSetOption } from '~/common';
import { options, multiChatOptions } from './options';
import store from '~/store';
import { useGetModelsQuery } from 'librechat-data-provider/react-query';

type TGoogleProps = {
showExamples: boolean;
Expand All @@ -23,13 +22,14 @@ export default function ModelSelect({
isMultiChat = false,
showAbove = true,
}: TSelectProps) {
const modelsConfig = useRecoilValue(store.modelsConfig);
const modelsQuery = useGetModelsQuery();

if (!conversation?.endpoint) {
return null;
}

const { endpoint: _endpoint, endpointType } = conversation;
const models = modelsConfig?.[_endpoint] ?? [];
const models = modelsQuery?.data?.[_endpoint] ?? [];
const endpoint = endpointType ?? _endpoint;

const OptionComponent = isMultiChat ? multiChatOptions[endpoint] : options[endpoint];
Expand Down
4 changes: 1 addition & 3 deletions client/src/hooks/Config/useConfigOverride.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type TempOverrideType = Record<string, unknown> & {
};

export default function useConfigOverride() {
const setModelsConfig = useSetRecoilState(store.modelsConfig);
const setEndpointsQueryEnabled = useSetRecoilState(store.endpointsQueryEnabled);
const overrideQuery = useGetEndpointsConfigOverride({
staleTime: Infinity,
Expand All @@ -33,10 +32,9 @@ export default function useConfigOverride() {
if (modelsConfig) {
await queryClient.cancelQueries([QueryKeys.models]);
queryClient.setQueryData([QueryKeys.models], modelsConfig);
setModelsConfig(modelsConfig);
}
},
[queryClient, setEndpointsQueryEnabled, setModelsConfig],
[queryClient, setEndpointsQueryEnabled],
);

useEffect(() => {
Expand Down
11 changes: 6 additions & 5 deletions client/src/hooks/useConversation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback } from 'react';
import { useSetRecoilState, useResetRecoilState, useRecoilCallback } from 'recoil';
import { useGetEndpointsQuery } from 'librechat-data-provider/react-query';
import { useGetEndpointsQuery, useGetModelsQuery } from 'librechat-data-provider/react-query';
import type {
TConversation,
TMessagesAtom,
Expand All @@ -16,20 +16,21 @@ import store from '~/store';
const useConversation = () => {
const navigate = useOriginNavigate();
const setConversation = useSetRecoilState(store.conversation);
const resetLatestMessage = useResetRecoilState(store.latestMessage);
const setMessages = useSetRecoilState<TMessagesAtom>(store.messages);
const setSubmission = useSetRecoilState<TSubmission | null>(store.submission);
const resetLatestMessage = useResetRecoilState(store.latestMessage);
const { data: endpointsConfig = {} as TEndpointsConfig } = useGetEndpointsQuery();
const modelsQuery = useGetModelsQuery();

const switchToConversation = useRecoilCallback(
({ snapshot }) =>
() =>
async (
conversation: TConversation,
messages: TMessagesAtom = null,
preset: TPreset | null = null,
modelsData?: TModelsConfig,
) => {
const modelsConfig = modelsData ?? snapshot.getLoadable(store.modelsConfig).contents;
const modelsConfig = modelsData ?? modelsQuery.data;
const { endpoint = null } = conversation;

if (endpoint === null) {
Expand Down Expand Up @@ -61,7 +62,7 @@ const useConversation = () => {
navigate('new');
}
},
[endpointsConfig],
[endpointsConfig, modelsQuery.data],
);

const newConversation = useCallback(
Expand Down
9 changes: 5 additions & 4 deletions client/src/hooks/useNewConvo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback } from 'react';
import { EModelEndpoint, FileSources, defaultOrderQuery } from 'librechat-data-provider';
import { useGetEndpointsQuery } from 'librechat-data-provider/react-query';
import { useGetEndpointsQuery, useGetModelsQuery } from 'librechat-data-provider/react-query';
import {
useSetRecoilState,
useResetRecoilState,
Expand Down Expand Up @@ -35,6 +35,7 @@ const useNewConvo = (index = 0) => {
const setSubmission = useSetRecoilState<TSubmission | null>(store.submissionByIndex(index));
const resetLatestMessage = useResetRecoilState(store.latestMessageFamily(index));
const { data: endpointsConfig = {} as TEndpointsConfig } = useGetEndpointsQuery();
const modelsQuery = useGetModelsQuery();

const { data: assistants = [] } = useListAssistantsQuery(defaultOrderQuery, {
select: (res) =>
Expand All @@ -51,15 +52,15 @@ const useNewConvo = (index = 0) => {
});

const switchToConversation = useRecoilCallback(
({ snapshot }) =>
() =>
async (
conversation: TConversation,
preset: Partial<TPreset> | null = null,
modelsData?: TModelsConfig,
buildDefault?: boolean,
keepLatestMessage?: boolean,
) => {
const modelsConfig = modelsData ?? snapshot.getLoadable(store.modelsConfig).contents;
const modelsConfig = modelsData ?? modelsQuery.data;
const { endpoint = null } = conversation;
const buildDefaultConversation = endpoint === null || buildDefault;
const activePreset =
Expand Down Expand Up @@ -137,7 +138,7 @@ const useNewConvo = (index = 0) => {
navigate('new');
}
},
[endpointsConfig, defaultPreset, assistants],
[endpointsConfig, defaultPreset, assistants, modelsQuery.data],
);

const newConversation = useCallback(
Expand Down
29 changes: 22 additions & 7 deletions client/src/routes/ChatRoute.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useRecoilValue } from 'recoil';
import { useEffect, useRef } from 'react';
import { useParams } from 'react-router-dom';
import {
Expand All @@ -23,16 +22,18 @@ export default function ChatRoute() {
const { data: startupConfig } = useGetStartupConfig();

const { conversation } = store.useCreateConversationAtom(index);
const modelsQueryEnabled = useRecoilValue(store.modelsQueryEnabled);
const { isAuthenticated } = useAuthRedirect();
const { newConversation } = useNewConvo();
const hasSetConversation = useRef(false);

const modelsQuery = useGetModelsQuery({ enabled: isAuthenticated && modelsQueryEnabled });
const modelsQuery = useGetModelsQuery({
enabled: isAuthenticated,
refetchOnMount: 'always',
});
const initialConvoQuery = useGetConvoIdQuery(conversationId ?? '', {
enabled: isAuthenticated && conversationId !== 'new',
});
const endpointsQuery = useGetEndpointsQuery({ enabled: isAuthenticated && modelsQueryEnabled });
const endpointsQuery = useGetEndpointsQuery({ enabled: isAuthenticated });
const { data: assistants = null } = useListAssistantsQuery(defaultOrderQuery, {
select: (res) =>
res.data.map(({ id, name, metadata, model }) => ({ id, name, metadata, model })),
Expand All @@ -50,6 +51,7 @@ export default function ChatRoute() {
conversationId === 'new' &&
endpointsQuery.data &&
modelsQuery.data &&
!modelsQuery.data?.initial &&
!hasSetConversation.current
) {
newConversation({ modelsData: modelsQuery.data });
Expand All @@ -58,6 +60,7 @@ export default function ChatRoute() {
initialConvoQuery.data &&
endpointsQuery.data &&
modelsQuery.data &&
!modelsQuery.data?.initial &&
!hasSetConversation.current
) {
newConversation({
Expand All @@ -68,10 +71,15 @@ export default function ChatRoute() {
keepLatestMessage: true,
});
hasSetConversation.current = !!assistants;
} else if (!hasSetConversation.current && conversationId === 'new' && assistants) {
} else if (
!hasSetConversation.current &&
!modelsQuery.data?.initial &&
conversationId === 'new' &&
assistants
) {
newConversation({ modelsData: modelsQuery.data });
hasSetConversation.current = true;
} else if (!hasSetConversation.current && assistants) {
} else if (!hasSetConversation.current && !modelsQuery.data?.initial && assistants) {
newConversation({
template: initialConvoQuery.data,
preset: initialConvoQuery.data as TPreset,
Expand All @@ -80,8 +88,15 @@ export default function ChatRoute() {
});
hasSetConversation.current = true;
}
/* Creates infinite render if all dependencies included */
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [initialConvoQuery.data, modelsQuery.data, endpointsQuery.data, assistants]);
}, [
initialConvoQuery.data,
modelsQuery.data,
endpointsQuery.data,
assistants,
conversation?.model,
]);

if (endpointsQuery.isLoading || modelsQuery.isLoading) {
return <Spinner className="m-auto text-black dark:text-white" />;
Expand Down
37 changes: 9 additions & 28 deletions client/src/routes/Root.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
/* eslint-disable react-hooks/exhaustive-deps */
import { useEffect, useState } from 'react';
import { Outlet, useLocation } from 'react-router-dom';
import { Outlet } from 'react-router-dom';
import { useRecoilValue, useSetRecoilState } from 'recoil';
import { useGetModelsQuery, useGetSearchEnabledQuery } from 'librechat-data-provider/react-query';
import { useGetSearchEnabledQuery } from 'librechat-data-provider/react-query';
import type { ContextType } from '~/common';
import {
useAuthContext,
useServerStream,
useConversation,
useAssistantsMap,
useFileMap,
} from '~/hooks';
import { useAuthContext, useServerStream, useAssistantsMap, useFileMap } from '~/hooks';
import { AssistantsMapContext, FileMapContext } from '~/Providers';
import { Nav, MobileNav } from '~/components/Nav';
import store from '~/store';

export default function Root() {
const location = useLocation();
const { newConversation } = useConversation();
const { isAuthenticated } = useAuthContext();
const [navVisible, setNavVisible] = useState(() => {
const savedNavVisible = localStorage.getItem('navVisible');
Expand All @@ -27,34 +18,24 @@ export default function Root() {
const submission = useRecoilValue(store.submission);
useServerStream(submission ?? null);

const modelsQueryEnabled = useRecoilValue(store.modelsQueryEnabled);
const setIsSearchEnabled = useSetRecoilState(store.isSearchEnabled);
const setModelsConfig = useSetRecoilState(store.modelsConfig);

const fileMap = useFileMap({ isAuthenticated });
const assistantsMap = useAssistantsMap({ isAuthenticated });
const searchEnabledQuery = useGetSearchEnabledQuery({ enabled: isAuthenticated });
const modelsQuery = useGetModelsQuery({ enabled: isAuthenticated && modelsQueryEnabled });

useEffect(() => {
if (modelsQuery.data && location.state?.from?.pathname.includes('/chat')) {
setModelsConfig(modelsQuery.data);
// Note: passing modelsQuery.data prevents navigation
newConversation({}, undefined, modelsQuery.data);
} else if (modelsQuery.data) {
setModelsConfig(modelsQuery.data);
} else if (modelsQuery.isError) {
console.error('Failed to get models', modelsQuery.error);
}
}, [modelsQuery.data, modelsQuery.isError]);

useEffect(() => {
if (searchEnabledQuery.data) {
setIsSearchEnabled(searchEnabledQuery.data);
} else if (searchEnabledQuery.isError) {
console.error('Failed to get search enabled', searchEnabledQuery.error);
}
}, [searchEnabledQuery.data, searchEnabledQuery.isError]);
}, [
searchEnabledQuery.data,
searchEnabledQuery.error,
searchEnabledQuery.isError,
setIsSearchEnabled,
]);

if (!isAuthenticated) {
return null;
Expand Down
2 changes: 0 additions & 2 deletions client/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import conversation from './conversation';
import conversations from './conversations';
import families from './families';
import endpoints from './endpoints';
import models from './models';
import user from './user';
import text from './text';
import toast from './toast';
Expand All @@ -17,7 +16,6 @@ export default {
...conversation,
...conversations,
...endpoints,
...models,
...user,
...text,
...toast,
Expand Down
33 changes: 0 additions & 33 deletions client/src/store/models.ts

This file was deleted.

19 changes: 19 additions & 0 deletions packages/data-provider/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { z } from 'zod';
import { EModelEndpoint, eModelEndpointSchema } from './schemas';
import { fileConfigSchema } from './file-config';
import { FileSources } from './types/files';
import { TModelsConfig } from './types';

export const defaultSocialLogins = ['google', 'facebook', 'openid', 'github', 'discord'];

Expand Down Expand Up @@ -332,6 +333,24 @@ export const defaultModels = {
],
};

const fitlerAssistantModels = (str: string) => {
return /gpt-4|gpt-3\\.5/i.test(str) && !/vision|instruct/i.test(str);
};

const openAIModels = defaultModels[EModelEndpoint.openAI];

export const initialModelsConfig: TModelsConfig = {
initial: [],
[EModelEndpoint.openAI]: openAIModels,
[EModelEndpoint.assistants]: openAIModels.filter(fitlerAssistantModels),
[EModelEndpoint.gptPlugins]: openAIModels,
[EModelEndpoint.azureOpenAI]: openAIModels,
[EModelEndpoint.bingAI]: ['BingAI', 'Sydney'],
[EModelEndpoint.chatGPTBrowser]: ['text-davinci-002-render-sha'],
[EModelEndpoint.google]: defaultModels[EModelEndpoint.google],
[EModelEndpoint.anthropic]: defaultModels[EModelEndpoint.anthropic],
};

export const EndpointURLs: { [key in EModelEndpoint]: string } = {
[EModelEndpoint.openAI]: `/api/ask/${EModelEndpoint.openAI}`,
[EModelEndpoint.bingAI]: `/api/ask/${EModelEndpoint.bingAI}`,
Expand Down
Loading

0 comments on commit f6a8488

Please sign in to comment.