feat: limit prompt

This commit is contained in:
archer
2023-06-24 18:55:46 +08:00
parent ec9852fc63
commit 83d755ad0e
10 changed files with 110 additions and 52 deletions

View File

@@ -5,6 +5,7 @@ export interface InitChatResponse {
chatId: string;
modelId: string;
systemPrompt?: string;
limitPrompt?: string;
model: {
name: string;
avatar: string;

View File

@@ -26,7 +26,7 @@ const MySelect = ({ placeholder, value, width = 'auto', list, onchange, ...props
return (
<Menu autoSelect={false} onOpen={onOpen} onClose={onClose}>
<MenuButton as={'span'}>
<MenuButton style={{ width: '100%' }} as={'span'}>
<Button
width={width}
px={3}

View File

@@ -82,6 +82,7 @@ export const defaultModel: ModelSchema = {
searchLimit: 5,
searchEmptyText: '',
systemPrompt: '',
limitPrompt: '',
temperature: 0,
maxToken: 4000,
chatModel: OpenAiChatEnum.GPT35

View File

@@ -101,6 +101,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
},
chatModel: model.chat.chatModel,
systemPrompt: isOwner ? model.chat.systemPrompt : '',
limitPrompt: isOwner ? model.chat.limitPrompt : '',
history
}
});

View File

@@ -28,7 +28,11 @@ type Response = {
userSystemPrompt: {
obj: ChatRoleEnum;
value: string;
};
}[];
userLimitPrompt: {
obj: ChatRoleEnum;
value: string;
}[];
quotePrompt: {
obj: ChatRoleEnum;
value: string;
@@ -130,17 +134,24 @@ export async function appKbSearch({
// 计算固定提示词的 token 数量
const userSystemPrompt = model.chat.systemPrompt // user system prompt
? {
obj: ChatRoleEnum.Human,
value: model.chat.systemPrompt
}
: {
obj: ChatRoleEnum.Human,
value: `知识库是关于 ${model.name} 的内容,参考知识库回答问题。与 "${model.name}" 无关内容,直接回复: "我不知道"。`
};
? [
{
obj: ChatRoleEnum.System,
value: model.chat.systemPrompt
}
]
: [];
const userLimitPrompt = [
{
obj: ChatRoleEnum.Human,
value: model.chat.limitPrompt
? model.chat.limitPrompt
: `知识库是关于 ${model.name} 的内容,参考知识库回答问题。与 "${model.name}" 无关内容,直接回复: "我不知道"。`
}
];
const fixedSystemTokens = modelToolMap[model.chat.chatModel].countTokens({
messages: [userSystemPrompt]
messages: [...userSystemPrompt, ...userLimitPrompt]
});
// filter part quote by maxToken
@@ -164,6 +175,7 @@ export async function appKbSearch({
return {
rawSearch,
userSystemPrompt,
userLimitPrompt,
quotePrompt: {
obj: ChatRoleEnum.System,
value: quoteText

View File

@@ -108,11 +108,12 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
const {
rawSearch = [],
userSystemPrompt = [],
userLimitPrompt = [],
quotePrompt = []
} = await (async () => {
// 使用了知识库搜索
if (model.chat.relatedKbs?.length > 0) {
const { rawSearch, userSystemPrompt, quotePrompt } = await appKbSearch({
const { rawSearch, quotePrompt, userSystemPrompt, userLimitPrompt } = await appKbSearch({
model,
userId,
fixedQuote: history[history.length - 1]?.quote,
@@ -123,21 +124,29 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
return {
rawSearch,
userSystemPrompt: userSystemPrompt ? [userSystemPrompt] : [],
userSystemPrompt,
userLimitPrompt,
quotePrompt: [quotePrompt]
};
}
if (model.chat.systemPrompt) {
return {
userSystemPrompt: [
{
obj: ChatRoleEnum.System,
value: model.chat.systemPrompt
}
]
};
}
return {};
return {
userSystemPrompt: model.chat.systemPrompt
? [
{
obj: ChatRoleEnum.System,
value: model.chat.systemPrompt
}
]
: [],
userLimitPrompt: model.chat.limitPrompt
? [
{
obj: ChatRoleEnum.Human,
value: model.chat.limitPrompt
}
]
: []
};
})();
// search result is empty
@@ -167,7 +176,13 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
}
// api messages. [quote,context,systemPrompt,question]
const completePrompts = [...quotePrompt, ...prompts.slice(0, -1), ...userSystemPrompt, prompt];
const completePrompts = [
...quotePrompt,
...userSystemPrompt,
...prompts.slice(0, -1),
...userLimitPrompt,
prompt
];
// chat temperature
const modelConstantsData = ChatModelMap[model.chat.chatModel];
// FastGpt temperature range: 1~10
@@ -176,7 +191,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
);
await sensitiveCheck({
input: `${prompt.value}`
input: `${userSystemPrompt[0]?.value}\n${userLimitPrompt[0]?.value}\n${prompt.value}`
});
// start model api. responseText and totalTokens: valid only if stream = false
@@ -259,7 +274,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
...(showAppDetail
? {
quote: rawSearch,
systemPrompt: userSystemPrompt?.[0]?.value
systemPrompt: `${userSystemPrompt[0]?.value}\n\n${userLimitPrompt[0]?.value}`
}
: {})
}

View File

@@ -219,7 +219,7 @@ const Chat = ({ modelId, chatId }: { modelId: string; chatId: string }) => {
...item,
status: 'finish',
quoteLen,
systemPrompt: chatData.systemPrompt
systemPrompt: `${chatData.systemPrompt}\n\n${chatData.limitPrompt}`
};
})
}));
@@ -234,13 +234,14 @@ const Chat = ({ modelId, chatId }: { modelId: string; chatId: string }) => {
[
chatId,
modelId,
chatData.systemPrompt,
setChatData,
loadHistory,
loadMyModels,
generatingMessage,
setForbidLoadChatData,
router
router,
chatData.systemPrompt,
chatData.limitPrompt,
loadHistory,
loadMyModels
]
);
@@ -749,7 +750,7 @@ const Chat = ({ modelId, chatId }: { modelId: string; chatId: string }) => {
px={[2, 4]}
onClick={() => setShowSystemPrompt(item.systemPrompt || '')}
>
&
</Button>
)}
{!!item.quoteLen && (

View File

@@ -1,5 +1,15 @@
import React, { useCallback, useState, useMemo } from 'react';
import { Box, Flex, Button, FormControl, Input, Textarea, Divider } from '@chakra-ui/react';
import {
Box,
Flex,
Button,
FormControl,
Input,
Textarea,
Divider,
Tooltip
} from '@chakra-ui/react';
import { QuestionOutlineIcon } from '@chakra-ui/icons';
import { useQuery } from '@tanstack/react-query';
import { useForm } from 'react-hook-form';
import { useRouter } from 'next/router';
@@ -20,6 +30,11 @@ import Avatar from '@/components/Avatar';
import MySelect from '@/components/Select';
import MySlider from '@/components/Slider';
const systemPromptTip =
'模型固定的引导词,通过调整该内容,可以引导模型聊天方向。该内容会被固定在上下文的开头。';
const limitPromptTip =
'限定模型对话范围,会被放置在本次提问前,拥有强引导和限定性。例如:\n1. 知识库是关于 Laf 的介绍,参考知识库回答问题,与 "Laf" 无关内容,直接回复: "我不知道"。\n2. 你仅回答关于 "xxx" 的问题,其他问题回复: "xxxx"';
const Settings = ({ modelId }: { modelId: string }) => {
const { toast } = useToast();
const router = useRouter();
@@ -60,7 +75,7 @@ const Settings = ({ modelId }: { modelId: string }) => {
}
return max;
}, [getValues, setValue, refresh]);
}, [getValues, setValue]);
// 提交保存模型修改
const saveSubmitSuccess = useCallback(
@@ -211,7 +226,7 @@ const Settings = ({ modelId }: { modelId: string }) => {
</Box>
<Textarea
rows={5}
rows={4}
maxLength={500}
placeholder={'给你的 AI 应用一个介绍'}
{...register('intro')}
@@ -225,11 +240,14 @@ const Settings = ({ modelId }: { modelId: string }) => {
</Box>
<MySelect
width={['200px', '240px']}
width={['100%', '280px']}
value={getValues('chat.chatModel')}
list={chatModelList.map((item) => ({
id: item.chatModel,
label: item.name
label: `${item.name} (${formatPrice(
ChatModelMap[getValues('chat.chatModel')]?.price,
1000
)} 元/1k tokens)`
}))}
onchange={(val: any) => {
setValue('chat.chatModel', val);
@@ -237,15 +255,6 @@ const Settings = ({ modelId }: { modelId: string }) => {
}}
/>
</Flex>
<Flex alignItems={'center'} mt={5}>
<Box w={['60px', '100px', '140px']} flexShrink={0}>
</Box>
<Box fontSize={['sm', 'md']}>
{formatPrice(ChatModelMap[getValues('chat.chatModel')]?.price, 1000)}
/1K tokens()
</Box>
</Flex>
<Flex alignItems={'center'} my={10}>
<Box w={['60px', '100px', '140px']} flexShrink={0}>
@@ -269,7 +278,7 @@ const Settings = ({ modelId }: { modelId: string }) => {
</Flex>
<Flex alignItems={'center'} mt={12} mb={10}>
<Box w={['60px', '100px', '140px']} flexShrink={0}>
</Box>
<Box flex={1} ml={'10px'}>
<MySlider
@@ -292,15 +301,29 @@ const Settings = ({ modelId }: { modelId: string }) => {
<Flex mt={10} alignItems={'flex-start'}>
<Box w={['60px', '100px', '140px']} flexShrink={0}>
<Tooltip label={systemPromptTip}>
<QuestionOutlineIcon display={['none', 'inline']} ml={1} />
</Tooltip>
</Box>
<Textarea
rows={8}
placeholder={
'模型默认的 prompt 词,通过调整该内容,可以引导模型聊天方向。\n\n如果使用了知识库搜索没有填写该内容时系统会自动补充提示词如果填写了内容则以填写的内容为准。'
}
placeholder={systemPromptTip}
{...register('chat.systemPrompt')}
></Textarea>
</Flex>
<Flex mt={5} alignItems={'flex-start'}>
<Box w={['60px', '100px', '140px']} flexShrink={0}>
<Tooltip label={limitPromptTip}>
<QuestionOutlineIcon display={['none', 'inline']} ml={1} />
</Tooltip>
</Box>
<Textarea
rows={5}
placeholder={limitPromptTip}
{...register('chat.limitPrompt')}
></Textarea>
</Flex>
<Flex mt={5} alignItems={'center'}>
<Box w={['60px', '100px', '140px']} flexShrink={0}></Box>

View File

@@ -43,7 +43,10 @@ const ModelSchema = new Schema({
default: ''
},
systemPrompt: {
// 系统提示词
type: String,
default: ''
},
limitPrompt: {
type: String,
default: ''
},

View File

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