perf: keys

This commit is contained in:
archer
2023-06-23 17:12:52 +08:00
parent ae1f7a888e
commit 41ada6ecda
18 changed files with 49 additions and 93 deletions

View File

@@ -55,7 +55,6 @@ export async function generateQA(): Promise<any> {
const { systemAuthKey } = await getApiKey({
model: OpenAiChatEnum.GPT35,
userId,
type: 'training',
mustPay: true
});

View File

@@ -58,7 +58,6 @@ export async function generateVector(): Promise<any> {
const vectors = await openaiEmbedding({
input: dataItems.map((item) => item.q),
userId,
type: 'training',
mustPay: true
});

View File

@@ -1,18 +1,6 @@
import { Schema, model, models } from 'mongoose';
const SystemSchema = new Schema({
openAIKeys: {
type: String,
default: ''
},
openAITrainingKeys: {
type: String,
default: ''
},
gpt4Key: {
type: String,
default: ''
},
vectorMaxProcess: {
type: Number,
default: 10

View File

@@ -15,9 +15,6 @@ export async function connectToDatabase(): Promise<void> {
global.qaQueueLen = 0;
global.vectorQueueLen = 0;
global.systemEnv = {
openAIKeys: process.env.OPENAIKEY || '',
openAITrainingKeys: process.env.OPENAI_TRAINING_KEY || '',
gpt4Key: process.env.GPT4KEY || '',
vectorMaxProcess: 10,
qaMaxProcess: 10,
pgIvfflatProbe: 10,

View File

@@ -11,7 +11,6 @@ import { ERROR_ENUM } from '../errorCode';
import { ChatModelType, OpenAiChatEnum } from '@/constants/model';
import { hashPassword } from '@/service/utils/tools';
export type ApiKeyType = 'training' | 'chat';
export type AuthType = 'token' | 'root' | 'apikey';
export const parseCookie = (cookie?: string): Promise<string> => {
@@ -163,37 +162,19 @@ export const authUser = async ({
};
/* random get openai api key */
export const getSystemOpenAiKey = (type: ApiKeyType) => {
const keys = (() => {
if (type === 'training') {
return global.systemEnv.openAITrainingKeys?.split(',') || [];
}
return global.systemEnv.openAIKeys?.split(',') || [];
})();
// 纯字符串类型
const i = Math.floor(Math.random() * keys.length);
return keys[i] || (global.systemEnv.openAIKeys as string);
};
export const getGpt4Key = () => {
const keys = global.systemEnv.gpt4Key?.split(',') || [];
// 纯字符串类型
const i = Math.floor(Math.random() * keys.length);
return keys[i] || (global.systemEnv.openAIKeys as string);
export const getSystemOpenAiKey = () => {
return process.env.OPENAIKEY || '';
};
/* 获取 api 请求的 key */
export const getApiKey = async ({
model,
userId,
mustPay = false,
type = 'chat'
mustPay = false
}: {
model: ChatModelType;
userId: string;
mustPay?: boolean;
type?: ApiKeyType;
}) => {
const user = await User.findById(userId);
if (!user) {
@@ -203,19 +184,19 @@ export const getApiKey = async ({
const keyMap = {
[OpenAiChatEnum.GPT35]: {
userOpenAiKey: user.openaiKey || '',
systemAuthKey: getSystemOpenAiKey(type) as string
systemAuthKey: getSystemOpenAiKey()
},
[OpenAiChatEnum.GPT3516k]: {
userOpenAiKey: user.openaiKey || '',
systemAuthKey: getSystemOpenAiKey(type) as string
systemAuthKey: getSystemOpenAiKey()
},
[OpenAiChatEnum.GPT4]: {
userOpenAiKey: user.openaiKey || '',
systemAuthKey: getGpt4Key() as string
systemAuthKey: getSystemOpenAiKey()
},
[OpenAiChatEnum.GPT432k]: {
userOpenAiKey: user.openaiKey || '',
systemAuthKey: getGpt4Key() as string
systemAuthKey: getSystemOpenAiKey()
}
};