Skip to content

Commit 454eda8

Browse files
authored
adjust token counting (#1112)
1 parent bfbd52d commit 454eda8

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

common/prompt.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export type PromptOpts = {
8282
resolvedScenario: string
8383
modelFormat?: ModelFormat
8484
jsonValues: Record<string, any> | undefined
85+
contextBuffer?: number
8586
}
8687

8788
export type BuildPromptOpts = {
@@ -231,7 +232,6 @@ export async function createPromptParts(opts: PromptOpts, encoder: TokenCounter)
231232
* The lines from `getLinesForPrompt` are returned in time-descending order
232233
*/
233234
let template = getTemplate(opts)
234-
const templateSize = await encoder(template)
235235

236236
if (opts.modelFormat) {
237237
template = replaceTags(template, opts.modelFormat)
@@ -244,10 +244,9 @@ export async function createPromptParts(opts: PromptOpts, encoder: TokenCounter)
244244
* If we ambitiously include the entire history then embeddings will never be included.
245245
* The queryable embeddings are messages that are _NOT_ included in the context
246246
*/
247-
const maxContext = opts.settings
248-
? getContextLimit(opts.user, opts.settings) - templateSize - opts.settings.maxTokens!
249-
: undefined
250-
const lines = await getLinesForPrompt(opts, encoder, maxContext)
247+
const contextBuffer = opts.contextBuffer ?? 0
248+
const maxContext = opts.settings ? getContextLimit(opts.user, opts.settings) : undefined
249+
const lines = await getLinesForPrompt(opts, encoder, (maxContext || 0) + contextBuffer)
251250
const parts = await buildPromptParts(opts, lines, encoder)
252251

253252
const prompt = await injectPlaceholders(template, {
@@ -697,6 +696,7 @@ export async function fillPromptWithLines(opts: {
697696
/** Nodes to be inserted at a particular depth in the `lines` */
698697
inserts?: Map<number, string>
699698
optional?: Array<{ id: string; content: string }>
699+
marker?: string
700700
}) {
701701
const { encoder, tokenLimit, context, lines, inserts = new Map(), optional = [] } = opts
702702
const insertsCost = await encoder(Array.from(inserts.values()).join(' '))
@@ -706,7 +706,11 @@ export async function fillPromptWithLines(opts: {
706706
* Optional placeholders do not count towards token counts.
707707
* They are optional after everything else has been inserted therefore we remove them from the prompt
708708
*/
709-
const cleanContext = optional.reduce((amble, { id }) => amble.replace(id, ''), context)
709+
let cleanContext = optional.reduce((amble, { id }) => amble.replace(id, ''), context)
710+
if (opts.marker) {
711+
cleanContext.replace(opts.marker, '')
712+
}
713+
710714
let count = await encoder(cleanContext)
711715
const adding: string[] = []
712716

common/template-parser.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,15 @@ export async function parseTemplate(
244244
const filled = await fillPromptWithLines({
245245
encoder: opts.limit.encoder,
246246
tokenLimit: opts.limit.context,
247-
context: output,
247+
context: result,
248248
lines,
249249
inserts: opts.inserts,
250250
optional: opts.lowpriority,
251+
marker: id,
251252
})
252253
unusedTokens = filled.unusedTokens
253254
const trimmed = filled.adding.slice().reverse()
254-
output = output.replace(new RegExp(id, 'gi'), trimmed.join('\n'))
255+
output = result.replace(new RegExp(id, 'gi'), trimmed.join('\n'))
255256
linesAddedCount += filled.linesAddedCount
256257
history = trimmed
257258
}
@@ -289,10 +290,12 @@ export async function parseTemplate(
289290

290291
output = output.replace(/\r\n/g, '\n').replace(/\n\n+/g, '\n\n').trim()
291292

293+
const length = await opts.limit?.encoder?.(output)
294+
292295
return {
293296
parsed: output,
294297
inserts: opts.inserts ?? new Map(),
295-
length: await opts.limit?.encoder?.(result),
298+
length,
296299
linesAddedCount,
297300
sections,
298301
}

web/store/data/bot-generate.ts

+1
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ async function createActiveChatPrompt(
275275
userEmbeds,
276276
resolvedScenario,
277277
jsonValues: props.json,
278+
contextBuffer: entities.settings.maxTokens,
278279
},
279280
encoder
280281
)

0 commit comments

Comments
 (0)