From ec3eaed498a5d2c6c171b6b3df1f19b8747a8d12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thor=20=E9=9B=B7=E7=A5=9E=20Schaeff?= <5748289+thorwebdev@users.noreply.github.com> Date: Tue, 28 May 2024 13:12:42 +0800 Subject: [PATCH] feat: amazon bedrock edge functions example. (#26698) * feat: amazon bedrock edge functions example. * chore: track aws sdk issue. * chore: mock file system for config files. * chore: fix migration. --- .../aws_bedrock_image_gen/supabase/.gitignore | 4 + .../supabase/config.toml | 171 ++++++++++++++++++ .../supabase/functions/image_gen/index.ts | 99 ++++++++++ .../20240523074718_storage_bucket.sql | 15 ++ .../aws_bedrock_image_gen/supabase/seed.sql | 0 5 files changed, 289 insertions(+) create mode 100644 examples/ai/aws_bedrock_image_gen/supabase/.gitignore create mode 100644 examples/ai/aws_bedrock_image_gen/supabase/config.toml create mode 100644 examples/ai/aws_bedrock_image_gen/supabase/functions/image_gen/index.ts create mode 100644 examples/ai/aws_bedrock_image_gen/supabase/migrations/20240523074718_storage_bucket.sql create mode 100644 examples/ai/aws_bedrock_image_gen/supabase/seed.sql diff --git a/examples/ai/aws_bedrock_image_gen/supabase/.gitignore b/examples/ai/aws_bedrock_image_gen/supabase/.gitignore new file mode 100644 index 00000000000..a3ad88055b7 --- /dev/null +++ b/examples/ai/aws_bedrock_image_gen/supabase/.gitignore @@ -0,0 +1,4 @@ +# Supabase +.branches +.temp +.env diff --git a/examples/ai/aws_bedrock_image_gen/supabase/config.toml b/examples/ai/aws_bedrock_image_gen/supabase/config.toml new file mode 100644 index 00000000000..a32c2d06a21 --- /dev/null +++ b/examples/ai/aws_bedrock_image_gen/supabase/config.toml @@ -0,0 +1,171 @@ +# A string used to distinguish different Supabase projects on the same host. Defaults to the +# working directory name when running `supabase init`. +project_id = "aws_bedrock_image_gen" + +[api] +enabled = true +# Port to use for the API URL. +port = 54321 +# Schemas to expose in your API. Tables, views and stored procedures in this schema will get API +# endpoints. `public` is always included. +schemas = ["public", "graphql_public"] +# Extra schemas to add to the search_path of every request. `public` is always included. +extra_search_path = ["public", "extensions"] +# The maximum number of rows returns from a view, table, or stored procedure. Limits payload size +# for accidental or malicious requests. +max_rows = 1000 + +[db] +# Port to use for the local database URL. +port = 54322 +# Port used by db diff command to initialize the shadow database. +shadow_port = 54320 +# The database major version to use. This has to be the same as your remote database's. Run `SHOW +# server_version;` on the remote database to check. +major_version = 15 + +[db.pooler] +enabled = false +# Port to use for the local connection pooler. +port = 54329 +# Specifies when a server connection can be reused by other clients. +# Configure one of the supported pooler modes: `transaction`, `session`. +pool_mode = "transaction" +# How many server connections to allow per user/database pair. +default_pool_size = 20 +# Maximum number of client connections allowed. +max_client_conn = 100 + +[realtime] +enabled = true +# Bind realtime via either IPv4 or IPv6. (default: IPv4) +# ip_version = "IPv6" +# The maximum length in bytes of HTTP request headers. (default: 4096) +# max_header_length = 4096 + +[studio] +enabled = true +# Port to use for Supabase Studio. +port = 54323 +# External URL of the API server that frontend connects to. +api_url = "http://127.0.0.1" +# OpenAI API Key to use for Supabase AI in the Supabase Studio. +openai_api_key = "env(OPENAI_API_KEY)" + +# Email testing server. Emails sent with the local dev setup are not actually sent - rather, they +# are monitored, and you can view the emails that would have been sent from the web interface. +[inbucket] +enabled = true +# Port to use for the email testing server web interface. +port = 54324 +# Uncomment to expose additional ports for testing user applications that send emails. +# smtp_port = 54325 +# pop3_port = 54326 + +[storage] +enabled = true +# The maximum file size allowed (e.g. "5MB", "500KB"). +file_size_limit = "50MiB" + +[storage.image_transformation] +enabled = true + +[auth] +enabled = true +# The base URL of your website. Used as an allow-list for redirects and for constructing URLs used +# in emails. +site_url = "http://127.0.0.1:3000" +# A list of *exact* URLs that auth providers are permitted to redirect to post authentication. +additional_redirect_urls = ["https://127.0.0.1:3000"] +# How long tokens are valid for, in seconds. Defaults to 3600 (1 hour), maximum 604,800 (1 week). +jwt_expiry = 3600 +# If disabled, the refresh token will never expire. +enable_refresh_token_rotation = true +# Allows refresh tokens to be reused after expiry, up to the specified interval in seconds. +# Requires enable_refresh_token_rotation = true. +refresh_token_reuse_interval = 10 +# Allow/disallow new user signups to your project. +enable_signup = true +# Allow/disallow anonymous sign-ins to your project. +enable_anonymous_sign_ins = false +# Allow/disallow testing manual linking of accounts +enable_manual_linking = false + +[auth.email] +# Allow/disallow new user signups via email to your project. +enable_signup = true +# If enabled, a user will be required to confirm any email change on both the old, and new email +# addresses. If disabled, only the new email is required to confirm. +double_confirm_changes = true +# If enabled, users need to confirm their email address before signing in. +enable_confirmations = false +# Controls the minimum amount of time that must pass before sending another signup confirmation or password reset email. +max_frequency = "1s" + +# Uncomment to customize email template +# [auth.email.template.invite] +# subject = "You have been invited" +# content_path = "./supabase/templates/invite.html" + +[auth.sms] +# Allow/disallow new user signups via SMS to your project. +enable_signup = true +# If enabled, users need to confirm their phone number before signing in. +enable_confirmations = false +# Template for sending OTP to users +template = "Your code is {{ .Code }} ." +# Controls the minimum amount of time that must pass before sending another sms otp. +max_frequency = "5s" + +# Use pre-defined map of phone number to OTP for testing. +# [auth.sms.test_otp] +# 4152127777 = "123456" + +# This hook runs before a token is issued and allows you to add additional claims based on the authentication method used. +# [auth.hook.custom_access_token] +# enabled = true +# uri = "pg-functions:////" + +# Configure one of the supported SMS providers: `twilio`, `twilio_verify`, `messagebird`, `textlocal`, `vonage`. +[auth.sms.twilio] +enabled = false +account_sid = "" +message_service_sid = "" +# DO NOT commit your Twilio auth token to git. Use environment variable substitution instead: +auth_token = "env(SUPABASE_AUTH_SMS_TWILIO_AUTH_TOKEN)" + +# Use an external OAuth provider. The full list of providers are: `apple`, `azure`, `bitbucket`, +# `discord`, `facebook`, `github`, `gitlab`, `google`, `keycloak`, `linkedin_oidc`, `notion`, `twitch`, +# `twitter`, `slack`, `spotify`, `workos`, `zoom`. +[auth.external.apple] +enabled = false +client_id = "" +# DO NOT commit your OAuth provider secret to git. Use environment variable substitution instead: +secret = "env(SUPABASE_AUTH_EXTERNAL_APPLE_SECRET)" +# Overrides the default auth redirectUrl. +redirect_uri = "" +# Overrides the default auth provider URL. Used to support self-hosted gitlab, single-tenant Azure, +# or any other third-party OIDC providers. +url = "" +# If enabled, the nonce check will be skipped. Required for local sign in with Google auth. +skip_nonce_check = false + +[analytics] +enabled = false +port = 54327 +vector_port = 54328 +# Configure one of the supported backends: `postgres`, `bigquery`. +backend = "postgres" + +# Experimental features may be deprecated any time +[experimental] +# Configures Postgres storage engine to use OrioleDB (S3) +orioledb_version = "" +# Configures S3 bucket URL, eg. .s3-.amazonaws.com +s3_host = "env(S3_HOST)" +# Configures S3 bucket region, eg. us-east-1 +s3_region = "env(S3_REGION)" +# Configures AWS_ACCESS_KEY_ID for S3 bucket +s3_access_key = "env(S3_ACCESS_KEY)" +# Configures AWS_SECRET_ACCESS_KEY for S3 bucket +s3_secret_key = "env(S3_SECRET_KEY)" diff --git a/examples/ai/aws_bedrock_image_gen/supabase/functions/image_gen/index.ts b/examples/ai/aws_bedrock_image_gen/supabase/functions/image_gen/index.ts new file mode 100644 index 00000000000..a1cfbb223e0 --- /dev/null +++ b/examples/ai/aws_bedrock_image_gen/supabase/functions/image_gen/index.ts @@ -0,0 +1,99 @@ +// AWS SDK issue: https://github.com/aws/aws-sdk-js-v3/issues/6134 +// We need to mock the file system for the AWS SDK to work. +import { + prepareVirtualFile, +} from "https://deno.land/x/mock_file@v1.1.2/mod.ts"; + +import { + BedrockRuntimeClient, + InvokeModelCommand, +} from "npm:@aws-sdk/client-bedrock-runtime"; +import { createClient } from "npm:@supabase/supabase-js"; +import { decode } from "npm:base64-arraybuffer"; + +console.log("Hello from Amazon Bedrock!"); + +Deno.serve(async (req) => { + prepareVirtualFile("./aws/config"); + prepareVirtualFile("./aws/credentials"); + + const client = new BedrockRuntimeClient({ + region: "us-west-2", + credentials: { + accessKeyId: Deno.env.get("AWS_ACCESS_KEY_ID") ?? "", + secretAccessKey: Deno.env.get("AWS_SECRET_ACCESS_KEY") ?? "", + sessionToken: Deno.env.get("AWS_SESSION_TOKEN") ?? "", + }, + }); + + const { prompt, seed } = await req.json(); + console.log(prompt); + const input = { + contentType: "application/json", + accept: "*/*", + modelId: "amazon.titan-image-generator-v1", + body: JSON.stringify({ + "taskType": "TEXT_IMAGE", + "textToImageParams": { "text": prompt }, + "imageGenerationConfig": { + "numberOfImages": 1, + "quality": "standard", + "cfgScale": 8.0, + "height": 512, + "width": 512, + "seed": seed ?? 0, + }, + }), + }; + + const command = new InvokeModelCommand(input); + const response = await client.send(command); + console.log(response); + + if (response.$metadata.httpStatusCode === 200) { + const { body, $metadata } = response; + + const textDecoder = new TextDecoder("utf-8"); + const jsonString = textDecoder.decode(body.buffer); + const parsedData = JSON.parse(jsonString); + console.log(parsedData); + const image = parsedData.images[0]; + + const supabaseClient = createClient( + // Supabase API URL - env var exported by default. + Deno.env.get("SUPABASE_URL")!, + // Supabase API ANON KEY - env var exported by default. + Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!, + ); + + const { data: upload, error: uploadError } = await supabaseClient.storage + .from("images") + .upload(`${$metadata.requestId ?? ""}.png`, decode(image), { + contentType: "image/png", + cacheControl: "3600", + upsert: false, + }); + if (!upload) { + return Response.json(uploadError); + } + const { data } = supabaseClient + .storage + .from("images") + .getPublicUrl(upload.path!); + return Response.json(data); + } + + return Response.json(response); +}); + +/* To invoke locally: + + 1. Run `supabase start` (see: https://supabase.com/docs/reference/cli/supabase-start) + 2. Start with env: `supabase functions serve --env-file supabase/.env` + 3. Make an HTTP request: + + curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/image_gen' \ + --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0' \ + --header 'Content-Type: application/json' \ + --data '{"prompt":"A beautiful picture of a bird"}' +*/ diff --git a/examples/ai/aws_bedrock_image_gen/supabase/migrations/20240523074718_storage_bucket.sql b/examples/ai/aws_bedrock_image_gen/supabase/migrations/20240523074718_storage_bucket.sql new file mode 100644 index 00000000000..0c756ff94cd --- /dev/null +++ b/examples/ai/aws_bedrock_image_gen/supabase/migrations/20240523074718_storage_bucket.sql @@ -0,0 +1,15 @@ +insert into "storage"."buckets" + ( + "id", + "name", + "owner", + "created_at", + "updated_at", + "public", + "avif_autodetection", + "file_size_limit", + "allowed_mime_types", + "owner_id" + ) +values + ('images', 'images', null, now(), now(), true, false, null, null, null); diff --git a/examples/ai/aws_bedrock_image_gen/supabase/seed.sql b/examples/ai/aws_bedrock_image_gen/supabase/seed.sql new file mode 100644 index 00000000000..e69de29bb2d