mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
### What problem does this PR solve? Feat: Convert the prompt field of the agent operator to an array #3221 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
@ -14,10 +14,13 @@ import { Message } from '@/interfaces/database/chat';
|
|||||||
import i18n from '@/locales/config';
|
import i18n from '@/locales/config';
|
||||||
import api from '@/utils/api';
|
import api from '@/utils/api';
|
||||||
import { message } from 'antd';
|
import { message } from 'antd';
|
||||||
|
import { get } from 'lodash';
|
||||||
import trim from 'lodash/trim';
|
import trim from 'lodash/trim';
|
||||||
import { useCallback, useEffect } from 'react';
|
import { useCallback, useEffect, useMemo } from 'react';
|
||||||
import { useParams } from 'umi';
|
import { useParams } from 'umi';
|
||||||
import { v4 as uuid } from 'uuid';
|
import { v4 as uuid } from 'uuid';
|
||||||
|
import { BeginId } from '../constant';
|
||||||
|
import useGraphStore from '../store';
|
||||||
import { receiveMessageError } from '../utils';
|
import { receiveMessageError } from '../utils';
|
||||||
|
|
||||||
const antMessage = message;
|
const antMessage = message;
|
||||||
@ -56,6 +59,17 @@ function findMessageFromList(eventList: IEventList) {
|
|||||||
return event?.data?.content;
|
return event?.data?.content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const useGetBeginNodePrologue = () => {
|
||||||
|
const getNode = useGraphStore((state) => state.getNode);
|
||||||
|
|
||||||
|
return useMemo(() => {
|
||||||
|
const formData = get(getNode(BeginId), 'data.form', {});
|
||||||
|
if (formData?.enablePrologue) {
|
||||||
|
return formData?.prologue;
|
||||||
|
}
|
||||||
|
}, [getNode]);
|
||||||
|
};
|
||||||
|
|
||||||
export const useSendNextMessage = () => {
|
export const useSendNextMessage = () => {
|
||||||
const {
|
const {
|
||||||
reference,
|
reference,
|
||||||
@ -75,6 +89,8 @@ export const useSendNextMessage = () => {
|
|||||||
api.runCanvas,
|
api.runCanvas,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const prologue = useGetBeginNodePrologue();
|
||||||
|
|
||||||
const sendMessage = useCallback(
|
const sendMessage = useCallback(
|
||||||
async ({ message }: { message: Message; messages?: Message[] }) => {
|
async ({ message }: { message: Message; messages?: Message[] }) => {
|
||||||
const params: Record<string, unknown> = {
|
const params: Record<string, unknown> = {
|
||||||
@ -138,19 +154,18 @@ export const useSendNextMessage = () => {
|
|||||||
});
|
});
|
||||||
}, [addNewestQuestion, handleSendMessage, done, setValue, value]);
|
}, [addNewestQuestion, handleSendMessage, done, setValue, value]);
|
||||||
|
|
||||||
const fetchPrologue = useCallback(async () => {
|
|
||||||
// fetch prologue
|
|
||||||
const sendRet = await send({ id: agentId });
|
|
||||||
if (receiveMessageError(sendRet)) {
|
|
||||||
message.error(sendRet?.data?.message);
|
|
||||||
} else {
|
|
||||||
refetch();
|
|
||||||
}
|
|
||||||
}, [agentId, refetch, send]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchPrologue();
|
if (prologue) {
|
||||||
}, [fetchPrologue]);
|
addNewestAnswer({
|
||||||
|
answer: prologue,
|
||||||
|
reference: {
|
||||||
|
chunks: [],
|
||||||
|
doc_aggs: [],
|
||||||
|
total: 0,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [addNewestAnswer, prologue]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
handlePressEnter,
|
handlePressEnter,
|
||||||
|
|||||||
4
web/src/pages/agent/form/agent-form/constant.ts
Normal file
4
web/src/pages/agent/form/agent-form/constant.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export enum PromptRole {
|
||||||
|
User = 'user',
|
||||||
|
Assistant = 'assistant',
|
||||||
|
}
|
||||||
@ -12,11 +12,7 @@ import { memo } from 'react';
|
|||||||
import { useFieldArray, useFormContext } from 'react-hook-form';
|
import { useFieldArray, useFormContext } from 'react-hook-form';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { PromptEditor } from '../components/prompt-editor';
|
import { PromptEditor } from '../components/prompt-editor';
|
||||||
|
import { PromptRole } from './constant';
|
||||||
export enum PromptRole {
|
|
||||||
User = 'user',
|
|
||||||
Assistant = 'assistant',
|
|
||||||
}
|
|
||||||
|
|
||||||
const options = [
|
const options = [
|
||||||
{ label: 'User', value: PromptRole.User },
|
{ label: 'User', value: PromptRole.User },
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import { FormContainer } from '@/components/form-container';
|
|||||||
import { LargeModelFormField } from '@/components/large-model-form-field';
|
import { LargeModelFormField } from '@/components/large-model-form-field';
|
||||||
import { LlmSettingSchema } from '@/components/llm-setting-items/next';
|
import { LlmSettingSchema } from '@/components/llm-setting-items/next';
|
||||||
import { MessageHistoryWindowSizeFormField } from '@/components/message-history-window-size-item';
|
import { MessageHistoryWindowSizeFormField } from '@/components/message-history-window-size-item';
|
||||||
|
import { BlockButton } from '@/components/ui/button';
|
||||||
import {
|
import {
|
||||||
Form,
|
Form,
|
||||||
FormControl,
|
FormControl,
|
||||||
@ -9,30 +10,29 @@ import {
|
|||||||
FormItem,
|
FormItem,
|
||||||
FormLabel,
|
FormLabel,
|
||||||
} from '@/components/ui/form';
|
} from '@/components/ui/form';
|
||||||
import { useFetchModelId } from '@/hooks/logic-hooks';
|
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { initialAgentValues } from '../../constant';
|
import { initialAgentValues } from '../../constant';
|
||||||
import { useFormValues } from '../../hooks/use-form-values';
|
|
||||||
import { useWatchFormChange } from '../../hooks/use-watch-form-change';
|
|
||||||
import { INextOperatorForm } from '../../interface';
|
import { INextOperatorForm } from '../../interface';
|
||||||
import { Output } from '../components/output';
|
import { Output } from '../components/output';
|
||||||
import { PromptEditor } from '../components/prompt-editor';
|
import { PromptEditor } from '../components/prompt-editor';
|
||||||
import DynamicPrompt from './dynamic-prompt';
|
import { useValues } from './use-values';
|
||||||
|
import { useWatchFormChange } from './use-watch-change';
|
||||||
|
|
||||||
const FormSchema = z.object({
|
const FormSchema = z.object({
|
||||||
sys_prompt: z.string(),
|
sys_prompt: z.string(),
|
||||||
prompts: z
|
prompts: z.string().optional(),
|
||||||
.array(
|
// prompts: z
|
||||||
z.object({
|
// .array(
|
||||||
role: z.string(),
|
// z.object({
|
||||||
content: z.string(),
|
// role: z.string(),
|
||||||
}),
|
// content: z.string(),
|
||||||
)
|
// }),
|
||||||
.optional(),
|
// )
|
||||||
|
// .optional(),
|
||||||
message_history_window_size: z.coerce.number(),
|
message_history_window_size: z.coerce.number(),
|
||||||
tools: z
|
tools: z
|
||||||
.array(
|
.array(
|
||||||
@ -46,11 +46,8 @@ const FormSchema = z.object({
|
|||||||
|
|
||||||
const AgentForm = ({ node }: INextOperatorForm) => {
|
const AgentForm = ({ node }: INextOperatorForm) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const llmId = useFetchModelId();
|
|
||||||
const defaultValues = useFormValues(
|
const defaultValues = useValues(node);
|
||||||
{ ...initialAgentValues, llm_id: llmId },
|
|
||||||
node,
|
|
||||||
);
|
|
||||||
|
|
||||||
const outputList = useMemo(() => {
|
const outputList = useMemo(() => {
|
||||||
return [
|
return [
|
||||||
@ -94,8 +91,22 @@ const AgentForm = ({ node }: INextOperatorForm) => {
|
|||||||
<MessageHistoryWindowSizeFormField></MessageHistoryWindowSizeFormField>
|
<MessageHistoryWindowSizeFormField></MessageHistoryWindowSizeFormField>
|
||||||
</FormContainer>
|
</FormContainer>
|
||||||
<FormContainer>
|
<FormContainer>
|
||||||
<DynamicPrompt></DynamicPrompt>
|
{/* <DynamicPrompt></DynamicPrompt> */}
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name={`prompts`}
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem className="flex-1">
|
||||||
|
<FormControl>
|
||||||
|
<section>
|
||||||
|
<PromptEditor {...field} showToolbar={false}></PromptEditor>
|
||||||
|
</section>
|
||||||
|
</FormControl>
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
</FormContainer>
|
</FormContainer>
|
||||||
|
<BlockButton>Add Agent</BlockButton>
|
||||||
<Output list={outputList}></Output>
|
<Output list={outputList}></Output>
|
||||||
</form>
|
</form>
|
||||||
</Form>
|
</Form>
|
||||||
|
|||||||
30
web/src/pages/agent/form/agent-form/use-values.ts
Normal file
30
web/src/pages/agent/form/agent-form/use-values.ts
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import { useFetchModelId } from '@/hooks/logic-hooks';
|
||||||
|
import { RAGFlowNodeType } from '@/interfaces/database/flow';
|
||||||
|
import { get, isEmpty } from 'lodash';
|
||||||
|
import { useMemo } from 'react';
|
||||||
|
import { initialAgentValues } from '../../constant';
|
||||||
|
|
||||||
|
export function useValues(node?: RAGFlowNodeType) {
|
||||||
|
const llmId = useFetchModelId();
|
||||||
|
|
||||||
|
const defaultValues = useMemo(
|
||||||
|
() => ({
|
||||||
|
...initialAgentValues,
|
||||||
|
llm_id: llmId,
|
||||||
|
prompts: '',
|
||||||
|
}),
|
||||||
|
[llmId],
|
||||||
|
);
|
||||||
|
|
||||||
|
const values = useMemo(() => {
|
||||||
|
const formData = node?.data?.form;
|
||||||
|
|
||||||
|
if (isEmpty(formData)) {
|
||||||
|
return defaultValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { ...formData, prompts: get(formData, 'prompts.0.content', '') };
|
||||||
|
}, [defaultValues, node?.data?.form]);
|
||||||
|
|
||||||
|
return values;
|
||||||
|
}
|
||||||
22
web/src/pages/agent/form/agent-form/use-watch-change.ts
Normal file
22
web/src/pages/agent/form/agent-form/use-watch-change.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { useEffect } from 'react';
|
||||||
|
import { UseFormReturn, useWatch } from 'react-hook-form';
|
||||||
|
import useGraphStore from '../../store';
|
||||||
|
import { PromptRole } from './constant';
|
||||||
|
|
||||||
|
export function useWatchFormChange(id?: string, form?: UseFormReturn) {
|
||||||
|
let values = useWatch({ control: form?.control });
|
||||||
|
const updateNodeForm = useGraphStore((state) => state.updateNodeForm);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// Manually triggered form updates are synchronized to the canvas
|
||||||
|
if (id && form?.formState.isDirty) {
|
||||||
|
values = form?.getValues();
|
||||||
|
let nextValues: any = {
|
||||||
|
...values,
|
||||||
|
prompts: [{ role: PromptRole.User, content: values.prompts }],
|
||||||
|
};
|
||||||
|
|
||||||
|
updateNodeForm(id, nextValues);
|
||||||
|
}
|
||||||
|
}, [form?.formState.isDirty, id, updateNodeForm, values]);
|
||||||
|
}
|
||||||
@ -56,10 +56,13 @@ function filterAllUpstreamNodeIds(edges: Edge[], nodeIds: string[]) {
|
|||||||
}, []);
|
}, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildOutputOptions(outputs: Record<string, any> = {}) {
|
function buildOutputOptions(
|
||||||
|
outputs: Record<string, any> = {},
|
||||||
|
nodeId?: string,
|
||||||
|
) {
|
||||||
return Object.keys(outputs).map((x) => ({
|
return Object.keys(outputs).map((x) => ({
|
||||||
label: x,
|
label: x,
|
||||||
value: x,
|
value: `${nodeId}@${x}`,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,7 +87,7 @@ export function useBuildNodeOutputOptions(nodeId?: string) {
|
|||||||
label: x.data.name,
|
label: x.data.name,
|
||||||
value: x.id,
|
value: x.id,
|
||||||
title: x.data.name,
|
title: x.data.name,
|
||||||
options: buildOutputOptions(x.data.form.outputs),
|
options: buildOutputOptions(x.data.form.outputs, x.id),
|
||||||
}));
|
}));
|
||||||
}, [edges, nodeId, nodes]);
|
}, [edges, nodeId, nodes]);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user