Flowise
Flowise copied to clipboard
Recursive Chains Generate Max Call Stack Size Exceeded Error
Describe the bug
I'm trying to create a self-improving code graph that is recursive in nature. The graph has 3 chains code, review, and refactor along with an if/else utility branch after review that checks for the word "APPROVED" in the review text and if found, directs the result to an ending node, otherwise, to the refactor chain. After the refactor chain runs, I would like to pass the output directly back to the review chain as its input, however when I do, I get the aforementioned error. Is this just not supported or a bug?
To Reproduce Steps to reproduce the behavior:
- create a chain
code(with chat prompt template) that takes the user input as a code generation request. Its sole job is to generate code based on the request. - create a chain
review(with chat prompt template) that takes the output fromcodeand a system message that tells it to review the code and if all good, reply "APPROVED" otherwise reply with the code review points. - add an
if/elseutility that takes in the the output fromreviewas an input variable, and the code fromcodeoutput as well. Set true to check if the word APPROVED in text, return $code, in false return a prompt like "refactor the following code: $code review points: $review". Send true to custom JS utility set as ending node, input variable as $answer and return it. Send false torefactorchain. - create a
refactorchain (with chat prompt template) that takes in input from the false condition of theif/elseutility, take the output and attach it back to the input of thereviewchain prompt. Set up a second dynamic token in thereviewchain prompt template, so that template looks like{code} {refactored_code}with inputs properly mapped to both from thecodechain andrefactorchain respectively. - Test it by giving it any semi-difficult coding task that will likely require some improvements after the first generation.
Expected behavior
The output from refactor chain gets passed back to the review chain in a recursive loop until the review chain model response contains the word "APPROVED".
Screenshots
Flow https://gist.github.com/transcendr/2507dc3dd24c4aa1f63a4c12a64ce9b5
Setup
- Installation: clone repo, etc, then
pnpm start --DEBUG=true - Flowise Version: latest
- OS: macOS
- Browser: chrome
Additional context No
Also I would like to note that running pnpm start with the DEBUG flag does not seem to change the output in any way.
Flowise currently don't have the For loop concept yet, it's something we are working on. The way you connect the last LLMChain to the first Prompt Template probably wouldn't work
Thanks @HenryHengZJ -- this would be amazing to support in the future. As an alternative I have created a second flow with a chain that takes in the user question, pretty much just regurgitates it as output to a custom js utility which is code that handles the recursion by calling the API endpoint of the first flow. I'd be curious to know if you have a different/better approach in mind, if not I'll just go with this for the time being.
Also, might I recommend adding to whatever validation that handles edges that a chain's output prediction cannot connect to prompt template input.
@transcendr do you mind sharing the alternative flow that you have? thanks for the suggestion, we have planned for this as well