perf: 减少聊天内容配置,自动截断上下文

This commit is contained in:
archer
2023-03-28 00:07:32 +08:00
parent 7fb6f62cf6
commit 7a6d0ea650
14 changed files with 144 additions and 172 deletions

View File

@@ -32,14 +32,11 @@ export const authChat = async (chatId: string, authorization?: string) => {
return Promise.reject('模型不存在');
}
// 安全校验
if (chat.loadAmount === 0 || chat.expiredTime <= Date.now()) {
return Promise.reject('聊天框已过期');
}
// 分享校验
// 凭证校验
if (!chat.isShare) {
await authToken(authorization);
} else if (chat.loadAmount === 0 || chat.expiredTime <= Date.now()) {
return Promise.reject('聊天框已过期');
}
// 获取 user 的 apiKey
@@ -47,6 +44,7 @@ export const authChat = async (chatId: string, authorization?: string) => {
const userApiKey = user.accounts?.find((item: any) => item.type === 'openai')?.value;
// 没有 apikey ,校验余额
if (!userApiKey && formatPrice(user.balance) <= 0) {
return Promise.reject('该账号余额不足');
}

View File

@@ -4,6 +4,8 @@ import { User } from '../models/user';
import tunnel from 'tunnel';
import type { UserModelSchema } from '@/types/mongoSchema';
import { formatPrice } from '@/utils/user';
import { ChatItemType } from '@/types/chat';
import { encode } from 'gpt-token-utils';
/* 密码加密 */
export const hashPassword = (psw: string) => {
@@ -91,3 +93,29 @@ export const httpsAgent =
}
})
: undefined;
/* tokens 截断 */
export const openaiChatFilter = (prompts: ChatItemType[], maxTokens: number) => {
let res: ChatItemType[] = [];
let systemPrompt: ChatItemType | null = null;
// System 词保留
if (prompts[0]?.obj === 'SYSTEM') {
systemPrompt = prompts.shift() as ChatItemType;
maxTokens -= encode(prompts[0].value).length;
}
// 从后往前截取
for (let i = prompts.length - 1; i >= 0; i--) {
const tokens = encode(prompts[i].value).length;
if (maxTokens >= tokens) {
res.unshift(prompts[i]);
maxTokens -= tokens;
} else {
break;
}
}
return systemPrompt ? [systemPrompt, ...res] : res;
};