From 458813ed6c361d99fd82f6f53a8f0981aa32a794 Mon Sep 17 00:00:00 2001 From: Muhammad Awais Date: Sat, 3 May 2025 23:45:22 +0500 Subject: [PATCH] Amazon Bedrock agent that responds with real-time information including the current time in the user's timezone --- .../IAM-permissions.json | 14 ++++ .../agent-instruction-prompt.txt | 10 +++ .../current_time_timezone_agent.py | 73 +++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 AWS-Bedrock/ai-agents-aws-bedrock/IAM-permissions.json create mode 100644 AWS-Bedrock/ai-agents-aws-bedrock/agent-instruction-prompt.txt create mode 100644 AWS-Bedrock/ai-agents-aws-bedrock/current_time_timezone_agent.py diff --git a/AWS-Bedrock/ai-agents-aws-bedrock/IAM-permissions.json b/AWS-Bedrock/ai-agents-aws-bedrock/IAM-permissions.json new file mode 100644 index 0000000..cd99ca8 --- /dev/null +++ b/AWS-Bedrock/ai-agents-aws-bedrock/IAM-permissions.json @@ -0,0 +1,14 @@ +{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "logs:CreateLogGroup", + "logs:CreateLogStream", + "logs:PutLogEvents" + ], + "Resource": "arn:aws:logs:*:*:*" + } + ] +} \ No newline at end of file diff --git a/AWS-Bedrock/ai-agents-aws-bedrock/agent-instruction-prompt.txt b/AWS-Bedrock/ai-agents-aws-bedrock/agent-instruction-prompt.txt new file mode 100644 index 0000000..5f00513 --- /dev/null +++ b/AWS-Bedrock/ai-agents-aws-bedrock/agent-instruction-prompt.txt @@ -0,0 +1,10 @@ +Agent Instruction Prompt: (When creating your Bedrock agent, include this in the instruction) + +You are a Time Information Assistant that helps users with time-related questions. +When asked about the current time: +1. Detect the timezone from the user's question if mentioned +2. If no timezone is specified, ask the user to clarify +3. Always provide the current date and time in the requested timezone +4. Format the response clearly with timezone information + +For timezone conversion questions, provide both timezones in the response. \ No newline at end of file diff --git a/AWS-Bedrock/ai-agents-aws-bedrock/current_time_timezone_agent.py b/AWS-Bedrock/ai-agents-aws-bedrock/current_time_timezone_agent.py new file mode 100644 index 0000000..ec027d8 --- /dev/null +++ b/AWS-Bedrock/ai-agents-aws-bedrock/current_time_timezone_agent.py @@ -0,0 +1,73 @@ +import json +import boto3 +from datetime import datetime +import pytz +import re + +def lambda_handler(event, context): + # Parse input from Bedrock agent + input_text = event['inputText'] + + # Extract timezone from user input (e.g., "What time is it in Europe/London?") + timezone = extract_timezone(input_text) + + if not timezone: + # If no timezone specified, use UTC or prompt user + return { + 'statusCode': 200, + 'response': { + 'message': "I can tell you the current time. Please specify a timezone (e.g., 'What time is it in New York?') or I can use UTC." + } + } + + try: + # Get current time in specified timezone + current_time = get_current_time(timezone) + response_message = f"The current time in {timezone} is {current_time}." + except pytz.UnknownTimeZoneError: + response_message = f"Sorry, I don't recognize the timezone '{timezone}'. Please try another major city or timezone." + + return { + 'statusCode': 200, + 'response': { + 'message': response_message + } + } + +def extract_timezone(text): + # Look for timezone patterns in the user input + patterns = [ + r'in\s+(?:the\s+)?(?:timezone\s+)?([A-Za-z\/_]+)', # "in Europe/London" + r'in\s+([A-Za-z\s]+)$', # "in New York" + r'for\s+([A-Za-z\s]+)$' # "time for Tokyo" + ] + + for pattern in patterns: + match = re.search(pattern, text, re.IGNORECASE) + if match: + timezone_str = match.group(1).strip() + return convert_city_to_tz(timezone_str) + + return None + +def convert_city_to_tz(city_name): + # Map common city names to timezones + city_to_tz = { + 'new york': 'America/New_York', + 'london': 'Europe/London', + 'tokyo': 'Asia/Tokyo', + 'paris': 'Europe/Paris', + 'los angeles': 'America/Los_Angeles', + 'chicago': 'America/Chicago', + 'sydney': 'Australia/Sydney', + 'berlin': 'Europe/Berlin' + } + + lower_city = city_name.lower() + return city_to_tz.get(lower_city, city_name) # Return original if not in map + +def get_current_time(timezone): + # Get current time in specified timezone + tz = pytz.timezone(timezone) + now = datetime.now(tz) + return now.strftime('%Y-%m-%d %H:%M:%S %Z') \ No newline at end of file