Hello everyone! Today I'd like to introduce you toFastGPT 4.8.11 An awesome node added to the version - the [Loop Run] node. If you often need to deal with large amounts of data, this feature can definitely make you twice as much work!
π‘ What does this node do?
Imagine this scenario: you have 100 articles that need to be summarized by AI, or 500 product descriptions that need to be translated into English.Previously you may have had to go line by line, which was time consuming and cumbersome. And with loop run nodes, you just need to:
- Put all the data into an array
- Setting up the processing flow
- And then... It's time to go get a cup of coffee! The node willAutomatically process all the data for you
That's right, it's that simple! It's like a little automated assistant that helps you with repetitive tasks.
π What can it do?
Let's see what properties this node has:
1. Automated batch processing capability
- π¦ Support for various types of data:
- Text arrays? No problem!
- An array of numbers? Sure!
- Boolean arrays? Fully supported!
- An array of objects? Not even close!
- π Fully automatic traversal of all array elements
- π Strictly sequential processing: no need to worry about disorganized data
- β‘ Also supports parallel processing: faster!
2. Intelligent process control
- π― Automatic triggering of subsequent nodes: set up the process and automate the whole process
- π Abort conditions can be set: stop whenever needed
- π’ Built-in counter: always know the progress of processing
- π Maintaining context: ensuring processing coherence
π― What are the best scenarios to use it in?
1. Batch content processing
- π Batch translation of articles
- π Batch Generation of Article Summaries
- βοΈ Batch Creation of Content
2. Data pipeline processing
- π Automatically analyze each search result
- π Processing knowledge base searches on a case-by-case basis
- π Process the list of data returned by the API
3. Recursive optimization tasks
- π Break long documents into smaller chunks for processing
- π Optimize content quality in multiple rounds
- βοΈ Chaining Complex Data
π How do I use it?
This node is very simple to use, just set up the input array and the loop body.
FastGPT domestic version:
FastGPT Overseas (requires vertical ladder):
Input parameter setting
γrun in a loopThe] node needs to be configured with two core input parameters:
-
Array (required): Receives an input of type array, which can be:
- String array (
Array<string>
) - Array of numbers (
Array<number>
) - Boolean arrays (
Array<boolean>
) - Array of objects (
Array<object>
)
- String array (
-
Loop Body (required): Define the node flow that needs to be executed for each loop to contain:
- Loop body start: Marks the start of the loop.
- End of loop body: Marks the end of the loop and optionally outputs the result variable.
Loop Body Configuration
-
Inside the loop body, any type of node can be added, such as:
- AI Dialogue Node
- HTTP request node
- Content extraction node
- Text processing nodes, etc.
-
The loop body ends the node configuration:
- Select the variables to be output via the drop-down menu
- This variable will be collected as the result of the current loop
- The results of all loops will form a new array as the final output
π Practical example: batch processing text
Let's start with a simple example: batch processing text.
Suppose we have an array containing multiple texts and need to perform AI processing on each text. This is the most basic and common application scenario for loop run nodes.
Implementation steps
-
Prepare the input array
Use the [Code Run] node to create a test array:
const texts = [ "This is the first text", "This is the second text", "This is the third text" ]; return { textArray: texts }; return { textArray: texts }; } return { textArray: texts };
-
Configure the loop run node
- Array Inputs: Select the output variables of the node where the code ran in the previous step
textArray
γ - Add an [AI Dialog] node inside the loop to process each text. Here we enter the prompt as:
Please translate this text into English
γ - Add another [Specify Response] node for outputting the translated text.
- The end node of the loop body selects the output variable as the content of the AI response.
- Array Inputs: Select the output variables of the node where the code ran in the previous step
workflow
- [Code Run] node execution, generating test arrays
- [Loop Runs] Node receives array, starts traversal
- for each array element:
- [AI Conversation] Nodes handle the current element
- The [Designated Response] node outputs the translated text.
- [End of loop body] Node collects processing results
- After completing the processing of all elements, output the result array
π Advanced Application: Long Text Translation
When dealing with long text translations, we often encounter the following challenges:
- Text length exceeds LLM token limit
- Need for consistency in translation style
- Need to maintain contextual coherence
- Translation quality requires multiple rounds of optimization
γrun in a loopThe] node can be a good solution to these problems.
I've posted about long text translation before, but I didn't use the [Run in Loop] node for that at the time.
Today we'll see how to use the [Loop Run] node for long text translation.
In fact, the realization of the idea and the above example of batch processing of text is similar, only the processing logic inside the loop body is slightly more complex. Just need to slightly adjust the workflow of the previous article.
One sentence summary:Starting after the text cut, move all the later processes inside the [Loop Run] node.
Let's look at the flow inside the [Loop Run] node in more detail.
First, the array input selects the output variable from the previous step of generation text slicingchunks
γ
Then add a [Code Run] node to format the source text.
The formatting code is as follows:
function main({source_text_chunks, current_chunk}){
const i = source_text_chunks?.indexOf(current_chunk) ?? -1;
if (i === -1) {
throw new Error('The specified text block was not found');
}
const tagged_text = `${source_text_chunks.slice(0, i).join('')} <TRANSLATE_THIS>${current_chunk}</TRANSLATE_THIS>${source_text_chunks.slice(i + 1).join('')}`;
return {
tagged_text,
chunk_to_translate: current_chunk,
}
}
It will eventually output two variables, wheretagged_text
contains the entire text, whilechunk_to_translate
Only blocks of text that need to be translated for this round are included.
Next add a [Search Thesaurus] node to use the thesaurus of proper nouns as a knowledge base to be searched before translation.
Remember to turn on question optimization and add conversation background descriptions!
Then add an [AI Conversation] node that uses the CoT thought chain to allow the LLM to explicitly and systematically generate a chain of reasoning that demonstrates the complete thought process of the translator.
Now we finally move on to translation, where we use CoT thought chains to allow LLM to explicitly and systematically generate chains of reasoning that demonstrate the complete thought process of translation.
Here the AI will do several rounds of translation, but we only need the final translation result, so we also need to continue to access the [Code Run] node to extract the translation result of the last round.
Here, a complete long text translation loop on the configuration is basically complete. But some models in the output of Chinese content, will be interspersed with English punctuation, so just in case, we can add a [Code Run] node to format the output content.
Finally add a [Specify Reply] node to output the translated text.
Finally, we just need to set the output variable of the end-of-loop node to the output variable of the [Formatted Chinese Translation] nodetext
, and set the output variable of the loop run node to an array of final translation results.
Let's take a look at the running results below.
Demonstration
Let's start by importing a thesaurus.
Download link:/
Importing is as simple as creating a new generic knowledge base in FastGPT:
Then import the table dataset:
Then upload your csv dataset, go all the way to the next step, and you end up with a processed thesaurus.
Tap in to see the details:
Once the import is complete, the thesaurus can be selected in the workflow.
Finally, let's test the translation:
Click on the paperclip icon on the left side of the chat box to upload an attachment, then select the document you need to upload.
Test document address:/
Once you have uploaded your document, click the Send button on the right to start the translation.
As with the workflow in the previous article, the consistency of terminology translation is maintained perfectly:
Complete workflow:/s/019132869eca
summarize
This article introduces the features and usage scenarios of FastGPT Loop Run Node, and shows how to use the Loop Run Node through two concrete cases.
Through these two cases, we can see the power of cyclic run nodes in dealing with tasks that need to be executed repeatedly. It can effectively organize and manage complex workflows, and is a very useful functional module in FastGPT.