perf: 优化tokens计算
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user