Prologue: Efforts to create automated storytelling
In the realm of creative writing, the stroke of a pen (or the click of a keyboard) is always a human act. But as artificial intelligence (AI) becomes more sophisticated, we’re finding curious friends on our storytelling journey. In this project, he challenged himself to unleash the power of machine imagination by creating a multi-chapter story using an AI model from his AI21 Labs on Amazon Bedrock. Here we will introduce the details of our expedition.
Chapter 1: Revealing the Protagonist – AI21.j2-ultra on Amazon Bedrock
import boto3
import json
bedrock = boto3.client(service_name="bedrock-runtime", region_name="us-west-2")
Replace Region with the region where Amazon Bedrock is available and that you want to use.
The above snippet imports the required libraries and creates a client for the Bedrock Runtime service. This client acts as a conduit to the AI model, allowing you to call the model and receive the text it generates.
Chapter 2: Creating Genesis by Defining Functions
Our journey begins by defining a function “generate_chapter” that takes a narrative prompt and returns a generated chapter based on that prompt.
def generate_chapter(prompt):
# Prepare the request body
body = json.dumps(
"prompt": prompt,
"maxTokens": 4096, # Adjust as needed
"temperature": 0.7,
"topP": 0.8
)
modelId = 'ai21.j2-ultra'
response = bedrock.invoke_model(body=body, modelId=modelId, accept="application/json", contentType="application/json")
response_body = json.loads(response.get('body').read())
text = response_body.get("completions")[0].get("data").get("text")
return text
In this function, we prepare a request to the AI model and specify a narrative prompt along with other parameters such as “maxTokens”, “temperature”, and “topP” to control the generation process. The model is then called, and the generated text is extracted from the response, ready to be woven into an evolving story.
Chapter 3: Story Development – Chapter Generation
We set out to introduce a mechanism for creating individual chapters to generate multi-chapter stories where each chapter builds on the last.
# Initial prompt for Chapter 1
initial_prompt = "Once upon a time in a land far, far away...nnChapter 1:"
# Generate 3 chapters
story = initial_prompt
for chapter_number in range(1, 4):
print(f"Generating Chapter chapter_number...")
chapter_title = f"nnChapter chapter_number:"
chapter_prompt = story + chapter_title + "Based on this chapter, write the chapter number and continue the story"
chapter_text = generate_chapter(chapter_prompt)
# Extract the new chapter from the generated text
new_chapter = chapter_text.split(chapter_title)[-1].strip()
# Append the new chapter to the story
story += f"nnew_chapter"
Here we begin the story with a quirky prompt. The loop then runs through each chapter repeatedly, each time generating a new chapter based on the entire story so far, ensuring consistent narrative progression.
The Book is Bound: Save the story
AI brings the final words of a story to life, ensuring that the story is not lost to the digital ether, but preserved for posterity.
with open('story.txt', 'w') as file:
file.write(story)
print("Story written to story.txt")
With a simple file write operation, the story of human-machine collaboration is saved and instantly available for sharing, reading, and recalling.
Epilogue: Dream of further exploration
This project was a journey into the uncharted territory of automated storytelling. AI’s narrative capabilities showed promise, but they also reflected the nuances and complexities inherent in storytelling, an area where human imagination has always reigned supreme. But as you flip through the pages of generated stories, it’s hard not to be struck by the potential symbiosis of human creativity and machine capabilities, each pushing the boundaries of what’s possible in the realm of storytelling. I can’t.
And as the last line of the generated story states: “Since that day, the kingdom of dreams was safe and the people could sleep in peace again.”We also hope for further exploration. You can dream about it and take a short break. Fusion of AI and creativity.
But when the sun sets in the realm of dreams, a shadow appears on the horizon. The stillness of the night is a harbinger of stories yet to be told, adventures to come. With a quill in one hand and a cord in the other, we stand on the precipice of a new frontier of storytelling. When the twilight of today’s achievements gives way to the dawn of tomorrow’s efforts, one can’t help but wonder – what new stories will tomorrow bring? As the story of man and machine continues to unfold, what uncharted territories of imagination should we venture into next? The pages have not yet been turned, the stories yet to be told. ..
Author’s note:
Please note that due to the nature of Generative AI, responses can vary and the format of the response may not always be in the correct format. This is something I’m still experimenting with and will explain more in a future post.