perf: 生成qa prompt
This commit is contained in:
parent
a0832af14b
commit
caf31faf31
@ -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 = '';
|
||||||
|
|||||||
@ -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 || '下面是一段长文本'
|
||||||
},请从中总结出5至30个问题和答案,答案尽量详细,并按以下格式返回: Q1:\nA1:\nQ2:\nA2:\n`
|
},请从中提取出5至30个问题和答案,并按以下格式返回: 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')
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user