November 2, 2023 No Comments

Introduction to Amazon-bedrock

The world is rapidly evolving, and Generative AI is leading the way to innovation and progress. This technology has made remarkable advancements, allowing it to understand and generate human-like text with incredible accuracy. AWS’ Amazon Bedrock is a fully managed and game-changing platform that bridges the gap between leading AI startups and Amazon’s vast resources, providing a simple API that integrates complex AI models. It’s more than just another AI service, with Bedrock, you have access to a wide range of Foundation Models (FMs) like Jurassic-2, Stable Diffusion, etc. You can choose the model that aligns with your specific use case.

Amazon Bedrock simplifies generative AI app development by providing easy access to Foundation Models (FMs) via a user-friendly API. This accelerates your project without the burden of infrastructure management. It offers a diverse choice of FMs, including those from top AI startups, ensuring a perfect fit for your unique needs. Plus, Bedrock seamlessly integrates with familiar AWS tools for secure, reliable, and scalable AI applications.

Key Use Cases:

Amazon Bedrock is a versatile tool with a broad range of applications for developers and businesses. Some key use cases include:

1. Text Generation: Create content, write code, or generate text for any purpose using the power of AI.

2. Chatbots: Develop conversational AI that can interact intelligently with users and provide solutions to their queries.

3. Search: Enhance search engines with AI to provide more accurate and relevant results.

4. Text Summarization: Automatically generate concise summaries from lengthy text documents.

5. Image Generation: Create stunning visuals or artwork with AI assistance.

Available Models:

With Amazon Bedrock, you have access to a variety of state-of-the-art models, each with its unique capabilities. Some of the notable models include:

1. Jurassic-2 (For Text generation)

2. Claude (For Text generation)

3. Command (For Text generation)

4. Stable Diffusion (For Image generation)

5. Amazon Titan (For Text generation)6. Titan Embeddings G1 – Text (For Embedding)

In this blog, we’ll take a step-by-step journey to explore how you can easily access Amazon Bedrock’s versatile models for chat, text, and image generation.

Initial setup of the Amazon bedrock account
To kickstart the blog, our first step is to set up an account on Amazon Bedrock. If you already have an account, you can skip this part. To set up the bedrock account, follow the below steps:
  • Go to the Amazon bedrock website.
  • Find and click on the “Get Started with Amazon Bedrock” button.
  • Sign in using your AWS (Amazon Web Services) credentials, which usually require your username and password.
  • Once logged in, you’ll be directed to the main dashboard of the Bedrock console.
Request access to the model

Amazon Bedrock users must request access to models for text, chat, and image generation before they can use them. To gain access to the models you need for your Amazon Bedrock projects, follow these steps:

  • Once you have logged in using your AWS credentials, On the left side navigation panel, locate the “Model access” link, or visit the “Edit model access” page as shown below. Then you need to select the checkbox next to the model you want to add access to. For Anthropic models, you must also request access when you click the Request Access button. Models are not available as a default setting in Amazon Bedrock.

  • Select Confirm to add access to any third-party models through Amazon Marketplace. Note: Your use of Amazon Bedrock and its models is subject to the seller’s pricing terms, EULA and the Amazon Bedrock service terms.
  • To complete the process, click the “Save Changes” button located in the lower right corner of the page, as shown in the below image. Please note that it may take several minutes to save changes to the Model access page. Models for which access is granted will appear as “Available” on the Model access page under “Access status”.

In our blog, we will utilize the Jurassic-2 Ultra model to test the chat model and text generation, and for image generation, we will use the Stable Diffusion model. For this, you need to request access to the Jurassic-2 and Stable Diffusion model as shown below.

How to use the chat model using Playground

Let’s begin by using the chat model in the playground. You need to follow the below steps to use the text model:

  • Go to the Bedrock dashboard. On the left-hand panel, locate and click on the “Chat” section. Within the “Chat” section, you’ll find a selection of available models. Choose the model that best suits your needs from the provided options. Here, we have selected the “Jurassic-2 Ultra” model of AI21 labs.

  • We also have an option to provide the specific instructions we want the model to follow. To add this, click on the “Add instructions” button, provided at the left bottom of the playground. Enter your guidelines to help guide the model’s output in the desired direction, and hit the “confirm” button.

    Here, we have added the guidelines to ask the model to behave like Donald Trump it’s like a prompt. To replicate the same, use the below instructions: Let’s chat like Donald Trump! Try to talk the way he does. Use his special way of speaking, words, and phrases. Think about his public speeches and tweets, and talk like that. But, make sure to be respectful and don’t say anything rude or bad. We’re just capturing the style, not crossing any lines. Keep responses short.

  • Now that we’re all set, we can begin chatting with the model. Simply, we need to type our message into the input box and click the “Run” button. Here are some chat examples to get you started.

How to use the text generation model using Playground

After successfully utilizing the chat model, it’s time to explore the text model. To access the text generation playground, please follow the below steps:

  • Start by navigating to the ‘Text’ section on the left-hand panel of the dashboard. Within this section, you’ll find a variety of available models. Choose the model that best suits your specific requirements and objectives from the options provided. Here, we have selected the “Jurassic-2 Ultra” model of AI21 labs.

  • Once we’ve selected the model, we need to enter the prompt, which will act as the input or question to the model. We can also set parameters from the right tab as per our requirements. Then initiate the text generation process by clicking the ‘Run’ button. Below is an instance of a text generation sample:

How to use the image generation model using Playground

Certainly, let’s get started with the image generation model. For this, you need to follow the below steps:

  • To use the Image generation service, you need to select the “Image” option from the left panel of the dashboard.

  • Now that we’ve entered the Image generation playground, it’s time to provide the prompt. You can input the prompt that corresponds to the image you’d like to generate. Here’s an example of a text-to-image generation task:

Leveraging the API for Model Interaction

In the previous section, we explored how to utilize the text, chat, and image generation models within Bedrock’s playground. Now, in this section, we’ll explore how we can use these models using the API through Python. To use the text generation model using the API, you need to follow the below steps:

  • Locate the service you want to access and click the “View API Request” button, which will reveal the request body. The body contains essential code and parameters for programmatic interaction, as shown in the image.

  • For text generation, copy the provided code and add it into the Python script as described below:

				
					import boto3
import json

prompt_data = """
Write a one-liner 90s-style B-movie horror/comedy pitch about 
a giant man-eating Python, with a hilarious and surprising twist.
"""

bedrock = boto3.client(
        service_name="bedrock-runtime",
        region_name="YOUR_REGION",
        aws_access_key_id="YOUR_AWS_ACCESS_KEY",
        aws_secret_access_key="YOUR_AWS_ACCESS_KEY")
payload = {
    "prompt": prompt_data,
    "maxTokens": 512,
    "temperature": 0.8,
    "topP": 0.8,
}

body = json.dumps(payload)
model_id = "ai21.j2-ultra-v1" #You can set different ids
response = bedrock.invoke_model(
    body=body,
    modelId=model_id,
    accept="*/*",
    contentType="application/json",
)

response_body = json.loads(response.get("body").read())
response_text = response_body.get("completions")[0].get("data").get("text")
print(response_text)
				
			

Below is the text generated from the above prompt:

Below is the code for Image Generation, you can create a new python script and add this code to it and test it.

				
					import base64
import os
import random
import boto3
import json


prompt_data = """
A high-red 4k HDR photo of a golden retriever puppy running on a beach.
Action shot, blue sky, white sand, and a big smile. Cinematic film quality.
"""


def main():
for i in range(0, 2):
    seed = random.randint(0, 100000)
    generate_image(prompt=prompt_data, seed=seed, index=i)




def generate_image(prompt: str, seed: int, index: int):
payload = {
"text_prompts": [{"text": prompt}],
"cfg_scale": 12,
"seed": seed,
"steps": 80,
}


# Create the client and invoke the model.
bedrock = boto3.client(
    service_name="bedrock-runtime",
    region_name=”YOUR_REGION”,
/          aws_access_key_id="YOUR_AWS_ACCESS_KEY",
          aws_secret_access_key="YOUR_AWS_ACCESS_KEY")
     	body = json.dumps(payload)
model_id = "stability.stable-diffusion-xl-v0"
response = bedrock.invoke_model(
    body=body,
    modelId=model_id,
    accept="application/json",
    contentType="application/json",
)


# Get the image from the response. It is base64 encoded.
response_body = json.loads(response.get("body").read())
artifact = response_body.get("artifacts")[0]
image_encoded = artifact.get("base64").encode("utf-8")
image_bytes = base64.b64decode(image_encoded)


# Save image to a file in the output directory.
output_dir = “Outputs”
os.makedirs(output_dir, exist_ok=True)
file_name = f"{output_dir}/generated-{index}.png"
with open(file_name, "wb") as f:
f.write(image_bytes)




if __name__ == "__main__":
main()
				
			

Below is the generated image

Write a comment

Your email address will not be published. Required fields are marked *

Pragnakalp Techlabs: Your trusted partner in Python, AI, NLP, Generative AI, ML, and Automation. Our skilled experts have successfully delivered robust solutions to satisfied clients, driving innovation and success.