diff --git a/src/_locales/en/main.json b/src/_locales/en/main.json index c22e405..baa0dcc 100644 --- a/src/_locales/en/main.json +++ b/src/_locales/en/main.json @@ -101,5 +101,6 @@ "Store to Independent Conversation Page": "Store to Independent Conversation Page", "Keep Conversation Window in Background": "Keep conversation window in background, so that you can use shortcut keys to call it up in any program", "Max Response Token Length": "Max Response Token Length", + "Max Conversation Length": "Max Conversation Length", "Always pin the floating window": "Always pin the floating window" } diff --git a/src/_locales/zh-hans/main.json b/src/_locales/zh-hans/main.json index b78a99e..172851e 100644 --- a/src/_locales/zh-hans/main.json +++ b/src/_locales/zh-hans/main.json @@ -101,5 +101,6 @@ "Store to Independent Conversation Page": "收纳到独立对话页", "Keep Conversation Window in Background": "保持对话窗口在后台, 以便在任何程序中使用快捷键呼出", "Max Response Token Length": "响应的最大token长度", + "Max Conversation Length": "对话处理的最大长度", "Always pin the floating window": "总是固定浮动窗口" } diff --git a/src/_locales/zh-hant/main.json b/src/_locales/zh-hant/main.json index 85282c5..96585f0 100644 --- a/src/_locales/zh-hant/main.json +++ b/src/_locales/zh-hant/main.json @@ -101,5 +101,6 @@ "Store to Independent Conversation Page": "收納到獨立對話頁", "Keep Conversation Window in Background": "保持對話窗口在後臺, 以便在任何程序中使用快捷鍵呼出", "Max Response Token Length": "響應的最大token長度", + "Max Conversation Length": "對話處理的最大長度", "Always pin the floating window": "總是固定浮動視窗" } diff --git a/src/background/apis/azure-openai-api.mjs b/src/background/apis/azure-openai-api.mjs index a26aa1b..776efc4 100644 --- a/src/background/apis/azure-openai-api.mjs +++ b/src/background/apis/azure-openai-api.mjs @@ -13,7 +13,10 @@ export async function generateAnswersWithAzureOpenaiApi(port, question, session) const { controller, messageListener } = setAbortController(port) const config = await getUserConfig() - const prompt = getConversationPairs(session.conversationRecords, false) + const prompt = getConversationPairs( + session.conversationRecords.slice(-config.maxConversationContextLength), + false, + ) prompt.unshift({ role: 'system', content: await getChatSystemPromptBase() }) prompt.push({ role: 'user', content: question }) diff --git a/src/background/apis/custom-api.mjs b/src/background/apis/custom-api.mjs index 157f32b..7187fda 100644 --- a/src/background/apis/custom-api.mjs +++ b/src/background/apis/custom-api.mjs @@ -21,10 +21,13 @@ import { getCustomApiPromptBase, pushRecord, setAbortController } from './shared export async function generateAnswersWithCustomApi(port, question, session, apiKey, modelName) { const { controller, messageListener } = setAbortController(port) - const prompt = getConversationPairs(session.conversationRecords, false) + const config = await getUserConfig() + const prompt = getConversationPairs( + session.conversationRecords.slice(-config.maxConversationContextLength), + false, + ) prompt.unshift({ role: 'system', content: await getCustomApiPromptBase() }) prompt.push({ role: 'user', content: question }) - const config = await getUserConfig() const apiUrl = config.customModelApiUrl let answer = '' diff --git a/src/background/apis/openai-api.mjs b/src/background/apis/openai-api.mjs index beb6882..22ed1f2 100644 --- a/src/background/apis/openai-api.mjs +++ b/src/background/apis/openai-api.mjs @@ -27,11 +27,14 @@ export async function generateAnswersWithGptCompletionApi( ) { const { controller, messageListener } = setAbortController(port) + const config = await getUserConfig() const prompt = (await getCompletionPromptBase()) + - getConversationPairs(session.conversationRecords, true) + + getConversationPairs( + session.conversationRecords.slice(-config.maxConversationContextLength), + true, + ) + `Human: ${question}\nAI: ` - const config = await getUserConfig() const apiUrl = config.customOpenAiApiUrl let answer = '' @@ -89,10 +92,13 @@ export async function generateAnswersWithGptCompletionApi( export async function generateAnswersWithChatgptApi(port, question, session, apiKey, modelName) { const { controller, messageListener } = setAbortController(port) - const prompt = getConversationPairs(session.conversationRecords, false) + const config = await getUserConfig() + const prompt = getConversationPairs( + session.conversationRecords.slice(-config.maxConversationContextLength), + false, + ) prompt.unshift({ role: 'system', content: await getChatSystemPromptBase() }) prompt.push({ role: 'user', content: question }) - const config = await getUserConfig() const apiUrl = config.customOpenAiApiUrl let answer = '' diff --git a/src/config/index.mjs b/src/config/index.mjs index 1b85dd5..18eadb8 100644 --- a/src/config/index.mjs +++ b/src/config/index.mjs @@ -104,6 +104,7 @@ export const defaultConfig = { // advanced maxResponseTokenLength: 1000, + maxConversationContextLength: 9, customChatGptWebApiUrl: 'https://chat.openai.com', customChatGptWebApiPath: '/backend-api/conversation', customOpenAiApiUrl: 'https://api.openai.com', diff --git a/src/popup/Popup.jsx b/src/popup/Popup.jsx index 841a3ac..eee368f 100644 --- a/src/popup/Popup.jsx +++ b/src/popup/Popup.jsx @@ -27,7 +27,14 @@ import wechatpay from './donation/wechatpay.jpg' import bugmeacoffee from './donation/bugmeacoffee.png' import { useWindowTheme } from '../hooks/use-window-theme.mjs' import { languageList } from '../config/language.mjs' -import { isEdge, isFirefox, isMobile, isSafari, openUrl } from '../utils/index.mjs' +import { + isEdge, + isFirefox, + isMobile, + isSafari, + openUrl, + parseIntWithClamp, +} from '../utils/index.mjs' import { useTranslation } from 'react-i18next' function GeneralPart({ config, updateConfig }) { @@ -413,11 +420,25 @@ function AdvancedPart({ config, updateConfig }) { step="100" value={config.maxResponseTokenLength} onChange={(e) => { - const value = parseInt(e.target.value) + const value = parseIntWithClamp(e.target.value, 1000, 100, 40000) updateConfig({ maxResponseTokenLength: value }) }} /> +