Short Story Generator with AWS Bedrock and Amplify

November 29, 2024

Recently, I got curious about how easy it is nowadays to build an AI-powered app. So, I decided to give it a shot and was blown away by how easy and fast it was.

Given some recent AWSome pre:Invent announcements, I saw that the Amplify team released an Amplify AI Kit for developers to build full-stack, AI-powered apps.

So, I followed the tutorial in the AWS documentation for a recipe assistant. I just wanted to see how easy and fast it was to have something up and running. My aim was to create a story-telling app that could take different inputs for the story, like genre, tone, and style.

Implementation

After approximately 40 minutes of implementing the tutorial (I like to read first and type the code instead of copy/pasting), I already had a simple POC ready, with authentication, somewhat nice views, and inference for creating cooking recipes!

After that, it was a matter of playing around with what the app would do. I added a couple of selects with options for the user to generate different kinds of stories:

  • Genre: Sci-fi, Mystery, Fantasy, Romance, Horror
  • Tone: Light, Humorous, Dark, Adventurous
  • Style: First Person, Third Person, Fairy Tale, Poetic

After that, I changed the definition of the amplify data’s resources, which defines the cloud resources that will be used. So three things mainly changed: the arguments, returns, and the system prompt:

const schema = a.schema({
  generateShortStory: a.generation({
    aiModel: a.ai.model('Claude 3.5 Sonnet'),
    systemPrompt: "You are a story-teller that generates short, engaging stories based on user-provided themes or genres. Each story should have a clear structure: an introduction to set the stage, a middle to develop the plot, a twist to surprise the reader, and a satisfying ending. The characters should be fictional, with at least one relatable protagonist. Adapt the tone and style based on user input. Ensure the story is concise and self-contained.",
  })
    .arguments({
      genre: a.string(),
      tone: a.string(),
      style: a.string(),
    })
    .returns(
      a.customType({
        numberOfParagraphs: a.integer(),
        story: a.string(),
      })
    )
    .authorization((allow) => allow.authenticated()),
})

After making this change and saving, while still having the amplify sandbox environment active (npx ampx sandbox), the cloud resources will compile and resources will be updated to reflect these changes! Just wait for a minute or two and it’s done!

After making a couple of minor tweaks to the UI, so the page looks a bit nicer, the end result was the following:

Screenshot of a "Short Story Generator POC" with options for genre, tone, and style. The selected options are Sci-fi, Dark, and Third Person. A generated story is displayed about a child named Echo discovering a sound-emitting artifact in a silent village surrounded by misty mountains. The story unfolds with the village experiencing sound for the first time, leading to chaos and awe.
The view of the completed story-telling POC app. Own creation

Pretty good considering it took me only a couple of hours of experimentation!

Changing the AI model is very easy as well, as I was experimenting with different Claude models. Ultimately I went with the Claude Sonnet 3.5

Architecture

So, what’s under the hood? When inspecting in the AWS console about what resources are spun up, we realize it’s some awesome AWS serverless infrastructure that’s powering this AI kit. So apps created with this AI kit are ready to power your web apps and scale as easily as a serverless stack allows.

A diagram illustrating a system architecture where a user interacts with AWS AppSync, which communicates with Amazon Cognito for authentication, Amazon DynamoDB for data storage, AWS Lambda for processing, and Amazon Bedrock for machine learning tasks.
Image that shows the general architecture of apps created using the aws amplify’s AI kit. Taken from Build fullstack AI apps in minutes with the new Amplify AI Kit

The image above is the one that powers the full AI kit from Amplify. For my simple POC app, which only uses the “generate” variant of the kit (no conversation), I discovered that the stack is a bit different. It doesn’t use DynamoDB or Lambda, instead, AWS AppSync makes a direct HTTP request to Bedrock, and the mapping template from AppSync does the logic of formatting the response properly:

Diagram showing a flow from a user to Cognito and AWS AppSync, which then connects to Amazon Bedrock.
Image that shows the architecture of the short-story teller app created in this blog post using aws amplify’s generation ai kit. Own creation

The full code repository of this POC can be found on Github.

Conclusion

This experiment showed me just how far tools like AWS Amplify and Bedrock have come. In just a few hours, I could get my app from idea to working proof-of-concept. The best part is that it’s backed by a scalable and efficient serverless infrastructure!

It’s exciting to see how easy it is nowadays to experiment with AI-powered projects, and I’m looking forward to exploring and enhancing the POC even further. Even launching it may also be a good idea!

References

Serverless Handbook
Access free book

The dream team

At Serverless Guru, we're a collective of proactive solution finders. We prioritize genuineness, forward-thinking vision, and above all, we commit to diligently serving our members each and every day.

See open positions

Looking for skilled architects & developers?

Join businesses around the globe that trust our services. Let's start your serverless journey. Get in touch today!
Ryan Jones
Founder
Book a meeting
arrow
Founder
Eduardo Marcos
Chief Technology Officer
Chief Technology Officer
Book a meeting
arrow

Join the Community

Gather, share, and learn about AWS and serverless with enthusiasts worldwide in our open and free community.