Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove unused assistants or models #460 #735 #777

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions webapp/components/views/Models/Explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { useContext, useEffect, useState } from 'react';
import { useRouter } from 'next/router';
import { BrainCircuit, HardDriveDownload } from 'lucide-react';
import { Ui, Model, ModelState } from '@/types';
import { Ui, Model, ModelState, AIService, AIServiceType } from '@/types';
import useBackend from '@/hooks/useBackendContext';
import useTranslation from '@/hooks/useTranslation';
import { ModalsContext } from '@/context/modals';
Expand All @@ -32,7 +32,8 @@ import { cn } from '@/lib/utils';
import { getLocalModels } from '@/utils/data/models';
import { AppContext } from '@/context';
import { useAssistantStore } from '@/stores';
import { isModelUsedInConversations } from '@/utils/data/conversations';
import { addConversationService, isModelUsedInConversations } from '@/utils/data/conversations';
import { getLocalProvider } from '@/utils/data/providers';
import { Button } from '../../ui/button';
import EditableItem from '../../common/EditableItem';
import ModelInfos from '../../common/ModelInfos';
Expand All @@ -42,7 +43,7 @@ export type ModelsExplorerProps = {
};

function ModelsExplorer({ selectedId: selectedModelId }: ModelsExplorerProps) {
const { conversations } = useContext(AppContext);
const { conversations, updateConversations, providers } = useContext(AppContext);
const { isModelUsedInAssistants } = useAssistantStore();
const { backendContext, updateBackendStore } = useBackend();
const router = useRouter();
Expand Down Expand Up @@ -91,8 +92,26 @@ function ModelsExplorer({ selectedId: selectedModelId }: ModelsExplorerProps) {
logger.info(`Uninstall ${modelId}`);
const model = models.find((m) => m.id === modelId);
if (model) {
const { activeService } = backendContext.config.services;
const isUsed: boolean =
isModelUsedInConversations(conversations, model) || isModelUsedInAssistants(model);
(activeService?.type === AIServiceType.Model && activeService.modelId === model.id) ||
isModelUsedInConversations(conversations, model) ||
isModelUsedInAssistants(model);
if (isUsed) {
const service: AIService = {
type: AIServiceType.Model,
modelId,
providerIdOrName: getLocalProvider(providers)?.id,
};
const updatedConversations = conversations.map((conversation) => {
let updatedConversation = conversation;
if (!conversation.services) {
updatedConversation = addConversationService(conversation, service);
}
return updatedConversation;
});
await updateConversations(updatedConversations);
}
const nextModelId =
models.findLast((m) => m.state !== ModelState.Removed && m.id !== modelId)?.id || '';
await uninstallModel(modelId, isUsed);
Expand Down
22 changes: 19 additions & 3 deletions webapp/components/views/Models/Model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { useContext, useEffect, useMemo, useState } from 'react';
import { useRouter } from 'next/router';
import { DownloadIcon } from '@radix-ui/react-icons';
import useTranslation from '@/hooks/useTranslation';
import { AIServiceType, Model, ModelState } from '@/types';
import { AIService, AIServiceType, Model, ModelState } from '@/types';
import { deepCopy, deepMerge, getEntityName, getResourceUrl } from '@/utils/data';
import useParameters from '@/hooks/useParameters';
import ContentView from '@/components/common/ContentView';
Expand All @@ -52,10 +52,11 @@ import EmptyView from '@/components/common/EmptyView';
import { BrainCircuit } from 'lucide-react';
import { fileExists } from '@/utils/backend/tauri';
import { toast } from 'sonner';
import { isModelUsedInConversations } from '@/utils/data/conversations';
import { addConversationService, isModelUsedInConversations } from '@/utils/data/conversations';
import { AppContext } from '@/context';
import { useAssistantStore } from '@/stores';
import { OrangePill } from '@/components/ui/Pills';
import { getLocalProvider } from '@/utils/data/providers';
import Parameter, { ParametersRecord } from '../../common/Parameter';
import { Button } from '../../ui/button';
import { Table, TableBody, TableRow, TableCell, TableHeader, TableHead } from '../../ui/table';
Expand All @@ -70,7 +71,7 @@ function ModelView({ selectedId: selectedModelId }: ModelViewProps) {

const [fullPathModel, setFullPathModel] = useState<string | undefined>();
const { backendContext, updateBackendStore } = useBackend();
const { conversations } = useContext(AppContext);
const { conversations, updateConversations, providers } = useContext(AppContext);
const { isModelUsedInAssistants } = useAssistantStore();
const [collection, setCollection] = useState<Model[]>([]);
const { showModal } = useContext(ModalsContext);
Expand Down Expand Up @@ -232,6 +233,21 @@ function ModelView({ selectedId: selectedModelId }: ModelViewProps) {
const nextModelId =
models.findLast((m) => m.state !== ModelState.Removed && m.id !== model.id)?.id || '';
await uninstallModel(model.id, inUse);
if (inUse) {
const service: AIService = {
type: AIServiceType.Model,
modelId: model.id,
providerIdOrName: getLocalProvider(providers)?.id,
};
const updatedConversations = conversations.map((conversation) => {
let updatedConversation = conversation;
if (!conversation.services) {
updatedConversation = addConversationService(conversation, service);
}
return updatedConversation;
});
await updateConversations(updatedConversations);
}
await updateBackendStore();
router.replace(`/models${nextModelId ? `/${nextModelId}` : ''}`);
};
Expand Down
7 changes: 3 additions & 4 deletions webapp/components/views/Threads/Header/ModelTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import { useContext } from 'react';
import { Ui, Provider, ProviderType, Model } from '@/types';
import { Provider, ProviderType, Model } from '@/types';
import useTranslation from '@/hooks/useTranslation';
import { ModalsContext } from '@/context/modals';
import { ModalIds } from '@/modals';
Expand All @@ -25,11 +25,10 @@ import logger from '@/utils/logger';
import ModelInfos from '../../../common/ModelInfos';

type ModelTitleProps = {
selectedItem: Ui.MenuItem;
selectedModel?: Model;
};

export default function ModelTitle({ selectedItem, selectedModel }: ModelTitleProps) {
export default function ModelTitle({ selectedModel }: ModelTitleProps) {
const { providers } = useContext(AppContext);
const { t } = useTranslation();
const { showModal } = useContext(ModalsContext);
Expand Down Expand Up @@ -69,7 +68,7 @@ export default function ModelTitle({ selectedItem, selectedModel }: ModelTitlePr
handleSetupChatGPT();
});

return selectedItem.label ? (
return selectedModel ? (
<>
<div className="grow capitalize text-foreground">
{selectedModel && <ModelInfos model={selectedModel} />}
Expand Down
4 changes: 2 additions & 2 deletions webapp/components/views/Threads/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ export default function ThreadHeader({
let title;
if (selectedAssistantId) {
title = <AssistantTitle assistant={assistant} />;
} else if (selectedItem) {
title = <ModelTitle selectedItem={selectedItem} selectedModel={selectedModel} />;
} else if (selectedModel) {
title = <ModelTitle selectedModel={selectedModel} />;
} else if (modelItems.length === 0 && !selectedConversationId) {
return (
<div className="flex w-full flex-col items-start justify-between px-4 py-0 sm:flex-row sm:items-center">
Expand Down
22 changes: 11 additions & 11 deletions webapp/components/views/Threads/Thread.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function Thread({
newMessages = newMessages.map((msg, index) => {
const { author } = msg;
if (author.role === 'assistant') {
const model = findModelInAll(author.name, providers, backendContext);
const model = findModelInAll(author.name, providers, backendContext, true);
author.name = model?.title || model?.name || author.name;
}
if (stream && index === newMessages.length - 1) {
Expand Down Expand Up @@ -191,7 +191,6 @@ function Thread({

const {
modelItems,
selectedModelItem,
commandManager,
assistant,
activeModelId: selectedModelId,
Expand All @@ -212,7 +211,7 @@ function Thread({
}
activeModel = findModel(modelId, backendContext.config.models.items);
} else {
activeModel = findModelInAll(modelId, providers, backendContext);
activeModel = findModelInAll(modelId, providers, backendContext, true);
}

const download = backendContext.downloads?.find((d) => d.id === activeModel?.id);
Expand All @@ -223,18 +222,17 @@ function Thread({
}

const items = getModelsAsItems(providers, backendContext, modelId);
const mi = items.find((m) => m.key === modelId);
let d = false;
if (!mi) {
if (!activeModel) {
modelId = undefined;
d = true;
} else if (activeModel?.state === ModelState.Downloading) {
} else if (activeModel.state === ModelState.Downloading) {
d = true;
}
const manager = getCommandManager(items);
return {
modelItems: items,
selectedModelItem: mi,
/* selectedModelItem: mi, */
commandManager: manager,
assistant: newAssistant,
activeModelId: modelId,
Expand Down Expand Up @@ -374,7 +372,7 @@ function Thread({
`ChangeService ${modelIdOrName} ${providerIdOrName} activeModel=${selectedModelId}`,
selectedConversation,
);
const activeModel = findModelInAll(modelIdOrName, providers, backendContext);
const activeModel = findModelInAll(modelIdOrName, providers, backendContext, true);
if (!activeModel) {
logger.error('Model not found', modelIdOrName);
return;
Expand Down Expand Up @@ -529,12 +527,13 @@ function Thread({
}
let selectedModel;
if (result.modelName) {
selectedModel = findModelInAll(result.modelName, providers, backendContext);
selectedModel = findModelInAll(result.modelName, providers, backendContext, true);
} else {
selectedModel = findModelInAll(
getConversationModelId(selectedConversation) || selectedModelId,
providers,
backendContext,
true,
);
}

Expand Down Expand Up @@ -619,12 +618,13 @@ function Thread({
}
let selectedModel;
if (result.modelName) {
selectedModel = findModelInAll(result.modelName, providers, backendContext);
selectedModel = findModelInAll(result.modelName, providers, backendContext, true);
} else {
selectedModel = findModelInAll(
getConversationModelId(selectedConversation) || selectedModelId,
providers,
backendContext,
true,
);
}
if (!selectedModel) {
Expand Down Expand Up @@ -855,7 +855,7 @@ function Thread({

let isLoading = conversationId ? isProcessing[conversationId] || false : false;
let placeholder;
if (!selectedModelItem || model?.state === ModelState.Downloading) {
if (!model || model?.state === ModelState.Downloading) {
isLoading = true;
if (model?.state === ModelState.Downloading) {
placeholder = t('Loading the model, Please wait...');
Expand Down
53 changes: 46 additions & 7 deletions webapp/components/views/Threads/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ import { useRouter } from 'next/router';
import { useSearchParams } from 'next/navigation';
import { v4 as uuid } from 'uuid';
import useBackend from '@/hooks/useBackendContext';
import { Conversation, PageSettings, Provider, ProviderType, Ui } from '@/types';
import { Conversation, ModelState, PageSettings, Provider, ProviderType, Ui } from '@/types';
import { DefaultPageSettings, DefaultThreadsExplorerGroups } from '@/utils/constants';
import logger from '@/utils/logger';
import useShortcuts, { ShortcutIds } from '@/hooks/useShortcuts';
import { getConversation } from '@/utils/data/conversations';
import {
getConversation,
getConversationAssistant,
getConversationModelId,
} from '@/utils/data/conversations';
import { ModalIds } from '@/modals';
import { ModalsContext } from '@/context/modals';
import { AppContext } from '@/context';
Expand All @@ -32,6 +36,9 @@ import { getAssistantId } from '@/utils/services';
import { deepEqual } from '@/utils/data';
import { createProvider } from '@/utils/data/providers';
import OpenAI from '@/utils/providers/openai';
import { useAssistantStore } from '@/stores';
import { findModelInAll } from '@/utils/data/models';
import { uninstallModel } from '@/utils/backend/commands';
import { ResizableHandle, ResizablePanel } from '../../ui/resizable';
import Settings from './Settings';
import Threads from './Threads';
Expand All @@ -55,6 +62,8 @@ export default function MainThreads({ selectedThreadId, view = ViewName.Recent }
setError([...errors, { id: uuid(), conversationId, message: error }]);
};

const { getAssistant, deleteAssistant } = useAssistantStore();

const {
conversations,
updateConversations,
Expand All @@ -66,7 +75,7 @@ export default function MainThreads({ selectedThreadId, view = ViewName.Recent }
deleteArchive,
providers,
} = useContext(AppContext);
const { backendContext, setSettings } = useBackend();
const { backendContext, setSettings, updateBackendStore } = useBackend();

const searchParams = useSearchParams();
const selectedConversation = conversations.find((c) => c.id === selectedThreadId);
Expand Down Expand Up @@ -131,10 +140,40 @@ export default function MainThreads({ selectedThreadId, view = ViewName.Recent }
}

const deleteAndCleanupConversation = async (conversationId: string, deleteFiles = false) =>
deleteConversation(conversationId, deleteFiles, async (cId) => {
// Delete associated settings
saveSettings(getSelectedPage(cId, ViewName.Recent));
});
deleteConversation(
conversationId,
deleteFiles,
async (removedConversation, updatedConversations) => {
const cAssistantId = getConversationAssistant(removedConversation);
const assistant = getAssistant(cAssistantId);
if (assistant?.hidden) {
let some = updatedConversations.some(
(conversation) => getConversationAssistant(conversation) === cAssistantId,
);
some =
some || archives.some((archive) => getConversationAssistant(archive) === cAssistantId);
if (!some) {
deleteAssistant(assistant.id);
}
}
const modelId = getConversationModelId(removedConversation, assistant);
const model = findModelInAll(modelId, providers, backendContext, true);
if (modelId && model?.state === ModelState.Removed) {
let some = updatedConversations.some(
(conversation) => getConversationModelId(conversation, assistant) === modelId,
);
some =
some ||
archives.some((archive) => getConversationModelId(archive, assistant) === modelId);
if (!some) {
await uninstallModel(modelId, false);
await updateBackendStore();
}
}
// Delete associated settings
saveSettings(getSelectedPage(removedConversation.id, ViewName.Recent));
},
);

const handleResizeSettings = (size: number) => {
saveSettings(selectedPage, { settingsWidth: size });
Expand Down
15 changes: 12 additions & 3 deletions webapp/context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { deepCopy } from '@/utils/data';
import { defaultPresets, mergePresets } from '@/utils/data/presets';
import { mergeMessages } from '@/utils/data/messages';
import { deleteUnusedConversationsDir } from '@/utils/backend/tauri';
import logger from '@/utils/logger';

export type Context = {
conversations: Array<Conversation>;
Expand All @@ -37,7 +38,7 @@ export type Context = {
deleteConversation: (
id: string,
deleteFiles: boolean,
cleanup?: (id: string) => Promise<void>,
cleanup?: (conversation: Conversation, conversations: Conversation[]) => Promise<void>,
) => Promise<void>;
readConversationMessages: (key: string, defaultValue: Message[]) => Promise<Message[]>;
getConversationMessages: (id: string | undefined) => Message[];
Expand Down Expand Up @@ -197,12 +198,20 @@ function AppContextProvider({ children }: { children: React.ReactNode }) {
);

const deleteConversation = useCallback(
async (id: string, deleteFiles: boolean, cleanup?: (id: string) => Promise<void>) => {
async (
id: string,
deleteFiles: boolean,
cleanup?: (conversation: Conversation, conversations: Conversation[]) => Promise<void>,
) => {
const conversation = getConversation(id, conversations) as Conversation;
if (!conversation) {
logger.info(`deleteConversation conversation doesn't exist : ${id}`);
}
const updatedConversations = removeConversation(id, conversations);
setConversations(updatedConversations);
// Delete any orphans messages
await deleteConversationMessages(id);
await cleanup?.(id);
await cleanup?.(conversation, updatedConversations);
if (deleteFiles) {
await deleteUnusedConversationsDir(conversations.map((c) => c.id));
}
Expand Down
9 changes: 9 additions & 0 deletions webapp/utils/data/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ export const getConversationService = (
return service;
};

export const getConversationAssistant = (conversation: Conversation) => {
let assistantId;
const service = conversation.services?.find((c) => c.type === AIServiceType.Assistant);
if (service?.type === AIServiceType.Assistant) {
({ assistantId } = service);
}
return assistantId;
};

export const removeConversationService = (
conversation: Conversation,
serviceType: AIServiceType,
Expand Down
Loading