From 144bed5a77c1ccd3d98911f084497cdd89727c3d Mon Sep 17 00:00:00 2001 From: archer <545436317@qq.com> Date: Wed, 5 Apr 2023 23:43:20 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96tokens=E8=AE=A1?= =?UTF-8?q?=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/constants/common.ts | 2 +- src/service/events/generateQA.ts | 33 ++++++++++++++------------- src/service/events/pushBill.ts | 39 +++++++++++++++----------------- src/service/utils/openai.ts | 13 +++++++---- 4 files changed, 44 insertions(+), 43 deletions(-) diff --git a/src/constants/common.ts b/src/constants/common.ts index d97cb86aa5..c67573fd3a 100644 --- a/src/constants/common.ts +++ b/src/constants/common.ts @@ -43,7 +43,7 @@ wx号: YNyiqi | --- | --- | | chatgpt - 对话 | 0.03 | | 知识库 - 对话 | 0.03 | -| 知识库 - 索引 | 0.01 | +| 知识库 - 索引 | 0.004 | | 文件拆分 | 0.03 | `; diff --git a/src/service/events/generateQA.ts b/src/service/events/generateQA.ts index 84162bf142..3cdc450f8e 100644 --- a/src/service/events/generateQA.ts +++ b/src/service/events/generateQA.ts @@ -40,7 +40,8 @@ export async function generateQA(next = false): Promise { 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 { 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 { 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) { diff --git a/src/service/events/pushBill.ts b/src/service/events/pushBill.ts index a0ba642821..db2523c15b 100644 --- a/src/service/events/pushBill.ts +++ b/src/service/events/pushBill.ts @@ -22,9 +22,9 @@ export const pushChatBill = async ({ try { // 计算 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) { await connectToDatabase(); @@ -33,7 +33,7 @@ export const pushChatBill = async ({ const modelItem = modelList.find((item) => item.model === modelName); // 计算价格 const unitPrice = modelItem?.price || 5; - const price = unitPrice * tokens.length; + const price = unitPrice * tokens; console.log(`unit price: ${unitPrice}, price: ${formatPrice(price)}元`); try { @@ -44,7 +44,7 @@ export const pushChatBill = async ({ modelName, chatId, textLen: text.length, - tokenLen: tokens.length, + tokenLen: tokens, price }); billId = res._id; @@ -66,11 +66,13 @@ export const pushChatBill = async ({ export const pushSplitDataBill = async ({ isPay, userId, + tokenLen, text, type }: { isPay: boolean; userId: string; + tokenLen: number; text: string; type: DataType; }) => { @@ -79,12 +81,7 @@ export const pushSplitDataBill = async ({ let billId; try { - // 计算 token 数量 - const tokens = encode(text); - - console.log( - `splitData generate success. text len: ${text.length}. token len: ${tokens.length}` - ); + console.log(`splitData generate success. text len: ${text.length}. token len: ${tokenLen}`); if (isPay) { try { @@ -92,7 +89,7 @@ export const pushSplitDataBill = async ({ const modelItem = modelList.find((item) => item.model === ChatModelNameEnum.GPT35); const unitPrice = modelItem?.price || 3; // 计算价格 - const price = unitPrice * tokens.length; + const price = unitPrice * tokenLen; console.log(`price: ${formatPrice(price)}元`); @@ -102,7 +99,7 @@ export const pushSplitDataBill = async ({ type, modelName: ChatModelNameEnum.GPT35, textLen: text.length, - tokenLen: tokens.length, + tokenLen, price }); billId = res._id; @@ -124,27 +121,27 @@ export const pushSplitDataBill = async ({ export const pushGenerateVectorBill = async ({ isPay, userId, - text + text, + tokenLen }: { isPay: boolean; userId: string; text: string; + tokenLen: number; }) => { await connectToDatabase(); let billId; try { - // 计算 token 数量 - const tokens = encode(text); - - console.log(`vector generate success. text len: ${text.length}. token len: ${tokens.length}`); + console.log(`vector generate success. text len: ${text.length}. token len: ${tokenLen}`); if (isPay) { try { - const unitPrice = 1; - // 计算价格 - const price = unitPrice * tokens.length; + const unitPrice = 0.4; + // 计算价格. 至少为1 + let price = unitPrice * tokenLen; + price = price > 1 ? price : 1; console.log(`price: ${formatPrice(price)}元`); @@ -154,7 +151,7 @@ export const pushGenerateVectorBill = async ({ type: BillTypeEnum.vector, modelName: ChatModelNameEnum.VECTOR, textLen: text.length, - tokenLen: tokens.length, + tokenLen, price }); billId = res._id; diff --git a/src/service/utils/openai.ts b/src/service/utils/openai.ts index 52f4a9e45b..29ecccb529 100644 --- a/src/service/utils/openai.ts +++ b/src/service/utils/openai.ts @@ -1,4 +1,3 @@ -import axios from 'axios'; import { getOpenAIApi } from '@/service/utils/chat'; import { httpsAgent } from './tools'; import { User } from '../models/user'; @@ -75,7 +74,7 @@ export const openaiCreateEmbedding = async ({ const chatAPI = getOpenAIApi(apiKey); // 把输入的内容转成向量 - const vector = await chatAPI + const res = await chatAPI .createEmbedding( { model: ChatModelNameEnum.VECTOR, @@ -86,16 +85,20 @@ export const openaiCreateEmbedding = async ({ httpsAgent } ) - .then((res) => res?.data?.data?.[0]?.embedding || []); + .then((res) => ({ + tokenLen: res.data.usage.total_tokens || 0, + vector: res?.data?.data?.[0]?.embedding || [] + })); pushGenerateVectorBill({ isPay, userId, - text + text, + tokenLen: res.tokenLen }); return { - vector, + vector: res.vector, chatAPI }; };