Generative AI with AWS Bedrock

Amarpreet Singh
5 min readDec 7, 2023

--

Photo by Pietro Jeng on Unsplash

In the ever-evolving landscape of artificial intelligence, one of the most captivating advancements has been the rise of generative AI. This transformative technology has captured the imagination of researchers, businesses, and creators alike. Generative AI enables machines to generate text, images, and even music that closely resemble human creations. As we delve into the possibilities, it becomes evident why the industry is gravitating towards generative AI.

“Generative models have the potential to fundamentally change the way we interact with machines.” — Anonymous

Generative AI operates on a simple yet powerful principle: given some input or context, it can produce meaningful and contextually relevant output. One of the most remarkable instances of generative AI is chatbots, and among them, OpenAI’s GPT (Generative Pre-trained Transformer) models stand out. GPT models, like GPT-3 and its successors, have demonstrated the capacity to understand and generate human-like text, carrying on coherent conversations and generating content across a wide range of topics.

The excitement surrounding generative AI is perfectly encapsulated in the words of the renowned computer scientist Yann LeCun: “Generative models are gonna change the world.” These models are not just tools; they are technological innovations that are shaping the way we interact with AI, create content, and solve complex problems.

Enter Bedrock

But what exactly is the bedrock upon which generative AI stands? Enter Amazon Bedrock, a revolutionary platform that empowers you to harness the potential of generative AI. Bedrock is a fully managed service that streamlines the process of building generative AI applications. At its core, Bedrock relies on Foundation Models (FMs), which are pre-trained machine learning models that serve as the building blocks for various AI applications. These FMs come from leading AI companies, including AI21 Labs, Anthropic, Cohere, Meta, Stability AI, and Amazon, offering a wide array of options to choose from.

AWS Bedrock empowers organizations to experiment, evaluate, and customize these FMs privately with their proprietary data. With Bedrock, developers can create AI-powered solutions that leverage an organization’s data and systems without the burden of dealing with the complexities of infrastructure management. This simplifies the development process and accelerates the deployment of intelligent AI agents within an enterprise.

One of the standout features of AWS Bedrock is model inference. Once you’ve customized your foundation model, you can seamlessly integrate it into your applications, allowing you to generate content, answer questions, and assist users in real-time. This opens up a world of possibilities for enhancing user experiences and automating tasks.

Tailoring AI to Perfection

In the realm of AI, one size does not fit all. That’s where custom models step in, allowing you to fine-tune and shape AI to meet your specific needs. Amazon Bedrock offers two powerful avenues for customization: the Fine-Tuned model and the Continued Pre-training model. The former lets you enhance a model’s performance in specific tasks by providing labeled training data, while the latter empowers you to imbue AI with domain-specific knowledge not found in base models. Whether you’re optimizing for precision or expanding AI’s horizons, the model customization job is your gateway. It starts with uploading your training and validation datasets to Amazon S3, configuring hyperparameters, and letting AI learn and adapt. The result? A tailored AI companion ready to assist and excel in your unique domain.

Sample Training Data and How to Train Using Available Models

Let’s walk through a simple example to demonstrate the power of Amazon Bedrock. Imagine you want to build a customer support chatbot that understands and responds to user inquiries about your products and services. You can start by preparing a training dataset with user queries and corresponding answers. Here’s a snippet of sample training data in JSONL format:

{"prompt": "What are the features of product X?", "completion": "Product X offers a range of features including..."}
{"prompt": "How can I contact customer support?", "completion": "You can reach our customer support team at..."}
{"prompt": "Tell me about pricing for service Y.", "completion": "Service Y has flexible pricing plans..."}

Once you have your training data ready, you can create a Fine-Tuned model customization or Continued Pre-training job with Bedrock, specifying the data location on Amazon S3, hyperparameters, and other configuration details. Bedrock will take care of the training process, and you’ll have a chatbot model tailored to your business needs.

It’s important to note that when customizing models with your data, you can only use Amazon’s Titan models as a foundation. This ensures that your customizations are built upon reliable and high-quality base models, resulting in AI companions that are both accurate and efficient.

Data Security and Precautions

The AWS shared responsibility model applies to data protection in Amazon Bedrock. As you embark on your AI journey, it’s crucial to prioritize data security and privacy. When customizing models with your proprietary data, follow essential best practices to ensure the utmost protection. Generally, when fine-tuning models with your own data, you get a copy of that model which is private to you. Follow best practices like data anonymization, eliminating any personally identifiable information and sensitive details. Next, implement strong encryption measures for data both at rest and in transit, using the encryption options available through Amazon Bedrock. To control access, enforce strict permissions and access controls, allowing only authorized personnel within your organization to handle data and customization processes. Regular auditing and monitoring of data usage are vital to detecting anomalies or security breaches. Comply with relevant data protection regulations and standards, and be conscious of data residency requirements. Manage data throughout its lifecycle, automating deletion or archiving when necessary. You can also implement automatic detection and remediation mechanism if there’s a security misconfiguration. By adhering to these precautions, you’ll ensure that your AI journey with Amazon Bedrock remains productive, compliant, and secure.

API Calls for Accessing 3rd Party Models

Amazon Bedrock provides access to a wide range of third-party models, expanding your AI toolkit. You can make API calls to integrate these models into your applications seamlessly. Here’s a simple Python code snippet to demonstrate how to use Bedrock’s API to interact with a third-party model:

prompt_text = f'\n\nHuman: Write {language} code for the following instructions: {message}\n\nAssistant:'
api_request = {
"modelId": "anthropic.claude-v2:1",
"contentType": "application/json",
"accept": "*/*",
"body": {
"prompt": prompt_text,
"max_tokens_to_sample": 300,
"temperature": 0.5,
"top_k": 250,
"top_p": 1,
"stop_sequences": ["\n\nHuman:"],
"anthropic_version": "bedrock-2023-05-31"
}
}

bedrock = boto3.client("bedrock-runtime", region_name="us-west-2", config=botocore.config.Config(read_timeout=300, retries={'max_attempts': 3}))
response = bedrock.invoke_model(
modelId=api_request["modelId"],
contentType=api_request["contentType"],
accept=api_request["accept"],
body=json.dumps(api_request["body"])
)

This code snippet generates code in any programming language and showcases how to make API calls to access third-party models seamlessly within your applications, expanding the scope and capabilities of your AI solutions.

Full source code can be accessed here!

In summary, Amazon Bedrock is more than just a platform; it’s your gateway to a new era of generative AI. It equips developers and organizations with the tools and technology needed to unlock AI’s full potential in a simple and secure way. With features like Foundation Models, customization options, robust data security, and access to third-party models, Bedrock offers a comprehensive AI ecosystem. As the AI landscape evolves, Bedrock stands as a reliable foundation for building the future of AI-powered solutions. Whether you’re refining a chatbot, broadening AI capabilities, or incorporating third-party models, Bedrock is your trusted ally in exploring the endless possibilities of generative AI.

Let me know in comments what your experience has been with Bedrock.

Happy Coding!

Further Reading

--

--

Amarpreet Singh

I'm a Solution Architect and engineering leader based in San Francisco, passionate about exploring new technologies and tackling interesting challenges.