Amazon Bedrock Tutorial: Generative AI on AWS

pixegami · Beginner ·🎨 Image & Video AI ·2y ago

Key Takeaways

This video teaches how to use Amazon Bedrock for generative AI on AWS, covering text and image generation

Full Transcript

Amazon Bedrock is a brand new service from AWS that lets you use AI to generate text and images if you've ever used open AI or chat GPT then it's similar to that for text generation you can use it to summarize content or even generate new text content you can use it to generate images as well so in this case I've provided a text prompt for a Golden Retriever image and this was generated using Amazon Bedrock it's quite similar to Mid Journey if you've ever used that before Amazon Bedrock itself is not an AI model it's just the platform that gives you API access to other state-of-the-art models so for text and chat you can use anthropics cloud model which is similar to open AI GPT model for images you get access to the stable diffusion XL model for image generation in this tutorial we'll cover the setup process for Amazon Bedrock then we'll take a look at a couple of basic examples of how to generate text content and images using the Bedrock API we'll be doing that using python code it's all really easy and only takes a couple of minutes finally we'll also take a look at pricing to see how much it all cost and how it compares to open AI so if that sounds good let's get started to set up Amazon Bedrock you first need an AWS account I also recommend that you have the AWS CLI installed and configured so that we can use it later if you don't have these things yet then pause the video and take a moment to install them and I will provide a link in the comments where I also have a video tutorial on how to set up AWS once you have that log into your AWS console and then type in bedrock in the search bar you should see the service pop up so just click on it to go to that page next you have to select to use one of the regions that support Bedrock right now there's not that many regions where it's available um but I recommend either Us East 1 or us West 2 because I've discovered that those ones have the most features so in this case I'm using Us West 2 once you've selected your region click here to get started you should get this popup saying that you need to manage model access that's because when you first sign on to bedrock you don't actually get any of the AI models we're about to use yet but this is really easy so if you have this popup just go ahead and click here but if you don't have the popup you can manage the model access here as well so go ahead and click that these are all the available models but they're disabled by default so to enable them click edit and then just click the check boxes to enable all of them click save changes now you might have to fill in a form to access this anthropic Cloud Model I've already filled it in so maybe it's not showing up again for me but if it shows you to fill in a form then I recommend you do that because this is by far the best text model available on bedrock and it's totally worth it um and make sure you also have the stable diffusion model because this is the only image generation model available here once you have access to the models you can play around with it by clicking on the playground and there's chat text and image playgrounds so let's go ahead and try it over there here I'm in the text playground and you have to start by choosing a model category so here I'm going to choose the anthropic category and then I'm going to select Claude instant V1 so here I'm asking it what are the seven wonders of the world and then I'll click run and after a while you should see it generate this response so this is pretty good and as you will see at the end of the video the on demand pricing is pretty cheap but it's still important to understand how it works so that you don't get surprised by how much you're build at the end of the month and on the side here there's a tab called inference configuration and if you don't know what these are then don't worry about them yet but one important one is this length so you could see see here my response from the AI is actually cut off at the end because the length is only 300 so if I want a longer response I can increase that to maybe something like 500 for example now let's take a look at image generation as well so go back to the playgrounds Tab and then click on this image playground in the prompt box just describe what you want it to generate here I'm asking for a high photo quality of a cat in a yellow space suit uh and you can also add modifiers to The Prompt for example things like 4K resolution or artistic photo kind of nudge the AI towards giving you a better quality image uh and once you have that you can click run and after a while you should see an output like this and you can play around with these settings as well to see if it makes any difference uh but I think the main one is the seed so if you generate something with a different seed it might come up with a totally different pose or a different composition so let's try setting the seed to five here and then running this again so this is the new result and you can see it's still respecting The Prompt but it's just a totally different image so if you just want to experiment with text or image generation then the console UI is pretty useful but things start to get a lot more interesting when you do this via code because now you can use it as part of building your next application let's take a look at that next to generate text with Amazon Bedrock from code start with a new python file so here I've called this file generate text cloud. py and we're going to need to import boto 3 which is the ABS SDK for python and also the Json library because we're going to need to convert our python objects into Json data next I'm going to store my prompt in a string so this is the prompt I want to use to generate the text with after that I want to create a boto3 client for the Bedrock runtime uh and if this doesn't work for you you might need to upgrade your boto 3 client because it might be using an older version where the Bedrock runtime is not available yet currently I'm using this 1.28 version of boto 3 so if yours is much older than that then I recommend you upgrading it um and you should be able to do that by just running pip install boto 3 and then-- upgrade now we're going to need to prepare the payload that we're going to send to the Bedrock client and here I actually have to turn my prompt into a structure that has a human and assistant part to it so I think the cloud AI model works like a chat and it's kind of expecting human and assistant actors as part of the prompt so here if you have just a simple Tex text prompt you might just have to format it like this for clot to accept it and here you can decide how many tokens for the model to return to you in one particular sample this is the max that it will be allowed to return and then the next part of the code we're actually going to invoke the model this is going to be the API request that will give us our generated text so you do that by running invoke model and then passing in this body as a Json string and then the model ID which is going to be this anthropic Cloud Model here or you you can use one of the other Amazon models if you don't want to use this one and once you have this response you can get the actual text the text result from this invocation is just going to be this completion attribute on the response body and then we can print it now that the script is ready we can test it by running this file in our terminal I've asked it to write me a 90 style oneliner pitch about a movie for a giant eing Python and I think it's given me a pretty good response now if you had an error when you did this then just make sure that you've set up a us CLI correctly in your terminal and that the default profile is using the same region that you've configured Bedrock 4 I'll put a link to all of this code in the video description so you can take a look at that if you just want to copy and try it out right away I've also included another example using the Jurassic model instead if you don't want to use the Claud model but try them out in my experience I found the Cloud Model A lot better so I've had much better results from actually using this one and just keep in mind that if you do switch to a different model the input parameters and the response body could actually have different structures next let's take a look at how you can generate images using Amazon Bedrock here I've got a new python file and I'm starting with my prompt again so my prompt is going to be for a Golden Retriever puppy running on a beach and I've added a couple of modifiers here um so that it's a nice high resolution HDR photo and it's got blue sky and white sand and next we're going to need to prepare our input parameters for the API request um and the structure is going to look like this so this is where our text prompt is going to go and I've generated a random number for the seed finally there's these two other parameters which for now I wouldn't worry about them you can read up on them on the documentation if you want to see what they do otherwise just put in any value like these for example next we're actually going to invoke the API so do that by first creating a Bedrock API client and this time we're going to use stable diffusion as the model and then just invoke the model with all of the parameters that we've prepared once you have the response body this is how you actually get the image data um it's going to be base 64 encoded so we have to decode it into image bytes first and once you've decoded into bytes you can pick a file to save that image to so here I'm going to create an output directory and I'm going to name it after the seed that I've randomized at the top here uh and then finally we're just going to use fite image bytes to write this as a PNG file so now our script is ready let's go ahead and run that in the terminal this should finish running after a couple of seconds and when it does you'll be able to find your generated image in this output folder here just look at that I don't know about you but I'm totally Blown Away by the quality of images and text that you can generate from this and I'm quite excited to see what people are going to build using these apis but before you go crazy with this you might want to know how much it's all going to cost so let's take a look at that now bedrocks pricing is an on demand model which means that there's no monthly fee and you just pay for what you use it isn't super straightforward at first but once you understand it it's pretty simple and predictable to calculate for text models the calculation of the price is based on tokens and tokens you can almost think think of them like words so 1,000 tokens is about 700 words in English and the AI pricing is based on how many tokens you send and receive from the model it's pretty cheap but it can add up quickly if you're unaware of how much it costs so let's take a look at some of the models available here there's two Claude AI models there's the instant model which is super cheap this one you'd probably have to use hundreds of thousands of tokens before it even starts costing you $1 the Claude V2 is more powerful and you could see it's about five times more expensive than the instant model but it's still pretty cheap in general as well and finally at the bottom you've had the Jurassic 2 model which is cheaper for output tokens than the cloud model but more expensive for the input tokens now the pricing difference is usually due to the models size and quality um so the more expensive the models are the idea is that they should give you better results for more complex use cases however as I mentioned earlier I actually prefer the cloud models to the Jurassic model um so even though the Jurassic one is more expensive for example than the clae instant um I still actually prefer the clae instant model so the pricing is not always a reliable indicator of the quality you just have to try it out and see which models work best for you for image generation the pricing is a little different it's pretty cheap as well but it does add up a lot faster than text generation here you can generate a standard image for about 2 cents using the stable diffusion model or about 4 cents using the premium image whether or not an image is considered premium or standard depends on this parameters here um anything over 50 steps is considered premium and anything under is considered a standard image uh and steps tells stable diffusion the number of times to kind of iterate with that image generation I find that something around 50 to 80 usually gives pretty good results but again this is up to you to experiment um higher doesn't always mean better I've actually found that some prompts work better with lower steps than if you add too many steps to it so it's up to you how many steps you want to use for your image generation but just keep in mind that after 50 steps you have to pay this premium image price instead of the standard image price now let's take a look at how Amazon's pricing compares to open AI um so at the top here you have the two Cloud models available on Amazon Bedrock um and at the bottom you have the GPT models available on open Ai and you can see the pricing is quite similar so GPT 3. 5 turbo is probably the cheapest out of the bunch uh but not by much and I'm guessing Claude instant and GPT turbo are roughly on the same power level so to speak for the larger models however Claude V2 comes in a lot cheaper than gp4 about half to three times as cheap now I don't really know how the quality of the output Compares between them but from what I've seen so far all of these models are pretty good so again you have to experiment and see if these models give you the output you want but but otherwise the bottom line is that the pricing is quite competitive between the two services but now if you actually compare the features available on Bedrock versus openai at the moment openai is still a little bit ahead this is especially clear in um the function calling feature system prompts and the ability to fine-tune the function calling feature is where you can define a Json structure for the response to have and this is useful if you want to create chat Bots or make external AP calls for example like getting the weather or sending an email system prompts are a way to give instructions to the AI agent um that it considers I guess at a higher level than the user so for example you've got this human user and then the AI response component which you also have in the cloud model but the cloud model as far as I'm aware anyways doesn't have this system role uh so that's where usually you can put a bunch of instructions or limitations that you want the model to have as it interacts with the user fine tuning is where you can take a lot of example data and then feed it to your model so that it can learn from those examples and then give you better quality responses um and as far as I know Bedrock doesn't support that yet on the cloud model but I do see options for it for other models so it could be coming soon overall though the core text generation capability and the pricing is quite similar between both Amazon bedrock and open AI so you might want to try out Amazon Bedrock if you're already running an abos stack and you just want to plug it in to what you already have I also look forward to see what features and what other models Amazon Bedrock will offer in the near future that's going to be all for this tutorial check out the description below for the code and other useful links if you've enjoyed this then leave a comment and let me know what other technology or projects You' like to see covered next otherwise I hope you found this useful and thank you for watching

Original Description

Learn how to use Amazon Bedrock for generative AI (text and images) on AWS. This tutorial will guide you through the setup process, showing you how to generate text and images via code. You'll learn how to generate text with the Anthropic Claude model, and images with the Stable Diffusion model. All you need is an AWS account and the AWS CLI installed and configured. 🔗 Code: https://github.com/pixegami/aws-bedrock-demo 🔗 Get Started on AWS: https://youtu.be/LFCTFzcMQuA 🔗 Amazon Bedrock: https://aws.amazon.com/bedrock/ 📚 Chapters 00:00 Introduction to Amazon Bedrock 01:12 Setting Up Amazon Bedrock 02:54 Text Playground 03:41 Image Playground 04:47 Generating Text with Code 07:43 Generating Images with Code 09:40 Amazon Bedrock Pricing 12:13 Amazon Bedrock vs OpenAI #pixegami #aws
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from pixegami · pixegami · 54 of 60

1 How to Build an AWS Lambda Function in Python in Just 7 Minutes!
How to Build an AWS Lambda Function in Python in Just 7 Minutes!
pixegami
2 AWS CDK Tutorial: Deploy a Python Lambda Function using AWS
AWS CDK Tutorial: Deploy a Python Lambda Function using AWS
pixegami
3 I used GPT-3 to Write Poetry • Is AI the Future of Creative Writing?
I used GPT-3 to Write Poetry • Is AI the Future of Creative Writing?
pixegami
4 Create NFT Generative Art with Python! (Full Tutorial)
Create NFT Generative Art with Python! (Full Tutorial)
pixegami
5 Build an AI-driven SaaS Application: FULLSTACK Tutorial with Python, React, and AWS
Build an AI-driven SaaS Application: FULLSTACK Tutorial with Python, React, and AWS
pixegami
6 NextJS and TailwindCSS: How to Build a Portfolio Site from Scratch
NextJS and TailwindCSS: How to Build a Portfolio Site from Scratch
pixegami
7 Python Web Scraping Tutorial • Step by Step Beginner's Guide
Python Web Scraping Tutorial • Step by Step Beginner's Guide
pixegami
8 Build Wordle in Python • Word Game Python Project for Beginners
Build Wordle in Python • Word Game Python Project for Beginners
pixegami
9 How to create 1000+ unique NFT-style images (like Cryptopunk) | Python Tutorial
How to create 1000+ unique NFT-style images (like Cryptopunk) | Python Tutorial
pixegami
10 Top 10 Python Modules 2022
Top 10 Python Modules 2022
pixegami
11 How to Send SMS Text Messages with Python & Twilio - Quick and Simple!
How to Send SMS Text Messages with Python & Twilio - Quick and Simple!
pixegami
12 How To Write Unit Tests in Python • Pytest Tutorial
How To Write Unit Tests in Python • Pytest Tutorial
pixegami
13 How to Style Your React Landing Page with Tailwind CSS
How to Style Your React Landing Page with Tailwind CSS
pixegami
14 FastAPI Python Tutorial - Learn How to Build a REST API
FastAPI Python Tutorial - Learn How to Build a REST API
pixegami
15 How to Deploy FastAPI on AWS EC2: Quick and Easy Steps!
How to Deploy FastAPI on AWS EC2: Quick and Easy Steps!
pixegami
16 PyScript • How to run Python in a browser
PyScript • How to run Python in a browser
pixegami
17 My Custom Ubuntu Linux Terminal with Themes and Plug-ins 💻
My Custom Ubuntu Linux Terminal with Themes and Plug-ins 💻
pixegami
18 Deploy FastAPI on AWS Lambda ⚡ Serverless hosting!
Deploy FastAPI on AWS Lambda ⚡ Serverless hosting!
pixegami
19 NextJS Firebase Auth Tutorial • How to Authenticate Users for Your App
NextJS Firebase Auth Tutorial • How to Authenticate Users for Your App
pixegami
20 AWS Lambda Python functions with a database (DynamoDB)
AWS Lambda Python functions with a database (DynamoDB)
pixegami
21 How To Build a CRUD (TO-DO) App on AWS using FastAPI and Python
How To Build a CRUD (TO-DO) App on AWS using FastAPI and Python
pixegami
22 How to Make a Discord Bot with Python
How to Make a Discord Bot with Python
pixegami
23 How To Use GitHub Copilot (with Python Examples)
How To Use GitHub Copilot (with Python Examples)
pixegami
24 PyTest • REST API Integration Testing with Python
PyTest • REST API Integration Testing with Python
pixegami
25 Python Beginner Project: Build a Caesar Cipher Encryption App
Python Beginner Project: Build a Caesar Cipher Encryption App
pixegami
26 Decorators in Python: How to Write Your Own Custom Decorators
Decorators in Python: How to Write Your Own Custom Decorators
pixegami
27 NextJS 13 Tutorial: Create a Static Blog from Markdown Files
NextJS 13 Tutorial: Create a Static Blog from Markdown Files
pixegami
28 Exploring ChatGPT for Coding and Business ✨ 8 Real Examples!
Exploring ChatGPT for Coding and Business ✨ 8 Real Examples!
pixegami
29 How I Would Learn Python (if I had to start over) • A Roadmap for 2023
How I Would Learn Python (if I had to start over) • A Roadmap for 2023
pixegami
30 Build an AI Pokemon Generator with Python and Midjourney
Build an AI Pokemon Generator with Python and Midjourney
pixegami
31 Why You Should Learn Python in 2023 (as your first programming language)
Why You Should Learn Python in 2023 (as your first programming language)
pixegami
32 ChatGPI API in Python ✨ How to Build a Custom AI Chat App
ChatGPI API in Python ✨ How to Build a Custom AI Chat App
pixegami
33 Learn Python • #1 Installation and Setup • Get Started With Python!
Learn Python • #1 Installation and Setup • Get Started With Python!
pixegami
34 Learn Python • #2 Variables and Data Types • Python's Building Blocks
Learn Python • #2 Variables and Data Types • Python's Building Blocks
pixegami
35 Learn Python • #3 Operators • Add, Subtract and More...
Learn Python • #3 Operators • Add, Subtract and More...
pixegami
36 Learn Python • #4 Conditions • If / Else Statements
Learn Python • #4 Conditions • If / Else Statements
pixegami
37 Learn Python • #5 Lists • Storing Collections of Data
Learn Python • #5 Lists • Storing Collections of Data
pixegami
38 Learn Python • #6 Loops • How to Repeat Code Execution
Learn Python • #6 Loops • How to Repeat Code Execution
pixegami
39 Learn Python • #7 Dictionaries • The Most Useful Data Structure?
Learn Python • #7 Dictionaries • The Most Useful Data Structure?
pixegami
40 Learn Python • #8 Tuples and Sets • More Ways To Store Data!
Learn Python • #8 Tuples and Sets • More Ways To Store Data!
pixegami
41 Learn Python • #9 Functions • Python's Most Important Concept?
Learn Python • #9 Functions • Python's Most Important Concept?
pixegami
42 Learn Python • #10 User Input • 4 Ways To Get Input From Your User
Learn Python • #10 User Input • 4 Ways To Get Input From Your User
pixegami
43 Learn Python • #11 Classes • Create and Use Classes in Python
Learn Python • #11 Classes • Create and Use Classes in Python
pixegami
44 Learn Python • #12 Final Project • Build an Expense Tracking App!
Learn Python • #12 Final Project • Build an Expense Tracking App!
pixegami
45 Stripe & Firebase Tutorial • Add Payments To Your NextJS App
Stripe & Firebase Tutorial • Add Payments To Your NextJS App
pixegami
46 How To Use GitHub Actions • Automate Your AWS Deployments
How To Use GitHub Actions • Automate Your AWS Deployments
pixegami
47 How to Run a Python Docker Image on AWS Lambda
How to Run a Python Docker Image on AWS Lambda
pixegami
48 My MacOS Terminal Setup for HIGH Productivity
My MacOS Terminal Setup for HIGH Productivity
pixegami
49 Host a Python Discord Bot on AWS Lambda (Free and Easy)
Host a Python Discord Bot on AWS Lambda (Free and Easy)
pixegami
50 Python FastAPI Tutorial: Build a REST API in 15 Minutes
Python FastAPI Tutorial: Build a REST API in 15 Minutes
pixegami
51 Pydantic Tutorial • Solving Python's Biggest Problem
Pydantic Tutorial • Solving Python's Biggest Problem
pixegami
52 How to Get Started with AWS • Crash Course
How to Get Started with AWS • Crash Course
pixegami
53 Python Requests Tutorial: HTTP Requests and Web Scraping
Python Requests Tutorial: HTTP Requests and Web Scraping
pixegami
Amazon Bedrock Tutorial: Generative AI on AWS
Amazon Bedrock Tutorial: Generative AI on AWS
pixegami
55 How to Publish a Python Package to PyPI (pip)
How to Publish a Python Package to PyPI (pip)
pixegami
56 Langchain: The BEST Library For Building AI Apps In Python?
Langchain: The BEST Library For Building AI Apps In Python?
pixegami
57 RAG + Langchain Python Project: Easy AI/Chat For Your Docs
RAG + Langchain Python Project: Easy AI/Chat For Your Docs
pixegami
58 Python Dataclasses: Here's 7 Ways It Will Improve Your Code
Python Dataclasses: Here's 7 Ways It Will Improve Your Code
pixegami
59 Build a Custom AI RPG Game with OpenAI GPTs
Build a Custom AI RPG Game with OpenAI GPTs
pixegami
60 Create a Custom AI Assistant + API in 10 Mins
Create a Custom AI Assistant + API in 10 Mins
pixegami

Related Reads

📰
How I Built an AI Pet Portrait Generator That Turns Photos Into Art
Learn how to build an AI pet portrait generator that turns photos into art using deep learning techniques and Python libraries
Dev.to · William Li
📰
I Put Google’s Squoosh Codecs in the Browser — and Cut My Image Bill Before the Upload Even…
Learn how to use Google's Squoosh codecs in the browser to compress images before upload and reduce costs
Medium · Programming
📰
I Put Google’s Squoosh Codecs in the Browser — and Cut My Image Bill Before the Upload Even…
Learn how to use Google's Squoosh codecs in the browser to compress images before upload, reducing costs and improving performance
Medium · JavaScript
📰
Which AI tools can generate ready-to-use 3D character models for games, animation, or 3D printing?
Learn about AI tools that generate ready-to-use 3D character models for games, animation, or 3D printing and how to evaluate them for different use cases
Reddit r/artificial

Chapters (8)

Introduction to Amazon Bedrock
1:12 Setting Up Amazon Bedrock
2:54 Text Playground
3:41 Image Playground
4:47 Generating Text with Code
7:43 Generating Images with Code
9:40 Amazon Bedrock Pricing
12:13 Amazon Bedrock vs OpenAI
Up next
VIRAL AI Videos 🤩 New INSTAGRAM TREND 🔥 Baby Kids Videos #ai #trending
Raj Photo Editing and Much More
Watch →