diff --git a/src/content-script/index.jsx b/src/content-script/index.jsx index 72d2aad..23915a3 100644 --- a/src/content-script/index.jsx +++ b/src/content-script/index.jsx @@ -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 diff --git a/src/content-script/site-adapters/bilibili/index.mjs b/src/content-script/site-adapters/bilibili/index.mjs index ca98050..091a94a 100644 --- a/src/content-script/site-adapters/bilibili/index.mjs +++ b/src/content-script/site-adapters/bilibili/index.mjs @@ -50,7 +50,7 @@ export default { else subtitleContent += subtitles[i].content + ',' } - return cropText( + return await cropText( `用尽量简练的语言,联系视频标题,对视频进行内容摘要,同时仍要保留重要细节,视频标题为:"${title}",字幕内容为:\n${subtitleContent}`, ) } catch (e) { diff --git a/src/content-script/site-adapters/github/index.mjs b/src/content-script/site-adapters/github/index.mjs index d295201..bf1bb7f 100644 --- a/src/content-script/site-adapters/github/index.mjs +++ b/src/content-script/site-adapters/github/index.mjs @@ -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}`, ) diff --git a/src/content-script/site-adapters/gitlab/index.mjs b/src/content-script/site-adapters/gitlab/index.mjs index 644a22d..25c6727 100644 --- a/src/content-script/site-adapters/gitlab/index.mjs +++ b/src/content-script/site-adapters/gitlab/index.mjs @@ -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}`, ) diff --git a/src/content-script/site-adapters/juejin/index.mjs b/src/content-script/site-adapters/juejin/index.mjs index 37eb094..386b3e2 100644 --- a/src/content-script/site-adapters/juejin/index.mjs +++ b/src/content-script/site-adapters/juejin/index.mjs @@ -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}.请以如下格式输出你的回答: {文章摘要和文章作者} ====== diff --git a/src/content-script/site-adapters/quora/index.mjs b/src/content-script/site-adapters/quora/index.mjs index bdc4033..ffa15b5 100644 --- a/src/content-script/site-adapters/quora/index.mjs +++ b/src/content-script/site-adapters/quora/index.mjs @@ -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}`, diff --git a/src/content-script/site-adapters/reddit/index.mjs b/src/content-script/site-adapters/reddit/index.mjs index 136450a..76a65b1 100644 --- a/src/content-script/site-adapters/reddit/index.mjs +++ b/src/content-script/site-adapters/reddit/index.mjs @@ -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}`, diff --git a/src/content-script/site-adapters/stackoverflow/index.mjs b/src/content-script/site-adapters/stackoverflow/index.mjs index c23be20..4bb6214 100644 --- a/src/content-script/site-adapters/stackoverflow/index.mjs +++ b/src/content-script/site-adapters/stackoverflow/index.mjs @@ -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}`, diff --git a/src/content-script/site-adapters/weixin/index.mjs b/src/content-script/site-adapters/weixin/index.mjs index 5c6888d..3013bfd 100644 --- a/src/content-script/site-adapters/weixin/index.mjs +++ b/src/content-script/site-adapters/weixin/index.mjs @@ -19,7 +19,7 @@ export default { sidebar.style.background = 'transparent' } - return cropText( + return await cropText( `以下是一篇文章,标题是:"${title}",文章来源是:"${author}公众号",内容是:\n"${description}".请以如下格式输出你的回答: {文章来源和文章摘要} ====== diff --git a/src/content-script/site-adapters/youtube/index.mjs b/src/content-script/site-adapters/youtube/index.mjs index 09ac827..6d2b17d 100644 --- a/src/content-script/site-adapters/youtube/index.mjs +++ b/src/content-script/site-adapters/youtube/index.mjs @@ -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}`, ) diff --git a/src/content-script/site-adapters/zhihu/index.mjs b/src/content-script/site-adapters/zhihu/index.mjs index 28ac3c0..e4c6b8a 100644 --- a/src/content-script/site-adapters/zhihu/index.mjs +++ b/src/content-script/site-adapters/zhihu/index.mjs @@ -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}"`, ) } diff --git a/src/utils/crop-text.mjs b/src/utils/crop-text.mjs index 411bada..88130fd 100644 --- a/src/utils/crop-text.mjs +++ b/src/utils/crop-text.mjs @@ -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}`,