From 260db644fba477592579ac0f8c4e7f06a4667614 Mon Sep 17 00:00:00 2001 From: Michael Kret Date: Fri, 29 Nov 2024 10:46:13 +0200 Subject: [PATCH 1/6] reducePayloadSize --- .../src/components/ButtonParameter/utils.ts | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/packages/editor-ui/src/components/ButtonParameter/utils.ts b/packages/editor-ui/src/components/ButtonParameter/utils.ts index b95846975ff74..3982b1776e728 100644 --- a/packages/editor-ui/src/components/ButtonParameter/utils.ts +++ b/packages/editor-ui/src/components/ButtonParameter/utils.ts @@ -57,6 +57,80 @@ export function getSchemas() { }; } +function reducePayloadSize(payload: AskAiRequest.RequestPayload, error: Error) { + const AVERAGE_TOKEN_LENGTH = 4; + try { + //calculate how many tokens has to be reduced + const tokens = error.message.match(/\d+/g); + if (!tokens || tokens.length < 2) throw error; + + const maxTokens = parseInt(tokens[0]); + const currentTokens = parseInt(tokens[1]); + + let tokensToReduce = currentTokens - maxTokens; + + //check if parent nodes schema taces more tokens than available + let parentNodesTokens = Math.ceil( + JSON.stringify(payload.context.schema).length / AVERAGE_TOKEN_LENGTH, + ); + + if (tokensToReduce > parentNodesTokens) { + tokensToReduce -= parentNodesTokens; + payload.context.schema = []; + } + + //remove parent nodes not referenced in the prompt + if (payload.context.schema.length) { + const nodesLength = payload.context.schema.length; + + for (let nodeIndex = nodesLength - 1; nodeIndex >= 0; nodeIndex--) { + if (payload.question.includes(payload.context.schema[nodeIndex].nodeName)) continue; + + const nodeTokens = Math.ceil( + JSON.stringify(payload.context.schema[nodeIndex]).length / AVERAGE_TOKEN_LENGTH, + ); + tokensToReduce -= nodeTokens; + parentNodesTokens -= nodeTokens; + + payload.context.schema.splice(nodeIndex, 1); + + if (tokensToReduce <= 0) break; + } + } + + if (tokensToReduce <= 0) return; + + //remove properties not referenced in the prompt from the input schema + if (typeof payload.context.inputSchema.schema.value === 'object') { + const propsLength = payload.context.inputSchema.schema.value.length; + for (let index = propsLength - 1; index >= 0; index--) { + const key = payload.context.inputSchema.schema.value[index].key; + if (key && payload.question.includes(key)) continue; + + const propTokens = Math.ceil( + JSON.stringify(payload.context.inputSchema.schema.value[index]).length / + AVERAGE_TOKEN_LENGTH, + ); + tokensToReduce -= propTokens; + + payload.context.inputSchema.schema.value.splice(index, 1); + + if (tokensToReduce <= 0) break; + } + } else throw error; + + //if tokensToReduce is still remainig, remove all parent nodes + if (tokensToReduce > 0 && tokensToReduce - parentNodesTokens <= 0) { + payload.context.schema = []; + return; + } + + if (tokensToReduce > 0) throw error; + } catch (e) { + throw e; + } +} + export async function generateCodeForAiTransform(prompt: string, path: string, retries = 1) { const schemas = getSchemas(); @@ -83,6 +157,11 @@ export async function generateCodeForAiTransform(prompt: string, path: string, r code = generatedCode; break; } catch (e) { + if (e.message.includes('maximum context length')) { + reducePayloadSize(payload, e); + continue; + } + retries--; if (!retries) throw e; } From f784d007d6040c44e9fea95a41cdd28447f58f99 Mon Sep 17 00:00:00 2001 From: Michael Kret Date: Fri, 29 Nov 2024 14:26:50 +0200 Subject: [PATCH 2/6] update, test --- .../components/ButtonParameter/utils.test.ts | 70 ++++++++++++++++++- .../src/components/ButtonParameter/utils.ts | 66 ++++++++--------- 2 files changed, 103 insertions(+), 33 deletions(-) diff --git a/packages/editor-ui/src/components/ButtonParameter/utils.test.ts b/packages/editor-ui/src/components/ButtonParameter/utils.test.ts index df7e13d477b41..7453d41fb5406 100644 --- a/packages/editor-ui/src/components/ButtonParameter/utils.test.ts +++ b/packages/editor-ui/src/components/ButtonParameter/utils.test.ts @@ -1,8 +1,10 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { describe, it, expect, vi, beforeEach } from 'vitest'; -import { generateCodeForAiTransform } from './utils'; +import { generateCodeForAiTransform, reducePayloadSizeOrThrow } from './utils'; import { createPinia, setActivePinia } from 'pinia'; import { generateCodeForPrompt } from '@/api/ai'; +import type { AskAiRequest } from '@/types/assistant.types'; +import type { Schema } from '@/Interface'; vi.mock('./utils', async () => { const actual = await vi.importActual('./utils'); @@ -86,3 +88,69 @@ describe('generateCodeForAiTransform - Retry Tests', () => { expect(generateCodeForPrompt).toHaveBeenCalledTimes(1); }); }); + +const mockPayload = () => + ({ + context: { + schema: [ + { nodeName: 'node1', data: 'some data' }, + { nodeName: 'node2', data: 'other data' }, + ], + inputSchema: { + schema: { + value: [ + { key: 'prop1', value: 'value1' }, + { key: 'prop2', value: 'value2' }, + ], + }, + }, + }, + question: 'What is node1 and prop1?', + }) as unknown as AskAiRequest.RequestPayload; + +describe('reducePayloadSizeOrThrow', () => { + it('reduces schema size when tokens exceed the limit', () => { + const payload = mockPayload(); + const error = new Error('Limit is 100 tokens, but 104 were provided'); + + reducePayloadSizeOrThrow(payload, error); + + expect(payload.context.schema.length).toBe(1); + expect(payload.context.schema[0]).toEqual({ nodeName: 'node1', data: 'some data' }); + }); + + it('removes unreferenced properties in input schema', () => { + const payload = mockPayload(); + const error = new Error('Limit is 100 tokens, but 150 were provided'); + + reducePayloadSizeOrThrow(payload, error); + + expect(payload.context.inputSchema.schema.value.length).toBe(1); + expect((payload.context.inputSchema.schema.value as Schema[])[0].key).toBe('prop1'); + }); + + it('removes all parent nodes if needed', () => { + const payload = mockPayload(); + const error = new Error('Limit is 100 tokens, but 150 were provided'); + + payload.question = ''; + + reducePayloadSizeOrThrow(payload, error); + + expect(payload.context.schema.length).toBe(0); + }); + + it('throws error if tokens still exceed after reductions', () => { + const payload = mockPayload(); + const error = new Error('Limit is 100 tokens, but 200 were provided'); + + expect(() => reducePayloadSizeOrThrow(payload, error)).toThrowError(error); + }); + + it('throws error if message format is invalid', () => { + const payload = mockPayload(); + const error = new Error('Invalid token message format'); + + expect(() => reducePayloadSizeOrThrow(payload, error)).toThrowError(error); + }); +}); diff --git a/packages/editor-ui/src/components/ButtonParameter/utils.ts b/packages/editor-ui/src/components/ButtonParameter/utils.ts index 3982b1776e728..4b6f909a5b055 100644 --- a/packages/editor-ui/src/components/ButtonParameter/utils.ts +++ b/packages/editor-ui/src/components/ButtonParameter/utils.ts @@ -1,5 +1,5 @@ import type { Schema } from '@/Interface'; -import { ApplicationError, type INodeExecutionData } from 'n8n-workflow'; +import { ApplicationError, IDataObject, type INodeExecutionData } from 'n8n-workflow'; import { useWorkflowsStore } from '@/stores/workflows.store'; import { useNDVStore } from '@/stores/ndv.store'; import { useDataSchema } from '@/composables/useDataSchema'; @@ -57,8 +57,15 @@ export function getSchemas() { }; } -function reducePayloadSize(payload: AskAiRequest.RequestPayload, error: Error) { - const AVERAGE_TOKEN_LENGTH = 4; +const calculateTokens = (item: object, averageTokenLength: number): number => { + return Math.ceil(JSON.stringify(item).length / averageTokenLength); +}; + +export function reducePayloadSizeOrThrow( + payload: AskAiRequest.RequestPayload, + error: Error, + averegeTokenLength = 4, +) { try { //calculate how many tokens has to be reduced const tokens = error.message.match(/\d+/g); @@ -67,65 +74,60 @@ function reducePayloadSize(payload: AskAiRequest.RequestPayload, error: Error) { const maxTokens = parseInt(tokens[0]); const currentTokens = parseInt(tokens[1]); - let tokensToReduce = currentTokens - maxTokens; + let remainingTokensToReduce = currentTokens - maxTokens; //check if parent nodes schema taces more tokens than available - let parentNodesTokens = Math.ceil( - JSON.stringify(payload.context.schema).length / AVERAGE_TOKEN_LENGTH, - ); + let parentNodesTokenCount = calculateTokens(payload.context.schema, averegeTokenLength); - if (tokensToReduce > parentNodesTokens) { - tokensToReduce -= parentNodesTokens; + if (remainingTokensToReduce > parentNodesTokenCount) { + remainingTokensToReduce -= parentNodesTokenCount; payload.context.schema = []; } //remove parent nodes not referenced in the prompt if (payload.context.schema.length) { - const nodesLength = payload.context.schema.length; + const nodes = [...payload.context.schema]; - for (let nodeIndex = nodesLength - 1; nodeIndex >= 0; nodeIndex--) { - if (payload.question.includes(payload.context.schema[nodeIndex].nodeName)) continue; + for (let nodeIndex = 0; nodeIndex < nodes.length; nodeIndex++) { + if (payload.question.includes(nodes[nodeIndex].nodeName)) continue; - const nodeTokens = Math.ceil( - JSON.stringify(payload.context.schema[nodeIndex]).length / AVERAGE_TOKEN_LENGTH, - ); - tokensToReduce -= nodeTokens; - parentNodesTokens -= nodeTokens; + const nodeTokens = calculateTokens(nodes[nodeIndex], averegeTokenLength); + remainingTokensToReduce -= nodeTokens; + parentNodesTokenCount -= nodeTokens; payload.context.schema.splice(nodeIndex, 1); - if (tokensToReduce <= 0) break; + if (remainingTokensToReduce <= 0) return; } } - if (tokensToReduce <= 0) return; + if (remainingTokensToReduce <= 0) return; //remove properties not referenced in the prompt from the input schema - if (typeof payload.context.inputSchema.schema.value === 'object') { - const propsLength = payload.context.inputSchema.schema.value.length; - for (let index = propsLength - 1; index >= 0; index--) { - const key = payload.context.inputSchema.schema.value[index].key; + if (Array.isArray(payload.context.inputSchema.schema.value)) { + const props = [...payload.context.inputSchema.schema.value]; + + for (let index = 0; index < props.length; index++) { + const key = props[index].key; if (key && payload.question.includes(key)) continue; - const propTokens = Math.ceil( - JSON.stringify(payload.context.inputSchema.schema.value[index]).length / - AVERAGE_TOKEN_LENGTH, - ); - tokensToReduce -= propTokens; + const propTokens = calculateTokens(props[index], averegeTokenLength); + + remainingTokensToReduce -= propTokens; payload.context.inputSchema.schema.value.splice(index, 1); - if (tokensToReduce <= 0) break; + if (remainingTokensToReduce <= 0) return; } } else throw error; //if tokensToReduce is still remainig, remove all parent nodes - if (tokensToReduce > 0 && tokensToReduce - parentNodesTokens <= 0) { + if (remainingTokensToReduce > 0 && remainingTokensToReduce - parentNodesTokenCount <= 0) { payload.context.schema = []; return; } - if (tokensToReduce > 0) throw error; + if (remainingTokensToReduce > 0) throw error; } catch (e) { throw e; } @@ -158,7 +160,7 @@ export async function generateCodeForAiTransform(prompt: string, path: string, r break; } catch (e) { if (e.message.includes('maximum context length')) { - reducePayloadSize(payload, e); + reducePayloadSizeOrThrow(payload, e); continue; } From d129a63a5d3f555fc36a7b946c8951f658feb819 Mon Sep 17 00:00:00 2001 From: Michael Kret Date: Fri, 29 Nov 2024 14:27:45 +0200 Subject: [PATCH 3/6] unused import removed --- packages/editor-ui/src/components/ButtonParameter/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/editor-ui/src/components/ButtonParameter/utils.ts b/packages/editor-ui/src/components/ButtonParameter/utils.ts index 4b6f909a5b055..25a82810f2c5d 100644 --- a/packages/editor-ui/src/components/ButtonParameter/utils.ts +++ b/packages/editor-ui/src/components/ButtonParameter/utils.ts @@ -1,5 +1,5 @@ import type { Schema } from '@/Interface'; -import { ApplicationError, IDataObject, type INodeExecutionData } from 'n8n-workflow'; +import { ApplicationError, type INodeExecutionData } from 'n8n-workflow'; import { useWorkflowsStore } from '@/stores/workflows.store'; import { useNDVStore } from '@/stores/ndv.store'; import { useDataSchema } from '@/composables/useDataSchema'; From d6858022e73a1f29d3745a520e5a113f353a4040 Mon Sep 17 00:00:00 2001 From: Michael Kret Date: Fri, 29 Nov 2024 14:31:29 +0200 Subject: [PATCH 4/6] spelling fixes --- .../src/components/ButtonParameter/utils.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/editor-ui/src/components/ButtonParameter/utils.ts b/packages/editor-ui/src/components/ButtonParameter/utils.ts index 25a82810f2c5d..97befa5053833 100644 --- a/packages/editor-ui/src/components/ButtonParameter/utils.ts +++ b/packages/editor-ui/src/components/ButtonParameter/utils.ts @@ -64,7 +64,7 @@ const calculateTokens = (item: object, averageTokenLength: number): number => { export function reducePayloadSizeOrThrow( payload: AskAiRequest.RequestPayload, error: Error, - averegeTokenLength = 4, + averageTokenLength = 4, ) { try { //calculate how many tokens has to be reduced @@ -76,8 +76,8 @@ export function reducePayloadSizeOrThrow( let remainingTokensToReduce = currentTokens - maxTokens; - //check if parent nodes schema taces more tokens than available - let parentNodesTokenCount = calculateTokens(payload.context.schema, averegeTokenLength); + //check if parent nodes schema takes more tokens than available + let parentNodesTokenCount = calculateTokens(payload.context.schema, averageTokenLength); if (remainingTokensToReduce > parentNodesTokenCount) { remainingTokensToReduce -= parentNodesTokenCount; @@ -91,7 +91,7 @@ export function reducePayloadSizeOrThrow( for (let nodeIndex = 0; nodeIndex < nodes.length; nodeIndex++) { if (payload.question.includes(nodes[nodeIndex].nodeName)) continue; - const nodeTokens = calculateTokens(nodes[nodeIndex], averegeTokenLength); + const nodeTokens = calculateTokens(nodes[nodeIndex], averageTokenLength); remainingTokensToReduce -= nodeTokens; parentNodesTokenCount -= nodeTokens; @@ -111,7 +111,7 @@ export function reducePayloadSizeOrThrow( const key = props[index].key; if (key && payload.question.includes(key)) continue; - const propTokens = calculateTokens(props[index], averegeTokenLength); + const propTokens = calculateTokens(props[index], averageTokenLength); remainingTokensToReduce -= propTokens; @@ -121,7 +121,7 @@ export function reducePayloadSizeOrThrow( } } else throw error; - //if tokensToReduce is still remainig, remove all parent nodes + //if tokensToReduce is still remaining, remove all parent nodes if (remainingTokensToReduce > 0 && remainingTokensToReduce - parentNodesTokenCount <= 0) { payload.context.schema = []; return; From 40ef2a898da056a2f7c21b32b2e67312c84379d7 Mon Sep 17 00:00:00 2001 From: Michael Kret Date: Mon, 2 Dec 2024 06:31:12 +0200 Subject: [PATCH 5/6] review update --- .../src/components/ButtonParameter/utils.ts | 142 ++++++++++++------ 1 file changed, 94 insertions(+), 48 deletions(-) diff --git a/packages/editor-ui/src/components/ButtonParameter/utils.ts b/packages/editor-ui/src/components/ButtonParameter/utils.ts index 97befa5053833..722490665161b 100644 --- a/packages/editor-ui/src/components/ButtonParameter/utils.ts +++ b/packages/editor-ui/src/components/ButtonParameter/utils.ts @@ -57,75 +57,121 @@ export function getSchemas() { }; } +//------ Reduce payload ------ + const calculateTokens = (item: object, averageTokenLength: number): number => { return Math.ceil(JSON.stringify(item).length / averageTokenLength); }; -export function reducePayloadSizeOrThrow( - payload: AskAiRequest.RequestPayload, - error: Error, - averageTokenLength = 4, -) { - try { - //calculate how many tokens has to be reduced - const tokens = error.message.match(/\d+/g); - if (!tokens || tokens.length < 2) throw error; - - const maxTokens = parseInt(tokens[0]); - const currentTokens = parseInt(tokens[1]); +const calculateRemainingTokens = (error: Error) => { + const tokens = error.message.match(/\d+/g); - let remainingTokensToReduce = currentTokens - maxTokens; + if (!tokens || tokens.length < 2) throw error; - //check if parent nodes schema takes more tokens than available - let parentNodesTokenCount = calculateTokens(payload.context.schema, averageTokenLength); + const maxTokens = parseInt(tokens[0]); + const currentTokens = parseInt(tokens[1]); - if (remainingTokensToReduce > parentNodesTokenCount) { - remainingTokensToReduce -= parentNodesTokenCount; - payload.context.schema = []; - } + return currentTokens - maxTokens; +}; - //remove parent nodes not referenced in the prompt - if (payload.context.schema.length) { - const nodes = [...payload.context.schema]; +const trimParentNodesSchema = ( + payload: AskAiRequest.RequestPayload, + remainingTokensToReduce: number, + averageTokenLength: number, +) => { + //check if parent nodes schema takes more tokens than available + let parentNodesTokenCount = calculateTokens(payload.context.schema, averageTokenLength); + + if (remainingTokensToReduce > parentNodesTokenCount) { + remainingTokensToReduce -= parentNodesTokenCount; + payload.context.schema = []; + } - for (let nodeIndex = 0; nodeIndex < nodes.length; nodeIndex++) { - if (payload.question.includes(nodes[nodeIndex].nodeName)) continue; + //remove parent nodes not referenced in the prompt + if (payload.context.schema.length) { + const nodes = [...payload.context.schema]; - const nodeTokens = calculateTokens(nodes[nodeIndex], averageTokenLength); - remainingTokensToReduce -= nodeTokens; - parentNodesTokenCount -= nodeTokens; + for (let nodeIndex = 0; nodeIndex < nodes.length; nodeIndex++) { + if (payload.question.includes(nodes[nodeIndex].nodeName)) continue; - payload.context.schema.splice(nodeIndex, 1); + const nodeTokens = calculateTokens(nodes[nodeIndex], averageTokenLength); + remainingTokensToReduce -= nodeTokens; + parentNodesTokenCount -= nodeTokens; + payload.context.schema.splice(nodeIndex, 1); - if (remainingTokensToReduce <= 0) return; - } + if (remainingTokensToReduce <= 0) break; } + } - if (remainingTokensToReduce <= 0) return; - - //remove properties not referenced in the prompt from the input schema - if (Array.isArray(payload.context.inputSchema.schema.value)) { - const props = [...payload.context.inputSchema.schema.value]; + return [remainingTokensToReduce, parentNodesTokenCount]; +}; - for (let index = 0; index < props.length; index++) { - const key = props[index].key; - if (key && payload.question.includes(key)) continue; +const trimInputSchemaProperties = ( + payload: AskAiRequest.RequestPayload, + remainingTokensToReduce: number, + averageTokenLength: number, + parentNodesTokenCount: number, +) => { + if (remainingTokensToReduce <= 0) return remainingTokensToReduce; - const propTokens = calculateTokens(props[index], averageTokenLength); + //remove properties not referenced in the prompt from the input schema + if (Array.isArray(payload.context.inputSchema.schema.value)) { + const props = [...payload.context.inputSchema.schema.value]; - remainingTokensToReduce -= propTokens; + for (let index = 0; index < props.length; index++) { + const key = props[index].key; - payload.context.inputSchema.schema.value.splice(index, 1); + if (key && payload.question.includes(key)) continue; - if (remainingTokensToReduce <= 0) return; - } - } else throw error; + const propTokens = calculateTokens(props[index], averageTokenLength); + remainingTokensToReduce -= propTokens; + payload.context.inputSchema.schema.value.splice(index, 1); - //if tokensToReduce is still remaining, remove all parent nodes - if (remainingTokensToReduce > 0 && remainingTokensToReduce - parentNodesTokenCount <= 0) { - payload.context.schema = []; - return; + if (remainingTokensToReduce <= 0) break; } + } + + //if tokensToReduce is still remaining, remove all parent nodes + if (remainingTokensToReduce > 0) { + payload.context.schema = []; + remainingTokensToReduce -= parentNodesTokenCount; + } + + return remainingTokensToReduce; +}; + +/** + * Attempts to reduce the size of the payload to fit within token limits or throws an error if unsuccessful, + * payload would be modified in place + * + * @param {AskAiRequest.RequestPayload} payload - The request payload to be trimmed, + * 'schema' and 'inputSchema.schema' will be modified. + * @param {Error} error - The error to throw if the token reduction fails. + * @param {number} [averageTokenLength=4] - The average token length used for estimation. + * @throws {Error} - Throws the provided error if the payload cannot be reduced sufficiently. + */ +export function reducePayloadSizeOrThrow( + payload: AskAiRequest.RequestPayload, + error: Error, + averageTokenLength = 4, +) { + try { + let remainingTokensToReduce = calculateRemainingTokens(error); + + const [remaining, parentNodesTokenCount] = trimParentNodesSchema( + payload, + remainingTokensToReduce, + averageTokenLength, + ); + + remainingTokensToReduce = remaining; + + remainingTokensToReduce = trimInputSchemaProperties( + payload, + remainingTokensToReduce, + averageTokenLength, + parentNodesTokenCount, + ); if (remainingTokensToReduce > 0) throw error; } catch (e) { From 29f48788c1049bc7858c4f6fe9e923b82d903455 Mon Sep 17 00:00:00 2001 From: Michael Kret Date: Mon, 9 Dec 2024 09:37:37 +0200 Subject: [PATCH 6/6] review update --- .../src/components/ButtonParameter/utils.ts | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/packages/editor-ui/src/components/ButtonParameter/utils.ts b/packages/editor-ui/src/components/ButtonParameter/utils.ts index 722490665161b..1044477bbbbd9 100644 --- a/packages/editor-ui/src/components/ButtonParameter/utils.ts +++ b/packages/editor-ui/src/components/ButtonParameter/utils.ts @@ -59,17 +59,23 @@ export function getSchemas() { //------ Reduce payload ------ -const calculateTokens = (item: object, averageTokenLength: number): number => { - return Math.ceil(JSON.stringify(item).length / averageTokenLength); +const estimateNumberOfTokens = (item: unknown, averageTokenLength: number): number => { + if (typeof item === 'object') { + return Math.ceil(JSON.stringify(item).length / averageTokenLength); + } + + return 0; }; const calculateRemainingTokens = (error: Error) => { + // Expected message format: + //'This model's maximum context length is 8192 tokens. However, your messages resulted in 10514 tokens.' const tokens = error.message.match(/\d+/g); if (!tokens || tokens.length < 2) throw error; - const maxTokens = parseInt(tokens[0]); - const currentTokens = parseInt(tokens[1]); + const maxTokens = parseInt(tokens[0], 10); + const currentTokens = parseInt(tokens[1], 10); return currentTokens - maxTokens; }; @@ -80,7 +86,7 @@ const trimParentNodesSchema = ( averageTokenLength: number, ) => { //check if parent nodes schema takes more tokens than available - let parentNodesTokenCount = calculateTokens(payload.context.schema, averageTokenLength); + let parentNodesTokenCount = estimateNumberOfTokens(payload.context.schema, averageTokenLength); if (remainingTokensToReduce > parentNodesTokenCount) { remainingTokensToReduce -= parentNodesTokenCount; @@ -94,7 +100,7 @@ const trimParentNodesSchema = ( for (let nodeIndex = 0; nodeIndex < nodes.length; nodeIndex++) { if (payload.question.includes(nodes[nodeIndex].nodeName)) continue; - const nodeTokens = calculateTokens(nodes[nodeIndex], averageTokenLength); + const nodeTokens = estimateNumberOfTokens(nodes[nodeIndex], averageTokenLength); remainingTokensToReduce -= nodeTokens; parentNodesTokenCount -= nodeTokens; payload.context.schema.splice(nodeIndex, 1); @@ -123,7 +129,7 @@ const trimInputSchemaProperties = ( if (key && payload.question.includes(key)) continue; - const propTokens = calculateTokens(props[index], averageTokenLength); + const propTokens = estimateNumberOfTokens(props[index], averageTokenLength); remainingTokensToReduce -= propTokens; payload.context.inputSchema.schema.value.splice(index, 1);