perf: 生成qa prompt

This commit is contained in:
archer 2023-04-03 01:39:00 +08:00
parent a0832af14b
commit caf31faf31
No known key found for this signature in database
GPG Key ID: 166CA6BF2383B2BB
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, ' '); const replaceText = text.replace(/(\\n|\n)+/g, ' ');
// 文本拆分成 chunk // 文本拆分成 chunk
let chunks = replaceText.match(/[^!?.。]+[!?.。]/g) || []; const chunks = replaceText.match(/[^!?.。]+[!?.。]/g) || [];
const textList: string[] = []; const textList: string[] = [];
let splitText = ''; let splitText = '';

View File

@ -47,6 +47,7 @@ export async function generateQA(next = false): Promise<any> {
textList: [], textList: [],
errorText: error.message errorText: error.message
}); });
throw new Error('账号余额不足');
} }
throw new Error('获取 openai key 失败'); throw new Error('获取 openai key 失败');
@ -62,7 +63,7 @@ export async function generateQA(next = false): Promise<any> {
role: 'system', role: 'system',
content: `${ content: `${
dataItem.prompt || '下面是一段长文本' dataItem.prompt || '下面是一段长文本'
},530,,并按以下格式返回: Q1:\nA1:\nQ2:\nA2:\n` },530,并按以下格式返回: Q1:\nA1:\nQ2:\nA2:\n`
}; };
// 请求 chatgpt 获取回答 // 请求 chatgpt 获取回答
@ -70,7 +71,7 @@ export async function generateQA(next = false): Promise<any> {
.createChatCompletion( .createChatCompletion(
{ {
model: ChatModelNameEnum.GPT35, model: ChatModelNameEnum.GPT35,
temperature: 0.4, temperature: 0.8,
n: 1, n: 1,
messages: [ messages: [
systemPrompt, systemPrompt,
@ -142,7 +143,7 @@ export async function generateQA(next = false): Promise<any> {
* *
*/ */
function splitText(text: string) { 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 matches = text.matchAll(regex); // 获取所有匹配到的结果
const result = []; // 存储最终的结果 const result = []; // 存储最终的结果
@ -150,7 +151,14 @@ function splitText(text: string) {
const q = match[2]; const q = match[2];
const a = match[5]; const a = match[5];
if (q && a) { if (q && a) {
result.push({ q, a }); // 如果Q和A都存在就将其添加到结果中 // 如果Q和A都存在就将其添加到结果中
result.push({
q,
a: a // 过滤空行
.split('\n')
.filter((item) => item)
.join('\n')
});
} }
} }