Skip to content

Commit 8e87436

Browse files
authored
Feat: Modify the Python language template code of the code operator #4977 (#7714)
### What problem does this PR solve? Feat: Modify the Python language template code of the code operator #4977 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
1 parent e8e2a95 commit 8e87436

File tree

5 files changed

+184
-100
lines changed

5 files changed

+184
-100
lines changed

web/src/constants/agent.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,19 @@ export enum ProgrammingLanguage {
44
}
55

66
export const CodeTemplateStrMap = {
7-
[ProgrammingLanguage.Python]: `
8-
def main(arg1: str, arg2: str) -> dict:
9-
return {
10-
"result": arg1 + arg2,
11-
}
12-
`,
13-
[ProgrammingLanguage.Javascript]: `
14-
const axios = require('axios');
15-
async function main(args) {
16-
try {
17-
const response = await axios.get('https://github.com/infiniflow/ragflow');
18-
console.log('Body:', response.data);
19-
} catch (error) {
20-
console.error('Error:', error.message);
21-
}
22-
}
7+
[ProgrammingLanguage.Python]: `def main(arg1: str, arg2: str) -> str:
8+
return f"result: {arg1 + arg2}"
9+
`,
10+
[ProgrammingLanguage.Javascript]: `const axios = require('axios');
11+
async function main(args) {
12+
try {
13+
const response = await axios.get('https://github.com/infiniflow/ragflow');
14+
console.log('Body:', response.data);
15+
} catch (error) {
16+
console.error('Error:', error.message);
17+
}
18+
}
2319
24-
module.exports = { main };
25-
`,
20+
module.exports = { main };
21+
`,
2622
};

web/src/hooks/use-knowledge-request.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export const useTestRetrieval = () => {
9191
page,
9292
pageSize,
9393
handleFilterSubmit,
94+
filterValue,
9495
};
9596
};
9697

Lines changed: 73 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,14 @@
1-
import { FormContainer } from '@/components/form-container';
2-
import { FilterButton } from '@/components/list-filter-bar';
3-
import { FilterPopover } from '@/components/list-filter-bar/filter-popover';
4-
import { FilterCollection } from '@/components/list-filter-bar/interface';
51
import { Button } from '@/components/ui/button';
6-
import { RAGFlowPagination } from '@/components/ui/ragflow-pagination';
7-
import { useTranslate } from '@/hooks/common-hooks';
82
import { useTestRetrieval } from '@/hooks/use-knowledge-request';
9-
import { ITestingChunk } from '@/interfaces/database/knowledge';
10-
import { camelCase } from 'lodash';
113
import { Plus } from 'lucide-react';
12-
import { useMemo } from 'react';
4+
import { useCallback, useState } from 'react';
135
import { TopTitle } from '../dataset-title';
146
import TestingForm from './testing-form';
7+
import { TestingResult } from './testing-result';
158

16-
const similarityList: Array<{ field: keyof ITestingChunk; label: string }> = [
17-
{ field: 'similarity', label: 'Hybrid Similarity' },
18-
{ field: 'term_similarity', label: 'Term Similarity' },
19-
{ field: 'vector_similarity', label: 'Vector Similarity' },
20-
];
21-
22-
const ChunkTitle = ({ item }: { item: ITestingChunk }) => {
23-
const { t } = useTranslate('knowledgeDetails');
24-
return (
25-
<div className="flex gap-3 text-xs text-text-sub-title-invert italic">
26-
{similarityList.map((x) => (
27-
<div key={x.field} className="space-x-1">
28-
<span>{((item[x.field] as number) * 100).toFixed(2)}</span>
29-
<span>{t(camelCase(x.field))}</span>
30-
</div>
31-
))}
32-
</div>
33-
);
34-
};
9+
function Vertical() {
10+
return <div>xxx</div>;
11+
}
3512

3613
export default function RetrievalTesting() {
3714
const {
@@ -43,22 +20,18 @@ export default function RetrievalTesting() {
4320
page,
4421
pageSize,
4522
handleFilterSubmit,
23+
filterValue,
4624
} = useTestRetrieval();
4725

48-
const filters: FilterCollection[] = useMemo(() => {
49-
return [
50-
{
51-
field: 'doc_ids',
52-
label: 'File',
53-
list:
54-
data.doc_aggs?.map((x) => ({
55-
id: x.doc_id,
56-
label: x.doc_name,
57-
count: x.count,
58-
})) ?? [],
59-
},
60-
];
61-
}, [data.doc_aggs]);
26+
const [count, setCount] = useState(1);
27+
28+
const addCount = useCallback(() => {
29+
setCount(2);
30+
}, []);
31+
32+
const removeCount = useCallback(() => {
33+
setCount(1);
34+
}, []);
6235

6336
return (
6437
<div className="p-5">
@@ -70,47 +43,66 @@ export default function RetrievalTesting() {
7043
></TopTitle>
7144
<Button>Save as Preset</Button>
7245
</section>
73-
<section className="flex divide-x h-full">
74-
<div className="p-4 flex-1">
75-
<div className="flex justify-between pb-2.5">
76-
<span className="text-text-title font-semibold text-2xl">
77-
Test setting
78-
</span>
79-
<Button variant={'outline'}>
80-
<Plus /> Add New Test
81-
</Button>
82-
</div>
83-
<TestingForm
84-
loading={loading}
85-
setValues={setValues}
86-
refetch={refetch}
87-
></TestingForm>
88-
</div>
89-
<div className="p-4 flex-1">
90-
<div className="flex justify-between pb-2.5">
91-
<span className="text-text-title font-semibold text-2xl">
92-
Test results
93-
</span>
94-
<FilterPopover filters={filters} onChange={handleFilterSubmit}>
95-
<FilterButton></FilterButton>
96-
</FilterPopover>
46+
{count === 1 ? (
47+
<section className="flex divide-x h-full">
48+
<div className="p-4 flex-1">
49+
<div className="flex justify-between pb-2.5">
50+
<span className="text-text-title font-semibold text-2xl">
51+
Test setting
52+
</span>
53+
<Button variant={'outline'} onClick={addCount}>
54+
<Plus /> Add New Test
55+
</Button>
56+
</div>
57+
<TestingForm
58+
loading={loading}
59+
setValues={setValues}
60+
refetch={refetch}
61+
></TestingForm>
9762
</div>
98-
<section className="flex flex-col gap-5 overflow-auto h-[76vh] mb-5">
99-
{data.chunks?.map((x) => (
100-
<FormContainer key={x.chunk_id} className="px-5 py-2.5">
101-
<ChunkTitle item={x}></ChunkTitle>
102-
<p className="!mt-2.5"> {x.content_with_weight}</p>
103-
</FormContainer>
104-
))}
105-
</section>
106-
<RAGFlowPagination
107-
total={data.total}
108-
onChange={onPaginationChange}
109-
current={page}
63+
<TestingResult
64+
data={data}
65+
page={page}
11066
pageSize={pageSize}
111-
></RAGFlowPagination>
112-
</div>
113-
</section>
67+
filterValue={filterValue}
68+
handleFilterSubmit={handleFilterSubmit}
69+
onPaginationChange={onPaginationChange}
70+
></TestingResult>
71+
</section>
72+
) : (
73+
<section className="flex gap-2">
74+
<div className="flex-1">
75+
<TestingForm
76+
loading={loading}
77+
setValues={setValues}
78+
refetch={refetch}
79+
></TestingForm>
80+
<TestingResult
81+
data={data}
82+
page={page}
83+
pageSize={pageSize}
84+
filterValue={filterValue}
85+
handleFilterSubmit={handleFilterSubmit}
86+
onPaginationChange={onPaginationChange}
87+
></TestingResult>
88+
</div>
89+
<div className="flex-1">
90+
<TestingForm
91+
loading={loading}
92+
setValues={setValues}
93+
refetch={refetch}
94+
></TestingForm>
95+
<TestingResult
96+
data={data}
97+
page={page}
98+
pageSize={pageSize}
99+
filterValue={filterValue}
100+
handleFilterSubmit={handleFilterSubmit}
101+
onPaginationChange={onPaginationChange}
102+
></TestingResult>
103+
</div>
104+
</section>
105+
)}
114106
</div>
115107
);
116108
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { FormContainer } from '@/components/form-container';
2+
import { FilterButton } from '@/components/list-filter-bar';
3+
import { FilterPopover } from '@/components/list-filter-bar/filter-popover';
4+
import { FilterCollection } from '@/components/list-filter-bar/interface';
5+
import { RAGFlowPagination } from '@/components/ui/ragflow-pagination';
6+
import { useTranslate } from '@/hooks/common-hooks';
7+
import { useTestRetrieval } from '@/hooks/use-knowledge-request';
8+
import { ITestingChunk } from '@/interfaces/database/knowledge';
9+
import camelCase from 'lodash/camelCase';
10+
import { useMemo } from 'react';
11+
12+
const similarityList: Array<{ field: keyof ITestingChunk; label: string }> = [
13+
{ field: 'similarity', label: 'Hybrid Similarity' },
14+
{ field: 'term_similarity', label: 'Term Similarity' },
15+
{ field: 'vector_similarity', label: 'Vector Similarity' },
16+
];
17+
18+
const ChunkTitle = ({ item }: { item: ITestingChunk }) => {
19+
const { t } = useTranslate('knowledgeDetails');
20+
return (
21+
<div className="flex gap-3 text-xs text-text-sub-title-invert italic">
22+
{similarityList.map((x) => (
23+
<div key={x.field} className="space-x-1">
24+
<span>{((item[x.field] as number) * 100).toFixed(2)}</span>
25+
<span>{t(camelCase(x.field))}</span>
26+
</div>
27+
))}
28+
</div>
29+
);
30+
};
31+
32+
type TestingResultProps = Pick<
33+
ReturnType<typeof useTestRetrieval>,
34+
| 'data'
35+
| 'filterValue'
36+
| 'handleFilterSubmit'
37+
| 'page'
38+
| 'pageSize'
39+
| 'onPaginationChange'
40+
>;
41+
42+
export function TestingResult({
43+
filterValue,
44+
handleFilterSubmit,
45+
page,
46+
pageSize,
47+
onPaginationChange,
48+
data,
49+
}: TestingResultProps) {
50+
const filters: FilterCollection[] = useMemo(() => {
51+
return [
52+
{
53+
field: 'doc_ids',
54+
label: 'File',
55+
list:
56+
data.doc_aggs?.map((x) => ({
57+
id: x.doc_id,
58+
label: x.doc_name,
59+
count: x.count,
60+
})) ?? [],
61+
},
62+
];
63+
}, [data.doc_aggs]);
64+
65+
return (
66+
<div className="p-4 flex-1">
67+
<div className="flex justify-between pb-2.5">
68+
<span className="text-text-title font-semibold text-2xl">
69+
Test results
70+
</span>
71+
<FilterPopover
72+
filters={filters}
73+
onChange={handleFilterSubmit}
74+
value={filterValue}
75+
>
76+
<FilterButton></FilterButton>
77+
</FilterPopover>
78+
</div>
79+
<section className="flex flex-col gap-5 overflow-auto h-[76vh] mb-5">
80+
{data.chunks?.map((x) => (
81+
<FormContainer key={x.chunk_id} className="px-5 py-2.5">
82+
<ChunkTitle item={x}></ChunkTitle>
83+
<p className="!mt-2.5"> {x.content_with_weight}</p>
84+
</FormContainer>
85+
))}
86+
</section>
87+
<RAGFlowPagination
88+
total={data.total}
89+
onChange={onPaginationChange}
90+
current={page}
91+
pageSize={pageSize}
92+
></RAGFlowPagination>
93+
</div>
94+
);
95+
}

web/src/pages/flow/form/code-form/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const CodeForm = ({ onValuesChange, form, node }: IOperatorForm) => {
5151
className="bg-gray-100 rounded dark:bg-gray-800"
5252
>
5353
<Editor
54-
height={200}
54+
height={600}
5555
theme="vs-dark"
5656
language={formData.lang}
5757
options={{

0 commit comments

Comments
 (0)