mirror of
https://github.com/labring/FastGPT.git
synced 2025-12-25 20:02:47 +00:00
Some checks failed
Build FastGPT images in Personal warehouse / get-vars (push) Waiting to run
Build FastGPT images in Personal warehouse / build-fastgpt-images (map[arch:amd64 runs-on:ubuntu-24.04]) (push) Blocked by required conditions
Build FastGPT images in Personal warehouse / build-fastgpt-images (map[arch:arm64 runs-on:ubuntu-24.04-arm]) (push) Blocked by required conditions
Build FastGPT images in Personal warehouse / release-fastgpt-images (push) Blocked by required conditions
Document deploy / sync-images (push) Has been cancelled
Document deploy / generate-timestamp (push) Has been cancelled
Document deploy / build-images (map[domain:https://fastgpt.cn suffix:cn]) (push) Has been cancelled
Document deploy / build-images (map[domain:https://fastgpt.io suffix:io]) (push) Has been cancelled
Document deploy / update-images (map[deployment:fastgpt-docs domain:https://fastgpt.cn kube_config:KUBE_CONFIG_CN suffix:cn]) (push) Has been cancelled
Document deploy / update-images (map[deployment:fastgpt-docs domain:https://fastgpt.io kube_config:KUBE_CONFIG_IO suffix:io]) (push) Has been cancelled
233 lines
6.1 KiB
TypeScript
233 lines
6.1 KiB
TypeScript
import React, { useCallback, useMemo, useState } from 'react';
|
|
|
|
import { useTranslation } from 'next-i18next';
|
|
import { useSystemStore } from '@/web/common/system/useSystemStore';
|
|
import MySelect, { type SelectProps } from '@fastgpt/web/components/common/MySelect';
|
|
import { HUGGING_FACE_ICON } from '@fastgpt/global/common/system/constants';
|
|
import { Box, Flex } from '@chakra-ui/react';
|
|
import Avatar from '@fastgpt/web/components/common/Avatar';
|
|
import MyTooltip from '@fastgpt/web/components/common/MyTooltip';
|
|
import { ModelProviderList } from '@fastgpt/global/core/ai/provider';
|
|
import MultipleRowSelect from '@fastgpt/web/components/common/MySelect/MultipleRowSelect';
|
|
import { getModelFromList } from '@fastgpt/global/core/ai/model';
|
|
|
|
type Props = SelectProps & {
|
|
disableTip?: string;
|
|
};
|
|
|
|
const OneRowSelector = ({ list, onChange, disableTip, ...props }: Props) => {
|
|
const { t } = useTranslation();
|
|
const { llmModelList, embeddingModelList, ttsModelList, sttModelList, reRankModelList } =
|
|
useSystemStore();
|
|
|
|
const avatarSize = useMemo(() => {
|
|
const size = {
|
|
sm: '1rem',
|
|
md: '1.2rem',
|
|
lg: '1.4rem'
|
|
};
|
|
//@ts-ignore
|
|
return props.size ? size[props.size] : size['md'];
|
|
}, [props.size]);
|
|
|
|
const avatarList = useMemo(() => {
|
|
const allModels = [
|
|
...llmModelList,
|
|
...embeddingModelList,
|
|
...ttsModelList,
|
|
...sttModelList,
|
|
...reRankModelList
|
|
];
|
|
return list
|
|
.map((item) => {
|
|
const modelData = getModelFromList(allModels, item.value)!;
|
|
if (!modelData) return;
|
|
|
|
return {
|
|
value: item.value,
|
|
label: (
|
|
<Flex alignItems={'center'} py={1}>
|
|
<Avatar
|
|
borderRadius={'0'}
|
|
mr={2}
|
|
src={modelData?.avatar || HUGGING_FACE_ICON}
|
|
w={avatarSize}
|
|
fallbackSrc={HUGGING_FACE_ICON}
|
|
/>
|
|
|
|
<Box noOfLines={1}>{modelData.name}</Box>
|
|
</Flex>
|
|
)
|
|
};
|
|
})
|
|
.filter(Boolean) as {
|
|
value: any;
|
|
label: React.JSX.Element;
|
|
}[];
|
|
}, [
|
|
list,
|
|
llmModelList,
|
|
embeddingModelList,
|
|
ttsModelList,
|
|
sttModelList,
|
|
reRankModelList,
|
|
avatarSize
|
|
]);
|
|
|
|
return (
|
|
<Box
|
|
css={{
|
|
span: {
|
|
display: 'block'
|
|
}
|
|
}}
|
|
>
|
|
<MyTooltip label={disableTip}>
|
|
<MySelect
|
|
className="nowheel"
|
|
isDisabled={!!disableTip}
|
|
list={avatarList}
|
|
placeholder={t('common:not_model_config')}
|
|
h={'40px'}
|
|
{...props}
|
|
onChange={(e) => {
|
|
return onChange?.(e);
|
|
}}
|
|
/>
|
|
</MyTooltip>
|
|
</Box>
|
|
);
|
|
};
|
|
|
|
const MultipleRowSelector = ({ list, onChange, disableTip, placeholder, ...props }: Props) => {
|
|
const { t } = useTranslation();
|
|
const { llmModelList, embeddingModelList, ttsModelList, sttModelList, reRankModelList } =
|
|
useSystemStore();
|
|
const modelList = useMemo(() => {
|
|
const allModels = [
|
|
...llmModelList,
|
|
...embeddingModelList,
|
|
...ttsModelList,
|
|
...sttModelList,
|
|
...reRankModelList
|
|
];
|
|
|
|
return list.map((item) => getModelFromList(allModels, item.value)!).filter(Boolean);
|
|
}, [llmModelList, embeddingModelList, ttsModelList, sttModelList, reRankModelList, list]);
|
|
|
|
const [value, setValue] = useState<string[]>([]);
|
|
|
|
const avatarSize = useMemo(() => {
|
|
const size = {
|
|
sm: '1rem',
|
|
md: '1.2rem',
|
|
lg: '1.4rem'
|
|
};
|
|
//@ts-ignore
|
|
return props.size ? size[props.size] : size['md'];
|
|
}, [props.size]);
|
|
|
|
const selectorList = useMemo(() => {
|
|
const renderList = ModelProviderList.map<{
|
|
label: React.JSX.Element;
|
|
value: string;
|
|
children: { label: string | React.ReactNode; value: string }[];
|
|
}>((provider) => ({
|
|
label: (
|
|
<Flex alignItems={'center'} py={1}>
|
|
<Avatar
|
|
borderRadius={'0'}
|
|
mr={2}
|
|
src={provider?.avatar || HUGGING_FACE_ICON}
|
|
fallbackSrc={HUGGING_FACE_ICON}
|
|
w={avatarSize}
|
|
/>
|
|
<Box>{t(provider.name as any)}</Box>
|
|
</Flex>
|
|
),
|
|
value: provider.id,
|
|
children: []
|
|
}));
|
|
|
|
for (const item of list) {
|
|
const modelData = getModelFromList(modelList, item.value);
|
|
if (!modelData) continue;
|
|
const provider =
|
|
renderList.find((item) => item.value === (modelData?.provider || 'Other')) ??
|
|
renderList[renderList.length - 1];
|
|
|
|
provider.children.push({
|
|
label: modelData.name,
|
|
value: modelData.model
|
|
});
|
|
}
|
|
|
|
return renderList.filter((item) => item.children.length > 0);
|
|
}, [avatarSize, list, modelList, t]);
|
|
|
|
const onSelect = useCallback(
|
|
(e: string[]) => {
|
|
return onChange?.(e[1]);
|
|
},
|
|
[onChange]
|
|
);
|
|
|
|
const SelectedLabel = useMemo(() => {
|
|
if (!props.value) return <>{t('common:not_model_config')}</>;
|
|
const modelData = getModelFromList(modelList, props.value);
|
|
|
|
if (!modelData) return <>{t('common:not_model_config')}</>;
|
|
|
|
setValue([modelData.provider, props.value]);
|
|
|
|
return (
|
|
<Flex alignItems={'center'} py={1}>
|
|
<Avatar
|
|
borderRadius={'0'}
|
|
mr={2}
|
|
src={modelData?.avatar}
|
|
fallbackSrc={HUGGING_FACE_ICON}
|
|
w={avatarSize}
|
|
/>
|
|
<Box noOfLines={1}>{modelData?.name}</Box>
|
|
</Flex>
|
|
);
|
|
}, [modelList, props.value, t, avatarSize]);
|
|
|
|
return (
|
|
<Box
|
|
css={{
|
|
span: {
|
|
display: 'block'
|
|
}
|
|
}}
|
|
>
|
|
<MyTooltip label={disableTip}>
|
|
<MultipleRowSelect
|
|
label={SelectedLabel}
|
|
list={selectorList}
|
|
onSelect={onSelect}
|
|
value={value}
|
|
placeholder={placeholder}
|
|
rowMinWidth="160px"
|
|
ButtonProps={{
|
|
isDisabled: !!disableTip,
|
|
h: '40px',
|
|
...props
|
|
}}
|
|
/>
|
|
</MyTooltip>
|
|
</Box>
|
|
);
|
|
};
|
|
|
|
const AIModelSelector = (props: Props) => {
|
|
return props.list.length > 100 ? (
|
|
<MultipleRowSelector {...props} />
|
|
) : (
|
|
<OneRowSelector {...props} />
|
|
);
|
|
};
|
|
|
|
export default AIModelSelector;
|