perf: 优化tokens计算

This commit is contained in:
archer
2023-04-05 23:43:20 +08:00
parent 96fc917bad
commit 144bed5a77
4 changed files with 44 additions and 43 deletions

View File

@@ -40,7 +40,8 @@ export async function generateQA(next = false): Promise<any> {
const textList: string[] = dataItem.textList.slice(-5);
// 获取 openapi Key
let userApiKey, systemKey;
let userApiKey = '',
systemKey = '';
try {
const key = await getOpenApiKey(dataItem.userId);
userApiKey = key.userApiKey;
@@ -93,10 +94,21 @@ export async function generateQA(next = false): Promise<any> {
httpsAgent
}
)
.then((res) => ({
rawContent: res?.data.choices[0].message?.content || '', // chatgpt原本的回复
result: splitText(res?.data.choices[0].message?.content || '') // 格式化后的QA对
}))
.then((res) => {
const rawContent = res?.data.choices[0].message?.content || '';
// 计费
pushSplitDataBill({
isPay: !userApiKey,
userId: dataItem.userId,
type: 'QA',
text: systemPrompt.content + text + rawContent,
tokenLen: res.data.usage?.total_tokens || 0
});
return {
rawContent, // chatgpt 原本的回复
result: splitText(res?.data.choices[0].message?.content || '') // 格式化后的QA对
};
})
)
);
@@ -141,17 +153,6 @@ export async function generateQA(next = false): Promise<any> {
resultList.length
);
// 计费
pushSplitDataBill({
isPay: !userApiKey && resultList.length > 0,
userId: dataItem.userId,
type: 'QA',
text:
systemPrompt.content +
textList.join('') +
successResponse.map((item) => item.rawContent).join('')
});
generateQA(true);
generateVector();
} catch (error: any) {