Hands-On Lab: Exploring Core GenAI Concepts with Amazon Bedrock
Core GenAI Concepts
Hands-On Lab: Exploring Core GenAI Concepts with Amazon Bedrock
Welcome to this guided lab! In this hands-on session, you will bridge the gap between generative AI theory and practice. We will explore foundation models (FMs), tokenization, and how inference parameters like temperature impact the output, all using Amazon Bedrock.
Prerequisites
Before starting this lab, ensure you have the following:
- AWS Account: An active AWS account with Administrator or PowerUser access.
- AWS CLI: Installed and authenticated locally (
aws configure). - IAM Permissions: Your user must have permissions to access Amazon Bedrock (
bedrock:InvokeModel). - Prior Knowledge: Basic understanding of what a Foundation Model is and how JSON structures work.
Learning Objectives
By completing this lab, you will be able to:
- Enable access to Foundation Models within Amazon Bedrock.
- Invoke a generative AI model using both the AWS Management Console and the AWS CLI.
- Manipulate inference parameters (like
temperature) to control model creativity and determinism. - Apply basic prompt engineering techniques to structure model outputs.
Architecture Overview
The following diagram illustrates how we will interact with the foundation models via Amazon Bedrock.
Step-by-Step Instructions
Step 1: Request Model Access in Amazon Bedrock
By default, foundation models in Amazon Bedrock are not immediately available; you must explicitly request access to them. We will use Amazon Titan Text G1 - Express for this lab.
- Log in to the AWS Management Console.
- Navigate to Amazon Bedrock.
- In the left navigation pane, scroll to the bottom and select Model access.
- Click the Manage model access button.
- Check the box next to Titan Text G1 - Express under Amazon.
- Scroll to the bottom and click Save changes.
📸 Screenshot: Bedrock Model Access screen showing "Access granted" next to Amazon Titan Text G1 - Express.
[!TIP] Model access is usually granted instantly for Amazon Titan models. If it shows "In Progress", refresh the page after 1-2 minutes.
Step 2: Your First Model Invocation (Zero-Shot Prompt)
Now that we have access, let's invoke the model to understand how it processes a basic prompt.
aws bedrock-runtime invoke-model \
--model-id amazon.titan-text-express-v1 \
--region <YOUR_REGION> \
--body '{"inputText": "Explain the concept of deep learning in one simple sentence.", "textGenerationConfig": {"temperature": 0.0}}' \
--cli-binary-format raw-in-base64-out \
--accept "application/json" \
--content-type "application/json" \
output.json
cat output.json▶Console alternative
- In the Amazon Bedrock console, go to Playgrounds > Text.
- Click Select model, choose Amazon, and then Titan Text G1 - Express.
- In the chat box, type:
Explain the concept of deep learning in one simple sentence. - Click Run.
Checkpoints
Checkpoint 1: Verify Invocation
Run cat output.json in your terminal. You should see a JSON response containing a results array with an outputText field containing the model's response.
Expected Output Snippet:
{"inputTextTokenCount":10,"results":[{"tokenCount":21,"outputText":"Deep learning is a machine learning technique that teaches computers to do what comes naturally to humans...","completionReason":"FINISH"}]}Step-by-Step Instructions (Continued)
Step 3: Manipulating Nondeterminism (Temperature)
Generative AI models can be tuned for either focused, deterministic answers or creative, highly variable outputs. This is controlled by the Temperature parameter. Let's ask a creative question with a high temperature.
aws bedrock-runtime invoke-model \
--model-id amazon.titan-text-express-v1 \
--region <YOUR_REGION> \
--body '{"inputText": "Write a two-sentence sci-fi story about a robot on Mars.", "textGenerationConfig": {"temperature": 1.0, "topP": 0.9}}' \
--cli-binary-format raw-in-base64-out \
--accept "application/json" \
--content-type "application/json" \
output_creative.json
cat output_creative.json[!TIP] Run this exact command three times. Because the temperature is set to
1.0, you will likely get a completely different story each time! This is the concept of nondeterminism in action.
▶Console alternative
- In the Bedrock Text Playground, look at the right-hand Configurations panel.
- Slide the Temperature slider to
1. - Ask the model to "Write a two-sentence sci-fi story about a robot on Mars."
- Click Run multiple times to observe the changing responses.
Step 4: Few-Shot Prompt Engineering
Prompt engineering is the practice of crafting clear inputs to guide model outputs. Here, we provide context and examples (few-shot) to force the model to output a specific format.
aws bedrock-runtime invoke-model \
--model-id amazon.titan-text-express-v1 \
--region <YOUR_REGION> \
--body '{"inputText": "Extract the sentiment from the following statements.\n\nStatement: I love this new phone!\nSentiment: POSITIVE\n\nStatement: The battery life is terrible and it keeps crashing.\nSentiment: NEGATIVE\n\nStatement: The screen is okay, but nothing special.\nSentiment:", "textGenerationConfig": {"temperature": 0.0}}' \
--cli-binary-format raw-in-base64-out \
--accept "application/json" \
--content-type "application/json" \
output_sentiment.json
cat output_sentiment.jsonCheckpoints
Checkpoint 2: Verify Formatting
Open output_sentiment.json. Because we used a temperature of 0.0 and provided a strict pattern, the output should simply be NEUTRAL (or a similar single-word classification) without extra conversational fluff.
Concept Review
During this lab, you interacted directly with a Foundation Model (FM). Let's briefly recap what happens behind the scenes when you send your text to the model:
| Concept | Definition | In This Lab |
|---|---|---|
| Tokens | The fundamental units of data processed by an LLM (words or sub-words). | Bedrock counts your inputTextTokenCount for billing. |
| Temperature | Controls the randomness/creativity of the output. | We changed it from 0.0 (factual) to 1.0 (creative). |
| Prompt Engineering | Structuring inputs to guide the model. | We used "few-shot" prompting to enforce sentiment output. |
Clean-Up / Teardown
[!WARNING] Remember to run the teardown commands or follow the clean-up steps to keep your workspace tidy. However, for Amazon Bedrock, you only pay for what you use (per token). Simply having model access enabled does not incur an hourly cost.
Since Bedrock inference is serverless, there are no provisioned instances to delete. However, you should clean up your local directory:
# Remove the local JSON output files created during the lab
rm output.json output_creative.json output_sentiment.jsonCost Estimate
- Amazon Bedrock: Amazon Titan Text G1 - Express costs roughly $0.0002 per 1,000 input tokens and $0.0006 per 1,000 output tokens.
- Total Estimated Cost:
< $0.01. This lab uses well under 1,000 tokens.
Troubleshooting
| Error / Issue | Probable Cause | Solution |
|---|---|---|
AccessDeniedException | IAM permissions or Model Access not granted. | Verify your IAM user has bedrock:InvokeModel. Ensure you completed Step 1 to request model access. |
ValidationException | Malformed JSON payload in the CLI command. | Check for missing quotation marks or unescaped characters in the --body string. |
ThrottlingException | Exceeded API rate limits. | Wait a few seconds and try the invocation again. |
| Could not resolve endpoint | Incorrect or unsupported AWS region. | Ensure --region is set to a region that supports Bedrock (e.g., us-east-1 or us-west-2). |