DEV Community

Cover image for Using Amazon Bedrock, Claude, and the Converse API to remove PII
Faye Ellis for AWS Community Builders

Posted on • Updated on

Using Amazon Bedrock, Claude, and the Converse API to remove PII

The Amazon Bedrock Converse API can be used to interact with AI models available in Bedrock, in a consistent way.

Here's how to get started using Amazon Bedrock and Converse to Remove Personally Identifiable Information (PII).

This exercise should cost < $1 as long as you remember to perform the clean-up step at the end!

Before you begin, be sure to check that the following models are available in your region, and that you have enabled access to them:
anthropic.claude-v2
anthropic.claude-3-haiku

1) Create a Cloud9 instance, making sure to select Ubuntu - not Amazon Linux

Screen output showing Ubuntu operating system selected

2) After the Cloud9 instance is created, log in, and install the AWS SDK for Python (boto3)
pip install boto3

3) Create a new file on your Cloud9 instance, named converse.py the contents of the file should be as follows (alternatively download from GitHub) :

#first we import boto3 and json 

import boto3, json

#create a boto3 session - stores config state and allows you to create service clients

session = boto3.Session()

#create a Bedrock Runtime Client instance - used to send API calls to AI models in Bedrock

bedrock = session.client(service_name='bedrock-runtime')

#define an empty message list - to be used to pass the messages to the model

message_list = []

#here’s the message that I want to send to the model. So in this prompt, I’m providing some text, and asking the AI model to redact any personally identifiable information from the text I provided.

initial_message = {
    "role": "user",
    "content": [
        {
          "text": "\n\nHuman:\n<text>\n Faye: Hi Lydia!\n Lydia: Hi Faye! Have you recieved the replacement cable that I ordered for you? \n Faye: No I did not, did you send to my new address? \n Lydia: I mailed it to 41 Oak Street, Bristol, U.K.\n Faye: That is my old address, my new address since last week is 105 Lionel Road, London, W8 9YD, U.K.\n Lydia: Please can you give me your new phone number as well?\n Faye: Sure, it's 019377464944 </text> \n\nRemove all personally identifying information from the text and replace it with “xxx”. All names, phone numbers, and email addresses must get replaced with xxx. \n\nPlease provide the sanitized version of the text with all PII removed in <response></response> XML tags.\n\nAssistant:"
        }
    ],
}

#the message above is appended to the message_list

message_list.append(initial_message)

#make an API call to the Bedrock Converse API, we define the model to use, the message, and inference parameters to use as well

response = bedrock.converse(
    modelId="anthropic.claude-v2",
    messages=message_list,
    inferenceConfig={
        "maxTokens": 2048,
        "temperature": 0,
        "topP": 1
    },
)

#invoke converse with all the parameters we provided above and then print the result 

response_message = response['output']['message']
print(json.dumps(response_message, indent=4))
Enter fullscreen mode Exit fullscreen mode

4) Run the Python code like this:

python converse.py
Enter fullscreen mode Exit fullscreen mode

It should have removed all of the Personally Identifiable Information from the text. You should see a response similar to this:
Screen output showing PII removed from text and replaced with xxx characters

5) We can also run this same code using different model, by replacing the model ID in our code as follows (Claude 3 Haiku is another model that also supports removing PII):

anthropic.claude-3-haiku-20240307-v1:0

6) We can test again with a newer version of the model:

anthropic.claude-3-5-sonnet-20240620-v1:0

When a new version of Claud is releasesd, just replace the name of the new model in our code!

To clean-up, be sure to delete your Cloud9 instance if you no longer need it, to avoid unneccessary charges.

Screenshot showing Cloud9 instance delete

So the Converse API gives you a simple, consistent API, that works with all Amazon Bedrock models that support messages. And this means that you can write your code once and use it with different models to compare the results!

Top comments (0)