feat: Max Conversation Length setting (#241)

This commit is contained in:
josc146
2023-04-21 20:58:54 +08:00
parent 72dcdf4826
commit 8c9693c4c7
10 changed files with 56 additions and 9 deletions
+1
View File
@@ -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"
}
+1
View File
@@ -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": "总是固定浮动窗口"
}
+1
View File
@@ -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": "總是固定浮動視窗"
}
+4 -1
View File
@@ -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 })
+5 -2
View File
@@ -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 = ''
+10 -4
View File
@@ -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 = ''
+1
View File
@@ -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',
+23 -2
View File
@@ -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 })
}}
/>
</label>
<label>
{t('Max Conversation Length')}
<input
type="number"
min="0"
max="100"
step="1"
value={config.maxConversationContextLength}
onChange={(e) => {
const value = parseIntWithClamp(e.target.value, 9, 0, 100)
updateConfig({ maxConversationContextLength: value })
}}
/>
</label>
<label>
{t('Custom ChatGPT Web API Url')}
<input
+1
View File
@@ -14,6 +14,7 @@ export * from './is-mobile'
export * from './is-safari'
export * from './limited-fetch'
export * from './open-url'
export * from './parse-int-with-clamp'
export * from './set-element-position-in-viewport'
export * from './stream-async-iterable'
export * from './update-ref-height'
+9
View File
@@ -0,0 +1,9 @@
export function parseIntWithClamp(value, defaultValue, min, max) {
value = parseInt(value)
if (isNaN(value)) value = defaultValue
else if (value > max) value = max
else if (value < min) value = min
return value
}