mirror of
https://github.com/labring/FastGPT.git
synced 2025-08-02 20:58:12 +00:00
perf: 减少聊天内容配置,自动截断上下文
This commit is contained in:
@@ -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('该账号余额不足');
|
||||
}
|
||||
|
@@ -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;
|
||||
};
|
||||
|
Reference in New Issue
Block a user