Foundation model ticket categorization
You're working at a customer service company that handles thousands of support tickets daily. The team wants to start using AI to help categorize incoming tickets before they reach human agents. As a proof of concept, you need to set up a basic integration with Amazon Bedrock to categorize a single support ticket.
The boto3
and json
libraries have been preloaded. A sample ticket saved as sample_ticket
and a list of categories
have also been preloaded.
This exercise is part of the course
Introduction to Amazon Bedrock
Exercise instructions
- Create a prompt mentioning the categories initialized in the
categories
variable and the sample ticket initialized in thesample_ticket
variable. - Add the
prompt
while invoking the model. - Extract the category by reading the
response
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
def categorize_ticket(sample_ticket, categories):
bedrock = boto3.client('bedrock-runtime', region_name='us-east-1')
# Create prompt with allowed categories and pass in the ticket variable
prompt = f"""Categorize this support ticket into exactly one of these categories: {', '.join(____)}.
Respond with only the category name. Ticket: {____}"""
# Send request to Bedrock and get response
response = bedrock.invoke_model(modelId='amazon.nova-lite-v1:0',
body=json.dumps({"messages": [{"role": "user", "content": ____}]}))
# Extract the response
return json.loads(____.get("body").____.decode())["output"]["message"]["content"][0]["text"]
result = categorize_ticket(sample_ticket, categories)
print(f"Ticket Category: {result}")