GithubHelp home page GithubHelp logo

aws-samples / aws-serverless-ai-stories Goto Github PK

View Code? Open in Web Editor NEW
147.0 7.0 47.0 6.34 MB

Example open source event-driven application that generates a new bed time story for your children every night using Lambda, EventBridge, DynamoDB, App Runner, ChatGPT and DALL-E.

Home Page: https://aws-blogs-prod.amazon.com/compute/implementing-an-event-driven-serverless-story-generation-application-with-chatgpt-and-dall-e/

License: MIT No Attribution

JavaScript 7.00% TypeScript 89.22% Dockerfile 3.62% CSS 0.16%
chatgpt dalle dynamodb event-driven-architecture eventbridge serverless

aws-serverless-ai-stories's Introduction

πŸ“– AI Generated Stories

Example open source event-driven application that generates a new bed time story for your children every night using Lambda, EventBridge, DynamoDB, App Runner, ChatGPT and DALL-E.

Read the blog post β†’


header

Features: New story every day, Audio using Amazon Polly, story using ChatGPT and images by DALL-E, all generated from an event-driven architecture.


Core Features

How it works

Architecture diagram

  1. Every day at a configured time an EventBridge Schedule is trigger which triggers a Lambda function.

  2. The create-story lambda function takes characters and scenes from the Amazon DynamoDB tables and uses ChatGPT (OpenAI API) to create the story. The story is stored with a 2 day TTL in DynamoDB.

  3. An Amazon EventBridge Pipe is configured to listen to all New items created inside the table using streams and triggers an Amazon EventBridge event (StoryCreated).

  4. EventBridge routes the StoryCreated event to three targets:

  • SNS for email
    • SNS for email: SNS is used in this example to notify the user that a new story has been created.
  • AWS Lambda function for Audio generation
    • Lambda for Audio: Amazon Polly is created to create audio for the story that has been generated. The audio file is stores into S3 with a signed URL (for 2 days).
  • AWS Lambda function for image generation.
    • Lambda for image generation: This function takes the story and scene and creates an image for the story using DALL-E (OpenAI API). This image is stored inside S3 with a signed URL (2 days).
  1. The frontend application is running on AWS App Runner and is hosting a NextJS SRR application. When the user goes to the URL in the Email (through SNS topic), the story is loaded and displayed.

Design choices

This application was designed as a proof of concept, and if you want to take extract patterns there might be some design considerations to understand before you do.

This application is designed for single use, every day it will email a single person a URL to a new story, if you wanted to scale this out to many users you would have to change the architecture to support that.

Hosting the frontend application

The frontend application is built with NextJS and hosted in App Runner, the App Runner container has the permission to talk to the DynamoDB table to get stories. The stories have a TTL of 2 days and will not be available after that duration (removed from the table).

EventBridge Pub/Sub

Once the story is created, EventBridge will raise an event to many consumers (audio processing, image creation, and SNS), this can lead to race conditions. There could be a chance that the audio or image is not ready when the user views the story on the screen. The application will check for audio and images, and fallback to render just the story if this information is not available yet (due to async processing). For a simple use case this might be fine but if you need to wait you may want to look at patterns like the aggregator or step function workflows that may help with this processing of state.

Three DynamoDB tables vs one

The application is fairly simple, and three tables seemed to be a pattern that worked well here. The characters and scenes table is not updated very often and the stories hold the generated stories. If you wanted to support many users you will need to consider your access patterns and table design.

Deploying

Prerequisites

Create your OpenAI API Key and add to Secret Manager.

First you will need an OpenAI API key, if you don’t have an account you will need to set one. You can go here to get started: https://platform.openai.com/overview

Once you have your key, you will need to add it to Secret Manager the secret name needs to be open-api-key.

Deploy into your AWS account

  1. Clone the repository

  2. Change the config.json file (add your email address and cron job)

  3. Run npm run install:all

  4. Run npm run deploy

    • This will deploy three stacks (Tables, Frontend, Backend) into your AWS account using CDK.
    • This can take a few minutes to deploy (containers need to start)
  5. Populate your DynamoDB databases (Scenes and Characters)

    • You can find the files in /backend/data/, change these to what you want.
    • Run npm run populate-db to populate these tables.
  6. Once done, your application is ready.

Generating a story

EventBridge scheduler will trigger your Lambda function to generate a story at the configured time set in your config.json file (default 7:15pm).

You can also manually trigger the function (-aiStoriesBackend-scheduledlambdafunction)

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

aws-serverless-ai-stories's People

Contributors

boyney123 avatar dependabot[bot] avatar jbesw avatar mmuller88 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

aws-serverless-ai-stories's Issues

issues with cdk deploy and running into spawnSync docker ENOENT

Great blog and fun idea -- when following the commands to set up the environment, after running npm run deploy I get the following error:

[email protected] deploy
cd backend && cdk deploy --all

Error: spawnSync docker ENOENT
at Object.spawnSync (node:internal/child_process:1117:20)
at Object.spawnSync (node:child_process:871:24)
at Object.dockerExec (/Users/aws/aws-serverless-ai-stories/backend/node_modules/aws-cdk-lib/core/lib/private/asset-staging.js:1:3313)
at Function.fromBuild (/Users/aws/aws-serverless-ai-stories/backend/node_modules/aws-cdk-lib/core/lib/bundling.js:1:3962)
at new Bundling (/Users/aws/aws-serverless-ai-stories/backend/node_modules/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.js:1:1944)
at Function.bundle (/Users/aws/aws-serverless-ai-stories/backend/node_modules/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.js:1:3046)
at new NodejsFunction (/Users/aws/aws-serverless-ai-stories/backend/node_modules/aws-cdk-lib/aws-lambda-nodejs/lib/function.js:1:1229)
at new BackendStack (/Users/aws/aws-serverless-ai-stories/backend/lib/backend.ts:60:45)
at Object. (/Users/aws/aws-serverless-ai-stories/backend/bin/backend.ts:20:1)
at Module._compile (node:internal/modules/cjs/loader:1275:14) {
errno: -2,
code: 'ENOENT',
syscall: 'spawnSync docker',
path: 'docker',
spawnargs: [
'build',
'-t',
'cdk-84e5538a8e2f550e0f75cd28a4bc048301af951bab38cd4ded047a33760a5a26',
'--platform',
'linux/amd64',
'--build-arg',
'IMAGE=public.ecr.aws/sam/build-nodejs18.x',
'--build-arg',
'ESBUILD_VERSION=0',
'/Users/aws/aws-serverless-ai-stories/backend/node_modules/aws-cdk-lib/aws-lambda-nodejs/lib'
]
}

Subprocess exited with error 1

I also notice that when I run anything with CDK, such as cdk metadata or cdk ls I get the same error.

Extra steps before 'npm run deploy' worked

Cool project - thanks!

I did get it to deploy, but had to take some extra steps to get it to succeed. If I didn't do these steps npm run deploy would bomb out.

  • cd backend/src/create-story && npm i
  • cd backend/src/generate-audio-for-story && npm i
  • cd backend/src/generate-images-for-story && npm i
  • cd frontend && touch .env

I pass this along in case it makes sense to you!

Error: Request failed with status code 429 from Lamba Function stg-aiStoriesBackend-scheduledlambdafunction

Hey, nice blog!
When I execute, the lambda function stg-aiStoriesBackend-scheduledlambdafunction generates an error 429 from the CloudWatch logs. Does someone have any ideas to work around?

2023-04-02T13:23:32.644Z	df3a5f89-d9f8-41d6-827f-f892dce7b4d5	ERROR	Invoke Error 	{
    "message": "Request failed with status code 429",
    "name": "Error",
    "stack": "Error: Request failed with status code 429\n    at createError (/var/task/index.js:356:19)\n    at settle (/var/task/index.js:372:16)\n    at IncomingMessage.handleStreamEnd (/var/task/index.js:2222:15)\n    at IncomingMessage.emit (node:events:525:35)\n    at endReadableNT (node:internal/streams/readable:1359:12)\n    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)",
    "config": {
        "transitional": {
            "silentJSONParsing": true,
            "forcedJSONParsing": true,
            "clarifyTimeoutError": false
        },
        "transformRequest": [
            null
        ],
        "transformResponse": [
            null
        ],
        "timeout": 0,
        "xsrfCookieName": "XSRF-TOKEN",
        "xsrfHeaderName": "X-XSRF-TOKEN",
        "maxContentLength": -1,
        "maxBodyLength": -1,
        "headers": {
            "Accept": "application/json, text/plain, */*",
            "Content-Type": "application/json",
            "User-Agent": "OpenAI/NodeJS/3.1.0",
            "Authorization": "Bearer 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",
            "Content-Length": 289
        },
        "method": "post",
        "data": "{\"model\":\"text-davinci-003\",\"prompt\":\"\\n  Write a title and a rhyming story on 2 main characters called Jerry and Tom.\\n  The story needs to be set within the scene Deep dark woods surrounded by small friendly monsters and be at least 200 words long\\n\",\"max_tokens\":1000,\"temperature\":0.7}",
        "url": "https://api.openai.com/v1/completions"
    },
    "status": 429
}

Error at position 0

I wonder if anyone has had this issue, but whenever I try to trigger the function I get the error below. Really looking forward to making it work!

{ "errorType": "SyntaxError", "errorMessage": "Unexpected token s in JSON at position 0", "stack": [ "SyntaxError: Unexpected token s in JSON at position 0", " at JSON.parse (<anonymous>)", " at Runtime.handler (/var/task/index.js:14326:23)", " at process.processTicksAndRejections (node:internal/process/task_queues:95:5)" ] }

cannot run npm run deploy


executor failed running [/bin/sh -c npm run build]: exit code: 1
[100%] fail: docker build --tag cdkasset-eac8aa482c5b0aaf9cf45dca81711c0e905f42cb36626f8893814cdde62907a0 --platform linux/amd64 . exited with error code 1: #1 [internal] load build definition from Dockerfile
#1 sha256:94ad57c069987cc5f2545ee37b0f671e5b0cc64f5b79f45035c90208c26c311d
#1 transferring dockerfile: 37B done
#1 DONE 0.0s

{A[Y].splice(0,A[v]npm ERR! code ELIFECYCLE
#14 0.836 npm ERR! errno 1
#14 0.839 npm ERR! @ build: next build
#14 0.839 npm ERR! Exit status 1
#14 0.839 npm ERR!
#14 0.840 npm ERR! Failed at the @ build script.
#14 0.840 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
#14 0.844
#14 0.844 npm ERR! A complete log of this run can be found in:
#14 0.844 npm ERR! /root/.npm/_logs/2023-05-19T21_05_51_228Z-debug.log
#14 ERROR: executor failed running [/bin/sh -c npm run build]: exit code: 1

[builder 5/5] RUN npm run build:


executor failed running [/bin/sh -c npm run build]: exit code: 1
stg-aiStoriesBackend: building assets...

[0%] start: Building b4dc6bab39417a56a230ec51c4b77694c3b7e2b741828a002aac1ca2bc339ebb:current_account-current_region
[0%] start: Building 4aea577eb744c3399c23dd93a5ab40e0b5a9bb67b0a65cd383ca9c1b728b6e1b:current_account-current_region
[0%] start: Building 369141d9c3fce053bfb036a3081d5b6ac09d809d33f4e7705d3bb95f4708fe08:current_account-current_region
[0%] start: Building 61c63449bfa374126c263ef985e327bb569ab247e7e1537d0542c77b2e18ab0d:current_account-current_region
[25%] success: Built b4dc6bab39417a56a230ec51c4b77694c3b7e2b741828a002aac1ca2bc339ebb:current_account-current_region
[50%] success: Built 4aea577eb744c3399c23dd93a5ab40e0b5a9bb67b0a65cd383ca9c1b728b6e1b:current_account-current_region
[75%] success: Built 369141d9c3fce053bfb036a3081d5b6ac09d809d33f4e7705d3bb95f4708fe08:current_account-current_region
[100%] success: Built 61c63449bfa374126c263ef985e327bb569ab247e7e1537d0542c77b2e18ab0d:current_account-current_region

stg-aiStoriesBackend: assets built

❌ Building assets failed: Error: Building Assets Failed: Error: Failed to build one or more assets. See the error messages above for more information.
at buildAllStackAssets (/Users/chiweitseng/WorkSpace/gluten-ai/aws-serverless-ai-stories/backend/node_modules/aws-cdk/lib/index.js:347:116743)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async CdkToolkit.deploy (/Users/chiweitseng/WorkSpace/gluten-ai/aws-serverless-ai-stories/backend/node_modules/aws-cdk/lib/index.js:347:142824)
at async exec4 (/Users/chiweitseng/WorkSpace/gluten-ai/aws-serverless-ai-stories/backend/node_modules/aws-cdk/lib/index.js:402:51795)

Building Assets Failed: Error: Failed to build one or more assets. See the error messages above for more information.

Dosen't work

Can I get any opinions on why it dosen't trigger at the schedule?

I had,

  1. Set open-api-key (ChatGPT) on Secret Manager.
  2. Deployed all stacks on aws via cdk and checked each stack's health.
  3. Populated characters and scenes into dynamoDB.
  4. Gotten an end-point link of apprunner which can connect like sample page.

image

is there anything I missed?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.