mirror of
https://github.com/wassname/chatGPTBox.git
synced 2026-07-19 11:22:09 +08:00
automatically adapt different models' context length
This commit is contained in:
+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