logo
Menu
Leveraging Amazon Bedrock for Streamlined Article Tagging and Review Classification

Leveraging Amazon Bedrock for Streamlined Article Tagging and Review Classification

In this article, we will delve into straightforward case studies, illustrating how AWS Bedrock can be harnessed to unlock the full potential of generative AI to classify reviews or tag articles.

Published Dec 14, 2023

Introduction

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.

How can you tag articles or classify reviews from websites 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.

Let's get started!

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.
Amazon Bedrock UI

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.
Test your Prompt on Playground
Get Keywords
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
Api request
  • 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 👇🏻
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
1
2
3
4
pip3 install --no-build-isolation --force-reinstall \
"boto3>=1.28.57" \
"awscli>=1.29.57" \
"botocore>=1.31.57"
  • Run Python Script
1
python3 demo_text_gen.py

Get results

  1. Extract relevant Keywords from the headline
Keyword Prompt
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.”
Review Prompt
  • 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.”
Bad Review

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

In a landscape where choices like OpenAI, Bing, and Bard have their merits, Bedrock emerges as a compelling alternative that merits exploration.
As you can see from these case studies, this is just one of many options where you can use generative AI to start driving your solutions, so what are you waiting to start using AWS Bedrock to classify or segment your data?

Links of interest

To get started with generative AI on AWS, please see resources:

Comments