Skip to content

Commit

Permalink
feat: Fetch mind map in search page infiniflow#2247 (infiniflow#2292)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?
feat: Fetch mind map in search page infiniflow#2247

### Type of change


- [x] New Feature (non-breaking change which adds functionality)
  • Loading branch information
cike8899 authored Sep 6, 2024
1 parent 92106a1 commit 29fd9c3
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 133 deletions.
6 changes: 4 additions & 2 deletions web/src/components/indented-tree/indented-tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from '@antv/g6';
import { TreeData } from '@antv/g6/lib/types';
import isEmpty from 'lodash/isEmpty';
import { useCallback, useEffect, useRef } from 'react';
import React, { useCallback, useEffect, useRef } from 'react';

const rootId = 'root';

Expand Down Expand Up @@ -294,9 +294,10 @@ register(
interface IProps {
data: TreeData;
show: boolean;
style?: React.CSSProperties;
}

const IndentedTree = ({ data, show }: IProps) => {
const IndentedTree = ({ data, show, style = {} }: IProps) => {
const containerRef = useRef<HTMLDivElement>(null);
const graphRef = useRef<Graph | null>(null);

Expand Down Expand Up @@ -388,6 +389,7 @@ const IndentedTree = ({ data, show }: IProps) => {
width: '90vw',
height: '80vh',
display: show ? 'block' : 'none',
...style,
}}
/>
);
Expand Down
21 changes: 20 additions & 1 deletion web/src/hooks/chat-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,13 +490,32 @@ export const useFetchMindMap = () => {
mutateAsync,
} = useMutation({
mutationKey: ['fetchMindMap'],
gcTime: 0,
mutationFn: async (params: IAskRequestBody) => {
const { data } = await chatService.getMindMap(params);

return data;
return data?.data ?? [];
},
});

return { data, loading, fetchMindMap: mutateAsync };
};

export const useFetchRelatedQuestions = () => {
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: ['fetchRelatedQuestions'],
gcTime: 0,
mutationFn: async (question: string): Promise<string[]> => {
const { data } = await chatService.getRelatedQuestions({ question });

return data?.data ?? [];
},
});

return { data, loading, fetchRelatedQuestions: mutateAsync };
};
//#endregion
2 changes: 1 addition & 1 deletion web/src/hooks/flow-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const useFetchFlowTemplates = (): ResponseType<IFlowTemplate[]> => {
data.data.unshift({
id: uuid(),
title: 'Blank',
description: 'Create from nothing',
description: 'Create your agent from scratch',
dsl: EmptyDsl,
});
}
Expand Down
2 changes: 1 addition & 1 deletion web/src/interfaces/request/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export interface IFeedbackRequestBody {
}

export interface IAskRequestBody {
questionkb_ids: string;
question: string;
kb_ids: string[];
}
6 changes: 3 additions & 3 deletions web/src/layouts/components/header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useLocation } from 'umi';
import Toolbar from '../right-toolbar';

import { useFetchAppConf } from '@/hooks/logic-hooks';
import { MessageOutlined } from '@ant-design/icons';
import { SearchOutlined } from '@ant-design/icons';
import styles from './index.less';

const { Header } = Layout;
Expand All @@ -26,8 +26,8 @@ const RagHeader = () => {
const tagsData = useMemo(
() => [
{ path: '/knowledge', name: t('knowledgeBase'), icon: KnowledgeBaseIcon },
{ path: '/chat', name: t('chat'), icon: MessageOutlined },
// { path: '/search', name: t('search'), icon: SearchOutlined },
// { path: '/chat', name: t('chat'), icon: MessageOutlined },
{ path: '/search', name: t('search'), icon: SearchOutlined },
{ path: '/flow', name: t('flow'), icon: GraphIcon },
{ path: '/file', name: t('fileManager'), icon: FileIcon },
],
Expand Down
3 changes: 2 additions & 1 deletion web/src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ The above is the content you need to summarize.`,
messagePlaceholder: 'message',
messageMsg: 'Please input message or delete this field.',
addField: 'Add field',
addMessage: 'Add message',
loop: 'Loop',
loopTip:
'Loop is the upper limit of the number of loops of the current component, when the number of loops exceeds the value of loop, it means that the component can not complete the current task, please re-optimize agent',
Expand Down Expand Up @@ -672,7 +673,7 @@ The above is the content you need to summarize.`,
begin: 'Begin',
message: 'Message',
blank: 'Blank',
createFromNothing: 'Create from nothing',
createFromNothing: 'Create your agent from scratch',
addItem: 'Add Item',
addSubItem: 'Add Sub Item',
nameRequiredMsg: 'Name is required',
Expand Down
1 change: 1 addition & 0 deletions web/src/locales/zh-traditional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ export default {
messagePlaceholder: '訊息',
messageMsg: '請輸入訊息或刪除此欄位。',
addField: '新增字段',
addMessage: '新增訊息',
loop: '循環上限',
loopTip:
'loop為目前元件循環次數上限,當循環次數超過loop的值時,表示元件無法完成目前任務,請重新最佳化agent',
Expand Down
1 change: 1 addition & 0 deletions web/src/locales/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ export default {
messagePlaceholder: '消息',
messageMsg: '请输入消息或删除此字段。',
addField: '新增字段',
addMessage: '新增消息',
loop: '循环上限',
loopTip:
'loop为当前组件循环次数上限,当循环次数超过loop的值时,说明组件不能完成当前任务,请重新优化agent',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ const AssistantSetting = ({ show }: ISegmentedContentProps) => {
>
<Switch />
</Form.Item>
<Form.Item
{/* <Form.Item
label={t('selfRag')}
valuePropName="checked"
name={['prompt_config', 'self_rag']}
tooltip={t('selfRagTip')}
initialValue={false}
>
<Switch />
</Form.Item>
</Form.Item> */}
{/* <Form.Item
label={t('tts')}
valuePropName="checked"
Expand Down
2 changes: 1 addition & 1 deletion web/src/pages/flow/message-form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const MessageForm = ({ onValuesChange, form }: IOperatorForm) => {
style={{ width: '80%' }}
icon={<PlusOutlined />}
>
{t('addField')}
{t('addMessage')}
</Button>
</Form.Item>
</>
Expand Down
109 changes: 0 additions & 109 deletions web/src/pages/force-graph/index.tsx
Original file line number Diff line number Diff line change
@@ -1,112 +1,3 @@
import { Graph } from '@antv/g6';
import { useSize } from 'ahooks';
import { useEffect, useRef } from 'react';
import { graphData } from './constant';
import InputWithUpload from './input-upload';

import styles from './index.less';
import { Converter } from './util';

const converter = new Converter();

const nextData = converter.buildNodesAndCombos(
graphData.nodes,
graphData.edges,
);
console.log('🚀 ~ nextData:', nextData);

const finalData = { ...graphData, ...nextData };

const ForceGraph = () => {
const containerRef = useRef<HTMLDivElement>(null);
const size = useSize(containerRef);
let graph: Graph;

const render = () => {
graph = new Graph({
container: containerRef.current!,
autoFit: 'view',
behaviors: [
'drag-element',
'drag-canvas',
'zoom-canvas',
'collapse-expand',
{
type: 'hover-activate',
degree: 1, // 👈🏻 Activate relations.
},
],
plugins: [
{
type: 'tooltip',
getContent: (e, items) => {
if (items.every((x) => x?.description)) {
let result = ``;
items.forEach((item) => {
if (item?.description) {
result += `<p>${item?.description}</p>`;
}
});
return result;
}
return undefined;
},
},
],
layout: {
type: 'combo-combined',
preventOverlap: true,
comboPadding: 1,
spacing: 20,
},
node: {
style: {
size: 20,
labelText: (d) => d.id,
labelPadding: 30,
// labelOffsetX: 20,
// labelOffsetY: 5,
labelPlacement: 'center',
labelWordWrap: true,
},
palette: {
type: 'group',
field: (d) => d.combo,
},
// state: {
// highlight: {
// fill: '#D580FF',
// halo: true,
// lineWidth: 0,
// },
// dim: {
// fill: '#99ADD1',
// },
// },
},
edge: {
style: (model) => {
const { size, color } = model.data;
return {
stroke: color || '#99ADD1',
lineWidth: size || 1,
};
},
},
// data: graphData,
});

graph.setData(finalData);

graph.render();
};

useEffect(() => {
console.info('rendered');
render();
}, []);

return <div ref={containerRef} className={styles.container} />;
};

export default InputWithUpload;
25 changes: 23 additions & 2 deletions web/src/pages/search/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useFetchMindMap, useFetchRelatedQuestions } from '@/hooks/chat-hooks';
import { useTestChunkRetrieval } from '@/hooks/knowledge-hooks';
import { useSendMessageWithSse } from '@/hooks/logic-hooks';
import { IAnswer } from '@/interfaces/database/chat';
Expand All @@ -10,15 +11,27 @@ export const useSendQuestion = (kbIds: string[]) => {
const { testChunk, loading } = useTestChunkRetrieval();
const [sendingLoading, setSendingLoading] = useState(false);
const [currentAnswer, setCurrentAnswer] = useState({} as IAnswer);
const { fetchRelatedQuestions, data: relatedQuestions } =
useFetchRelatedQuestions();
const {
fetchMindMap,
data: mindMap,
loading: mindMapLoading,
} = useFetchMindMap();

const sendQuestion = useCallback(
(question: string) => {
setCurrentAnswer({} as IAnswer);
setSendingLoading(true);
send({ kb_ids: kbIds, question });
testChunk({ kb_id: kbIds, highlight: true, question });
fetchMindMap({
question,
kb_ids: kbIds,
});
fetchRelatedQuestions(question);
},
[send, testChunk, kbIds],
[send, testChunk, kbIds, fetchRelatedQuestions, fetchMindMap],
);

useEffect(() => {
Expand All @@ -33,5 +46,13 @@ export const useSendQuestion = (kbIds: string[]) => {
}
}, [done]);

return { sendQuestion, loading, sendingLoading, answer: currentAnswer };
return {
sendQuestion,
loading,
sendingLoading,
answer: currentAnswer,
relatedQuestions: relatedQuestions?.slice(0, 5) ?? [],
mindMap,
mindMapLoading,
};
};
6 changes: 5 additions & 1 deletion web/src/pages/search/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
.card {
width: 100%;
}
.tag {
padding: 4px 8px;
font-size: 14px;
}
}

.searchSide {
Expand Down Expand Up @@ -49,6 +53,6 @@

.graph {
width: 40%;
background-color: bisque;
padding-right: 10px;
}
}
Loading

0 comments on commit 29fd9c3

Please sign in to comment.