automatically adapt different models' context length

This commit is contained in:
josc146
2023-06-14 11:08:23 +08:00
parent 912412f2d8
commit ed790ebb95
12 changed files with 31 additions and 17 deletions
+18 -4
View File
@@ -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}`,