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
caf31faf
authored
Apr 03, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: 生成qa prompt
parent
a0832af1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
5 deletions
+13
-5
src/pages/api/model/data/splitData.ts
+1
-1
src/service/events/generateQA.ts
+12
-4
No files found.
src/pages/api/model/data/splitData.ts
View file @
caf31faf
...
...
@@ -31,7 +31,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const
replaceText
=
text
.
replace
(
/
(\\
n|
\n)
+/g
,
' '
);
// 文本拆分成 chunk
le
t
chunks
=
replaceText
.
match
(
/
[^
!?.。
]
+
[
!?.。
]
/g
)
||
[];
cons
t
chunks
=
replaceText
.
match
(
/
[^
!?.。
]
+
[
!?.。
]
/g
)
||
[];
const
textList
:
string
[]
=
[];
let
splitText
=
''
;
...
...
src/service/events/generateQA.ts
View file @
caf31faf
...
...
@@ -47,6 +47,7 @@ export async function generateQA(next = false): Promise<any> {
textList
:
[],
errorText
:
error
.
message
});
throw
new
Error
(
'账号余额不足'
);
}
throw
new
Error
(
'获取 openai key 失败'
);
...
...
@@ -62,7 +63,7 @@ export async function generateQA(next = false): Promise<any> {
role
:
'system'
,
content
:
`
${
dataItem
.
prompt
||
'下面是一段长文本'
}
,请从中
总结出5至30个问题和答案,答案尽量详细
,并按以下格式返回: Q1:\nA1:\nQ2:\nA2:\n`
}
,请从中
提取出5至30个问题和答案
,并按以下格式返回: Q1:\nA1:\nQ2:\nA2:\n`
};
// 请求 chatgpt 获取回答
...
...
@@ -70,7 +71,7 @@ export async function generateQA(next = false): Promise<any> {
.
createChatCompletion
(
{
model
:
ChatModelNameEnum
.
GPT35
,
temperature
:
0.
4
,
temperature
:
0.
8
,
n
:
1
,
messages
:
[
systemPrompt
,
...
...
@@ -142,7 +143,7 @@ export async function generateQA(next = false): Promise<any> {
* 检查文本是否按格式返回
*/
function
splitText
(
text
:
string
)
{
const
regex
=
/Q
\d
+:
(\s
*
)(
.*
)(\s
*
)
A
\d
+:
(\s
*
)(
.*
)(\s
*
)
/g
;
// 匹配Q和A的正则表达式
const
regex
=
/Q
\d
+:
(\s
*
)(
.*
)(\s
*
)
A
\d
+:
(\s
*
)(
[\s\S]
*
?)(?=
Q|$
)
/g
;
// 匹配Q和A的正则表达式
const
matches
=
text
.
matchAll
(
regex
);
// 获取所有匹配到的结果
const
result
=
[];
// 存储最终的结果
...
...
@@ -150,7 +151,14 @@ function splitText(text: string) {
const
q
=
match
[
2
];
const
a
=
match
[
5
];
if
(
q
&&
a
)
{
result
.
push
({
q
,
a
});
// 如果Q和A都存在,就将其添加到结果中
// 如果Q和A都存在,就将其添加到结果中
result
.
push
({
q
,
a
:
a
// 过滤空行
.
split
(
'\n'
)
.
filter
((
item
)
=>
item
)
.
join
(
'\n'
)
});
}
}
...
...
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