Skip to content

fix(ui): Adds related model support to embeddings #8184

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
85 changes: 70 additions & 15 deletions invokeai/frontend/web/src/features/prompt/PromptTriggerSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ChakraProps, ComboboxOnChange, ComboboxOption } from '@invoke-ai/ui-library';
import { Combobox, FormControl } from '@invoke-ai/ui-library';
import { Combobox, FormControl, Icon } from '@invoke-ai/ui-library';
import { skipToken } from '@reduxjs/toolkit/query';
import { useAppSelector } from 'app/store/storeHooks';
import type { GroupBase } from 'chakra-react-select';
Expand All @@ -10,12 +10,16 @@ import type { PromptTriggerSelectProps } from 'features/prompt/types';
import { t } from 'i18next';
import { memo, useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { PiLinkSimple } from 'react-icons/pi';
import { useGetRelatedModelIdsBatchQuery } from 'services/api/endpoints/modelRelationships';
import { useGetModelConfigQuery } from 'services/api/endpoints/models';
import { useEmbeddingModels, useLoRAModels } from 'services/api/hooks/modelsByType';
import { isNonRefinerMainModelConfig } from 'services/api/types';

const noOptionsMessage = () => t('prompt.noMatchingTriggers');

type RelatedEmbedding = ComboboxOption & { starred?: boolean };

export const PromptTriggerSelect = memo(({ onSelect, onClose }: PromptTriggerSelectProps) => {
const { t } = useTranslation();

Expand All @@ -27,6 +31,27 @@ export const PromptTriggerSelect = memo(({ onSelect, onClose }: PromptTriggerSel
const [loraModels, { isLoading: isLoadingLoRAs }] = useLoRAModels();
const [tiModels, { isLoading: isLoadingTIs }] = useEmbeddingModels();

// Get related model keys for current selected models
const selectedModelKeys = useMemo(() => {
const keys: string[] = [];
if (mainModel) {
keys.push(mainModel.key);
}
for (const { model } of addedLoRAs) {
keys.push(model.key);
}
return keys;
}, [mainModel, addedLoRAs]);

const { relatedModelKeys } = useGetRelatedModelIdsBatchQuery(selectedModelKeys, {
selectFromResult: ({ data }) => {
if (!data) {
return { relatedModelKeys: [] };
}
return { relatedModelKeys: data };
},
});

const _onChange = useCallback<ComboboxOnChange>(
(v) => {
if (!v) {
Expand All @@ -42,19 +67,6 @@ export const PromptTriggerSelect = memo(({ onSelect, onClose }: PromptTriggerSel
const options = useMemo(() => {
const _options: GroupBase<ComboboxOption>[] = [];

if (tiModels) {
const embeddingOptions = tiModels
.filter((ti) => ti.base === mainModelConfig?.base)
.map((model) => ({ label: model.name, value: `<${model.name}>` }));

if (embeddingOptions.length > 0) {
_options.push({
label: t('prompt.compatibleEmbeddings'),
options: embeddingOptions,
});
}
}

if (loraModels) {
const triggerPhraseOptions = loraModels
.filter((lora) => map(addedLoRAs, (l) => l.model.key).includes(lora.key))
Expand All @@ -74,6 +86,35 @@ export const PromptTriggerSelect = memo(({ onSelect, onClose }: PromptTriggerSel
}
}

if (tiModels) {
// Create embedding options with starred property for related models
const embeddingOptions: RelatedEmbedding[] = tiModels
.filter((ti) => ti.base === mainModelConfig?.base)
.map((model) => ({
label: model.name,
value: `<${model.name}>`,
starred: relatedModelKeys.includes(model.key),
}));

// Sort so related embeddings come first
embeddingOptions.sort((a, b) => {
if (a.starred && !b.starred) {
return -1;
}
if (!a.starred && b.starred) {
return 1;
}
return 0;
});

if (embeddingOptions.length > 0) {
_options.push({
label: t('prompt.compatibleEmbeddings'),
options: embeddingOptions,
});
}
}

if (mainModelConfig && isNonRefinerMainModelConfig(mainModelConfig) && mainModelConfig.trigger_phrases?.length) {
_options.push({
label: t('modelManager.mainModelTriggerPhrases'),
Expand All @@ -85,7 +126,20 @@ export const PromptTriggerSelect = memo(({ onSelect, onClose }: PromptTriggerSel
}

return _options;
}, [tiModels, loraModels, mainModelConfig, t, addedLoRAs]);
}, [tiModels, loraModels, mainModelConfig, t, addedLoRAs, relatedModelKeys]);

const formatOptionLabel = useCallback((option: ComboboxOption) => {
const embeddingOption = option as RelatedEmbedding;
if (embeddingOption.starred) {
return (
<div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}>
<Icon as={PiLinkSimple} color="invokeYellow.500" boxSize={3} />
{option.label}
</div>
);
}
return option.label;
}, []);

return (
<FormControl>
Expand All @@ -104,6 +158,7 @@ export const PromptTriggerSelect = memo(({ onSelect, onClose }: PromptTriggerSel
onMenuClose={onClose}
data-testid="add-prompt-trigger"
sx={selectStyles}
formatOptionLabel={formatOptionLabel}
/>
</FormControl>
);
Expand Down