mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-28 09:03:53 +00:00
feat: maxToken setting
This commit is contained in:
@@ -83,6 +83,7 @@ export const defaultModel: ModelSchema = {
|
|||||||
searchEmptyText: '',
|
searchEmptyText: '',
|
||||||
systemPrompt: '',
|
systemPrompt: '',
|
||||||
temperature: 0,
|
temperature: 0,
|
||||||
|
maxToken: 4000,
|
||||||
chatModel: OpenAiChatEnum.GPT35
|
chatModel: OpenAiChatEnum.GPT35
|
||||||
},
|
},
|
||||||
share: {
|
share: {
|
||||||
|
@@ -180,6 +180,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
|||||||
await modelServiceToolMap[model.chat.chatModel].chatCompletion({
|
await modelServiceToolMap[model.chat.chatModel].chatCompletion({
|
||||||
apiKey: userOpenAiKey || apiKey,
|
apiKey: userOpenAiKey || apiKey,
|
||||||
temperature: +temperature,
|
temperature: +temperature,
|
||||||
|
maxToken: model.chat.maxToken,
|
||||||
messages: completePrompts,
|
messages: completePrompts,
|
||||||
stream,
|
stream,
|
||||||
res
|
res
|
||||||
|
@@ -36,11 +36,6 @@ const Settings = ({ modelId }: { modelId: string }) => {
|
|||||||
const [btnLoading, setBtnLoading] = useState(false);
|
const [btnLoading, setBtnLoading] = useState(false);
|
||||||
const [refresh, setRefresh] = useState(false);
|
const [refresh, setRefresh] = useState(false);
|
||||||
|
|
||||||
const isOwner = useMemo(
|
|
||||||
() => modelDetail.userId === userInfo?._id,
|
|
||||||
[modelDetail.userId, userInfo?._id]
|
|
||||||
);
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
register,
|
register,
|
||||||
setValue,
|
setValue,
|
||||||
@@ -52,6 +47,20 @@ const Settings = ({ modelId }: { modelId: string }) => {
|
|||||||
defaultValues: modelDetail
|
defaultValues: modelDetail
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const isOwner = useMemo(
|
||||||
|
() => modelDetail.userId === userInfo?._id,
|
||||||
|
[modelDetail.userId, userInfo?._id]
|
||||||
|
);
|
||||||
|
const tokenLimit = useMemo(() => {
|
||||||
|
const max = ChatModelMap[getValues('chat.chatModel')]?.contextMaxToken || 4000;
|
||||||
|
|
||||||
|
if (max < getValues('chat.maxToken')) {
|
||||||
|
setValue('chat.maxToken', max);
|
||||||
|
}
|
||||||
|
|
||||||
|
return max;
|
||||||
|
}, [getValues, setValue, refresh]);
|
||||||
|
|
||||||
// 提交保存模型修改
|
// 提交保存模型修改
|
||||||
const saveSubmitSuccess = useCallback(
|
const saveSubmitSuccess = useCallback(
|
||||||
async (data: ModelSchema) => {
|
async (data: ModelSchema) => {
|
||||||
@@ -256,6 +265,27 @@ const Settings = ({ modelId }: { modelId: string }) => {
|
|||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
</Flex>
|
</Flex>
|
||||||
|
<Flex alignItems={'center'} mt={12} mb={10}>
|
||||||
|
<Box w={['60px', '100px', '140px']} flexShrink={0}>
|
||||||
|
最大长度
|
||||||
|
</Box>
|
||||||
|
<Box flex={1} ml={'10px'}>
|
||||||
|
<MySlider
|
||||||
|
markList={[
|
||||||
|
{ label: '100', value: 100 },
|
||||||
|
{ label: `${tokenLimit}`, value: tokenLimit }
|
||||||
|
]}
|
||||||
|
width={['100%', '260px']}
|
||||||
|
min={100}
|
||||||
|
max={tokenLimit}
|
||||||
|
activeVal={getValues('chat.maxToken')}
|
||||||
|
setVal={(val) => {
|
||||||
|
setValue('chat.maxToken', val);
|
||||||
|
setRefresh(!refresh);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
</Flex>
|
||||||
<Flex mt={10} alignItems={'flex-start'}>
|
<Flex mt={10} alignItems={'flex-start'}>
|
||||||
<Box w={['60px', '100px', '140px']} flexShrink={0}>
|
<Box w={['60px', '100px', '140px']} flexShrink={0}>
|
||||||
提示词
|
提示词
|
||||||
|
@@ -47,6 +47,11 @@ const ModelSchema = new Schema({
|
|||||||
type: String,
|
type: String,
|
||||||
default: ''
|
default: ''
|
||||||
},
|
},
|
||||||
|
maxToken: {
|
||||||
|
type: Number,
|
||||||
|
default: 4000,
|
||||||
|
min: 100
|
||||||
|
},
|
||||||
temperature: {
|
temperature: {
|
||||||
type: Number,
|
type: Number,
|
||||||
min: 0,
|
min: 0,
|
||||||
|
@@ -12,6 +12,7 @@ import { textAdaptGptResponse } from '@/utils/adapt';
|
|||||||
export type ChatCompletionType = {
|
export type ChatCompletionType = {
|
||||||
apiKey: string;
|
apiKey: string;
|
||||||
temperature: number;
|
temperature: number;
|
||||||
|
maxToken?: number;
|
||||||
messages: ChatItemType[];
|
messages: ChatItemType[];
|
||||||
chatId?: string;
|
chatId?: string;
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
|
@@ -19,22 +19,31 @@ export const chatResponse = async ({
|
|||||||
model,
|
model,
|
||||||
apiKey,
|
apiKey,
|
||||||
temperature,
|
temperature,
|
||||||
|
maxToken = 4000,
|
||||||
messages,
|
messages,
|
||||||
stream
|
stream
|
||||||
}: ChatCompletionType & { model: `${OpenAiChatEnum}` }) => {
|
}: ChatCompletionType & { model: `${OpenAiChatEnum}` }) => {
|
||||||
|
const modelTokenLimit = ChatModelMap[model]?.contextMaxToken || 4000;
|
||||||
const filterMessages = ChatContextFilter({
|
const filterMessages = ChatContextFilter({
|
||||||
model,
|
model,
|
||||||
prompts: messages,
|
prompts: messages,
|
||||||
maxTokens: Math.ceil(ChatModelMap[model].contextMaxToken * 0.85)
|
maxTokens: Math.ceil(modelTokenLimit - 300) // filter token. not response maxToken
|
||||||
});
|
});
|
||||||
|
|
||||||
const adaptMessages = adaptChatItem_openAI({ messages: filterMessages, reserveId: false });
|
const adaptMessages = adaptChatItem_openAI({ messages: filterMessages, reserveId: false });
|
||||||
const chatAPI = getOpenAIApi();
|
const chatAPI = getOpenAIApi();
|
||||||
|
|
||||||
|
const promptsToken = modelToolMap[model].countTokens({
|
||||||
|
messages: filterMessages
|
||||||
|
});
|
||||||
|
|
||||||
|
maxToken = maxToken + promptsToken > modelTokenLimit ? modelTokenLimit - promptsToken : maxToken;
|
||||||
|
|
||||||
const response = await chatAPI.createChatCompletion(
|
const response = await chatAPI.createChatCompletion(
|
||||||
{
|
{
|
||||||
model,
|
model,
|
||||||
temperature: Number(temperature || 0),
|
temperature: Number(temperature || 0),
|
||||||
|
max_tokens: maxToken,
|
||||||
messages: adaptMessages,
|
messages: adaptMessages,
|
||||||
frequency_penalty: 0.5, // 越大,重复内容越少
|
frequency_penalty: 0.5, // 越大,重复内容越少
|
||||||
presence_penalty: -0.5, // 越大,越容易出现新内容
|
presence_penalty: -0.5, // 越大,越容易出现新内容
|
||||||
|
1
client/src/types/mongoSchema.d.ts
vendored
1
client/src/types/mongoSchema.d.ts
vendored
@@ -44,6 +44,7 @@ export interface ModelSchema {
|
|||||||
searchEmptyText: string;
|
searchEmptyText: string;
|
||||||
systemPrompt: string;
|
systemPrompt: string;
|
||||||
temperature: number;
|
temperature: number;
|
||||||
|
maxToken: number;
|
||||||
chatModel: ChatModelType; // 聊天时用的模型,训练后就是训练的模型
|
chatModel: ChatModelType; // 聊天时用的模型,训练后就是训练的模型
|
||||||
};
|
};
|
||||||
share: {
|
share: {
|
||||||
|
Reference in New Issue
Block a user