fix: 账单余额问题

This commit is contained in:
archer
2023-04-04 21:32:51 +08:00
parent c12aa7fdf7
commit b13c3c4da5
7 changed files with 32 additions and 45 deletions

View File

@@ -36,6 +36,15 @@ wx: YNyiqi
4. 使用该模型对话。
注意使用知识库模型对话时tokens 消耗会加快。
### 价格表
如果使用了自己的 Api Key不会计费。可以在账号页看到详细账单。单纯使用 chatGPT 模型进行对话,只有一个计费项目。使用知识库时,包含**对话**和**索引**生成两个计费项。
| 计费项 | 价格: 元/ 1K tokens包含上下文|
| --- | --- |
| chatgpt - 对话 | 0.003 |
| 知识库 - 对话 | 0.003 |
| 知识库 - 索引 | 0.001 |
| 文件拆分 | 0.003 |
`;
export const chatProblem = `

View File

@@ -39,7 +39,7 @@ export async function generateAbstract(next = false): Promise<any> {
// 获取 openapi Key
let userApiKey, systemKey;
try {
const key = await getOpenApiKey(dataItem.userId, true);
const key = await getOpenApiKey(dataItem.userId);
userApiKey = key.userApiKey;
systemKey = key.systemKey;
} catch (error: any) {

View File

@@ -38,7 +38,7 @@ export async function generateQA(next = false): Promise<any> {
// 获取 openapi Key
let userApiKey, systemKey;
try {
const key = await getOpenApiKey(dataItem.userId, true);
const key = await getOpenApiKey(dataItem.userId);
userApiKey = key.userApiKey;
systemKey = key.systemKey;
} catch (error: any) {
@@ -48,7 +48,7 @@ export async function generateQA(next = false): Promise<any> {
textList: [],
errorText: error.message
});
throw new Error('账号余额不足');
throw new Error(error?.message);
}
throw new Error('获取 openai key 失败');

View File

@@ -39,7 +39,19 @@ export async function generateVector(next = false): Promise<any> {
};
// 获取 openapi Key
const { userApiKey, systemKey } = await getOpenApiKey(dataItem.userId);
let userApiKey, systemKey;
try {
const res = await getOpenApiKey(dataItem.userId);
userApiKey = res.userApiKey;
systemKey = res.systemKey;
} catch (error: any) {
if (error?.code === 501) {
await redis.del(dataItem.id);
throw new Error(error?.message);
}
throw new Error('获取 openai key 失败');
}
// 生成词向量
const { vector } = await openaiCreateEmbedding({

View File

@@ -90,7 +90,7 @@ export const pushSplitDataBill = async ({
try {
// 获取模型单价格, 都是用 gpt35 拆分
const modelItem = modelList.find((item) => item.model === ChatModelNameEnum.GPT35);
const unitPrice = modelItem?.price || 5;
const unitPrice = modelItem?.price || 3;
// 计算价格
const price = unitPrice * tokens.length;

View File

@@ -36,10 +36,7 @@ export const authChat = async (chatId: string, authorization?: string) => {
}
// 获取 user 的 apiKey
const { user, userApiKey, systemKey } = await getOpenApiKey(
chat.userId as unknown as string,
false
);
const { user, userApiKey, systemKey } = await getOpenApiKey(chat.userId as unknown as string);
// filter 掉被 deleted 的内容
chat.content = chat.content.filter((item) => item.deleted !== true);

View File

@@ -6,20 +6,6 @@ import { formatPrice } from '@/utils/user';
import { ChatModelNameEnum } from '@/constants/model';
import { pushGenerateVectorBill } from '../events/pushBill';
/* 判断 apikey 是否还有余额 */
export const checkKeyGrant = async (apiKey: string) => {
const grant = await axios.get('https://api.openai.com/dashboard/billing/credit_grants', {
headers: {
Authorization: `Bearer ${apiKey}`
},
httpsAgent
});
if (grant.data?.total_available <= 0.2) {
return false;
}
return true;
};
/* 获取用户 api 的 openai 信息 */
export const getUserApiOpenai = async (userId: string) => {
const user = await User.findById(userId);
@@ -30,15 +16,6 @@ export const getUserApiOpenai = async (userId: string) => {
return Promise.reject('缺少ApiKey, 无法请求');
}
// 余额校验
const hasGrant = await checkKeyGrant(userApiKey);
if (!hasGrant) {
return Promise.reject({
code: 501,
message: 'API 余额不足'
});
}
return {
user,
openai: getOpenAIApi(userApiKey),
@@ -47,27 +24,19 @@ export const getUserApiOpenai = async (userId: string) => {
};
/* 获取 open api key如果用户没有自己的key就用平台的用平台记得加账单 */
export const getOpenApiKey = async (userId: string, checkGrant = false) => {
export const getOpenApiKey = async (userId: string) => {
const user = await User.findById(userId);
if (!user) {
return Promise.reject('找不到用户');
return Promise.reject({
code: 501,
message: '找不到用户'
});
}
const userApiKey = user?.accounts?.find((item: any) => item.type === 'openai')?.value;
// 有自己的key
if (userApiKey) {
// api 余额校验
if (checkGrant) {
const hasGrant = await checkKeyGrant(userApiKey);
if (!hasGrant) {
return Promise.reject({
code: 501,
message: 'API 余额不足'
});
}
}
return {
user,
userApiKey,