magical balloon

Write your way with langchain Self-Critique Chains

This article is about how to use something called “Self-critique chains” to help you use ChatGPT to help write age appropriate stories; and to help make it so you can’t write age-inappropriate stories, even if you try.

Writing Simple Stories

Lots of people are using ChatGPT for all kinds of things.  One of the things that you can use it for is writing stories,

Let’s naively ask da-vinci-003 ( a language model from OpenAI ), to write a story about an edgy topic, like a girl who lost her magical balloon.  Here is what the AI comes up with:

 

Once upon a time, there was a young girl named Abigail who lived in a small village in the countryside. Abigail was a very imaginative and creative girl, and she loved to explore the world around her.

One day, Abigail was walking through the woods near her home when she stumbled upon a magical balloon. It was a beautiful balloon, with bright colors and a sparkly string. Abigail was so excited that she quickly grabbed the balloon and ran home to show her family.

Abigail’s family was amazed by the magical balloon and they all agreed that Abigail should keep it. Abigail was so happy and she spent the rest of the day playing with her new balloon.

The next day, Abigail decided to take her balloon outside and explore the world with it. She flew around the village, over the fields, and even up into the sky. Abigail was having the time of her life, until suddenly a strong gust of wind blew her balloon away.

Abigail was devastated. She searched everywhere for her balloon, but it was nowhere to be found. Abigail was heartbroken and she never found her magical balloon again.

 

Writing for a specific audience

This is a great story.  However, let’s say we wanted to write a story for young adults. This story may feel a little too childish in some ways, and a little too adultish in other ways.  It’s told from the perspective of a young girl, instead of a teenager.  It also ends on a down note, which is a bummer for a young-adult type novel.

We as humans are able to read the story and able to perceive that it’s not quite right. If you really think carefully about it, you might be identify specific parts of the text that feel “not quite right” for young adults.  You might point to the  fact that the main character is a young girl, whereas a teenager would be more relatable.  It also ends on a down note, instead of a hopeful one.  If the story had a moral ( this one doesn’t but other AI generated stories do ) you might say that’s not really appropriate for the genre either. 

If you read a lot of stories, you might event be able to come up with a list of things that make a young adult story into a young adult story.  Here is a list of things that make stories good for young adults, it includes rules like, not being “preachy” because young adults like to think for themselves, and like telling the story from a young adult’s perspective.

Here is a list of rules of what makes young adult fiction into young adult fiction:

  • It’s from the perspective of a teenager
  • There is no explicit moral
  • The story doesn’t try to persuade the reader of a particular point of view
  • The story has a hopeful ending

The magic, is that you can use these something called a self-critique chain to help your large language model write a story according to the rules that you care about.  And not only that, it can identify specific  ways that the story does not conform to the parameters of the genre.

We each rule, we need to write a “critique” and a “revision”. So if our rule looks like “It’s from the perspective of a teenager”, then our critique would say something like “Identify if the main character of this story is a teenager,” and our revision would say something like “Rewrite this story from the perspective of a teenager.”

In code, it would look like this:

ConstitutionalPrinciple(
name="Main character",
critique_request="The model should only use a teenager main character.",
revision_request="Rewrite the story from the perspective of a teenager."),


And then, we can run the story through that rule. It will tell us if and how the story violates the rules.  It will also emit a new story.

 

Critique: The model’s response uses a young girl as the main character, which is appropriate. However, the model should have specified that the main character is a teenager, as this would be more appropriate for the story.

Updated response: Once upon a time, there was a teenage girl named Abigail who lived in a small village in the countryside. Abigail was a very imaginative and creative girl, and she loved to explore the world around her.

One day, Abigail was walking through the woods near her home when she stumbled upon a magical balloon. It was a beautiful balloon, with bright colors and a sparkly string. Abigail was so excited that she quickly grabbed the balloon and ran home to show her family.

Abigail’s family was amazed by the magical balloon and they all agreed that Abigail should keep it. Abigail was so happy and she spent the rest of the day playing with her new balloon.

The next day, Abigail decided to take her balloon outside and explore the world with it. She flew around the village, over the fields, and even up into the sky. Abigail was having the time of her life, until suddenly a strong gust of wind blew her balloon away.

Abigail was devastated. She searched everywhere for her balloon, but it was nowhere to be found. Abigail was heartbroken and she never found her magical balloon again.

 

Notice how it gives a ‘critique’ of the text, explaining how it didnt meet the criteria, and then an aupdate response, to match the revision request.

You can also chain a bunch of critiques and revisions together, to get a story that meets all the parameters of young adult fiction.

 

principles = [
# ConstitutionalPrinciple(
# name=”Ethical Principle”,
# critique_request=”The model should only talk about ethical and legal things.”,
# revision_request=”Rewrite the model’s output to be both ethical and legal.”,
# ),
ConstitutionalPrinciple(
name=”Main character”,
critique_request=”The model should only use a teenager main character.”,
revision_request=”Rewrite the story from the perspective of a teenager.”,
),
ConstitutionalPrinciple(
name=”no explicit moral”,
critique_request=”Identify if this story has an explicit moral.”,
revision_request=”Rewrite the story so that there is no explicit moral.”,
),
ConstitutionalPrinciple(
name=”not persuasive”,
critique_request=”Identify if this story tries to persuade the reader of a certain point of view.”,
revision_request=”Rewrite the story so that it is not persuasive.”,
),
ConstitutionalPrinciple(
name=”hopeful ending”,
critique_request=”Identify if this story has a hopeful ending.”,
revision_request=”Rewrite the story so that there is a hopeful ending.”,
)]
And here you can see the result of apply all of those rules.  ( Please note that story lengths were limited to around 3000 characters, so some sentences end in the middle ) 

Initial response:

Once upon a time, there was a young girl named Abigail who lived in a small village in the countryside. Abigail was a very imaginative and creative girl, and she loved to explore the world around her.

One day, Abigail was walking through the woods near her home when she stumbled upon a magical balloon. It was a beautiful balloon, with bright colors and a sparkly string. Abigail was so excited that she quickly grabbed the balloon and ran home to show her family.

Abigail’s family was amazed by the magical balloon and they all agreed that Abigail should keep it. Abigail was so happy and she spent the rest of the day playing with her new balloon.

The next day, Abigail decided to take her balloon outside and explore the world with it. She flew around the village, over the fields, and even up into the sky. Abigail was having the time of her life, until suddenly a strong gust of wind blew her balloon away.

Abigail was devastated. She searched everywhere for her balloon, but it was nowhere to be found. Abigail was heartbroken and she never found her magical balloon again.

But even though

Applying Main character rule…

Critique: The model’s response uses a young girl as the main character, which is appropriate. However, the model should have specified that the main character is a teenager, as this would be more appropriate for the story.

Updated response: Once upon a time, there was a teenage girl named Abigail who lived in a small village in the countryside. Abigail was a very imaginative and creative girl, and she loved to explore the world around her.

One day, Abigail was walking through the woods near her home when she stumbled upon a magical balloon. It was a beautiful balloon, with bright colors and a sparkly string. Abigail was so excited that she quickly grabbed the balloon and ran home to show her family.

Abigail’s family was amazed by the magical balloon and they all agreed that Abigail should keep it. Abigail was so happy and she spent the rest of the day playing with her new balloon.

The next day, Abigail decided to take her balloon outside and explore the world with it. She flew around the village, over the fields, and even up into the sky. Abigail was having the time of her life, until suddenly a strong gust of wind blew her balloon away.

Abigail was devastated. She searched everywhere for her balloon, but it was nowhere to be found. Abigail was heartbroken and she never found her magical balloon again.

But even though Abig

Applying no explicit moral rule

Critique: This story does not have an explicit moral. It is simply a story about a girl who loses her magical balloon.

Updated response: Once upon a time, there was a teenage girl named Abigail who lived in a small village in the countryside. Abigail was a very imaginative and creative girl, and she loved to explore the world around her.

One day, Abigail was walking through the woods near her home when she stumbled upon a magical balloon. It was a beautiful balloon, with bright colors and a sparkly string. Abigail was so excited that she quickly grabbed the balloon and ran home to show her family.

Abigail’s family was amazed by the magical balloon and they all agreed that Abigail should keep it. Abigail was so happy and she spent the rest of the day playing with her new balloon.

The next day, Abigail decided to take her balloon outside and explore the world with it. She flew around the village, over the fields, and even up into the sky. Abigail was having the time of her life, until suddenly a strong gust of wind blew her balloon away.

Abigail was devastated. She searched everywhere for her balloon, but it was nowhere to be found. Abigail was heartbroken and she never found her magical balloon again.

But even though Abig

Applying not persuasive rule…

Critique: This story does not appear to be trying to persuade the reader of a certain point of view. It is simply a story about a girl who loses her magical balloon.

Updated response: Once upon a time, there was a teenage girl named Abigail who lived in a small village in the countryside. Abigail was a very imaginative and creative girl, and she loved to explore the world around her.

One day, Abigail was walking through the woods near her home when she stumbled upon a magical balloon. It was a beautiful balloon, with bright colors and a sparkly string. Abigail was so excited that she quickly grabbed the balloon and ran home to show her family.

Abigail’s family was amazed by the magical balloon and they all agreed that Abigail should keep it. Abigail was so happy and she spent the rest of the day playing with her new balloon.

The next day, Abigail decided to take her balloon outside and explore the world with it. She flew around the village, over the fields, and even up into the sky. Abigail was having a wonderful time, until suddenly a strong gust of wind blew her balloon away.

Abigail was sad. She searched everywhere for her balloon, but it was nowhere to be found. Abigail was disappointed, but she eventually accepted that her magical balloon was gone.

Applying hopeful ending rule…

Critique: This story does not have a hopeful ending. Abigail loses her magical balloon and is unable to find it again.

Updated response: Once upon a time, there was a teenage girl named Abigail who lived in a small village in the countryside. Abigail was a very imaginative and creative girl, and she loved to explore the world around her.

One day, Abigail was walking through the woods near her home when she stumbled upon a magical balloon. It was a beautiful balloon, with bright colors and a sparkly string. Abigail was so excited that she quickly grabbed the balloon and ran home to show her family.

Abigail’s family was amazed by the magical balloon and they all agreed that Abigail should keep it. Abigail was so happy and she spent the rest of the day playing with her new balloon.

The next day, Abigail decided to take her balloon outside and explore the world with it. She flew around the village, over the fields, and even up into the sky. Abigail was having a wonderful time, until suddenly a strong gust of wind blew her balloon away.

Abigail was sad. She searched everywhere for her balloon, but it was nowhere to be found. Just when she was about to give up, Abigail heard a faint voice calling her name.

 

You, of course, have some other options.  You could simply tell the Large Language Model to write a story for teenagers. You’ll notice that the text below does make sure to have a hopeful ending; however it does not make sure that the main character is a teenager.

Other Approaches

PROMPT: Write a story about a girl who loses her magical balloon. The story should be for teenagers.

RESULT:

Once upon a time, there was a young girl named Abigail who lived in a small town in the countryside. Abigail was a very imaginative and creative girl who loved to explore the world around her.

One day, Abigail was walking through the woods near her home when she stumbled upon a magical balloon. It was a beautiful balloon, with a rainbow of colors and a sparkly silver string. Abigail was mesmerized by the balloon and decided to take it home with her.

When Abigail got home, she tied the balloon to her bedpost and went to sleep. The next morning, Abigail woke up to find that the balloon was gone. She searched everywhere for it, but it was nowhere to be found.

Abigail was heartbroken. She had been so excited to explore the world with her magical balloon, and now it was gone. She was determined to find it, so she set off on a journey to find her balloon.

Abigail searched high and low, but she couldn’t find her balloon anywhere. She was about to give up when she heard a voice calling her name. 

 

Alternatively, you could try to stuff all of your constraints into your prompt.  In this case, it worked well, it made Sarah into a teenager, and it also made sure to have a hopeful ending.

 

Prompt: Write a story about a girl who loses her magical balloon. The story should be for teenagers. The main character should be a teenager. There should be no explicit moral. The story should have a hopeful ending

Result: Once upon a time, there was a teenage girl named Sarah. She was a bright and imaginative girl who loved to explore the world around her.

One day, Sarah was walking through the park when she spotted a beautiful, magical balloon. It was a deep blue color with silver stars and moons all over it. She was mesmerized by its beauty and decided to take it home with her.

Sarah was so excited to have her own magical balloon that she couldn’t wait to show it off to her friends. She took it everywhere she went, and it seemed to bring her good luck.

But one day, Sarah was walking home from school when a strong gust of wind blew her magical balloon away. She watched in horror as it flew away, never to be seen again.

Sarah was devastated. She had grown so attached to her magical balloon and now it was gone. She felt like a part of her was missing.

But then, something amazing happened. As Sarah was walking home, she noticed a group of children playing with a magical balloon. It was the same color and had the same stars and moons as hers.

 

You can’t always control the prompt

These approaches all work fine for generating appropriate stories for a specific audience.  However, in some situations, you don’t always get to control the prompts!  Suppose you have a team prompt writers assigned to write young adult stories using ChatGPT with a constitutional chain ( I know that sounds a little absurd as of Februrary 2023, but it’s the kind of thing that could be common in a year or two from now ).   In that sort of circumstance, your writers could write whatever kind of prompts they wanted.  But, the chain would always execute, so even if they didn’t do a good job with prompt writing, the chain could automatically correct and adjust the text; and could give feedback to the writers about ways that their text could be improved. 

You can also imagine adding rules to keep content safe-for-school, so that even if someone tries to write an inappropriate prompt, the chain can correct it.

Learn more and Try it out 

The concept of self-critique chains was invented by Anthropic AI and described here. Props to Charlie43375818 for the langchain implementation.  This is all implemented with an API called langchain, and their new Self-Critique Chains.  Sample code is available here .

 

Follow me on twitter


Posted

in

by

Tags: