AI Personal Learning
and practical guidance

Dify comes with a long story generator workflow

This article briefly introduces the practice of Dify's own long story generator workflow, especially the flexible combination of iteration nodes and code nodes, which can make Dify workflow like a tiger. When the workflow execution is over, you can view the log -> trace, for each node in the workflow execution for detailed view and debugging.

-1


 

I. Start (start node)

Starting the node is relatively simple and consists of inputting the title and outline of the story article. The detailed inputs and outputs are as follows:

1. Input
{
"article_title": "Five Mind-Enlightening Stories by Zhuangzi Cured My Spiritual Exhaustion",
"article_outline": "Article outline: five stories on evaluation, gain and loss, dilemmas, choices and mindfulness",
"sys.query": "Story Generation",
"sys.files": [],
"sys.conversation_id": "5e874989-ea79-4de0-b6bb-2cca6a8ef6fc",
"sys.user_id": "4bc1624f-773d-4a39-a0c6-52008a13320e"
}
2. Output
{
"article_title": "Five Mind-Enlightening Stories by Zhuangzi Cured My Spiritual Exhaustion",
"article_outline": "Article outline: five stories on evaluation, gain and loss, dilemmas, choices and mindfulness",
"sys.query": "Story Generation",
"sys.files": [],
"sys.conversation_id": "5e874989-ea79-4de0-b6bb-2cca6a8ef6fc",
"sys.user_id": "4bc1624f-773d-4a39-a0c6-52008a13320e"
}

 

II. Generating subheadings and outlines (LLM nodes)

The function of this node is to expand section (understandable as first level headings) into bullets (understandable as second level headings) based on article_title (article title) and article_outline (article outline). The focus is on getting the prompt words right and giving a sample (One-Shot Learning). The sections involving cue words usually require fine-tuning. The node section setup information is shown below:

-2

1. Data processing
{
"model_mode": "chat",
"prompts": [
{
"role": "system", "text":"\n1.
"text":"\n1. Write a long article based on the article title and outline provided. Follow these steps to complete the task:\n2. use article_title as the title of the article. \n3. organize the article according to the article_outline provided. Each section of the outline should correspond to a section in the article. \n4. ensure that the article is informative and that each section contains detailed information, explanations, examples, and any other relevant content to fully cover the topic. \n5. ensure that transitions between sections are natural to maintain coherence. \n6. The output should not contain any XML tags. Provide only a JSON array of the following keys and values: \n "section\" (titles of the sections of the article), \n "bullets\" (summaries of each section of the article). \n\n\n
Impacts of Climate Change on Coastal Cities
\n
\n 1. Introduction\n 2. Sea Level Rise\n 3. Increase in Storm Frequency\n 4. Conclusion\n
\n\n\n [\n {\n \n \"section\": \"Introduction\",\n \"bullets\": \"1. Overview of the impacts of climate change on coastal cities 2. Importance of understanding these impacts\"\n },\n {\n \n \"section\": \"Sea Level Rise\". ",\n \"bullets\": \"1. causes of sea level rise 2. impacts on coastal infrastructure and communities 3. examples of affected cities\"\n },\n {\n {\n \"section\": \"Increased frequency of storms\",\n \"bullets\": \"1. link between climate change and storm frequency 2. impacts of more frequent and severe storms on coastal zones 3. impacts of recent storms on coastal zones 4. impacts of recent storms on coastal zones 5. impacts of recent storms on coastal zones 6. impacts of recent storms on coastal zones 7. impacts of recent storms on coastal zones 8. impacts of recent storms on coastal zones 9. Impacts of more frequent and severe storms on coastal areas 3. Case studies of recent storms\"\n },\n {\n \"section\": \n "conclusion\",\n \n \"bullets\": \n "1. summary of key points 2. urgency of addressing climate change 3. call to action for policy makers and communities\"\n }\n ]\n\n\n\n\n
Chuang Tzu's Five Mind-Enlightening Stories Cured My Spiritual Exhaustion
\n
Article_outline> Five Stories on Evaluation, Gains and Losses, Dilemmas, Choices, and Mindset \n\n", "files": [] } ] }
2. Output

The output is basically a big model to generate subheadings (section|understandable as first level headings) and outlines (bullets|understandable as second level headings), i.e., the content of the text field.

\ N (\ N (\ N (\ N))
"text": "[\n {\n \n \"section\": \"About Evaluation\",\n \n \"bullets\": \"1. Background: Zhuangzi and his disciples 2. Relativity of Evaluation: Value Judgment under different perspectives 3. Revelation: How to look at others' evaluation and keep inner peace\"\n },\n {\n \n \"section\": \" Gain and Loss\",\n \"bullets\": \" 1. storyline: Zhuangzi's conversation with a fisherman 2. change of concept of gain and loss: from materialistic to spiritual pursuits 3. revelation: learn to cherish the present moment and let go of obsessions\"\n },\n {\n \n \"section\": \""Difficulty\",\n \"bullets\": \" 1. storyline: Zhuangzi's thoughts during the Dilemma \"\",\n \"bullets\": \"1. Storyline: Zhuangzi's Dilemma 2. Growth and Reflection brought by Dilemma 3. Revelation: How to adjust your mindset and find solutions when facing difficulties \"\n },\n {\n \"section\": \"choices\",\n \"bullets\": \"1. Storyline: Zhuangzi's Dilemma 2. Wisdom and Courage in the process of decision making 3. Revelation: When making choices in life. Listen to your heart and move forward with courage \"\n },\n {\n \"section\": \"mindset\",\n \"bullets\": \"1. story summary: Zhuang Zi's Philosophy of Mindset 2. the importance of a positive mindset and its impact 3. revelation: cultivate a positive mindset in dealing with life's challenges to enhance well-being \"\n }\n]", "usage": {\n \"bullets\": \"1","\n {"section\": \"mindfulness": \"1.
"usage": {
"prompt_tokens": 515,
"prompt_unit_price": "0.15",
"prompt_price_unit": "0.000001",
"prompt_price": "0.0000773", "completion_tokens".



"completion_price": "0.0002088", "total_tokens".


"currency": "USD", "latency": 4.52
"latency": 4.521080200036522
}
}

 

III. Extracting subheadings and outlines (code nodes)

This node is a code node, i.e., it is passedPythonThe code way to extract the subtitle and outline isArray[Object]Type.

def main(arg1: str) -> dict.
import json
data = json.loads(arg1)
# Create an array of objects
result = [{'section': item["section"], 'bullets': item["bullets"]} for item in data]
return {
'result': result
}
1. Input
\ N (\ N (\ N (\ N))
"arg1": "[\n {\n \n \n "section\": \"About Evaluation\",\n \n \n "bullets\": \"1. story background: Zhuang Zi and his disciples 2. the relativity of evaluation: value judgment under different perspectives 3. revelation: how to look at others' evaluation and maintain inner peace\"\n },\n {\n \n "section\": \" Gain and Loss\",\n \"bullets\": \" 1. storyline: Zhuangzi's conversation with a fisherman 2. change of concept of gain and loss: from materialistic to spiritual pursuits 3. revelation: learn to cherish the present moment and let go of obsessions\"\n },\n {\n \n \"section\": \""Difficulty\",\n \"bullets\": \" 1. storyline: Zhuangzi's thoughts during the Dilemma \"\",\n \"bullets\": \"1. Storyline: Zhuangzi's Dilemma 2. Growth and Reflection brought by Dilemma 3. Revelation: How to adjust your mindset and find solutions when facing difficulties \"\n },\n {\n \"section\": \"choices\",\n \"bullets\": \"1. Storyline: Zhuangzi's Dilemma 2. Wisdom and Courage in Decision Making 3. Revelation: When making choices in life. Listen to your heart and move forward with courage\"\n },\n {\n \n \"section\": \"mindset\",\n \"bullets\": \"1. Story summary: Zhuangzi's philosophy of mindset 2. Importance of a positive mindset and its impact 3. Revelation: Cultivate a positive mindset to cope with life's challenges and enhance your sense of well-being\"\n }\n]"
}
2. Output
{
"result": [
{
"section": "About Evaluation".
"bullets": "1. story background: Zhuangzi and his disciples 2. the relativity of evaluation: value judgment under different perspectives 3. revelation: how to look at the evaluation of others and keep inner peace"
}
{
"section": "Gains and Losses", "bullets": "1.
"bullets": "1. Story content: A conversation between Zhuangzi and a fisherman 2. The change of the concept of gain and loss: from material to spiritual pursuits 3. Revelation: Learning to cherish the present moment and let go of attachment"
}, {
{
"section": "Dilemma", "bullets": "1.
"bullets": "1. storyline: Zhuangzi's thoughts in the midst of a dilemma 2. growth and reflection brought by the dilemma 3. revelation: how to adjust the mindset and find solutions when facing difficulties"
},.
{
"section": "Choices".
"bullets": "1. story description: Zhuangzi's dilemma 2. wisdom and courage in the decision-making process 3. revelation: when making choices in life, listen to your heart and move forward with courage"
}
{
"section": "Mindset".
"bullets": "1. story summary: Zhuangzi's philosophy of mindfulness 2. the importance of a positive mindset and its influence 3. revelation: cultivate a positive mindset to cope with life's challenges and enhance happiness"
}
]
}

 

IV. Iteration (Iteration nodes + LLM nodes)

An iteration node can be understood as a loop, and the contents of the loop are the operations in the iteration node.

-3

The point is still to write a good prompt that tells the title of the article and a complete outline for the LLM to write a section.

-4

Since there are 5 first level headings, 5 iterations are required to generate the corresponding chapter content.

-5

1. Input

Here are the inputs for the entire iteration node, i.e., the subtitle and outline:

{
"iterator_selector": [
{
"section": "About Evaluation".
"bullets": "1. story background: Zhuangzi and his disciples 2. the relativity of evaluation: value judgment under different perspectives 3. revelation: how to look at other people's evaluation and keep inner peace"
}
{
"section": "Gains and Losses", "bullets": "1.
"bullets": "1. Story content: A conversation between Zhuangzi and a fisherman 2. The change of the concept of gain and loss: from material to spiritual pursuits 3. Revelation: Learning to cherish the present moment and let go of attachment"
}, {
{
"section": "Dilemma", "bullets": "1.
"bullets": "1. storyline: Zhuangzi's thoughts in the midst of a dilemma 2. growth and reflection brought by the dilemma 3. revelation: how to adjust the mindset and find solutions when facing difficulties"
},.
{
"section": "Choices".
"bullets": "1. story description: Zhuangzi's dilemma 2. wisdom and courage in the decision-making process 3. revelation: when making choices in life, listen to your heart and move forward with courage"
}
{
"section": "Mindset".
"bullets": "1. story summary: Zhuangzi's philosophy of mindfulness 2. the importance of a positive mindset and its influence 3. revelation: cultivate a positive mindset to cope with life's challenges and enhance happiness"
}
]
}
2. Output

Below is the output of the entire iteration node, generating the appropriate chapter content based on the subtitle and outline.

{
"output": [
"### About Evaluation\n\n In the philosophical world of Zhuangzi, evaluation is not only a judgment of things, but also a relative existence. Stories between Zhuangzi and his disciples often reveal this. In a casual conversation, the disciple asked Zhuangzi for advice, "Teacher, how do you think a person's value is judged?" With a slight smile, Zhuangzi replied, "Just like a butterfly, some people find it beautiful, while others find it ordinary. Everyone has a different vision and naturally comes to different conclusions." \n\nThis backstory provides us with an important perspective: evaluation is relative. Both perceptions of others and self-perceptions are influenced by multiple factors such as personal experiences, cultural backgrounds and emotional states. Therefore, when facing external evaluations, we should realize that these voices do not necessarily reflect the true self, but are merely an interpretation from the perspective of others. \n\\nFurthermore, this relativity triggers our thinking about how to view others' evaluations. Zhuangzi once said, "Where the Tao exists, the heart is at peace." This quote reminds us that it is especially important to maintain inner peace in a complicated society. When we encounter criticism or praise, it is good to stop and think: are these comments truly in line with our own values? Or, are they merely a projection of others' emotions? Through such reflection, we can understand ourselves better and thus not be swayed by the outside world. \n\n Thus, from the wisdom of Zhuangzi, we gain a revelation: in life, no matter what kind of evaluations we face, we have to keep our inner peace and firmness. Only in this way can we find our own direction in the tumultuous journey of life without being confused by the voices of others. This not only helps us grow spiritually, but also enables us to navigate through complex relationships and live an authentic and fulfilling life." ,
"### Gain and Loss\n\n In the philosophical world of Zhuangzi, the idea of gain and loss is often revisited and deconstructed. A famous story recounts a conversation between Zhuangzi and a fisherman, a conversation that not only reveals the profound relationship between matter and spirit, but also provides us with important insights about how to view gain and loss. \n\nThe story takes place on a quiet morning when Zhuangzi strolls along the river and by chance meets a fisherman who is fishing. The fisherman was smiling and leisurely throwing his nets into the water, seemingly in no hurry to catch any fish. He told Zhuangzi in simple and plain words, "I only need to catch a few fish a day to satisfy my life. I don't seek more, for that would wear me out." \n\n Zhuangzi listened and pondered: what this fisherman possessed was not only the fish in his hands, but also a state of mind that transcends material things. He understood that true abundance does not come from the accumulation of the outside world, but the inner cherishing and fulfillment of the present state of life. This attitude of calmness and indifference and contentment has enabled him to find happiness in the ordinary. \n\n As the conversation deepens, Zhuangzi asks the fisherman a question, "If you were able to catch more fish, would you feel happier?" The fisherman shook his head and replied, "I don't need more. Pursuing too much will only cause me to lose what I have now, and I would rather enjoy the tranquility." These words touched Zhuangzi deeply, and he realized that in modern society, people are often driven by material desires and neglect the pursuit of the spiritual dimension. \n\n Through this exchange, Zhuangzi's concept of gain and loss was also transformed. He began to reflect on his once relentless pursuit of success and wealth, and gradually realized that the most important thing in life is to cherish the present moment and let go of obsessions. No matter how the external environment changes, we should maintain inner peace and fulfillment. Just like the wisdom shown by the fisherman, what is really worth pursuing is a deep inner peace and ease. \n\nThe inspiration that this story gives us is far-reaching and lasting. In the fast-paced, high-stress modern life, we often get lost in the relentless pursuit of material gain. However, by learning from the fisherman's simple and pure attitude towards life, we can learn to cherish what is in front of us, thus alleviating our mental burden and realizing self-healing. Finding joy in every moment and filling life with meaning is true wisdom in life." ,
"### Dilemma\n\nThere was a time in Zhuangzi's life when he faced a great dilemma. It was a stormy night when Zhuang Zi sat alone on the top of a mountain, surrounded by silence, with only the sound of the wind and the raindrops pounding on the rocks. His mind was filled with confusion and uneasiness: as a philosopher, he often pondered the meaning of life, but at the moment he was plagued by the pressure of reality. His friends' expectations of him, society's judgment of him, and his own pursuit of an ideal life all weighed heavily on him. \n\n In such a predicament, Zhuangzi began to reflect on his own situation. He realized that while these external factors put pressure on him, they also prompted him to think deeply about what his heart truly desired. He recalled what he had once taught his disciples, "The Tao begets one, one begets two, two begets three, and three begets all things." This statement reminded him that no matter how complex a problem is, it can actually be reduced to simple choices and understandings. So he decided not to be troubled anymore, but to find a way out of this storm. \n\n Through deep introspection and contemplation, Zhuangzi gradually realized that the predicament was not a desperate situation, but an opportunity for growth. He began to try to adjust his mindset and turn his attention from the outside world to the inside. He realized that in the face of difficulties, one should not focus only on the problem itself, but should look for a solution. Just like the rough waves in the ocean, there will always be a moment of calm, and with patience, you can find your way. \n\nThis process not only allowed Zhuangzi to re-examine his values, but also made him more determined to pursue the importance of inner peace. After this spiritual baptism, he finally found a way to cope with his predicament: to remain calm, to face it positively, and to believe that every challenge is a step towards higher wisdom. \n\nThis story tells us that when we are in a difficult situation, we might as well do as Zhuangzi did and reflect and adjust from within. In the face of difficulties, we can choose to let go of our obsessions and explore solutions with an open mind. This will not only help us get out of the haze of the moment, but also give us a stronger inner strength in the future journey of life." ,
"### Choice\n\n In the philosophical world of Zhuangzi, choice is often a thought-provoking topic. The story takes place on a sunny morning when Zhuangzi and his disciples are strolling along a mountain trail, enjoying the peace and beauty of nature. Suddenly, they came across a river with crystal clear water, but it was flowing very fast. On each bank of the river, there was a path leading to a bustling marketplace on one side and leading to a secluded mountain forest on the other. The \n\n disciple was faced with a dilemma: he could choose to go to the bazaar and pursue material wealth and fame; or he could choose to enter the mountains and forests and pursue inner peace and self-cultivation. Faced with this choice, he was overwhelmed with confusion and did not know what to decide. \n\n Zhuangzi saw the disciple's hesitation and began to guide him in his thinking. He told his disciple, "Life is like this river, sometimes we have to make choices, and each choice has its own unique meaning." Zhuangzi went on to share his wisdom that there is no absolute right or wrong in pursuing either the material or the spiritual. The key lies in whether or not we are able to listen to our true inner voice and take that step with courage. \n\n After much deliberation, the disciple finally realizes that what he truly desires is inner peace, not the hustle and bustle of the outside world. He decided to give up the temptation of traveling to the bazaar and instead headed for the secluded mountain forest. There, he feels the peace and tranquility that nature brings, and finds the opportunity for self-reflection and growth. \n\nThe lesson of this story is that when making choices in life, we are often faced with a variety of temptations and pressures. However, what really matters is to listen to your heart and not be swayed by the outside world. To bravely pursue that direction in life that makes us feel fulfilled and happy is the philosophy of life that we should strive to practice. \n\n Through the wisdom of Zhuangzi, we have come to understand that every choice is a journey of self-discovery, and that if we are firm in our beliefs and brave enough to move forward, we will be able to find our own path. This courage not only helps us to overcome our immediate predicament, but also heals our spiritual exhaustion and gives life a new luster." ,
"### Mindfulness \n\n Zhuangzi's philosophy of mindfulness is an integral part of his system of thought, and he conveys a deep understanding of life through vivid stories. In Zhuangzi's writings, one can often see the importance he placed on mindfulness. He believed that the state of mind not only affects one's feelings and experiences, but also largely determines how one faces the various challenges in life. \n\n#### Story Summary: Zhuangzi's Philosophy of Mindfulness \n\n Once, Zhuangzi and his disciples were walking along the river, when they saw a small bird chirping happily on a branch. The disciples marveled at the bird's freedom and happiness, but Zhuangzi smiled slightly and said, "Have you ever thought that this little bird does not worry about tomorrow or regret the past; it just lives in the present and enjoys the moment." This simple story made the disciples realize that true happiness stems from an uninhibited, go-with-the-flow mentality. Zhuangzi's wisdom lies in the fact that he teaches people to let go of their uneasiness about the future and their attachment to the past, and to face life with a calm and open mind. \n\n#### The Importance of a Positive Mindset and Its Influence \n\n The importance of a positive mindset is manifested in several ways. First, it can help us cope with stress and difficulties better. When we look at problems in a positive light, we will find that solutions are often easier to find than we thought. Second, a positive mindset can also enhance our sense of well-being and fulfillment. Studies have shown that those with a positive mindset are usually more likely to feel good about their lives and are more effective at building good relationships. In addition, a positive mindset is also contagious, as it inspires those around us to work together for the better, making the whole environment more harmonious. \n\n#### Revelation: Cultivating a Positive Mindset to Cope with Life's Challenges and Enhance Happiness \n\nFrom Zhuangzi's story, we can get the revelation that cultivating a positive mindset is an important strategy to cope with life's challenges. There are some simple ways we can enhance our positive thinking in our daily lives. For example, it is worthwhile to spend a few minutes each day reflecting on the good things we have, whether it is the company of our family, the support of our friends or the achievements of our work. At the same time, when faced with difficulties, try to see problems as opportunities for growth rather than obstacles. This shift not only reduces the psychological burden, but also allows us to find the motivation to move forward in the face of adversity. \n\n In short, by studying Zhuangzi's philosophy of mindfulness, we are able to gain a deeper understanding of how to adjust our inner state in order to face the storms of life with greater equanimity, thereby enhancing our overall sense of well-being. In the process, let's work together to cultivate an optimistic, open and tolerant mindset so that the flower of life can blossom."
]
}

 

V. Codes (code nodes)

The input is a list and the output is the elements of the list spliced by "\n":

def main(articleSections: list): data = articleSections: list
data = articleSections
return {
"result": "\n".join(data)
}

 

VI. Answers (direct response nodes)

The output of the code is the input of the answer, just output it directly.

 

bibliography

[1] Iteration node: https://docs.dify.ai/v/zh-hans/guides/workflow/node/iteration

AI Easy Learning

The layman's guide to getting started with AI

Help you learn how to utilize AI tools at a low cost and from a zero base.AI, like office software, is an essential skill for everyone. Mastering AI will give you an edge in your job search and half the effort in your future work and studies.

View Details>
May not be reproduced without permission:Chief AI Sharing Circle " Dify comes with a long story generator workflow

Chief AI Sharing Circle

Chief AI Sharing Circle specializes in AI learning, providing comprehensive AI learning content, AI tools and hands-on guidance. Our goal is to help users master AI technology and explore the unlimited potential of AI together through high-quality content and practical experience sharing. Whether you are an AI beginner or a senior expert, this is the ideal place for you to gain knowledge, improve your skills and realize innovation.

Contact Us
en_USEnglish