Modernizing an old folktale with Generative AI using Langchain and Streamlit - Part 1 of 3
LLM APPSGENERATIVE AIAI
9/12/20247 min read
Introduction
A peculiar interest of mine is around Children's books. I love reading them and have written one in the past and currently planning another one. The simplicity and yet the profoundness in meaning is very appealing to me. My favorites are those that have humor (e.g., "Don't let the pigeon drive the bus!, or "The day the crayons quit" - so creative!!). Those were big hits with my kids when they were younger and inspirational for a budding author (my daughter when she was 8!). And of course folktales which can be great way to learn about other distant lands! You can never no wrong with classics, or can you?
The Why (what problem are we solving):
To truly enjoy many folks tales, you have to set aside modern ideas - and that has never gelled well with me. Think about how many folktales have a "girl being rescued" or "a daughter being given away in marriage". When sharing these folktales with my kids, I used to modify them as many teachers/parents do enjoy them without the archaic ideas. Reading these tales as is, with the heavy stereotyping or objectification, to young impressionable minds does not serve us well if we truly aim to solve these issues in society. So, how can we enjoy these tales without the drama. Well - I do like drama, so, without the unwanted drama π. With the recent advances in Generative AI, this felt like a use case to me. So, what I'd like to do is to build an app where we can use LLMs to modify a folktale based on some criteria.
Who are we solving this for:
When my kids were in elementary school, I used to make up stories to teach my kids a special skill or even math. It really worked to have them correct a wrong through a story vs me telling them not to do it. The story I wrote came into being because of this (link to blog). Teachers are very skilled with this - I remember my kid's first grade teacher modifed the "The three little pigs" story to teach about natural resources and the use of resources to build things. How creative! I imagine parents, teachers (at school or at home for homeschoolers)and child caregivers (think day care centers or nannies) in general will find this LLM app useful.
How are we solving this:
In this Part 1, we're going to use Langchain to build a prototype for an LLM app to modify an example folktale, "The three little pigs", to make it inclusive and having a powerful uplifting message. The focus in this Part 1 will be more around the use of LLMs and Langchain to build the app. In a future Part 2, we will work on robustness, generalizing the app to work with any folktale that the user wishes to modernize, and add other features. In part 3 we will work on putting guard rails on the app and making it secure. We need to build a responsible AI app here as it will generate stories that will be read by kids either directly or being read to by an adult. We will not release this out wide till Part 3 is done, but we may seek quick intermediate feedback from some select end users to course correct as needed.


TLDR (the demo for Part 1)
The demo below shows the Streamlit app and the generation of a modernized three little pigs story.
Details - the longer story π
To keep the focus of the blog on the application of LLMs, I will do a bare bone introduction into core concepts, noting resources for the curious reader to learn more.
What are LLMs?
LLMs, or Large Language Models, are advanced artificial intelligence systems trained on vast amounts of text data. They can understand and generate human-like text, performing tasks such as answering questions, writing content, and assisting with various language-related activities. Examples of LLMs include GPT (Generative Pre-trained Transformer) models like the one we will use below in our app.
π To learn more, I recommend the 3-week in-depth course on Coursera from DeepLearning.AI in partnership with AWS. You don't need a heavy knowledge of AWS to appreciate the course and even if you don't do the lab exercises, you come away learning a lot about Generative AI and LLMs. The course creators and instructors have done a phenomenal job of explaining concepts in ways that many people coming from various technical backgrounds an understand. And it's FREE except if you want the certificate ($45). DeepLearning.AI has several introductory courses like AI for Everyone (~6 hours), and Generative AI for Everyone (~5 hours) which can be great options before attempting the more in-depth course mentioned above.
What is Langchain?
Langchain is an open-source framework designed to simplify the development of applications using large language models (LLMs). It provides a set of tools and components that help developers chain together different AI capabilities, integrate external data sources, and create more complex, context-aware AI applications. Langchain is particularly useful for building chatbots, question-answering systems, and other AI-powered tools that require sophisticated language processing and reasoning. This makes it a perfect choice for what we're trying to achieve here with our prototype.
π To learn more, I recommend taking DeepLearning.AI's LangChain for LLM Application Development short course (~3-4 hours) if you already have some basic knowledge of programming and Machine Learning concepts or once you've taken the above introductory courses. This is also FREE.
Which model to use?
As Meta released their latest open source model, Llama 3.1, on July 24, 2024, it is a great choice to build this LLM app to keep costs low. For the prototype in Part 1, we won't fuss on this too much and get stuck on the right choice just yet. We err on bias-for-action for the prototype and this is a 2-way door decision at this point. In my case, as I was already playing around with the now very popular Open AI's GPT models, I just went ahead with that, but you can use what you are comfortable with at this stage. In Part 2, we will replace with the open source Llama model and see how it performs. I use the GPT-4o mini model below which worked well for the prototype. The documentation states,
"GPT-4o mini enables a broad range of tasks with its low cost and latency, such as applications that chain or parallelize multiple model calls (e.g., calling multiple APIs), pass a large volume of context to the model (e.g., full code base or conversation history), or interact with customers through fast, real-time text responses (e.g., customer support chatbots)."
In our prototype, we are not currently require a large context window, but the fast, real-time text responses is an important feature. Otherwise, the users will bounce if they're waiting for several minutes to get the generated story. Note that you have to make a minimum payment of $5 to access the GPT-4, GPT-4o models via the OpenAI API.
The API key
API keys are unique identifiers used to authenticate and authorize access to an application programming interface (API). They serve as a form of digital credential, allowing developers or applications to make requests to a service or platform. API keys typically consist of a long string of letters and numbers, and they help control and track usage of an API, ensuring that only authorized users can access the service and often limiting the number or type of requests based on the user's subscription level or permissions. Open AI has introduced Project API keys and can be created here: https://platform.openai.com/api-keys. If you already have a key, you can access the same and use below. Remember to fund your OpenAI account with the $5 min (which is more than enough for us at this stage) before proceeding with next steps to save yourself from headaches of API returning an error.
Load the API key into the app
Prompting with GPT
Chaining with LangChain to invoke the LLM
Part 1 Conclusion
Stories are powerful tools to convey ideas and are to be used responsibly. In the next parts, we will explore putting guardrails and adding more functionality.
The final result
When we run this, the output is as below. Note that because of the random nature of Generative AI, each time we run this app, the story is different. Here is a sample run and the generated story. Some key ideas I liked in the story are
that it tried to find a positive strength in each of the characters.
the idea that you can help one another even if you disagree on approach
example of teamwork
sense of inclusiveness
Title: The Three Resilient Pigs
Once upon a time in a vibrant community, there lived three cheerful pigs named Penny, Percy, and Polly. Each pig was unique, with dreams of building their own homes that reflected their personalities and values.
Penny was creative and loved the arts. She decided to build her house out of brightly painted straw, making beautiful designs that brought smiles to everyone who passed by. Percy's practical nature led him to construct his house with sturdy sticks. He believed that simplicity and resilience were key. Polly, the youngest, was a thinker and an engineer. She chose to build her house with bricks, knowing it would withstand any challenge.
As they worked on their houses, the three pigs often helped one another, sharing tools and ideas. Their collaboration showed the strength of community and the joy of teamwork. One day, while they were enjoying a picnic together, they overheard that a clever wolf was creeping into their neighborhood, known for trying to take advantage of others.
Being cautious but confident, they decided to prepare for the unexpected. They discussed their strengths and how they could support one another. Penny suggested they create a plan together, using each of their unique skills. Percy offered to reinforce the community watch, while Polly volunteered to design a strong gate for their neighborhood.
The wolf soon arrived, but instead of just approaching the pigs individually, he found them united and working together. Penny's artistic charm, Percy's practical wisdom, and Polly's solid plans formed an unbreakable bond that caught the wolf off guard.
βWhy donβt you join us in our community?β Penny said, extending a hand. The wolf, surprised by their kindness and teamwork, hesitated. Intrigued by their strength in unity, he eventually confessed he felt lonely and disconnected. Instead of being threatened, the pigs invited him to share in their creativity, encourage his strengths, and learn from one another.
Over time, the wolf became a part of the community, using his cunning for good and finding joy in friendship. He helped the pigs improve their homes and even started a gardening club. Through their willingness to embrace diversity and practice inclusivity, the pigs and the wolf built a vibrant neighborhood, where everyone felt valued and respected.
In the end, the three resilient pigs learned that true strength comes from collaboration, understanding, and compassion. And they all lived happily ever after, uplifting one another along the way.
Moral of the Story: Together, we are stronger and can turn challenges into opportunities for connection and growth. Embracing diversity and practicing kindness can transform adversaries into allies.
In the theme of this blog, the above image was AI generated.