Trying to loop with ChatGPT completions Step 1, Step2, Step 3, etc. until list is finished

I am taking in the response com gpt3.5turbo:

The starting prompt to create the 12 Steps is this one here:

Generate a table of contents, the article title is "How to find friends as an introvert".

Add at the beginning of each step "Step 1: ", "Step 2: ", etc.

The first point is always "Introduction" and the last point is always "Conclusion", make sure these 2 steps also include "Step" at the beginning.

Do not write "Table of Contents:" at the beginning.

and using this js code to get the total number of steps openai came up with to use it for the iteration of loops.

const responseText = items[0].json.text;

// Extract the last number from each step
const regex = /Step (\d+)/g;
let match;
let lastNumber;

while ((match = regex.exec(responseText)) !== null) {
  lastNumber = parseInt(match[1], 10);

return [
    response: lastNumber

In this case the response is 12

And i want to use this integer to loop the chat completion 12x with this simple user prompt:

Write about step {{ $node.Code1.json.response }}

I am trying to accomplish this somehow but i don’t know what to do as I am not a developer, all the things seen here are just me coming up with an idea how to do this.

The next step would be to append the response to html and save it.

In the end, a nice 3000 words article should come together.

@Ed_P you already made a few openai workflows, any ideas? :slight_smile: i appreciate any help!

Hey @mikulabeutl,

Do you have an example workflow you have started on already for this? I have had a quick play and I have come up with the below which does the looping but it has another issue where it doesn’t have any context for the steps so you would need to also pass in the output from the first chat to build up the history.

This should give you a starting point to work from though.

I think some mockup examples with desired data outputs would help.
Alternative solution would be to ask GPT to put these steps in a JSON format.

So you can work with the returned object directly after using JSON.parse().

I’ve written a small example here:

It’s a different use-case, but idea is the same. Hope I understood your request correctly

Thanks for your solutions, i will keep trying

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.