mirror of
https://github.com/labring/FastGPT.git
synced 2025-10-16 16:04:34 +00:00
perf: 优化tokens计算
This commit is contained in:
@@ -43,7 +43,7 @@ wx号: YNyiqi
|
|||||||
| --- | --- |
|
| --- | --- |
|
||||||
| chatgpt - 对话 | 0.03 |
|
| chatgpt - 对话 | 0.03 |
|
||||||
| 知识库 - 对话 | 0.03 |
|
| 知识库 - 对话 | 0.03 |
|
||||||
| 知识库 - 索引 | 0.01 |
|
| 知识库 - 索引 | 0.004 |
|
||||||
| 文件拆分 | 0.03 |
|
| 文件拆分 | 0.03 |
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
@@ -40,7 +40,8 @@ export async function generateQA(next = false): Promise<any> {
|
|||||||
const textList: string[] = dataItem.textList.slice(-5);
|
const textList: string[] = dataItem.textList.slice(-5);
|
||||||
|
|
||||||
// 获取 openapi Key
|
// 获取 openapi Key
|
||||||
let userApiKey, systemKey;
|
let userApiKey = '',
|
||||||
|
systemKey = '';
|
||||||
try {
|
try {
|
||||||
const key = await getOpenApiKey(dataItem.userId);
|
const key = await getOpenApiKey(dataItem.userId);
|
||||||
userApiKey = key.userApiKey;
|
userApiKey = key.userApiKey;
|
||||||
@@ -93,10 +94,21 @@ export async function generateQA(next = false): Promise<any> {
|
|||||||
httpsAgent
|
httpsAgent
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.then((res) => ({
|
.then((res) => {
|
||||||
rawContent: res?.data.choices[0].message?.content || '', // chatgpt原本的回复
|
const rawContent = res?.data.choices[0].message?.content || '';
|
||||||
result: splitText(res?.data.choices[0].message?.content || '') // 格式化后的QA对
|
// 计费
|
||||||
}))
|
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
|
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);
|
generateQA(true);
|
||||||
generateVector();
|
generateVector();
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
@@ -22,9 +22,9 @@ export const pushChatBill = async ({
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// 计算 token 数量
|
// 计算 token 数量
|
||||||
const tokens = encode(text);
|
const tokens = encode(text).length;
|
||||||
|
|
||||||
console.log(`chat generate success. text len: ${text.length}. token len: ${tokens.length}`);
|
console.log(`chat generate success. text len: ${text.length}. token len: ${tokens}`);
|
||||||
|
|
||||||
if (isPay) {
|
if (isPay) {
|
||||||
await connectToDatabase();
|
await connectToDatabase();
|
||||||
@@ -33,7 +33,7 @@ export const pushChatBill = async ({
|
|||||||
const modelItem = modelList.find((item) => item.model === modelName);
|
const modelItem = modelList.find((item) => item.model === modelName);
|
||||||
// 计算价格
|
// 计算价格
|
||||||
const unitPrice = modelItem?.price || 5;
|
const unitPrice = modelItem?.price || 5;
|
||||||
const price = unitPrice * tokens.length;
|
const price = unitPrice * tokens;
|
||||||
console.log(`unit price: ${unitPrice}, price: ${formatPrice(price)}元`);
|
console.log(`unit price: ${unitPrice}, price: ${formatPrice(price)}元`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -44,7 +44,7 @@ export const pushChatBill = async ({
|
|||||||
modelName,
|
modelName,
|
||||||
chatId,
|
chatId,
|
||||||
textLen: text.length,
|
textLen: text.length,
|
||||||
tokenLen: tokens.length,
|
tokenLen: tokens,
|
||||||
price
|
price
|
||||||
});
|
});
|
||||||
billId = res._id;
|
billId = res._id;
|
||||||
@@ -66,11 +66,13 @@ export const pushChatBill = async ({
|
|||||||
export const pushSplitDataBill = async ({
|
export const pushSplitDataBill = async ({
|
||||||
isPay,
|
isPay,
|
||||||
userId,
|
userId,
|
||||||
|
tokenLen,
|
||||||
text,
|
text,
|
||||||
type
|
type
|
||||||
}: {
|
}: {
|
||||||
isPay: boolean;
|
isPay: boolean;
|
||||||
userId: string;
|
userId: string;
|
||||||
|
tokenLen: number;
|
||||||
text: string;
|
text: string;
|
||||||
type: DataType;
|
type: DataType;
|
||||||
}) => {
|
}) => {
|
||||||
@@ -79,12 +81,7 @@ export const pushSplitDataBill = async ({
|
|||||||
let billId;
|
let billId;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 计算 token 数量
|
console.log(`splitData generate success. text len: ${text.length}. token len: ${tokenLen}`);
|
||||||
const tokens = encode(text);
|
|
||||||
|
|
||||||
console.log(
|
|
||||||
`splitData generate success. text len: ${text.length}. token len: ${tokens.length}`
|
|
||||||
);
|
|
||||||
|
|
||||||
if (isPay) {
|
if (isPay) {
|
||||||
try {
|
try {
|
||||||
@@ -92,7 +89,7 @@ export const pushSplitDataBill = async ({
|
|||||||
const modelItem = modelList.find((item) => item.model === ChatModelNameEnum.GPT35);
|
const modelItem = modelList.find((item) => item.model === ChatModelNameEnum.GPT35);
|
||||||
const unitPrice = modelItem?.price || 3;
|
const unitPrice = modelItem?.price || 3;
|
||||||
// 计算价格
|
// 计算价格
|
||||||
const price = unitPrice * tokens.length;
|
const price = unitPrice * tokenLen;
|
||||||
|
|
||||||
console.log(`price: ${formatPrice(price)}元`);
|
console.log(`price: ${formatPrice(price)}元`);
|
||||||
|
|
||||||
@@ -102,7 +99,7 @@ export const pushSplitDataBill = async ({
|
|||||||
type,
|
type,
|
||||||
modelName: ChatModelNameEnum.GPT35,
|
modelName: ChatModelNameEnum.GPT35,
|
||||||
textLen: text.length,
|
textLen: text.length,
|
||||||
tokenLen: tokens.length,
|
tokenLen,
|
||||||
price
|
price
|
||||||
});
|
});
|
||||||
billId = res._id;
|
billId = res._id;
|
||||||
@@ -124,27 +121,27 @@ export const pushSplitDataBill = async ({
|
|||||||
export const pushGenerateVectorBill = async ({
|
export const pushGenerateVectorBill = async ({
|
||||||
isPay,
|
isPay,
|
||||||
userId,
|
userId,
|
||||||
text
|
text,
|
||||||
|
tokenLen
|
||||||
}: {
|
}: {
|
||||||
isPay: boolean;
|
isPay: boolean;
|
||||||
userId: string;
|
userId: string;
|
||||||
text: string;
|
text: string;
|
||||||
|
tokenLen: number;
|
||||||
}) => {
|
}) => {
|
||||||
await connectToDatabase();
|
await connectToDatabase();
|
||||||
|
|
||||||
let billId;
|
let billId;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 计算 token 数量
|
console.log(`vector generate success. text len: ${text.length}. token len: ${tokenLen}`);
|
||||||
const tokens = encode(text);
|
|
||||||
|
|
||||||
console.log(`vector generate success. text len: ${text.length}. token len: ${tokens.length}`);
|
|
||||||
|
|
||||||
if (isPay) {
|
if (isPay) {
|
||||||
try {
|
try {
|
||||||
const unitPrice = 1;
|
const unitPrice = 0.4;
|
||||||
// 计算价格
|
// 计算价格. 至少为1
|
||||||
const price = unitPrice * tokens.length;
|
let price = unitPrice * tokenLen;
|
||||||
|
price = price > 1 ? price : 1;
|
||||||
|
|
||||||
console.log(`price: ${formatPrice(price)}元`);
|
console.log(`price: ${formatPrice(price)}元`);
|
||||||
|
|
||||||
@@ -154,7 +151,7 @@ export const pushGenerateVectorBill = async ({
|
|||||||
type: BillTypeEnum.vector,
|
type: BillTypeEnum.vector,
|
||||||
modelName: ChatModelNameEnum.VECTOR,
|
modelName: ChatModelNameEnum.VECTOR,
|
||||||
textLen: text.length,
|
textLen: text.length,
|
||||||
tokenLen: tokens.length,
|
tokenLen,
|
||||||
price
|
price
|
||||||
});
|
});
|
||||||
billId = res._id;
|
billId = res._id;
|
||||||
|
@@ -1,4 +1,3 @@
|
|||||||
import axios from 'axios';
|
|
||||||
import { getOpenAIApi } from '@/service/utils/chat';
|
import { getOpenAIApi } from '@/service/utils/chat';
|
||||||
import { httpsAgent } from './tools';
|
import { httpsAgent } from './tools';
|
||||||
import { User } from '../models/user';
|
import { User } from '../models/user';
|
||||||
@@ -75,7 +74,7 @@ export const openaiCreateEmbedding = async ({
|
|||||||
const chatAPI = getOpenAIApi(apiKey);
|
const chatAPI = getOpenAIApi(apiKey);
|
||||||
|
|
||||||
// 把输入的内容转成向量
|
// 把输入的内容转成向量
|
||||||
const vector = await chatAPI
|
const res = await chatAPI
|
||||||
.createEmbedding(
|
.createEmbedding(
|
||||||
{
|
{
|
||||||
model: ChatModelNameEnum.VECTOR,
|
model: ChatModelNameEnum.VECTOR,
|
||||||
@@ -86,16 +85,20 @@ export const openaiCreateEmbedding = async ({
|
|||||||
httpsAgent
|
httpsAgent
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.then((res) => res?.data?.data?.[0]?.embedding || []);
|
.then((res) => ({
|
||||||
|
tokenLen: res.data.usage.total_tokens || 0,
|
||||||
|
vector: res?.data?.data?.[0]?.embedding || []
|
||||||
|
}));
|
||||||
|
|
||||||
pushGenerateVectorBill({
|
pushGenerateVectorBill({
|
||||||
isPay,
|
isPay,
|
||||||
userId,
|
userId,
|
||||||
text
|
text,
|
||||||
|
tokenLen: res.tokenLen
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
vector,
|
vector: res.vector,
|
||||||
chatAPI
|
chatAPI
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user