fix: add stop sequence support to ChatOpenAI custom#6434
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a stopSequence input parameter to the ChatOpenAICustom model, allowing users to define comma-separated stop words, and includes unit tests verifying this behavior. A review comment suggests filtering out empty strings from the parsed stop sequences to prevent potential 400 Bad Request errors from the OpenAI API when handling consecutive or trailing commas.
| if (stopSequence) { | ||
| const stopSequenceArray = stopSequence.split(',').map((item) => item.trim()) | ||
| obj.stop = stopSequenceArray | ||
| } |
There was a problem hiding this comment.
If the stopSequence contains empty segments (e.g., trailing commas or multiple consecutive commas like foo,,bar), splitting and mapping will result in empty strings in the stop array. The OpenAI API will reject requests containing empty strings in the stop parameter with a 400 Bad Request error. Filtering out empty strings using simple, chained operations ensures robustness and maintains readability.
| if (stopSequence) { | |
| const stopSequenceArray = stopSequence.split(',').map((item) => item.trim()) | |
| obj.stop = stopSequenceArray | |
| } | |
| if (stopSequence) { | |
| const stopSequenceArray = stopSequence | |
| .split(',') | |
| .map((item) => item.trim()) | |
| .filter((item) => item !== '') | |
| if (stopSequenceArray.length > 0) { | |
| obj.stop = stopSequenceArray | |
| } | |
| } |
References
- Prioritize code readability and understandability over conciseness. A series of simple, chained operations can be preferable to a single, more complex one if it improves understandability and reduces the potential for future errors.
There was a problem hiding this comment.
Thanks for the review. I’ve addressed this in 0c594c8 by filtering empty stop sequence entries and added a regression test for consecutive/trailing commas.
Description
Adds
Stop Sequencesupport to the ChatOpenAI Custom node so users can pass comma-separated stop words such as<|im_end|>from Additional Parameters.Fixes #2499
Changes
stopSequenceadditional parameter to ChatOpenAI Customstoparray passed toChatOpenAITests
pnpm --filter flowise-components test -- ChatOpenAICustom.test.tspnpm exec prettier --check packages/components/nodes/chatmodels/ChatOpenAICustom/ChatOpenAICustom.ts packages/components/nodes/chatmodels/ChatOpenAICustom/ChatOpenAICustom.test.tsNote: local environment used Node v23.5.0, so pnpm printed an engine warning because the repo expects Node ^20, but the focused test passed.