Looping, appending a variable and using the variable in the next item

Okay, so here is a tough one. A brainteaser for me at the moment.

I am trying the following:

I have a OpenAI Chat Completion flow which shall write an article.

This article is written in sequences, so section by section.

The Outline is coming from a Google sheet. I have already parsed the json of the sections and am receiving a run with several items (each item is one section)

I am now able to write each section separately.
But I want the later loops to know what has been written so far.

So with each loop I need to aggregate what has been written so far and bring it over as field into the next loop.

First Loop: Priowritten variable is empty
Next Loop: Writes second section, Priorwritten variable has the first section.
Last Loop: Writes last section, Priorwritten contains an almost finished article excluding the very last section that is now been written.

I am trying to wrap my head around it, how I could achieve it in N8N.

Any pointers?

This is the current loop that just writes.

This is my main issue at the moment to make the switch from make to N8N.

So if anyone has any idea, that would be much appreciated.

Hey @Lars_Langenstuck,

this is an advanced use case of n8n and super easy to solve. You could think of a key value store to store your data and access it in each loop. This will also work with another google sheet where you save all generated texts to the article and load it in each loop before making the next openai call.

You could also access the data within of n8n with javascript code.

results = results.concat($('OpenAI Mock').all());

This code get all data which is created by the node OpenAI Mock. The node OpenAI Mock is wihtin the loop and with the .all()- function we have access to all data which was produced within all loop executions.

Here is an example flow for generating sections. I don’t have access to an OpenAI API account right now so I mocked the api for this purpose. You need to change the nodes to your needs but in general this is the concept to get data of previous iterations within the next iteration.

And here is another template which uses this technique: