mirror of
https://github.com/csunny/DB-GPT.git
synced 2025-08-13 14:06:43 +00:00
feat: add documents
This commit is contained in:
parent
db5f35d334
commit
6335e05b74
@ -2,14 +2,60 @@
|
|||||||
|
|
||||||
import { useRouter, useSearchParams } from 'next/navigation'
|
import { useRouter, useSearchParams } from 'next/navigation'
|
||||||
import React, { useState, useEffect } from 'react'
|
import React, { useState, useEffect } from 'react'
|
||||||
import { Button, Table } from '@/lib/mui'
|
import {
|
||||||
|
Button,
|
||||||
|
Table,
|
||||||
|
Sheet,
|
||||||
|
Modal,
|
||||||
|
Box,
|
||||||
|
Stack,
|
||||||
|
Input,
|
||||||
|
styled
|
||||||
|
} from '@/lib/mui'
|
||||||
import moment from 'moment'
|
import moment from 'moment'
|
||||||
import { message } from 'antd'
|
import { message } from 'antd'
|
||||||
|
|
||||||
|
const stepsOfAddingDocument = [
|
||||||
|
'Choose a Datasource type',
|
||||||
|
'Setup the Datasource'
|
||||||
|
]
|
||||||
|
const documentTypeList = [
|
||||||
|
{
|
||||||
|
type: 'text',
|
||||||
|
title: 'Text',
|
||||||
|
subTitle: 'Paste some text'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'webPage',
|
||||||
|
title: 'Web Page',
|
||||||
|
subTitle: 'Crawl text from a web page'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'file',
|
||||||
|
title: 'File',
|
||||||
|
subTitle: 'It can be: PDF, CSV, JSON, Text, PowerPoint, Word, Excel'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
const Item = styled(Sheet)(({ theme }) => ({
|
||||||
|
width: '50%',
|
||||||
|
backgroundColor:
|
||||||
|
theme.palette.mode === 'dark' ? theme.palette.background.level1 : '#fff',
|
||||||
|
...theme.typography.body2,
|
||||||
|
padding: theme.spacing(1),
|
||||||
|
textAlign: 'center',
|
||||||
|
borderRadius: 4,
|
||||||
|
color: theme.vars.palette.text.secondary
|
||||||
|
}))
|
||||||
|
|
||||||
const Documents = () => {
|
const Documents = () => {
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const spaceName = useSearchParams().get('name')
|
const spaceName = useSearchParams().get('name')
|
||||||
|
const [isAddDocumentModalShow, setIsAddDocumentModalShow] =
|
||||||
|
useState<boolean>(false)
|
||||||
|
const [activeStep, setActiveStep] = useState<number>(0)
|
||||||
const [documents, setDocuments] = useState<any>([])
|
const [documents, setDocuments] = useState<any>([])
|
||||||
|
const [webPageUrl, setWebPageUrl] = useState<string>('')
|
||||||
|
const [documentName, setDocumentName] = useState<string>('')
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function fetchDocuments() {
|
async function fetchDocuments() {
|
||||||
const res = await fetch(
|
const res = await fetch(
|
||||||
@ -31,6 +77,19 @@ const Documents = () => {
|
|||||||
}, [])
|
}, [])
|
||||||
return (
|
return (
|
||||||
<div className="p-4">
|
<div className="p-4">
|
||||||
|
<Sheet
|
||||||
|
sx={{
|
||||||
|
display: 'flex',
|
||||||
|
flexDirection: 'row-reverse'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Button
|
||||||
|
variant="outlined"
|
||||||
|
onClick={() => setIsAddDocumentModalShow(true)}
|
||||||
|
>
|
||||||
|
+ Add Datasource
|
||||||
|
</Button>
|
||||||
|
</Sheet>
|
||||||
<Table sx={{ '& thead th:nth-child(1)': { width: '40%' } }}>
|
<Table sx={{ '& thead th:nth-child(1)': { width: '40%' } }}>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
@ -70,9 +129,9 @@ const Documents = () => {
|
|||||||
)
|
)
|
||||||
const data = await res.json()
|
const data = await res.json()
|
||||||
if (data.success) {
|
if (data.success) {
|
||||||
message.success('success');
|
message.success('success')
|
||||||
} else {
|
} else {
|
||||||
message.error(data.err_msg || 'failed');
|
message.error(data.err_msg || 'failed')
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
@ -95,6 +154,137 @@ const Documents = () => {
|
|||||||
))}
|
))}
|
||||||
</tbody>
|
</tbody>
|
||||||
</Table>
|
</Table>
|
||||||
|
<Modal
|
||||||
|
sx={{
|
||||||
|
display: 'flex',
|
||||||
|
justifyContent: 'center',
|
||||||
|
alignItems: 'center',
|
||||||
|
'z-index': 1000
|
||||||
|
}}
|
||||||
|
open={isAddDocumentModalShow}
|
||||||
|
onClose={() => setIsAddDocumentModalShow(false)}
|
||||||
|
>
|
||||||
|
<Sheet
|
||||||
|
variant="outlined"
|
||||||
|
sx={{
|
||||||
|
width: 800,
|
||||||
|
borderRadius: 'md',
|
||||||
|
p: 3,
|
||||||
|
boxShadow: 'lg'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Box sx={{ width: '100%' }}>
|
||||||
|
<Stack spacing={2} direction="row">
|
||||||
|
{stepsOfAddingDocument.map((item: any, index: number) => (
|
||||||
|
<Item
|
||||||
|
key={item}
|
||||||
|
sx={{ fontWeight: activeStep === index ? 'bold' : '' }}
|
||||||
|
>
|
||||||
|
{item}
|
||||||
|
</Item>
|
||||||
|
))}
|
||||||
|
</Stack>
|
||||||
|
</Box>
|
||||||
|
{activeStep === 0 ? (
|
||||||
|
<>
|
||||||
|
<Box sx={{ margin: '30px auto' }}>
|
||||||
|
{documentTypeList.map((item: any) => (
|
||||||
|
<Sheet
|
||||||
|
key={item.type}
|
||||||
|
sx={{
|
||||||
|
boxSizing: 'border-box',
|
||||||
|
height: '80px',
|
||||||
|
padding: '12px',
|
||||||
|
display: 'flex',
|
||||||
|
flexDirection: 'column',
|
||||||
|
justifyContent: 'space-between',
|
||||||
|
border: '1px solid gray',
|
||||||
|
borderRadius: '6px',
|
||||||
|
marginBottom: '20px',
|
||||||
|
cursor: 'pointer'
|
||||||
|
}}
|
||||||
|
onClick={() => {
|
||||||
|
if (item.type === 'webPage') {
|
||||||
|
setActiveStep(1)
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Sheet sx={{ fontSize: '20px', fontWeight: 'bold' }}>
|
||||||
|
{item.title}
|
||||||
|
</Sheet>
|
||||||
|
<Sheet>{item.subTitle}</Sheet>
|
||||||
|
</Sheet>
|
||||||
|
))}
|
||||||
|
</Box>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<Box sx={{ margin: '30px auto' }}>
|
||||||
|
Name:
|
||||||
|
<Input
|
||||||
|
placeholder="Please input the name"
|
||||||
|
onChange={(e: any) => setDocumentName(e.target.value)}
|
||||||
|
sx={{ marginBottom: '20px' }}
|
||||||
|
/>
|
||||||
|
Web Page URL:
|
||||||
|
<Input
|
||||||
|
placeholder="Please input the Web Page URL"
|
||||||
|
onChange={(e: any) => setWebPageUrl(e.target.value)}
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
<Button
|
||||||
|
onClick={async () => {
|
||||||
|
if (documentName === '') {
|
||||||
|
message.error('Please input the name')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (webPageUrl === '') {
|
||||||
|
message.error('Please input the Web Page URL')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const res = await fetch(
|
||||||
|
`http://localhost:8000/knowledge/${spaceName}/document/add`,
|
||||||
|
{
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
doc_name: documentName,
|
||||||
|
content: webPageUrl,
|
||||||
|
doc_type: 'URL'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
)
|
||||||
|
const data = await res.json()
|
||||||
|
if (data.success) {
|
||||||
|
message.success('success')
|
||||||
|
setIsAddDocumentModalShow(false)
|
||||||
|
const res = await fetch(
|
||||||
|
`http://localhost:8000/knowledge/${spaceName}/document/list`,
|
||||||
|
{
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({})
|
||||||
|
}
|
||||||
|
)
|
||||||
|
const data = await res.json()
|
||||||
|
if (data.success) {
|
||||||
|
setDocuments(data.data)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
message.error(data.err_msg || 'failed')
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Finish
|
||||||
|
</Button>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</Sheet>
|
||||||
|
</Modal>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -75,15 +75,21 @@ const Index = () => {
|
|||||||
}, [])
|
}, [])
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="page-header p-4">
|
<Sheet sx={{
|
||||||
<div className="page-header-title">Knowledge Spaces</div>
|
display: "flex",
|
||||||
|
justifyContent: "space-between"
|
||||||
|
}} className="p-4">
|
||||||
|
<Sheet sx={{
|
||||||
|
fontSize: '30px',
|
||||||
|
fontWeight: 'bold'
|
||||||
|
}}>Knowledge Spaces</Sheet>
|
||||||
<Button
|
<Button
|
||||||
onClick={() => setIsAddKnowledgeSpaceModalShow(true)}
|
onClick={() => setIsAddKnowledgeSpaceModalShow(true)}
|
||||||
variant="outlined"
|
variant="outlined"
|
||||||
>
|
>
|
||||||
+ New Knowledge Space
|
+ New Knowledge Space
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</Sheet>
|
||||||
<div className="page-body p-4">
|
<div className="page-body p-4">
|
||||||
<Table sx={{ '& thead th:nth-child(1)': { width: '40%' } }}>
|
<Table sx={{ '& thead th:nth-child(1)': { width: '40%' } }}>
|
||||||
<thead>
|
<thead>
|
||||||
@ -116,7 +122,6 @@ const Index = () => {
|
|||||||
</Table>
|
</Table>
|
||||||
</div>
|
</div>
|
||||||
<Modal
|
<Modal
|
||||||
title="Add Knowledge Space"
|
|
||||||
sx={{
|
sx={{
|
||||||
display: 'flex',
|
display: 'flex',
|
||||||
justifyContent: 'center',
|
justifyContent: 'center',
|
||||||
@ -288,24 +293,6 @@ const Index = () => {
|
|||||||
)}
|
)}
|
||||||
</Sheet>
|
</Sheet>
|
||||||
</Modal>
|
</Modal>
|
||||||
<style jsx>{`
|
|
||||||
.page-header {
|
|
||||||
display: flex;
|
|
||||||
justify-content: space-between;
|
|
||||||
.page-header-title {
|
|
||||||
font-size: 30px;
|
|
||||||
font-weight: bold;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.datasource-type-wrap {
|
|
||||||
height: 100px;
|
|
||||||
line-height: 100px;
|
|
||||||
border: 1px solid black;
|
|
||||||
border-radius: 20px;
|
|
||||||
margin-bottom: 20px;
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
`}</style>
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user