Skip to content

Commit

Permalink
fix: Merge models of the same category infiniflow#2479 (infiniflow#2536)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?

fix: Merge models of the same category infiniflow#2479

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
  • Loading branch information
cike8899 authored Sep 23, 2024
1 parent 4b5cd48 commit e3d0f26
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
12 changes: 6 additions & 6 deletions web/src/components/llm-select/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LlmModelType } from '@/constants/knowledge';
import { useSelectLlmOptionsByModelType } from '@/hooks/llm-hooks';
import { useComposeLlmOptionsByModelTypes } from '@/hooks/llm-hooks';
import { Popover, Select } from 'antd';
import LlmSettingItems from '../llm-setting-items';

Expand All @@ -10,7 +10,10 @@ interface IProps {
}

const LLMSelect = ({ id, value, onChange }: IProps) => {
const modelOptions = useSelectLlmOptionsByModelType();
const modelOptions = useComposeLlmOptionsByModelTypes([
LlmModelType.Chat,
LlmModelType.Image2text,
]);

const content = (
<div style={{ width: 400 }}>
Expand All @@ -29,10 +32,7 @@ const LLMSelect = ({ id, value, onChange }: IProps) => {
destroyTooltipOnHide
>
<Select
options={[
...modelOptions[LlmModelType.Chat],
...modelOptions[LlmModelType.Image2text],
]}
options={modelOptions}
style={{ width: '100%' }}
dropdownStyle={{ display: 'none' }}
id={id}
Expand Down
15 changes: 6 additions & 9 deletions web/src/components/llm-setting-items/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Divider, Flex, Form, InputNumber, Select, Slider, Switch } from 'antd';
import camelCase from 'lodash/camelCase';

import { useTranslate } from '@/hooks/common-hooks';
import { useSelectLlmOptionsByModelType } from '@/hooks/llm-hooks';
import { useComposeLlmOptionsByModelTypes } from '@/hooks/llm-hooks';
import { useCallback, useMemo } from 'react';
import styles from './index.less';

Expand Down Expand Up @@ -39,7 +39,10 @@ const LlmSettingItems = ({ prefix, formItemLayout = {} }: IProps) => {

const memorizedPrefix = useMemo(() => (prefix ? [prefix] : []), [prefix]);

const modelOptions = useSelectLlmOptionsByModelType();
const modelOptions = useComposeLlmOptionsByModelTypes([
LlmModelType.Chat,
LlmModelType.Image2text,
]);

return (
<>
Expand All @@ -50,13 +53,7 @@ const LlmSettingItems = ({ prefix, formItemLayout = {} }: IProps) => {
{...formItemLayout}
rules={[{ required: true, message: t('modelMessage') }]}
>
<Select
options={[
...modelOptions[LlmModelType.Chat],
...modelOptions[LlmModelType.Image2text],
]}
showSearch
/>
<Select options={modelOptions} showSearch />
</Form.Item>
<Divider></Divider>
<Form.Item
Expand Down
23 changes: 22 additions & 1 deletion web/src/hooks/llm-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import userService from '@/services/user-service';
import { sortLLmFactoryListBySpecifiedOrder } from '@/utils/common-util';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { message } from 'antd';
import { DefaultOptionType } from 'antd/es/select';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';

Expand Down Expand Up @@ -42,7 +43,7 @@ export const useSelectLlmOptions = () => {
label: key,
options: value.map((x) => ({
label: x.llm_name,
value: x.llm_name,
value: `${x.llm_name}@${x.fid}`,
disabled: !x.available,
})),
};
Expand Down Expand Up @@ -91,6 +92,26 @@ export const useSelectLlmOptionsByModelType = () => {
};
};

export const useComposeLlmOptionsByModelTypes = (
modelTypes: LlmModelType[],
) => {
const allOptions = useSelectLlmOptionsByModelType();

return modelTypes.reduce<DefaultOptionType[]>((pre, cur) => {
const options = allOptions[cur];
options.forEach((x) => {
const item = pre.find((y) => y.label === x.label);
if (item) {
item.options.push(...x.options);
} else {
pre.push(x);
}
});

return pre;
}, []);
};

export const useFetchLlmFactoryList = (): ResponseGetType<IFactory[]> => {
const { data, isFetching: loading } = useQuery({
queryKey: ['factoryList'],
Expand Down

0 comments on commit e3d0f26

Please sign in to comment.