Flowise icon indicating copy to clipboard operation
Flowise copied to clipboard

Recursive Chains Generate Max Call Stack Size Exceeded Error

Open transcendr opened this issue 1 year ago • 4 comments

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:

  1. 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.
  2. create a chain review (with chat prompt template) that takes the output from code and a system message that tells it to review the code and if all good, reply "APPROVED" otherwise reply with the code review points.
  3. add an if/else utility that takes in the the output from review as an input variable, and the code from code output 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 to refactor chain.
  4. create a refactor chain (with chat prompt template) that takes in input from the false condition of the if/else utility, take the output and attach it back to the input of the review chain prompt. Set up a second dynamic token in the review chain prompt template, so that template looks like {code} {refactored_code} with inputs properly mapped to both from the code chain and refactor chain respectively.
  5. 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 image

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

transcendr avatar Mar 31 '24 09:03 transcendr

Also I would like to note that running pnpm start with the DEBUG flag does not seem to change the output in any way.

transcendr avatar Mar 31 '24 09:03 transcendr

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

HenryHengZJ avatar Mar 31 '24 09:03 HenryHengZJ

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 avatar Mar 31 '24 15:03 transcendr

@transcendr do you mind sharing the alternative flow that you have? thanks for the suggestion, we have planned for this as well

HenryHengZJ avatar Apr 01 '24 08:04 HenryHengZJ