Description

In this hands-on workshop, we will explore Amazon Bedrock, a fully managed service that makes foundation models from leading AI companies accessible via the AWS cloud. This challenge will help participants understand how to use Bedrock to solve real-world problems with generative AI.

Introduction

Amazon Bedrock is an AWS service designed to simplify the use of large-scale foundation models for various generative AI tasks such as text generation, chatbots, document summarization, and more. With Bedrock, users can quickly and easily build applications powered by models from Amazon, Anthropic, Meta, and other leading AI companies without managing any infrastructure.

Key Features:

  1. Access to Foundation Models: Bedrock provides access to pre-trained models for text, image, and speech generation from leading providers.
  2. Simplified Model Management: No need to manage infrastructure for scaling, maintaining, or updating models.
  3. Easy Integration: Bedrock models can be easily integrated into your applications with just a few API calls.
  4. Customizable Models: Customize foundation models for specific tasks without the need to train from scratch.
  5. Secure and Scalable: Bedrock inherits the scalability, security, and compliance of AWS services.

Use Cases:

  1. Text Generation: Generate blog posts, product descriptions, and customer service responses.
  2. Chatbots: Build conversational agents that handle customer queries.
  3. Image Generation: Generate images from text prompts

The Challenge Scenario: AI-Powered Content Generation for a Marketing Agency

Scenario:

Imagine a Marketing Agency called Android Media. They help clients with social media management by creating and posting engaging posts on the behalf of their clients. The company has many clients, and their workload is increasing day by day.

Problem Statement: Android Media needs an efficient way to generated social media posts using LLM AIs. They need to generate short posts for X(Twitter) on a certain topic each time, and also need an image to attach to the post. The company also wants to evaluate the output of multiple AI models instead of just sticking to one, since they want to choose the best quality post.

Your Mission: Your task is to help Android Media use Amazon Bedrock to create a easy-to-use tool for them to generate multiple social media posts from multiple models, as well as an image, all from one prompt text.


Hands-on Activity: Integrating Amazon Bedrock for Text & Image Generation

Requirements

  • AWS Account
  • Basic understanding of Python (for Developers only)
  • Familiarity with LLM prompts
  • Familiarity with Amazon Bedrock

Step 1: Setup Amazon Bedrock

You must explicitly request access to use each of the models in Amazon Bedrock.

  1. Open the Amazon Bedrock Console.
  2. Go to the Model Access page
  3. Enable access to the following models:
  • Amazon Titan Text G1 – Express
  • Meta Llama 3 8B Instruct

Step 2: Create a S3 Bucket

Create a S3 bucket to store the output files.

  1. Open the Amazon S3 Console
  2. Create a S3 bucket (with default settings)

Step 3: Writing the Script

  • Create a new Cloud9 instance
  • Install boto3 using pip: mkdir bedrock-gen cd bedrock-gen python3 -m venv myenv source myenv/bin/activate pip install boto3
  • Copy the Python script below. Remember to fill in the bucket name
import base64
import boto3
import json
import random
from datetime import datetime
from io import BytesIO
from botocore.exceptions import ClientError

# Create a Bedrock Runtime client in the AWS Region of your choice.
client = boto3.client("bedrock-runtime", region_name="us-east-1")

# Create an S3 client
s3_client = boto3.client('s3')

# S3 Bucket name
bucket_name = 'my-bedrock-content'

# Image model
image_model = "amazon.titan-image-generator-v2:0"

# Text models: A dictionary to store the models and their respective native request formats and response handlers.
text_models = {
    "amazon.titan-text-express-v1": {
        "request_format": lambda text_prompt: {
            "inputText": text_prompt,
            "textGenerationConfig": {
                "maxTokenCount": 1024,
                "temperature": 1,
            },
        },
        "response_handler": lambda model_response: model_response["results"][0]["outputText"]
    },
    "meta.llama3-8b-instruct-v1:0": {
        "request_format": lambda text_prompt: {
            "prompt": f"""
            <|begin_of_text|>
            <|start_header_id|>user<|end_header_id|>
            {text_prompt}
            <|eot_id|>
            <|start_header_id|>assistant<|end_header_id|>
            """,
            "max_gen_len": 512,
            "temperature": 0.5,
        },
        "response_handler": lambda model_response: model_response["generation"]
    }
}

# Function to save text data to S3
def save_text_to_s3(bucket_name, file_name, text_data):
    # Upload the text data as a file to the specified S3 bucket
    s3_client.put_object(Body=text_data, Bucket=bucket_name, Key=file_name)
    print(f"Text '{file_name}' uploaded to S3 bucket '{bucket_name}'.")

# Function to save image data to S3
def save_image_to_s3(bucket_name, image_file_name, base64_image_data):
    # Decode the base64 image data
    image_data = base64.b64decode(base64_image_data)

    # Create a temporary in-memory file for the image
    image_file = BytesIO(image_data)

    # Upload the image to the specified S3 bucket
    s3_client.upload_fileobj(
        image_file,
        bucket_name,
        image_file_name
    )
    print(f"Image '{image_file_name}' uploaded to S3 bucket '{bucket_name}'.")

# Generate the S3 URL for the image
def get_s3_image_url(bucket_name, file_name):
    s3_url = f"https://{bucket_name}.s3.amazonaws.com/{file_name}"
    return s3_url

# Define the prompt as user input
prompt_input = input("Enter the topic of your social media post: ")

# Embed the input in prompt
text_prompt = f"""
You are a social media expert.
Write a short post for X(Twitter) on the following topic.
The post should be 2 or 3 sentences short, catchy, and enthusiastic. Use one emoji.

## TOPIC
{prompt_input}
"""

# Generate a random seed for image generation
seed = random.randint(0, 2147483647)

# Iterate over the models for text generation
for model, model_info in text_models.items():
    print(f"Generating text with {model}...")

    # Format the request payload using the model's native structure.
    native_request = model_info["request_format"](text_prompt)

    # Convert the native request to JSON.
    request = json.dumps(native_request)

    # Invoke the model with the request.
    try:
        response = client.invoke_model(modelId=model, body=request)
    except ClientError as e:
        print(f"Error generating text with {model}: {e}")

    # Decode the response body.
    model_response = json.loads(response["body"].read())

    # Extract and print the response text.
    generated_text = model_info["response_handler"](model_response)

    # Define the file name for the generated text
    current_time = datetime.now().strftime("%Y%m%d%H%M%S")
    text_file_name = f"{model}_text_result_{current_time}.txt"

    # Upload the text result to S3
    save_text_to_s3(bucket_name, text_file_name, generated_text)

    # Print the S3 URL for the uploaded text
    text_url = get_s3_image_url(bucket_name, text_file_name)
    print(f"Generated text saved at: {text_url}")

print(f"\\\\nGenerating image with {image_model}...")

# Embed the input in prompt for image
image_prompt = f"""
A vividly colored, engaging thumbnail image for a social media post with the following topic:
{prompt_input}
"""

# Define the image generation request for the image model
image_request = {
    "taskType": "TEXT_IMAGE",
    "textToImageParams": {"text": image_prompt},
    "imageGenerationConfig": {
        "numberOfImages": 1,
        "quality": "standard",
        "cfgScale": 8.0,
        "height": 512,
        "width": 512,
        "seed": seed,
    },
}

# Convert the image request to JSON.
image_request_json = json.dumps(image_request)

# Invoke the image generation model
try:
    image_response = client.invoke_model(modelId=image_model, body=image_request_json)
    # Decode the response body for the image
    image_model_response = json.loads(image_response["body"].read())
    # Extract the base64 image data
    base64_image_data = image_model_response["images"][0]

    # Define the image file name
    image_file_name = f"{image_model}_{seed}.png"

    # Save the generated image to S3
    save_image_to_s3(bucket_name, image_file_name, base64_image_data)

    # Get and print the S3 URL of the uploaded image
    image_url = get_s3_image_url(bucket_name, image_file_name)
    print(f"Generated image saved at: {image_url}")

except ClientError as e:
    print(f"Error generating image with {image_model}: {e}")

Step 4: Running the Script

  1. Save the script as bedrock-gen.py.
  2. Run the script in your terminal or command prompt: python3 bedrock_text_gen.py
  3. Enter a topic for a social media post when prompted by your script.
  4. After completion, go to your S3 Console again and check the output files.

Conclusion

In this hands-on challenge, you learned how to use Amazon Bedrock to solve a real-world problem for a fictional startup. By invoking different foundation models, Android Media can generate high-quality social media content for their clients with minimal effort.

Categorias: Tecnologia

0 comentário

Deixe uma resposta

Este site utiliza o Akismet para reduzir spam. Saiba como seus dados em comentários são processados.