mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
Feat: Fixed the chat model setting echo issue (#9521)
### What problem does this PR solve? Feat: Fixed the chat model setting echo issue ### Type of change - [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
@ -28,20 +28,32 @@ interface LlmSettingFieldItemsProps {
|
|||||||
options?: any[];
|
options?: any[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export const LlmSettingSchema = {
|
export const LLMIdFormField = {
|
||||||
llm_id: z.string(),
|
llm_id: z.string(),
|
||||||
temperature: z.coerce.number().optional(),
|
};
|
||||||
top_p: z.number().optional(),
|
|
||||||
presence_penalty: z.coerce.number().optional(),
|
export const LlmSettingEnabledSchema = {
|
||||||
frequency_penalty: z.coerce.number().optional(),
|
|
||||||
temperatureEnabled: z.boolean().optional(),
|
temperatureEnabled: z.boolean().optional(),
|
||||||
topPEnabled: z.boolean().optional(),
|
topPEnabled: z.boolean().optional(),
|
||||||
presencePenaltyEnabled: z.boolean().optional(),
|
presencePenaltyEnabled: z.boolean().optional(),
|
||||||
frequencyPenaltyEnabled: z.boolean().optional(),
|
frequencyPenaltyEnabled: z.boolean().optional(),
|
||||||
maxTokensEnabled: z.boolean().optional(),
|
maxTokensEnabled: z.boolean().optional(),
|
||||||
|
};
|
||||||
|
|
||||||
|
export const LlmSettingFieldSchema = {
|
||||||
|
temperature: z.coerce.number().optional(),
|
||||||
|
top_p: z.number().optional(),
|
||||||
|
presence_penalty: z.coerce.number().optional(),
|
||||||
|
frequency_penalty: z.coerce.number().optional(),
|
||||||
max_tokens: z.number().optional(),
|
max_tokens: z.number().optional(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const LlmSettingSchema = {
|
||||||
|
...LLMIdFormField,
|
||||||
|
...LlmSettingFieldSchema,
|
||||||
|
...LlmSettingEnabledSchema,
|
||||||
|
};
|
||||||
|
|
||||||
export function LlmSettingFieldItems({
|
export function LlmSettingFieldItems({
|
||||||
prefix,
|
prefix,
|
||||||
options,
|
options,
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import { settledModelVariableMap } from '@/constants/knowledge';
|
import { settledModelVariableMap } from '@/constants/knowledge';
|
||||||
import { AgentFormContext } from '@/pages/agent/context';
|
import { AgentFormContext } from '@/pages/agent/context';
|
||||||
import useGraphStore from '@/pages/agent/store';
|
import useGraphStore from '@/pages/agent/store';
|
||||||
|
import { setChatVariableEnabledFieldValuePage } from '@/utils/chat';
|
||||||
import { useCallback, useContext } from 'react';
|
import { useCallback, useContext } from 'react';
|
||||||
import { useFormContext } from 'react-hook-form';
|
import { useFormContext } from 'react-hook-form';
|
||||||
|
|
||||||
@ -11,6 +12,20 @@ export function useHandleFreedomChange(
|
|||||||
const node = useContext(AgentFormContext);
|
const node = useContext(AgentFormContext);
|
||||||
const updateNodeForm = useGraphStore((state) => state.updateNodeForm);
|
const updateNodeForm = useGraphStore((state) => state.updateNodeForm);
|
||||||
|
|
||||||
|
const setLLMParameters = useCallback(
|
||||||
|
(values: Record<string, any>, withPrefix: boolean) => {
|
||||||
|
for (const key in values) {
|
||||||
|
if (Object.prototype.hasOwnProperty.call(values, key)) {
|
||||||
|
const realKey = getFieldWithPrefix(key);
|
||||||
|
const element = values[key as keyof typeof values];
|
||||||
|
|
||||||
|
form.setValue(withPrefix ? realKey : key, element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[form, getFieldWithPrefix],
|
||||||
|
);
|
||||||
|
|
||||||
const handleChange = useCallback(
|
const handleChange = useCallback(
|
||||||
(parameter: string) => {
|
(parameter: string) => {
|
||||||
const currentValues = { ...form.getValues() };
|
const currentValues = { ...form.getValues() };
|
||||||
@ -25,16 +40,12 @@ export function useHandleFreedomChange(
|
|||||||
updateNodeForm(node?.id, nextValues);
|
updateNodeForm(node?.id, nextValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const key in values) {
|
const variableCheckBoxFieldMap = setChatVariableEnabledFieldValuePage();
|
||||||
if (Object.prototype.hasOwnProperty.call(values, key)) {
|
|
||||||
const realKey = getFieldWithPrefix(key);
|
|
||||||
const element = values[key as keyof typeof values];
|
|
||||||
|
|
||||||
form.setValue(realKey, element);
|
setLLMParameters(values, true);
|
||||||
}
|
setLLMParameters(variableCheckBoxFieldMap, false);
|
||||||
}
|
|
||||||
},
|
},
|
||||||
[form, getFieldWithPrefix, node?.id, updateNodeForm],
|
[form, node?.id, setLLMParameters, updateNodeForm],
|
||||||
);
|
);
|
||||||
|
|
||||||
return handleChange;
|
return handleChange;
|
||||||
|
|||||||
@ -54,6 +54,8 @@ export function useAgentToolInitialValues() {
|
|||||||
return pick(initialValues, 'top_n');
|
return pick(initialValues, 'top_n');
|
||||||
case Operator.WenCai:
|
case Operator.WenCai:
|
||||||
return pick(initialValues, 'top_n', 'query_type');
|
return pick(initialValues, 'top_n', 'query_type');
|
||||||
|
case Operator.Code:
|
||||||
|
return {};
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return initialValues;
|
return initialValues;
|
||||||
|
|||||||
@ -3,6 +3,10 @@ import { Form } from '@/components/ui/form';
|
|||||||
import { Separator } from '@/components/ui/separator';
|
import { Separator } from '@/components/ui/separator';
|
||||||
import { useFetchDialog, useSetDialog } from '@/hooks/use-chat-request';
|
import { useFetchDialog, useSetDialog } from '@/hooks/use-chat-request';
|
||||||
import { transformBase64ToFile, transformFile2Base64 } from '@/utils/file-util';
|
import { transformBase64ToFile, transformFile2Base64 } from '@/utils/file-util';
|
||||||
|
import {
|
||||||
|
removeUselessFieldsFromValues,
|
||||||
|
setLLMSettingEnabledValues,
|
||||||
|
} from '@/utils/form';
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import { X } from 'lucide-react';
|
import { X } from 'lucide-react';
|
||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
@ -26,6 +30,7 @@ export function ChatSettings({ switchSettingVisible }: ChatSettingsProps) {
|
|||||||
|
|
||||||
const form = useForm<FormSchemaType>({
|
const form = useForm<FormSchemaType>({
|
||||||
resolver: zodResolver(formSchema),
|
resolver: zodResolver(formSchema),
|
||||||
|
shouldUnregister: true,
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
name: '',
|
name: '',
|
||||||
language: 'English',
|
language: 'English',
|
||||||
@ -47,14 +52,18 @@ export function ChatSettings({ switchSettingVisible }: ChatSettingsProps) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
async function onSubmit(values: FormSchemaType) {
|
async function onSubmit(values: FormSchemaType) {
|
||||||
const icon = values.icon;
|
const nextValues: Record<string, any> = removeUselessFieldsFromValues(
|
||||||
|
values,
|
||||||
|
'llm_setting.',
|
||||||
|
);
|
||||||
|
const icon = nextValues.icon;
|
||||||
const avatar =
|
const avatar =
|
||||||
Array.isArray(icon) && icon.length > 0
|
Array.isArray(icon) && icon.length > 0
|
||||||
? await transformFile2Base64(icon[0])
|
? await transformFile2Base64(icon[0])
|
||||||
: '';
|
: '';
|
||||||
setDialog({
|
setDialog({
|
||||||
...data,
|
...data,
|
||||||
...values,
|
...nextValues,
|
||||||
icon: avatar,
|
icon: avatar,
|
||||||
dialog_id: id,
|
dialog_id: id,
|
||||||
});
|
});
|
||||||
@ -65,9 +74,14 @@ export function ChatSettings({ switchSettingVisible }: ChatSettingsProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const llmSettingEnabledValues = setLLMSettingEnabledValues(
|
||||||
|
data.llm_setting,
|
||||||
|
);
|
||||||
|
|
||||||
const nextData = {
|
const nextData = {
|
||||||
...data,
|
...data,
|
||||||
icon: data.icon ? [transformBase64ToFile(data.icon)] : [],
|
icon: data.icon ? [transformBase64ToFile(data.icon)] : [],
|
||||||
|
...llmSettingEnabledValues,
|
||||||
};
|
};
|
||||||
form.reset(nextData as FormSchemaType);
|
form.reset(nextData as FormSchemaType);
|
||||||
}, [data, form]);
|
}, [data, form]);
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
import { LlmSettingSchema } from '@/components/llm-setting-items/next';
|
import {
|
||||||
|
LlmSettingEnabledSchema,
|
||||||
|
LlmSettingFieldSchema,
|
||||||
|
} from '@/components/llm-setting-items/next';
|
||||||
import { rerankFormSchema } from '@/components/rerank';
|
import { rerankFormSchema } from '@/components/rerank';
|
||||||
import { vectorSimilarityWeightSchema } from '@/components/similarity-slider';
|
import { vectorSimilarityWeightSchema } from '@/components/similarity-slider';
|
||||||
import { topnSchema } from '@/components/top-n-item';
|
import { topnSchema } from '@/components/top-n-item';
|
||||||
import { useTranslate } from '@/hooks/common-hooks';
|
import { useTranslate } from '@/hooks/common-hooks';
|
||||||
import { omit } from 'lodash';
|
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
export function useChatSettingSchema() {
|
export function useChatSettingSchema() {
|
||||||
@ -39,20 +41,23 @@ export function useChatSettingSchema() {
|
|||||||
}),
|
}),
|
||||||
prompt_config: promptConfigSchema,
|
prompt_config: promptConfigSchema,
|
||||||
...rerankFormSchema,
|
...rerankFormSchema,
|
||||||
llm_setting: z.object(omit(LlmSettingSchema, 'llm_id')),
|
llm_setting: z.object(LlmSettingFieldSchema),
|
||||||
|
...LlmSettingEnabledSchema,
|
||||||
llm_id: z.string().optional(),
|
llm_id: z.string().optional(),
|
||||||
...vectorSimilarityWeightSchema,
|
...vectorSimilarityWeightSchema,
|
||||||
...topnSchema,
|
...topnSchema,
|
||||||
meta_data_filter: z
|
meta_data_filter: z
|
||||||
.object({
|
.object({
|
||||||
method: z.string().optional(),
|
method: z.string().optional(),
|
||||||
manual: z.array(
|
manual: z
|
||||||
|
.array(
|
||||||
z.object({
|
z.object({
|
||||||
key: z.string(),
|
key: z.string(),
|
||||||
op: z.string(),
|
op: z.string(),
|
||||||
value: z.string(),
|
value: z.string(),
|
||||||
}),
|
}),
|
||||||
),
|
)
|
||||||
|
.optional(),
|
||||||
})
|
})
|
||||||
.optional(),
|
.optional(),
|
||||||
});
|
});
|
||||||
|
|||||||
@ -30,3 +30,22 @@ export const removeUselessFieldsFromValues = (values: any, prefix?: string) => {
|
|||||||
export function buildOptions(data: Record<string, any>) {
|
export function buildOptions(data: Record<string, any>) {
|
||||||
return Object.values(data).map((val) => ({ label: val, value: val }));
|
return Object.values(data).map((val) => ({ label: val, value: val }));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function setLLMSettingEnabledValues(
|
||||||
|
initialLlmSetting?: Record<string, any>,
|
||||||
|
) {
|
||||||
|
const values = Object.keys(variableEnabledFieldMap).reduce<
|
||||||
|
Record<string, boolean>
|
||||||
|
>((pre, field) => {
|
||||||
|
pre[field] =
|
||||||
|
initialLlmSetting === undefined
|
||||||
|
? false
|
||||||
|
: !!initialLlmSetting[
|
||||||
|
variableEnabledFieldMap[
|
||||||
|
field as keyof typeof variableEnabledFieldMap
|
||||||
|
]
|
||||||
|
];
|
||||||
|
return pre;
|
||||||
|
}, {});
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user