Build Serverless, Scalable Generative AI Apps with This GitHub Repo
Create scalable generative AI apps using AWS Step Functions and Amazon Bedrock. Apply your serverless skills to build with large language models (LLMs) on AWS.
Brooke Jamieson
Amazon Employee
Published Jun 3, 2024
Last Modified Jun 4, 2024
While flashy generative AI demos wow audiences, many developers I've spoken to feel a bit disconnected - excited by the potential, but unsure how to apply their expertise to actually build this future in production. That changed when my AWS colleague Clare Liguori (Senior Principal Engineer, AWS) shared her GitHub repo called “Amazon Bedrock Serverless Prompt Chaining”.
Clare's repo focuses on prompt chaining - breaking complex AI tasks into sequential prompts for large language models. Using AWS Step Functions and Amazon Bedrock, she shows you how you can create serverless generative AI workflows without managing infrastructure.
The examples range from simple prompt chains to complex ones with parallelism, conditional logic, human input integration and more. The "Plan a Meal" workflow, where AI agents debate recipe ideas, stands out.
Whether for analysis, writing, planning or new use cases, this repo makes prompt chaining accessible. Keep reading to dive into the powerful prompt chaining techniques and practical examples for your next generative AI project.
Prompt chaining involves breaking down a complex task into smaller, individual prompts that are fed to a large language model in a specific order or based on defined rules. Clare's repo demonstrates numerous approaches for orchestrating these prompt chains using AWS Step Functions:
Step Functions can invoke models in Bedrock using the optimized integration, with the prompt and inference properties defined in the body parameter.
Prompts can be dynamically generated by injecting values from the execution input or previous step outputs using Step Functions' intrinsic functions.
The output from one prompt can be included as context for the next prompt in the chain, enabling multi-step workflows.
Multiple prompts can be executed in parallel, with their outputs merged as input for a subsequent prompt in the chain.
Choice states in Step Functions can branch the workflow based on evaluating the model's response against specified conditions.
Map states allow iterating over a collection of inputs, processing each one with a prompt to generate an array of outputs.
Prompts can be chained with interactions with other AWS services, such as retrieving data from S3 or invoking Lambda functions.
If a model's response fails validation, it can be re-prompted to fix its output, with error handling capabilities in Step Functions.
State machines can pause execution and wait for human input using task tokens, enabling interactive workflows.
Clare's repo includes comprehensive examples showing the various prompt chaining techniques for building generative AI applications in action.
Let's walk through each example:
This workflow generates an in-depth literature analysis for a blog by breaking down the task into sequential prompts.
Here's how it works:
Here's how it works:
- The first prompt generates a 1-2 sentence overall summary of the novel.
- The next prompt uses that summary to write a paragraph describing the plot.
- Another prompt then takes the summary and plot as context to generate a paragraph analyzing the novel's key themes.
- Using the accumulating context, the next prompt writes a paragraph examining the novel's writing style and tone.
- The final prompt combines the summary, plot description, themes analysis, and writing style analysis into a complete, multi-paragraph blog post with an intro and conclusion.
Produce a short story on a given topic by generating a character list, looping through character arcs, and synthesizing the outputs into a narrative.
Here’s how it works:
Here’s how it works:
- The first prompt generates a list of 5 characters as a JSON array, including their names and descriptions.
- A Map state is used to process the character list:
- For each character, a new prompt generates that character's story arc using their name and description.
- This "Generate Character Story Arc" step runs in parallel for all characters.
- The outputs from all the character arc prompts are merged together.
- The final prompt takes the merged character arcs as context to generate the full short story narrative.
Create a weekend vacation itinerary by parallelizing prompts for hotel, activity, and restaurant recommendations, then compiling them into a daily schedule and PDF.
Here’s how it works:
Here’s how it works:
- Three prompts run in parallel to generate suggestions for hotels, restaurants, and activities at the given location.
- The outputs from those parallel prompts are passed into the next prompt, which combines them to create a structured daily itinerary in Markdown format.
- The Markdown itinerary is passed to a Lambda function (not involving any language model).
- The Lambda function renders the Markdown into a PDF file and uploads it to an S3 bucket.
An interactive workflow that pitches movie ideas, parallelizing idea generation, enabling human approval input, generating longer pitches, and looping for new ideas if rejected.
Here’s how it works:
Here’s how it works:
- Three prompts run in parallel to generate one-paragraph movie pitch ideas, each with a different temperature setting (low, medium, high).
- A follow-up prompt evaluates the three pitch ideas and chooses the best one.
- The chosen one-paragraph pitch is presented to the human user (acting as a movie producer) for approval.
- The user provides input on whether to "greenlight" the pitch idea or not.
- If greenlighted, a final prompt takes the one-paragraph pitch as context to generate a longer, one-page movie pitch document.
- If not greenlighted, the workflow loops back to step 1 to generate new pitch ideas in parallel.
Generate a recipe based on given ingredients by having two "AI chef" personas iteratively debate and improve parallel meal suggestions until reaching consensus, with code selecting the highest-scored meal.
Here’s how it works:
Here’s how it works:
- Two "AI chef" agents generate initial meal ideas in parallel based on provided ingredients
- A "judge" agent scores both meal ideas for tastiness on a 0-100 scale
- Iterative "debate" process:
- Chefs attempt to improve their meal idea to outscore the other
- "Referee" agent determines if chefs reached consensus on the same/similar meal
- A Lambda function (no language model) selects the highest scoring meal idea
- Final step generates a complete recipe for the winning meal using the original ingredients
Summarize today's top trending GitHub repository by chaining React prompts that identify the trending repo and retrieve its README content via GitHub APIs.
Here’s how it works:
Here’s how it works:
- The first prompt/agent looks up the current highest trending open source repository on GitHub by scraping the GitHub Trending page.
- The second prompt/agent takes the identified repo name as input and retrieves the README content for that repo by calling the GitHub API.
- The second prompt/agent then summarizes the key details from the retrieved README.
This example is provided in two versions - one using the Bedrock Agents framework and one using the Langchain agents library. The core architecture of chaining the two agents is the same between versions.
Look, I get it - generative AI can seem out of reach for lots of developers. But you don't need to start over from scratch. Clare's repo proves you can apply your existing Step Functions and workflow orchestration skills to build cool (and functional!) AI apps.
The examples show how to integrate large language models into familiar serverless practices using Amazon Bedrock. There's no need to ditch the AWS services and patterns you already know and love. This repo lets you gradually blend generative AI into your serverless skillset at your own pace. You get to leverage your hard-earned and valuable experience with modern AI capabilities.
About the Author: Brooke Jamieson is a Senior Developer Advocate at AWS. You can follow Brooke on LinkedIn, Twitter, Instagram & TikTok.Read other articles from Brooke:
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.