mirror of
https://github.com/wassname/chatGPTBox.git
synced 2026-07-03 13:09:07 +08:00
automatically adapt different models' context length
This commit is contained in:
@@ -232,7 +232,7 @@ async function prepareForRightClickMenu() {
|
||||
const menuItem = menuConfig[data.itemId]
|
||||
if (!menuItem.genPrompt) return
|
||||
else prompt = await menuItem.genPrompt()
|
||||
if (prompt) prompt = cropText(`Reply in ${await getPreferredLanguage()}.\n` + prompt)
|
||||
if (prompt) prompt = await cropText(`Reply in ${await getPreferredLanguage()}.\n` + prompt)
|
||||
}
|
||||
|
||||
const position = data.useMenuPosition
|
||||
|
||||
@@ -50,7 +50,7 @@ export default {
|
||||
else subtitleContent += subtitles[i].content + ','
|
||||
}
|
||||
|
||||
return cropText(
|
||||
return await cropText(
|
||||
`用尽量简练的语言,联系视频标题,对视频进行内容摘要,同时仍要保留重要细节,视频标题为:"${title}",字幕内容为:\n${subtitleContent}`,
|
||||
)
|
||||
} catch (e) {
|
||||
|
||||
@@ -40,7 +40,7 @@ export default {
|
||||
const patchData = await getPatchData(patchUrl)
|
||||
if (!patchData) return
|
||||
|
||||
return cropText(
|
||||
return await cropText(
|
||||
`Analyze the contents of a git commit,provide a suitable commit message,and summarize the contents of the commit.` +
|
||||
`The patch contents of this commit are as follows:\n${patchData}`,
|
||||
)
|
||||
|
||||
@@ -22,7 +22,7 @@ export default {
|
||||
const patchData = await getPatchData(patchUrl)
|
||||
if (!patchData) return
|
||||
|
||||
return cropText(
|
||||
return await cropText(
|
||||
`Analyze the contents of a git commit,provide a suitable commit message,and summarize the contents of the commit.` +
|
||||
`The patch contents of this commit are as follows:\n${patchData}`,
|
||||
)
|
||||
|
||||
@@ -20,7 +20,7 @@ export default {
|
||||
for (let i = 1; i <= comments.length && i <= 4; i++) {
|
||||
comment += `answer${i}: ${comment[i - 1].textContent}|`
|
||||
}
|
||||
return cropText(
|
||||
return await cropText(
|
||||
`以下是一篇文章,标题是:"${title}",作者是:"${author}",内容是:\n"${description}".各个评论如下:\n${comment}.请以如下格式输出你的回答:
|
||||
{文章摘要和文章作者}
|
||||
======
|
||||
|
||||
@@ -14,7 +14,7 @@ export default {
|
||||
answers += `answer${i}:${texts[i].textContent}|`
|
||||
}
|
||||
|
||||
return cropText(
|
||||
return await cropText(
|
||||
`Below is the content from a question and answer platform,giving the corresponding summary and your opinion on it.` +
|
||||
`The question is:'${title}',` +
|
||||
`Some answers are as follows:\n${answers}`,
|
||||
|
||||
@@ -13,7 +13,7 @@ export default {
|
||||
answers += `answer${i}:${texts[i].textContent}|`
|
||||
}
|
||||
|
||||
return cropText(
|
||||
return await cropText(
|
||||
`Below is the content from a social forum,giving the corresponding summary and your opinion on it.` +
|
||||
`The title is:'${title}',and the further description of the title is:'${description}'.` +
|
||||
`Some answers are as follows:\n${answers}`,
|
||||
|
||||
@@ -13,7 +13,7 @@ export default {
|
||||
answer += `answer${i}: ${answers[i - 1].textContent}|`
|
||||
}
|
||||
|
||||
return cropText(
|
||||
return await cropText(
|
||||
`Below is the content from a developer Q&A platform. Analyze answers and provide a brief solution that can solve the question first,` +
|
||||
`then give an overview of all answers. The question is: "${title}", and the further description of the question is: "${description}".` +
|
||||
`The answers are as follows:\n${answer}`,
|
||||
|
||||
@@ -19,7 +19,7 @@ export default {
|
||||
sidebar.style.background = 'transparent'
|
||||
}
|
||||
|
||||
return cropText(
|
||||
return await cropText(
|
||||
`以下是一篇文章,标题是:"${title}",文章来源是:"${author}公众号",内容是:\n"${description}".请以如下格式输出你的回答:
|
||||
{文章来源和文章摘要}
|
||||
======
|
||||
|
||||
@@ -52,7 +52,7 @@ export default {
|
||||
|
||||
subtitleContent = replaceHtmlEntities(subtitleContent)
|
||||
|
||||
return cropText(
|
||||
return await cropText(
|
||||
`Provide a structured summary of the following video in markdown format, focusing on key takeaways and crucial information, and ensuring to include the video title. The summary should be easy to read and concise, yet comprehensive.` +
|
||||
`The video title is "${title}". The subtitle content is as follows:\n${subtitleContent}`,
|
||||
)
|
||||
|
||||
@@ -11,7 +11,7 @@ export default {
|
||||
let answer = ''
|
||||
if (location.pathname.includes('answer')) {
|
||||
answer = document.querySelector(answerQuery)?.textContent
|
||||
return cropText(
|
||||
return await cropText(
|
||||
`以下是一个问答平台的提问与回答内容,给出相应的摘要,以及你对此的看法.问题是:"${title}",问题的进一步描述是:"${description}".` +
|
||||
`其中一个回答如下:\n${answer}`,
|
||||
)
|
||||
@@ -20,7 +20,7 @@ export default {
|
||||
for (let i = 1; i <= answers.length && i <= 4; i++) {
|
||||
answer += `answer${i}: ${answers[i - 1].textContent}|`
|
||||
}
|
||||
return cropText(
|
||||
return await cropText(
|
||||
`以下是一个问答平台的提问与回答内容,给出相应的摘要,以及你对此的看法.问题是:"${title}",问题的进一步描述是:"${description}".` +
|
||||
`各个回答如下:\n${answer}`,
|
||||
)
|
||||
@@ -30,7 +30,7 @@ export default {
|
||||
const description = document.querySelector('.Post-RichText')?.textContent
|
||||
|
||||
if (title) {
|
||||
return cropText(
|
||||
return await cropText(
|
||||
`以下是一篇文章,给出相应的摘要,以及你对此的看法.标题是:"${title}",内容是:\n"${description}"`,
|
||||
)
|
||||
}
|
||||
|
||||
+18
-4
@@ -21,15 +21,28 @@
|
||||
// SOFTWARE.
|
||||
|
||||
import { encode } from '@nem035/gpt-3-encoder'
|
||||
import { getUserConfig, Models } from '../config/index.mjs'
|
||||
|
||||
// TODO add model support
|
||||
export function cropText(
|
||||
const clamp = (v, min, max) => {
|
||||
return Math.min(Math.max(v, min), max)
|
||||
}
|
||||
|
||||
export async function cropText(
|
||||
text,
|
||||
maxLength = 3900 - 1000,
|
||||
maxLength = 4000,
|
||||
startLength = 400,
|
||||
endLength = 300,
|
||||
tiktoken = true,
|
||||
) {
|
||||
const userConfig = await getUserConfig()
|
||||
const k = Models[userConfig.modelName].desc.match(/[- ]*([0-9]+)k/)?.[1]
|
||||
if (k) {
|
||||
maxLength = Number(k) * 1000
|
||||
maxLength -= 100 + clamp(userConfig.maxResponseTokenLength, 1, maxLength - 1000)
|
||||
} else {
|
||||
maxLength -= 100 + clamp(userConfig.maxResponseTokenLength, 1, maxLength - 1000)
|
||||
}
|
||||
|
||||
const splits = text.split(/[,,。??!!;;]/).map((s) => s.trim())
|
||||
const splitsLength = splits.map((s) => (tiktoken ? encode(s).length : s.length))
|
||||
const length = splitsLength.reduce((sum, length) => sum + length, 0)
|
||||
@@ -73,7 +86,8 @@ export function cropText(
|
||||
croppedText += endPart
|
||||
|
||||
console.log(
|
||||
`maxLength: ${maxLength}\n` +
|
||||
`input maxLength: ${maxLength}\n` +
|
||||
`maxResponseTokenLength: ${userConfig.maxResponseTokenLength}\n` +
|
||||
// `croppedTextLength: ${tiktoken ? encode(croppedText).length : croppedText.length}\n` +
|
||||
`desiredLength: ${currentLength}\n` +
|
||||
`content: ${croppedText}`,
|
||||
|
||||
Reference in New Issue
Block a user