feat: maxToken setting

This commit is contained in:
archer
2023-06-18 21:23:36 +08:00
parent ee9afa310a
commit ff2043c0fb
7 changed files with 54 additions and 6 deletions

View File

@@ -83,6 +83,7 @@ export const defaultModel: ModelSchema = {
searchEmptyText: '',
systemPrompt: '',
temperature: 0,
maxToken: 4000,
chatModel: OpenAiChatEnum.GPT35
},
share: {

View File

@@ -180,6 +180,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
await modelServiceToolMap[model.chat.chatModel].chatCompletion({
apiKey: userOpenAiKey || apiKey,
temperature: +temperature,
maxToken: model.chat.maxToken,
messages: completePrompts,
stream,
res

View File

@@ -36,11 +36,6 @@ const Settings = ({ modelId }: { modelId: string }) => {
const [btnLoading, setBtnLoading] = useState(false);
const [refresh, setRefresh] = useState(false);
const isOwner = useMemo(
() => modelDetail.userId === userInfo?._id,
[modelDetail.userId, userInfo?._id]
);
const {
register,
setValue,
@@ -52,6 +47,20 @@ const Settings = ({ modelId }: { modelId: string }) => {
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(
async (data: ModelSchema) => {
@@ -256,6 +265,27 @@ const Settings = ({ modelId }: { modelId: string }) => {
/>
</Box>
</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'}>
<Box w={['60px', '100px', '140px']} flexShrink={0}>

View File

@@ -47,6 +47,11 @@ const ModelSchema = new Schema({
type: String,
default: ''
},
maxToken: {
type: Number,
default: 4000,
min: 100
},
temperature: {
type: Number,
min: 0,

View File

@@ -12,6 +12,7 @@ import { textAdaptGptResponse } from '@/utils/adapt';
export type ChatCompletionType = {
apiKey: string;
temperature: number;
maxToken?: number;
messages: ChatItemType[];
chatId?: string;
[key: string]: any;

View File

@@ -19,22 +19,31 @@ export const chatResponse = async ({
model,
apiKey,
temperature,
maxToken = 4000,
messages,
stream
}: ChatCompletionType & { model: `${OpenAiChatEnum}` }) => {
const modelTokenLimit = ChatModelMap[model]?.contextMaxToken || 4000;
const filterMessages = ChatContextFilter({
model,
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 chatAPI = getOpenAIApi();
const promptsToken = modelToolMap[model].countTokens({
messages: filterMessages
});
maxToken = maxToken + promptsToken > modelTokenLimit ? modelTokenLimit - promptsToken : maxToken;
const response = await chatAPI.createChatCompletion(
{
model,
temperature: Number(temperature || 0),
max_tokens: maxToken,
messages: adaptMessages,
frequency_penalty: 0.5, // 越大,重复内容越少
presence_penalty: -0.5, // 越大,越容易出现新内容

View File

@@ -44,6 +44,7 @@ export interface ModelSchema {
searchEmptyText: string;
systemPrompt: string;
temperature: number;
maxToken: number;
chatModel: ChatModelType; // 聊天时用的模型,训练后就是训练的模型
};
share: {