DEV Community

Pradeep Kasula
Pradeep Kasula

Posted on

Accessing Amazon Nova Lite on AWS Bedrock

Comprehensive Guide to Accessing Amazon Nova Lite on AWS Bedrock with Sample Use Cases

Amazon Nova Lite is a powerful multimodal foundation model available on Amazon Bedrock that can process text, images, and video inputs to generate text outputs. With its impressive context window of 300K tokens and lightning-fast processing capabilities, it's an excellent choice for developers looking to build sophisticated AI applications. In this comprehensive guide, I'll walk you through everything you need to know about accessing and leveraging Amazon Nova Lite, complete with sample use cases to demonstrate its capabilities.

Understanding Amazon Nova Lite

Amazon Nova Lite is part of Amazon's new generation of state-of-the-art foundation models that deliver frontier intelligence with industry-leading price performance[1]. It's positioned as a very low-cost multimodal model that processes image, video, and text inputs with exceptional speed[1].

Key Specifications

Feature Details
Model ID amazon.nova-lite-v1:0
Inference Profile ID us.amazon.nova-lite-v1:0
Input Modalities Text, Image, Video
Output Modalities Text
Context Window 300K tokens
Max Output Tokens 5K tokens
Supported Languages 200+
Available Regions US East (N. Virginia)
Document Support pdf, csv, doc, docx, xls, xlsx, html, txt, md
API Support Converse API, InvokeAPI
Streaming Yes

Pricing

Amazon Nova Lite offers competitive pricing:

  • Input Cost: $0.06 per million tokens
  • Output Cost: $0.24 per million tokens[3]

This makes it significantly more cost-effective than many competing models while maintaining high performance levels.

Getting Started with Amazon Nova Lite

Before you can use Amazon Nova Lite, you need to set up your AWS environment and request access to the model. Let's walk through the process step by step.

Prerequisites

  1. AWS Account: You need an active AWS account with appropriate permissions.
  2. IAM Access: Your IAM User or Role should have the AmazonBedrockFullAccess AWS managed policy attached[5].
  3. Region Selection: Amazon Nova Lite is currently available only in US East (N. Virginia) (us-east-1)[1][4].

Requesting Access to Amazon Nova Lite

Follow these steps to request access to the model:

  1. Open the Amazon Bedrock console at https://console.aws.amazon.com/bedrock/
  2. Ensure you're in the US East (N. Virginia) (us-east-1) Region
  3. From the left navigation pane, choose Model access under Bedrock configurations
  4. In What is model access, choose Enable specific models
  5. Select Nova Lite from the Base models list
  6. Click Next
  7. On the Review and submit page, click Submit
  8. Refresh the Base models table to confirm that access has been granted[4][5]

Setting Up Your Development Environment

To interact with Amazon Nova Lite, you'll need to set up your development environment:

  1. Install the AWS SDK: For Python, install boto3:
pip install boto3
Enter fullscreen mode Exit fullscreen mode
  1. Configure AWS Credentials: Ensure your AWS credentials are properly configured, either through environment variables, the AWS credentials file, or IAM roles[5].

Accessing Amazon Nova Lite through APIs

Amazon Nova Lite can be accessed through two primary APIs: InvokeAPI and Converse API. Let's explore both methods.

Using InvokeAPI

The InvokeAPI is a straightforward way to interact with the model. Here's a sample Python code to get you started:

import boto3
import json
from datetime import datetime

# Create a Bedrock Runtime client
client = boto3.client("bedrock-runtime", region_name="us-east-1")

LITE_MODEL_ID = "us.amazon.nova-lite-v1:0"

# Define your system prompt(s)
system_list = [
    {
        "role": "system",
        "content": [
            {
                "text": "You are a helpful AI assistant."
            }
        ]
    }
]

# Define your user message
user_message = "Explain the concept of machine learning in simple terms."

# Create the request body
request_body = {
    "anthropic_version": "bedrock-2023-05-31",
    "system": system_list[0]["content"][0]["text"],
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": user_message
                }
            ]
        }
    ],
    "max_tokens": 1000,
    "temperature": 0.7
}

# Invoke the model
response = client.invoke_model(
    modelId=LITE_MODEL_ID,
    body=json.dumps(request_body)
)

# Parse and print the response
response_body = json.loads(response["body"].read())
print(response_body["content"][0]["text"])
Enter fullscreen mode Exit fullscreen mode

Using Converse API with Streaming

The Converse API provides a more interactive experience, especially with streaming enabled. Here's how to implement it:

import boto3
import json

# Create a Bedrock Runtime client
client = boto3.client("bedrock-runtime", region_name="us-east-1")

# Set the model ID
model_id = "amazon.nova-lite-v1:0"

# Define the user message
user_message = "Describe the purpose of a 'hello world' program in one line."

# Create the request
request = {
    "modelId": model_id,
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "text": user_message
                }
            ]
        }
    ],
    "inferenceConfig": {
        "maxTokens": 512,
        "temperature": 0.5,
        "topP": 0.9
    }
}

# Send the request and process the streaming response
response = client.converse_stream(**request)

# Process and print the streaming response
for event in response.get("stream"):
    if "contentBlockDelta" in event:
        print(event["contentBlockDelta"]["delta"]["text"], end="")
Enter fullscreen mode Exit fullscreen mode

This code demonstrates how to receive and process streaming responses, which is particularly useful for creating more interactive applications[6].

Sample Use Cases for Amazon Nova Lite

Now that we understand how to access Amazon Nova Lite, let's explore some practical use cases that showcase its capabilities.

Use Case 1: Intelligent Document Analysis

Amazon Nova Lite excels at analyzing complex documents, including PDFs, spreadsheets, and more. Here's a sample implementation for extracting insights from financial reports:

import boto3
import json
import base64

# Create a Bedrock Runtime client
client = boto3.client("bedrock-runtime", region_name="us-east-1")

# Function to encode an image file to base64
def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

# Path to the financial report (PDF first page as image)
financial_report_image = "quarterly_report_page1.jpg"
base64_image = encode_image(financial_report_image)

# Create the request
request = {
    "modelId": "amazon.nova-lite-v1:0",
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "text": "Analyze this financial report page and extract the key financial metrics, revenue growth, and any highlighted risks."
                },
                {
                    "image": base64_image
                }
            ]
        }
    ],
    "inferenceConfig": {
        "maxTokens": 1000,
        "temperature": 0.2,
        "topP": 0.9
    }
}

# Invoke the model
response = client.converse(**request)

# Process and print the response
for content_block in response["output"]["message"]["content"]:
    if "text" in content_block:
        print(content_block["text"])
Enter fullscreen mode Exit fullscreen mode

This use case demonstrates Nova Lite's ability to process images and extract meaningful information from complex documents.

Use Case 2: Multimodal Customer Support Assistant

Create a customer support assistant that can analyze product images and provide troubleshooting advice:

import boto3
import json
import base64

# Create a Bedrock Runtime client
client = boto3.client("bedrock-runtime", region_name="us-east-1")

# Function to encode an image file to base64
def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

# Path to the customer's product image
product_image = "device_error_screen.jpg"
base64_image = encode_image(product_image)

# System prompt for the assistant
system_prompt = """
You are a technical support assistant for our smart home devices. 
When customers send images of their devices showing error messages or problems, 
analyze the image carefully and provide step-by-step troubleshooting instructions.
Be concise but thorough, and always prioritize safety.
"""

# Create the request
request = {
    "modelId": "amazon.nova-lite-v1:0",
    "messages": [
        {
            "role": "system",
            "content": [
                {
                    "text": system_prompt
                }
            ]
        },
        {
            "role": "user",
            "content": [
                {
                    "text": "My smart thermostat is showing this error. Can you help me fix it?"
                },
                {
                    "image": base64_image
                }
            ]
        }
    ],
    "inferenceConfig": {
        "maxTokens": 1000,
        "temperature": 0.3,
        "topP": 0.9
    }
}

# Invoke the model with streaming for a more interactive experience
response = client.converse_stream(**request)

# Process and print the streaming response
for event in response.get("stream"):
    if "contentBlockDelta" in event:
        print(event["contentBlockDelta"]["delta"]["text"], end="")
Enter fullscreen mode Exit fullscreen mode

This implementation showcases Nova Lite's ability to analyze images and provide contextual responses, making it ideal for customer support applications.

Use Case 3: Video Content Analysis

Amazon Nova Lite can also process video inputs, making it suitable for applications like content moderation or video summarization:

import boto3
import json
import base64

# Create a Bedrock Runtime client
client = boto3.client("bedrock-runtime", region_name="us-east-1")

# Function to encode a video file to base64
def encode_video(video_path):
    with open(video_path, "rb") as video_file:
        return base64.b64encode(video_file.read()).decode('utf-8')

# Path to the video file
video_path = "product_demo.mp4"
base64_video = encode_video(video_path)

# Create the request
request = {
    "modelId": "amazon.nova-lite-v1:0",
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "text": "Analyze this product demonstration video and create a detailed summary of the key features shown, the benefits highlighted, and any technical specifications mentioned."
                },
                {
                    "video": base64_video
                }
            ]
        }
    ],
    "inferenceConfig": {
        "maxTokens": 2000,
        "temperature": 0.4,
        "topP": 0.9
    }
}

# Invoke the model
response = client.converse(**request)

# Process and print the response
for content_block in response["output"]["message"]["content"]:
    if "text" in content_block:
        print(content_block["text"])
Enter fullscreen mode Exit fullscreen mode

This use case demonstrates Nova Lite's video processing capabilities, which can be valuable for marketing teams, content creators, and moderation systems.

Advanced Techniques and Best Practices

To get the most out of Amazon Nova Lite, consider these advanced techniques and best practices.

Fine-Tuning for Your Use Case

While Amazon Nova Lite comes with impressive out-of-the-box capabilities, fine-tuning can help tailor the model to your specific needs. Amazon Bedrock supports fine-tuning for Nova models, allowing you to customize the model's behavior using your own data[1].

Optimizing Prompts for Better Results

Effective prompt engineering can significantly improve the quality of responses from Nova Lite:

  1. Be Specific: Clearly state what you want the model to do
  2. Provide Context: Include relevant background information
  3. Use Examples: Demonstrate the desired output format
  4. Control Output Format: Specify the structure you want for the response

Managing Costs

To optimize costs when using Amazon Nova Lite:

  1. Monitor Token Usage: Keep track of input and output tokens
  2. Batch Processing: Where possible, batch requests to reduce API calls
  3. Implement Caching: Cache common responses to avoid redundant API calls
  4. Optimize Context Window: Only include necessary information in your prompts

Error Handling and Retries

Implement robust error handling to manage API failures gracefully:

import boto3
import json
import time
from botocore.exceptions import ClientError

def invoke_model_with_retry(client, request, max_retries=3, backoff_factor=2):
    retries = 0
    while retries < max_retries:
        try:
            response = client.converse(**request)
            return response
        except ClientError as e:
            error_code = e.response.get("Error", {}).get("Code")

            # Handle different types of errors
            if error_code == "ThrottlingException":
                wait_time = backoff_factor ** retries
                print(f"Rate limited. Waiting {wait_time} seconds...")
                time.sleep(wait_time)
                retries += 1
            elif error_code == "ModelStreamErrorException":
                print("Model streaming error. Retrying with non-streaming API...")
                return client.converse(**request)
            else:
                # For other errors, raise the exception
                raise

    # If we've exhausted retries
    raise Exception(f"Failed after {max_retries} retries")
Enter fullscreen mode Exit fullscreen mode

Building a Complete Application: Content Creation Assistant

Let's put everything together to create a comprehensive content creation assistant that leverages Amazon Nova Lite's capabilities. This application will help bloggers generate high-quality, SEO-optimized content.

Step 1: Set Up the Project Structure

content-assistant/
├── app.py
├── utils/
│   ├── __init__.py
│   ├── bedrock_client.py
│   ├── prompt_templates.py
│   └── content_processor.py
├── templates/
│   ├── index.html
│   └── result.html
├── static/
│   ├── css/
│   └── js/
└── requirements.txt
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the Bedrock Client Utility

# utils/bedrock_client.py
import boto3
import json
import base64
from botocore.exceptions import ClientError

class BedrockClient:
    def __init__(self, region="us-east-1"):
        self.client = boto3.client("bedrock-runtime", region_name=region)
        self.model_id = "amazon.nova-lite-v1:0"

    def encode_image(self, image_path):
        with open(image_path, "rb") as image_file:
            return base64.b64encode(image_file.read()).decode('utf-8')

    def generate_content(self, prompt, image_path=None, temperature=0.7, max_tokens=2000):
        content_blocks = [{"text": prompt}]

        if image_path:
            base64_image = self.encode_image(image_path)
            content_blocks.append({"image": base64_image})

        request = {
            "modelId": self.model_id,
            "messages": [
                {
                    "role": "user",
                    "content": content_blocks
                }
            ],
            "inferenceConfig": {
                "maxTokens": max_tokens,
                "temperature": temperature,
                "topP": 0.9
            }
        }

        try:
            response = self.client.converse(**request)
            result = ""
            for content_block in response["output"]["message"]["content"]:
                if "text" in content_block:
                    result += content_block["text"]
            return result
        except ClientError as e:
            error_message = e.response.get("Error", {}).get("Message", "Unknown error")
            return f"Error: {error_message}"
Enter fullscreen mode Exit fullscreen mode

Step 3: Create Prompt Templates

# utils/prompt_templates.py

BLOG_OUTLINE_PROMPT = """
Create a detailed outline for a blog post titled "{title}" about {topic}. 
The outline should include:
1. Introduction
2. At least 5 main sections with 2-3 subsections each
3. Conclusion
For each section, provide a brief description of what should be covered.
Target audience: {audience}
Keywords to include: {keywords}
"""

SECTION_EXPANSION_PROMPT = """
Expand the following section of a blog post about {topic} into a detailed, engaging, and informative paragraph.
Section title: {section_title}
Section description: {section_description}
Include relevant examples, statistics if applicable, and make it conversational in tone.
Target word count: {word_count}
Keywords to naturally include: {keywords}
"""

IMAGE_SUGGESTION_PROMPT = """
Based on this blog section about {topic}, suggest 3 specific image ideas that would complement the content.
Section content: {section_content}
For each image suggestion, provide:
1. A descriptive title
2. What the image should show
3. Why it would enhance this particular section
"""
Enter fullscreen mode Exit fullscreen mode

Step 4: Create the Content Processor


python
# utils/content_processor.py
from .bedrock_client import BedrockClient
from .prompt_templates import *

class ContentProcessor:
    def __init__(self):
        self.client = BedrockClient()

    def generate_blog_outline(self, title, topic, audience, keywords):
        prompt = BLOG_OUTLINE_PROMPT.format(
            title=title,
            topic=topic,
            audience=audience,
            keywords=", ".join(keywords)
        )
        return self.client.generate_content(prompt, max_tokens=2000, temperature=0.7)

    def expand_section(self, topic, section_title, section_description, word_count, keywords):
        prompt = SECTION_EXPANSION_PROMPT.format(
            topic=topic,
            section_title=section_title,
            section_description=section_description,
            word_count=word_count,
            keywords=", ".join(keywords)
        )
        return self.client.generate_content(prompt, max_tokens=1500, temperature=0.6)

    def suggest_images(self, topic, section_content):
        prompt = IMAGE_SUGGESTION_PROMPT.format(
            topic=topic,
            section_content=section_content
        )
        return self.client.generate_content(prompt, max_tokens=1000, temperature=0.7)

    def generate_full_blog(self, title, topic, audience, keywords, section_word_count=300):
        # Generate outline
        outline = self.generate_blog_outline(title, topic, audience, keywords)

        # Parse outline into sections (simplified parsing logic)
        sections = []
        current_section = None

        for line in outline.split('\n'):
            if line.strip().startswith('##'):
                if current_section:
                    sections.append(current_section)
                current_section = {
                    'title': line.replace('#', '').strip

Enter fullscreen mode Exit fullscreen mode

Top comments (0)