perf: 生成qa prompt

This commit is contained in:
archer
2023-04-03 01:39:00 +08:00
parent a0832af14b
commit caf31faf31
2 changed files with 13 additions and 5 deletions

View File

@@ -31,7 +31,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const replaceText = text.replace(/(\\n|\n)+/g, ' ');
// 文本拆分成 chunk
let chunks = replaceText.match(/[^!?.。]+[!?.。]/g) || [];
const chunks = replaceText.match(/[^!?.。]+[!?.。]/g) || [];
const textList: string[] = [];
let splitText = '';

View File

@@ -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')
});
}
}