Summarize file in AWS Bedrock without RAG

AWS Bedrock, RAG, Generative AI, what are those?

Well, before we get started, here is the topic list that we are going to discuss.

  • What is RAG
  • What is AWS Bedrock
  • Prerequisite
  • Get your hands dirty with Python

What is RAG?

Note: for today, we are not going to use RAG for this article. But for better understanding, I’ll give you a little brief about RAG.

RAG (Retrieval-Augmented Generation) is a method in generative AI that helps make

answers more accurate by first looking up relevant information and then using that information to generate a response.

Here's how it works:

  • When you ask a question, the AI first finds related information from a big collection of documents or knowledge sources.
  • Then, it uses this information to generate an answer that’s more reliable and informed.

So instead of just guessing, RAG allows the AI to "check its notes" before responding, making it especially useful for answering specific questions or giving accurate details.

RAG does need a database or knowledge source to pull information from. This is a key part of how it works, as it combines AI's language generation ability with real, external information.

Without a database, RAG wouldn’t be able to look up accurate information and would have to rely solely on what it already knows from training, which may not be complete or up-to-date.

Here is the diagram.

AWS support vectorDB: 

  • Amazon OpenSearch Service
  • Amazon Aurora PostgreSQL-Compatible Edition
  • Amazon Neptune ML
  • Vector search for Amazon MemoryDB
  • Amazon DocumentDB

AWS Docs: https://aws.amazon.com/what-is/vector-databases/

What is AWS Bedrock?

AWS Bedrock is a service from Amazon Web Services that makes it easier for developers to build and scale generative AI applications using foundational models (FMs). Bedrock provides access to a range of pre-trained AI models from multiple providers (like Anthropic, Stability AI, and Amazon’s own models), making it easier to select the best model for specific tasks without needing to train or manage models from scratch.

Here’s what AWS Bedrock enables developers to do:

  1. Choose and Customize Models: Bedrock offers access to a library of foundational models for tasks like text generation, image creation, or chatbot interactions. Developers can use these models as they are or fine-tune them with their own data to better fit their applications.
  2. Integrated Tools and Scaling: Bedrock is integrated with other AWS tools and services, so users can manage and deploy models within the AWS ecosystem. This includes support for scaling the model usage, integrating with data processing tools, and managing security and compliance.
  3. API-Based Access: Bedrock models can be accessed through simple API calls, making it easy to embed generative AI capabilities into applications without needing deep expertise in machine learning.

Why is AWS Bedrock a better option to build genAI? I would say, if you are using another genAI provider and not really satisfied with the result, you are going to change to another genAI. But here, in AWS Bedrock, you can just switch the FM that suits your case.

AWS Docs: https://aws.amazon.com/bedrock/

Prerequisite

Here is the list of the prerequisite:

  • IAM permission
  • AWS CLI
  • Python
  • Boto3 Library
  • Foundation model access
IAM permission

Create one IAM user access with AmazonBedrockFullAccess, or if you want to restrict the permission you can create this one.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Amazon Bedrock Agents permissions",
            "Effect": "Allow",
            "Action": [
                "bedrock:ListFoundationModels",
                "bedrock:GetFoundationModel",
                "bedrock:TagResource",
                "bedrock:UntagResource",
                "bedrock:ListTagsForResource",
                "bedrock:CreateAgent",
                "bedrock:UpdateAgent",
                "bedrock:GetAgent",
                "bedrock:ListAgents",
                "bedrock:DeleteAgent",
                "bedrock:CreateAgentActionGroup",
                "bedrock:UpdateAgentActionGroup",
                "bedrock:GetAgentActionGroup",
                "bedrock:ListAgentActionGroups",
                "bedrock:DeleteAgentActionGroup",
                "bedrock:GetAgentVersion",
                "bedrock:ListAgentVersions",
                "bedrock:DeleteAgentVersion",
                "bedrock:CreateAgentAlias",
                "bedrock:UpdateAgentAlias",
                "bedrock:GetAgentAlias",
                "bedrock:ListAgentAliases",
                "bedrock:DeleteAgentAlias",
                "bedrock:AssociateAgentKnowledgeBase",
                "bedrock:DisassociateAgentKnowledgeBase",
                "bedrock:GetKnowledgeBase",
                "bedrock:ListKnowledgeBases",
                "bedrock:PrepareAgent",
                "bedrock:InvokeAgent"
            ],
            "Resource": "*"
        }
    ]
}

AWS Docs: https://docs.aws.amazon.com/bedrock/latest/userguide/security_iam_id-based-policy-examples-agent.html#iam-agents-ex-all 

Create IAM user with this permission, and create an access key and secret key.

AWS CLI

You need to install AWS CLI on your local computer (AWS CLI download link can be found in here https://aws.amazon.com/cli/ ), and type aws configure after install, in your terminal or CMD. There will be several prompts to input your access key and secret key and lastly, you can put N.Virginia in your region (us-east-1).

At this point, you already have AWS CLI, and ready to interact with AWS environment. To verify this CLI works properly, you can type aws s3 ls or aws sts get-caller-identity.

Boto3

To install boto3 you can use python pip and call pip install boto3.

Foundation model access

Login to your AWS account, and search for AWS Bedrock in the search bar. Make to choose N.Virginia for the region. In the left panel, scroll down, and find Model access under Bedrock configurations.

Enable anthropic, or if you have another preferred FM just enable it.

It takes some time to request.

Get your hands dirty with Python

Write our first code, starts with create one function called summarized_text

import boto3
import json
import os
import csv

# Initialize the Bedrock client
client = boto3.client('bedrock-runtime', region_name="us-east-1")

# Function to summarize text using AWS Bedrock
def summarize_text(text, model_id='anthropic.claude-v2'):
    # Prepare the payload for the model
    payload = {
        "prompt": f"\n\nHuman: Please summarize this document {text}\n\nAssistant:",
        "max_tokens_to_sample": 500,  # Limit the size of the output
        "temperature": 0.5            # Adjust randomness (between 0 and 1)
    }
   
    # Invoke the model using AWS Bedrock
    response = client.invoke_model(
        modelId=model_id,
        contentType="application/json",
        accept="application/json",
        body=json.dumps(payload)
    )
   
    # Get the response from the model
    result = json.loads(response['body'].read())
   
    # Extract and return the summarized text
    summary = result.get('completion', "No summary available")
    return summary

{text} variable in that code above should be read from file. To read a file, we need to create another function. Let’s create function named read_text_from_file.

# Function to read text from a file
def read_text_from_file(file_path):
    with open(file_path, 'r') as file:
        return file.read()

Now let's combine and call those functions.

import boto3
import json
import os
import csv


# Initialize the Bedrock client
client = boto3.client('bedrock-runtime', region_name="us-east-1")


# Function to summarize text using AWS Bedrock
def summarize_text(text, model_id='anthropic.claude-v2'):
    # Prepare the payload for the model
    payload = {
        "prompt": f"\n\nHuman: Please summarize this document {text}\n\nAssistant:",
        "max_tokens_to_sample": 500,  # Limit the size of the output
        "temperature": 0.5            # Adjust randomness (between 0 and 1)
    }
   
    # Invoke the model using AWS Bedrock
    response = client.invoke_model(
        modelId=model_id,
        contentType="application/json",
        accept="application/json",
        body=json.dumps(payload)
    )
   
    # Get the response from the model
    result = json.loads(response['body'].read())
   
    # Extract and return the summarized text
    summary = result.get('completion', "No summary available")
    return summary


# Function to read text from a file
def read_text_from_file(file_path):
    with open(file_path, 'r') as file:
        return file.read()
   
myfile = 'myfile.txt' # or if your file is in folder, you can specify the folder as well 'folder/myfile.txt'
mytext = read_text_from_file(myfile)
summary = summarize_text(mytext)


print(summary)

That’s it. Another way, if you have so many documents in your folder, you can add another function to automate it. 

To iterate the process and summarize files in the folder, it should be around this.

txt_files = [f for f in os.listdir(directoryName) if f.endswith('.txt')]
for file in txt_files:
    summary = summarize_text(directoryName + file)
    print(summary)

That is the clue. I’ll let your creativity solve this puzzle on how to automate bulk file summarization. 

Stay creative, and keep exploring. Cheers 🍻