Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 14, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 14, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 14, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 14, 2025

      I test a lot of AI coding tools, and this stunning new OpenAI release just saved me days of work

      May 14, 2025

      How to use your Android phone as a webcam when your laptop’s default won’t cut it

      May 14, 2025

      The 5 most customizable Linux desktop environments – when you want it your way

      May 14, 2025

      Gen AI use at work saps our motivation even as it boosts productivity, new research shows

      May 14, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Strategic Cloud Partner: Key to Business Success, Not Just Tech

      May 14, 2025
      Recent

      Strategic Cloud Partner: Key to Business Success, Not Just Tech

      May 14, 2025

      Perficient’s “What If? So What?” Podcast Wins Gold at the 2025 Hermes Creative Awards

      May 14, 2025

      PIM for Azure Resources

      May 14, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Windows 11 24H2’s Settings now bundles FAQs section to tell you more about your system

      May 14, 2025
      Recent

      Windows 11 24H2’s Settings now bundles FAQs section to tell you more about your system

      May 14, 2025

      You can now share an app/browser window with Copilot Vision to help you with different tasks

      May 14, 2025

      Microsoft will gradually retire SharePoint Alerts over the next two years

      May 14, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Build generative AI applications on Amazon Bedrock with the AWS SDK for Python (Boto3)

    Build generative AI applications on Amazon Bedrock with the AWS SDK for Python (Boto3)

    November 22, 2024

    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 through a single API, along with a broad set of capabilities to build generative AI applications with security, privacy, and responsible AI. With Amazon Bedrock, you can experiment with and evaluate top FMs for your use case, privately customize them with your data using techniques such as fine-tuning and Retrieval Augmented Generation (RAG), and build agents that run tasks using your enterprise systems and data sources. Because Amazon Bedrock is serverless, you don’t have to manage any infrastructure, and you can securely integrate and deploy generative AI capabilities into your applications using the AWS services you are already familiar with.

    In this post, we demonstrate how to use Amazon Bedrock with the AWS SDK for Python (Boto3) to programmatically incorporate FMs.

    Solution overview

    The solution uses an AWS SDK for Python script with features that invoke Anthropic’s Claude 3 Sonnet on Amazon Bedrock. By using this FM, it generates an output using a prompt as input. The following diagram illustrates the solution architecture.

    Prerequisites

    Before you invoke the Amazon Bedrock API, make sure you have the following:

    • An AWS account that provides access to AWS services, including Amazon Bedrock
    • The AWS Command Line Interface (AWS CLI) set up
    • An AWS Identity and Access Management (IAM) user set up for the Amazon Bedrock API and appropriate permissions added to the IAM user
    • The IAM user access key and secret key to configure the AWS CLI and permissions
    • Access to FMs on Amazon Bedrock
    • The latest Boto3 library
    • The minimum Python version 3.8 configured with your integrated development environment (IDE)

    Deploy the solution

    After you complete the prerequisites, you can start using Amazon Bedrock. Begin by scripting with the following steps:

    1. Import the required libraries:
    import boto3
    import json
    1. Set up the Boto3 client to use the Amazon Bedrock runtime and specify the AWS Region:
    # Set up the Amazon Bedrock client
    bedrock_client = boto3.client(
        	service_name="bedrock-runtime",
        region_name="us-east-1"
    )
    
    1. Define the model to invoke using its model ID. In this example, we use Anthropic’s Claude 3 Sonnet on Amazon Bedrock:
    # Define the model ID
    model_id = "anthropic.claude-3-sonnet-20240229-v1:0"
    
    1. Assign a prompt, which is your message that will be used to interact with the FM at invocation:
    # Prepare the input prompt.
    prompt = "Hello, how are you?"
    

    Prompt engineering techniques can improve FM performance and enhance results.

    Before invoking the Amazon Bedrock model, we need to define a payload, which acts as a set of instructions and information guiding the model’s generation process. This payload structure varies depending on the chosen model. In this example, we use Anthropic’s Claude 3 Sonnet on Amazon Bedrock. Think of this payload as the blueprint for the model, and provide it with the necessary context and parameters to generate the desired text based on your specific prompt. Let’s break down the key elements within this payload:

    • anthropic_version – This specifies the exact Amazon Bedrock version you’re using.
    • max_tokens – This sets a limit on the total number of tokens the model can generate in its response. Tokens are the smallest meaningful unit of text (word, punctuation, subword) processed and generated by large language models (LLMs).
    • temperature – This parameter controls the level of randomness in the generated text. Higher values lead to more creative and potentially unexpected outputs, and lower values promote more conservative and consistent results.
    • top_k – This defines the number of most probable candidate words considered at each step during the generation process.
    • top_p – This influences the sampling probability distribution for selecting the next word. Higher values favor frequent words, whereas lower values allow for more diverse and potentially surprising choices.
    • messages – This is an array containing individual messages for the model to process.
    • role – This defines the sender’s role within the message (the user for the prompt you provide).
    • content – This array holds the actual prompt text itself, represented as a “text” type object.
    1. Define the payload as follows:
    payload = {
        "anthropic_version": "bedrock-2023-05-31",
        "max_tokens": 2048,
        "temperature": 0.9,
        "top_k": 250,
        "top_p": 1,
        "messages": [
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": prompt
                    }
                ]
            }
        ]
    }
    
    1. You have set the parameters and the FM you want to interact with. Now you send a request to Amazon Bedrock by providing the FM to interact with and the payload that you defined:
    # Invoke the Amazon Bedrock model
    response = bedrock_client.invoke_model(
        modelId=model_id,
        body=json.dumps(payload)
    )
    1. After the request is processed, you can display the result of the generated text from Amazon Bedrock:
    # Process the response
    result = json.loads(response["body"].read())
    generated_text = "".join([output["text"] for output in result["content"]])
    print(f"Response: {generated_text}")
    

    Let’s look at our complete script:

    import boto3
    import json
    
    # Set up the Amazon Bedrock client
    bedrock_client = boto3.client(
        service_name="bedrock-runtime",
        region_name="us-east-1"
    )
    
    # Define the model ID
    model_id = "anthropic.claude-3-sonnet-20240229-v1:0"
    
    # Prepare the input prompt
    prompt = "Hello, how are you?"
    
    # Create the request payload
    payload = {
        "anthropic_version": "bedrock-2023-05-31",
        "max_tokens": 2048,
        "temperature": 0.9,
        "top_k": 250,
        "top_p": 1,
        "messages": [
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": prompt
                    }
                ]
            }
        ]
    }
    
    # Invoke the Amazon Bedrock model
    response = bedrock_client.invoke_model(
        modelId=model_id,
        body=json.dumps(payload)
    )
    
    # Process the response
    result = json.loads(response["body"].read())
    generated_text = "".join([output["text"] for output in result["content"]])
    print(f"Response: {generated_text}")
    

     

    Invoking the model with the prompt “Hello, how are you?” will yield the result shown in the following screenshot.

    Clean up

    When you’re done using Amazon Bedrock, clean up temporary resources like IAM users and Amazon CloudWatch logs to avoid unnecessary charges. Cost considerations depend on usage frequency, chosen model pricing, and resource utilization while the script runs. See Amazon Bedrock Pricing for pricing details and cost-optimization strategies like selecting appropriate models, optimizing prompts, and monitoring usage.

    Conclusion

    In this post, we demonstrated how to programmatically interact with Amazon Bedrock FMs using Boto3. We explored invoking a specific FM and processing the generated text, showcasing the potential for developers to use these models in their applications for a variety of use cases, such as:

    • Text generation – Generate creative content like poems, scripts, musical pieces, or even different programming languages
    • Code completion – Enhance developer productivity by suggesting relevant code snippets based on existing code or prompts
    • Data summarization – Extract key insights and generate concise summaries from large datasets
    • Conversational AI – Develop chatbots and virtual assistants that can engage in natural language conversations

    Stay curious and explore how generative AI can revolutionize various industries. Explore the different models and APIs and run comparisons of how each model provides different outputs. Find the model that will fit your use case and use this script as a base to create agents and integrations in your solution.


    About the Author

    Merlin Naidoo is a Senior Technical Account Manager at AWS with over 15 years of experience in digital transformation and innovative technical solutions. His passion is connecting with people from all backgrounds and leveraging technology to create meaningful opportunities that empower everyone. When he’s not immersed in the world of tech, you can find him taking part in active sports.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleOrchestrate generative AI workflows with Amazon Bedrock and AWS Step Functions
    Next Article Improve factual consistency with LLM Debates

    Related Posts

    Machine Learning

    Georgia Tech and Stanford Researchers Introduce MLE-Dojo: A Gym-Style Framework Designed for Training, Evaluating, and Benchmarking Autonomous Machine Learning Engineering (MLE) Agents

    May 15, 2025
    Machine Learning

    A Step-by-Step Guide to Build an Automated Knowledge Graph Pipeline Using LangGraph and NetworkX

    May 15, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    How To Write Test Cases For Button 

    Development

    I’m Not a Coder! But, I can teach you to write 1 Lakh Lines of Python Code in Minutes thanks to AI technology!

    Artificial Intelligence

    Use Vue or React Components in a Livewire App with MingleJS

    Development

    Laravel Advanced String Package

    Development

    Highlights

    Microsoft removes guide for installing Windows 11 on unsupported PCs – but this hack still works

    February 4, 2025

    A support page no longer describes the Registry hack that lets you bypass Windows 11’s…

    Device emulator-5554 is not online – while executing my appium code on android emulator

    November 15, 2024

    Artificial General Intelligence (AGI): Crash Course – The Full Guidebook by Bookspotz

    August 29, 2024

    Understanding Accounts in Salesforce: A Detailed Guide

    June 27, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.