Leveraging AWS Bedrock for Effective Article and Review Classification

Alexander Bolaño Cervantes
5 min readOct 25, 2023

Learn an easy way to classify your articles or user reviews

Source: Amazon bedrock workshop

Amazon Bedrock, recently unveiled as part of a comprehensive suite of tools for harnessing the potential of generative AI within the AWS ecosystem, represents a significant leap forward in the field. As a fully managed service, it provides users with a versatile array of high-performing foundation models (FMs) sourced from prominent AI pioneers such as AI21 Labs, Anthropic, Cohere, Stability AI, and Amazon itself. This wealth of options is complemented by a wide range of capabilities tailored for the development of generative AI applications. With an unwavering commitment to privacy and security, Bedrock streamlines the process of creating AI-driven solutions while ensuring data integrity and confidentiality.

In a landscape where choices like OpenAI, Bing, and Bard have their merits, Bedrock emerges as a compelling alternative that merits exploration. In this article, we will delve into a straightforward case study, illustrating how AWS Bedrock can be harnessed to unlock the full potential of generative AI.

How can you classify articles or reviews website using AWS Bedrock?

In a world where more and more people rate you, and it is more necessary to segment your data, generative AI is here to stay, as you know currently we can use several tools for this task such as ChatGPT, Bard, Bing, among others but this time we will use AWS Bedrock, an amazing and easy new comprehensive management service for classifying articles or reviews.

In the following case study, we will select a headline from a news article to identify the most relevant keywords. Subsequently, we will utilize two random reviews from a website, with the aim of categorizing them as either bad, regular, or excellent. To achieve this, we will set up a Cloud9 environment on AWS, and using a straightforward Python script, we will be able to obtain the desired responses.

Here we go!

Before launching a Cloud9 instance you should keep in mind that is necessary to get grant permission to available Models on AWS Bedrock, in this case, we are going to use the Cohere Model in order to generate text from any given prompt, for you must go to the Bedrock home on AWS console.

Create a new Cloud9 environment

We are ready to create a Cloud9 instance, as follows :

  • OS 👉 Ubuntu 22.04 LTS
  • Instance Type 👉 t2.micro (1 GiB RAM + 1 vCPU)
  • Create instance

Test the Cohere model on Bedrock

Now you must write a prompt using the following headline “Nikola Jokić leads Denver Nuggets to victory over LA Lakers as title defense begins on NBA Opening Night” to find the most relevant keywords.

Cohere response

Response: Nikola Jokić, Denver Nuggets, LA Lakers, NBA, Opening Night, title defense

Run API request from Python Script

  • Go to the View API Request button to see the following text
  • Open the Cloud9 environment previously deployed, create a new Python file, and call it demo_test_gen.py.
  • Copy and paste the code here, using as input variable the text of the View API Request 👇🏻
import boto3
import json

bedrock = boto3.client(
service_name='bedrock-runtime',
region_name='us-east-1'
)

input = {
"modelId": "cohere.command-text-v14",
"contentType": "application/json",
"accept": "*/*",
"body": "{\"prompt\":\"Could you find the most relevant keywords focused on sports and personalities from this headline \\\"Nikola Jokić leads Denver Nuggets to victory over LA Lakers as title defense begins on NBA Opening Night\\\"\\n\",\"max_tokens\":400,\"temperature\":0.75,\"p\":0.01,\"k\":0,\"stop_sequences\":[],\"return_likelihoods\":\"NONE\"}"
}

response = bedrock.invoke_model(body=input["body"],
modelId=input["modelId"],
accept=input["accept"],
contentType=input["contentType"])

response_body = json.loads(response['body'].read())

print(response_body['generations'][0]['text'])
  • Install boto3 and aws-cli
pip3 install --no-build-isolation --force-reinstall \
"boto3>=1.28.57" \
"awscli>=1.29.57" \
"botocore>=1.31.57"
  • Run Python Script
python3 demo_text_gen.py

Get results

  1. Extract relevant Keywords from the headline

2. Classify any given review as bad, regular, or excellent from Quality Inn Miami Airport Hotel Review

  • Prompt used 👉🏻 Could you classify this review as bad, regular or excellent? : {review}
  • Regular Review — 8/10 👉🏻 “The only thing we didn’t like was that they never told us the suggested temperature in the room (72° F), and on the 3rd day we had water condensation all over the room (walls, room ceiling, bathroom, and floor). However, the attention of the receptionist, and dining room very good in everything.”
  • Bad Review — 2/10 👉🏻 “This hotel didn’t have clean towels and wash clothes at the time of check in. They told us to go to bed dirty and they will have some in the morning. Unacceptable. The manager was very rude and didn’t care to find a solution. When asked about a refund, he refused.”

Challenge

I want to propose to you the following challenge:

Try to extract 1000 reviews or news headlines from any website of your interest, such as food, hotels, or e-commerce, and try to classify them and save them to CSV files in S3 Buckets using this method automatically.

Summary

As you can see from this case study, this is just one of many options where you can use generative AI to start driving your challenge, so what are you waiting to start using AWS Bedrock to classify or segment your data?

Follow me 👉 Medium

Follow me 👉 LinkedIn

Follow me 👉 Twitter

Thank you for reading this far. If you find this article useful, like and share this article. Someone could find it useful too, and why not invite me for a coffee?

Donate with PayPal 👆

--

--

Alexander Bolaño Cervantes

Hey, 👋 my name is Alex I’m a Senior Data Engineer passionate for automating tasks , Big Data and cutting-edge Technologies