Skip to content

Commit

Permalink
automatically adapt different models' context length
Browse files Browse the repository at this point in the history
  • Loading branch information
josStorer committed Jun 14, 2023
1 parent 912412f commit ed790eb
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/content-script/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/content-script/site-adapters/bilibili/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default {
else subtitleContent += subtitles[i].content + ','
}

return cropText(
return await cropText(
`用尽量简练的语言,联系视频标题,对视频进行内容摘要,同时仍要保留重要细节,视频标题为:"${title}",字幕内容为:\n${subtitleContent}`,
)
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/content-script/site-adapters/github/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
)
Expand Down
2 changes: 1 addition & 1 deletion src/content-script/site-adapters/gitlab/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
)
Expand Down
2 changes: 1 addition & 1 deletion src/content-script/site-adapters/juejin/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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}.请以如下格式输出你的回答:
{文章摘要和文章作者}
======
Expand Down
2 changes: 1 addition & 1 deletion src/content-script/site-adapters/quora/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down
2 changes: 1 addition & 1 deletion src/content-script/site-adapters/reddit/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down
2 changes: 1 addition & 1 deletion src/content-script/site-adapters/stackoverflow/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down
2 changes: 1 addition & 1 deletion src/content-script/site-adapters/weixin/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default {
sidebar.style.background = 'transparent'
}

return cropText(
return await cropText(
`以下是一篇文章,标题是:"${title}",文章来源是:"${author}公众号",内容是:\n"${description}".请以如下格式输出你的回答:
{文章来源和文章摘要}
======
Expand Down
2 changes: 1 addition & 1 deletion src/content-script/site-adapters/youtube/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
)
Expand Down
6 changes: 3 additions & 3 deletions src/content-script/site-adapters/zhihu/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
)
Expand All @@ -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}`,
)
Expand All @@ -30,7 +30,7 @@ export default {
const description = document.querySelector('.Post-RichText')?.textContent

if (title) {
return cropText(
return await cropText(
`以下是一篇文章,给出相应的摘要,以及你对此的看法.标题是:"${title}",内容是:\n"${description}"`,
)
}
Expand Down
22 changes: 18 additions & 4 deletions src/utils/crop-text.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}`,
Expand Down

0 comments on commit ed790eb

Please sign in to comment.