Generate Images with Amazon Bedrock
Amazon Bedrock is a fully managed service that offers a choice of high-performing foundation models (FMs) from leading AI companies like AI21 Labs, Anthropic, Cohere, Meta, Mistral AI, Stability AI, and Amazon. Each model is accessible through a common API which implements a broad set of features to help build generative AI applications with security, privacy, and responsible AI in mind.
This guide will walk you through an example using the Amazon Bedrock JavaScript SDK in Supabase Edge Functions to generate images using the Amazon Titan Image Generator G1 model.
Setup
- In your AWS console, navigate to Amazon Bedrock and under "Request model access", select the Amazon Titan Image Generator G1 model.
- In your Supabase project, create a
.env
file in thesupabase
directory with the following contents:
_10AWS_DEFAULT_REGION="<your_region>"_10AWS_ACCESS_KEY_ID="<replace_your_own_credentials>"_10AWS_SECRET_ACCESS_KEY="<replace_your_own_credentials>"_10AWS_SESSION_TOKEN="<replace_your_own_credentials>"_10_10# Mocked config files_10AWS_SHARED_CREDENTIALS_FILE="./aws/credentials"_10AWS_CONFIG_FILE="./aws/config"
Configure Storage
- [locally] Run
supabase start
- Open Studio URL: locally | hosted
- Navigate to Storage
- Click "New bucket"
- Create a new public bucket called "images"
Code
Create a new function in your project:
_10supabase functions new amazon-bedrock
And add the code to the index.ts
file:
Run the function locally
- Run
supabase start
(see: https://supabase.com/docs/reference/cli/supabase-start) - Start with env:
supabase functions serve --env-file supabase/.env
- Make an HTTP request:
_10 curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/amazon-bedrock' \_10 --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0' \_10 --header 'Content-Type: application/json' \_10 --data '{"prompt":"A beautiful picture of a bird"}'
- Navigate back to your storage bucket. You might have to hit the refresh button to see the uploaded image.
Deploy to your hosted project
_10supabase link_10supabase functions deploy amazon-bedrock_10supabase secrets set --env-file supabase/.env
That's it, you've now deployed a serverless function that uses AI to generate and upload images to your Supabase storage bucket.