Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@
import os
import subprocess
from pathlib import Path
from datetime import datetime

print(f"Boto3 version: {boto3.__version__}")

iam_client = boto3.client('iam')
sts_client = boto3.client('sts')
s3_client = boto3.client('s3')
session = boto3.session.Session()
# Custom JSON encoder to handle datetime objects in trace data
class DateTimeEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.isoformat()
return super().default(obj)

iam_client = boto3.client('iam', region_name="us-west-2")
sts_client = boto3.client('sts', region_name="us-west-2")
s3_client = boto3.client('s3', region_name="us-west-2")
session = boto3.session.Session(region_name="us-west-2")
region = session.region_name
account_id = sts_client.get_caller_identity()["Account"]
dynamodb_client = boto3.client('dynamodb')
dynamodb_resource = boto3.resource('dynamodb')
lambda_client = boto3.client('lambda')
dynamodb_client = boto3.client('dynamodb', region_name="us-west-2")
dynamodb_resource = boto3.resource('dynamodb', region_name="us-west-2")
lambda_client = boto3.client('lambda', region_name="us-west-2")
# bedrock_agent_client = boto3.client('bedrock-agent')
bedrock_agent_client = boto3.client(
'bedrock-agent',
Expand Down Expand Up @@ -728,13 +736,23 @@ def invoke_agent_helper(
for key in event['chunk']:
if key != 'bytes':
logger.info(f"Chunck {key}:\n")
logger.info(json.dumps(event['chunk'][key], indent=3))
try:
logger.info(json.dumps(event['chunk'][key], indent=3, cls=DateTimeEncoder))
except Exception as chunk_error:
logger.warning(f"Could not serialize chunk data: {chunk_error}")
logger.info(f"Chunk data: {str(event['chunk'][key])}")
agent_answer = data.decode('utf8')
return agent_answer
# End event indicates that the request finished successfully
elif 'trace' in event:
if enable_trace:
logger.info(json.dumps(event['trace'], indent=2))
try:
logger.info(json.dumps(event['trace'], indent=2, cls=DateTimeEncoder))
except Exception as trace_error:
logger.warning(f"Could not serialize trace data: {trace_error}")
logger.info(f"Trace event type: {type(event['trace'])}")
# Fallback to string representation
logger.info(f"Trace data: {str(event['trace'])}")
else:
raise Exception("unexpected event.", event)
except Exception as e:
Expand Down Expand Up @@ -772,7 +790,7 @@ def create_lambda_layer(agent_name, requirements):
], cwd="layer")

# Create Lambda layer
lambda_client = boto3.client('lambda')
lambda_client = boto3.client('lambda', region_name="us-west-2")

try:
with open("layer/layer.zip", 'rb') as zip_file:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@
},
"outputs": [],
"source": [
"s3_client = boto3.client('s3')\n",
"sts_client = boto3.client('sts')\n",
"session = boto3.session.Session()\n",
"s3_client = boto3.client('s3', region_name=\"us-west-2\")\n",
"sts_client = boto3.client('sts', region_name=\"us-west-2\")\n",
"session = boto3.session.Session(region_name=\"us-west-2\")\n",
"region = session.region_name\n",
"account_id = sts_client.get_caller_identity()[\"Account\"]\n",
"knowledge_base_name = f'restaurant-kb'\n",
Expand All @@ -112,7 +112,7 @@
" knowledge_base_name,\n",
" knowledge_base_description,\n",
" bucket_name\n",
")"
")\n"
]
},
{
Expand Down Expand Up @@ -390,7 +390,7 @@
},
"outputs": [],
"source": [
"%%time\n",
"import time\n",
"import uuid\n",
"session_id:str = str(uuid.uuid1())\n",
"\n",
Expand Down Expand Up @@ -437,7 +437,7 @@
},
"outputs": [],
"source": [
"%%time\n",
"import time\n",
"import uuid\n",
"session_id:str = str(uuid.uuid1())\n",
"\n",
Expand Down Expand Up @@ -478,7 +478,7 @@
},
"outputs": [],
"source": [
"%%time\n",
"import time\n",
"import uuid\n",
"session_id:str = str(uuid.uuid1())\n",
"\n",
Expand Down Expand Up @@ -619,7 +619,7 @@
},
"outputs": [],
"source": [
"%%time\n",
"import time\n",
"import uuid\n",
"session_id:str = str(uuid.uuid1())\n",
"session_state={\n",
Expand Down Expand Up @@ -668,7 +668,7 @@
},
"outputs": [],
"source": [
"%%time\n",
"import time\n",
"import uuid\n",
"session_id:str = str(uuid.uuid1())\n",
"session_state={\n",
Expand Down Expand Up @@ -713,7 +713,7 @@
},
"outputs": [],
"source": [
"%%time\n",
"import time\n",
"import uuid\n",
"session_id:str = str(uuid.uuid1())\n",
"query = \"What do you serve for dinner? can you make a reservation for 4 people, at 9pm tonight.\"\n",
Expand Down Expand Up @@ -825,12 +825,12 @@
},
"outputs": [],
"source": [
"# clean up react agent\n",
"clean_up_resources(\n",
" agent_name_react,\n",
" custom_orchestration_lambda_function_name=None,\n",
" dynamodb_table=f'{agent_name_react}-table'\n",
")"
"# # clean up react agent\n",
"# clean_up_resources(\n",
"# agent_name_react,\n",
"# custom_orchestration_lambda_function_name=None,\n",
"# dynamodb_table=f'{agent_name_react}-table'\n",
"# )"
]
},
{
Expand All @@ -840,12 +840,12 @@
"metadata": {},
"outputs": [],
"source": [
"# clean up rewoo agent\n",
"clean_up_resources(\n",
" agent_name_rewoo,\n",
" custom_orchestration_lambda_function_name=custom_orchestration_lambda_name,\n",
" dynamodb_table=f'{agent_name_rewoo}-table'\n",
")"
"# # clean up rewoo agent\n",
"# clean_up_resources(\n",
"# agent_name_rewoo,\n",
"# custom_orchestration_lambda_function_name=custom_orchestration_lambda_name,\n",
"# dynamodb_table=f'{agent_name_rewoo}-table'\n",
"# )"
]
},
{
Expand All @@ -857,10 +857,10 @@
},
"outputs": [],
"source": [
"# delete kb\n",
"kb.delete_kb(\n",
" kb_name=knowledge_base_name, delete_s3_bucket=True, delete_iam_roles_and_policies=True\n",
")"
"# # delete kb\n",
"# kb.delete_kb(\n",
"# kb_name=knowledge_base_name, delete_s3_bucket=True, delete_iam_roles_and_policies=True\n",
"# )"
]
},
{
Expand Down Expand Up @@ -1491,7 +1491,7 @@
],
"instance_type": "ml.t3.medium",
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "3.12.5",
"language": "python",
"name": "python3"
},
Expand All @@ -1505,7 +1505,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
"version": "3.12.5"
}
},
"nbformat": 4,
Expand Down
Loading