Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵月辉
/
fastgpt-migrated
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Unverified
Commit
7fb6f62c
authored
Mar 27, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: 文本拆分
parent
af385b1b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
13 deletions
+20
-13
src/pages/api/data/splitData.ts
+20
-13
No files found.
src/pages/api/data/splitData.ts
View file @
7fb6f62c
...
...
@@ -8,11 +8,10 @@ import { generateAbstract } from '@/service/events/generateAbstract';
/* 拆分数据成QA */
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
try
{
le
t
{
text
,
dataId
}
=
req
.
body
as
{
text
:
string
;
dataId
:
string
};
cons
t
{
text
,
dataId
}
=
req
.
body
as
{
text
:
string
;
dataId
:
string
};
if
(
!
text
||
!
dataId
)
{
throw
new
Error
(
'参数错误'
);
}
text
=
text
.
replace
(
/
\n
+/g
,
'\n'
);
await
connectToDatabase
();
const
{
authorization
}
=
req
.
headers
;
...
...
@@ -24,19 +23,27 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if
(
!
DataRecord
)
{
throw
new
Error
(
'找不到数据集'
);
}
const
replaceText
=
text
.
replace
(
/
[\r\n\\
n
]
+/g
,
' '
);
// 文本拆分成 chunk
let
chunks
=
replaceText
.
match
(
/
[^
!?.。
]
+
[
!?.。
]
/g
)
||
[];
const
dataItems
:
any
[]
=
[];
let
splitText
=
''
;
// 每 1000 字符一组
for
(
let
i
=
0
;
i
<=
text
.
length
/
1000
;
i
++
)
{
dataItems
.
push
({
userId
,
dataId
,
type
:
DataRecord
.
type
,
text
:
text
.
slice
(
i
*
1000
,
(
i
+
1
)
*
1000
),
status
:
1
});
}
chunks
.
forEach
((
chunk
)
=>
{
splitText
+=
chunk
;
if
(
splitText
.
length
>=
980
)
{
dataItems
.
push
({
userId
,
dataId
,
type
:
DataRecord
.
type
,
text
:
splitText
,
status
:
1
});
splitText
=
''
;
}
});
// 批量插入数据
await
DataItem
.
insertMany
(
dataItems
);
...
...
@@ -49,7 +56,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
jsonRes
(
res
,
{
data
:
''
data
:
{
chunks
,
replaceText
}
});
}
catch
(
err
)
{
jsonRes
(
res
,
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment