Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(AI Transform Node): Reduce payload size #11965

Conversation

michael-radency
Copy link
Contributor

Summary

if request returned 'maximum context length' error reduce payload size and try again

Related Linear tickets, Github issues, and Community forum posts

https://linear.app/n8n/issue/NODE-1866/improve-error-handling-request-entity-too-large-error

@michael-radency michael-radency added node/improvement New feature or request n8n team Authored by the n8n team labels Nov 29, 2024
Copy link

codecov bot commented Nov 29, 2024

Codecov Report

Attention: Patch coverage is 90.90909% with 8 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
.../editor-ui/src/components/ButtonParameter/utils.ts 90.90% 8 Missing ⚠️

📢 Thoughts on this report? Let us know!

return Math.ceil(JSON.stringify(item).length / averageTokenLength);
};

export function reducePayloadSizeOrThrow(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add JSDoc comment on what this function does?

payload: AskAiRequest.RequestPayload,
error: Error,
averageTokenLength = 4,
) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is too long and has high cyclomatic complexity. Let's please split it in parts

question: 'What is node1 and prop1?',
}) as unknown as AskAiRequest.RequestPayload;

describe('reducePayloadSizeOrThrow', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice tests 👏

@michael-radency
Copy link
Contributor Author

thanks for review @tomi, updated as suggested

@michael-radency michael-radency requested a review from tomi December 2, 2024 04:32
Copy link
Contributor

@tomi tomi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for splitting up the function. It's much easier to follow now. Couple comments more

Comment on lines 71 to 72
const maxTokens = parseInt(tokens[0]);
const currentTokens = parseInt(tokens[1]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should explicitly define the radix, as otherwise it might get detected to something else

Suggested change
const maxTokens = parseInt(tokens[0]);
const currentTokens = parseInt(tokens[1]);
const maxTokens = parseInt(tokens[0], 10);
const currentTokens = parseInt(tokens[1], 10);

};

const calculateRemainingTokens = (error: Error) => {
const tokens = error.message.match(/\d+/g);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are assuming the error message contains two integer numbers. Could we have a comment telling what the expected error message would look like?

Also, is there a chance that some other error would be thrown that could contain 2 numbers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated as suggested, regarding other messages - we are checking message to contain specific subsisting 'maximum context length'

@@ -57,6 +57,128 @@ export function getSchemas() {
};
}

//------ Reduce payload ------

const calculateTokens = (item: object, averageTokenLength: number): number => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use unknown than object. Also since the calculation is not precise but only an estimation, we could make that explicit

Suggested change
const calculateTokens = (item: object, averageTokenLength: number): number => {
const estimateNumberOfTokens = (item: unknown, averageTokenLength: number): number => {

@michael-radency michael-radency requested a review from tomi December 9, 2024 07:40
Copy link
Contributor

@tomi tomi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@michael-radency
Copy link
Contributor Author

thanks again @tomi , updated as suggested

Copy link

cypress bot commented Dec 9, 2024

n8n    Run #8243

Run Properties:  status check passed Passed #8243  •  git commit 29f48788c1: 🌳 🖥️ browsers:node18.12.0-chrome107 🤖 michael-radency 🗃️ e2e/*
Project n8n
Branch Review node-1866-improve-error-handling-request-entity-too-large-error
Run status status check passed Passed #8243
Run duration 04m 30s
Commit git commit 29f48788c1: 🌳 🖥️ browsers:node18.12.0-chrome107 🤖 michael-radency 🗃️ e2e/*
Committer Michael Kret
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 480
View all changes introduced in this branch ↗︎

Copy link
Contributor

github-actions bot commented Dec 9, 2024

✅ All Cypress E2E specs passed

@michael-radency michael-radency merged commit d8ca8de into master Dec 9, 2024
50 checks passed
@michael-radency michael-radency deleted the node-1866-improve-error-handling-request-entity-too-large-error branch December 9, 2024 08:21
@github-actions github-actions bot mentioned this pull request Dec 11, 2024
@janober
Copy link
Member

janober commented Dec 11, 2024

Got released with n8n@1.72.0

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
n8n team Authored by the n8n team node/improvement New feature or request Released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants