diff --git a/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/.nvmrc b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/.nvmrc new file mode 100644 index 000000000..209e3ef4b --- /dev/null +++ b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/.nvmrc @@ -0,0 +1 @@ +20 diff --git a/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/Readme.md b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/Readme.md index d2b79a90e..3a59a45d9 100644 --- a/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/Readme.md +++ b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/Readme.md @@ -50,7 +50,7 @@ Clone the repository to your local machine or AWS environment, set up a virtual ```bash git clone https://github.com/aws-samples/amazon-bedrock-samples.git -cd ./amazon-bedrock-samples/agents-and-function-calling-for-bedrock/use-case-examples/text-2-sql-agent-cdk-enhanced +cd ./amazon-bedrock-samples/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced export AWS_PROFILE=XXX python3.9 -m venv .venv source .venv/bin/activate @@ -64,7 +64,7 @@ Deploy the stack using the AWS CDK. If you want to run this with sample data, use the data provided as an example, which is "EV_WA.zip" in the "Data" directory. This is public data from [Electric Vehicle Population Data](https://catalog.data.gov/dataset/electric-vehicle-population-data). This dataset shows the Battery Electric Vehicles (BEVs) and Plug-in Hybrid Electric Vehicles (PHEVs) that are currently registered through the Washington State Department of Licensing (DOL). For the purpose of this repository, the data was split into 4 CSV files by the author. ```bash -cdk bootstrap --profile XXX --context zip_file_name=EV_WA.zip +cdk bootstrap --profile XXX --context zip_file_name=EV_WA.zip --context region=us-east-1 cdk deploy --profile XXX --context zip_file_name=EV_WA.zip --context region=us-east-1 ``` diff --git a/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/__pycache__/Prep_Data.cpython-311.pyc b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/__pycache__/Prep_Data.cpython-311.pyc new file mode 100644 index 000000000..1a1f89410 Binary files /dev/null and b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/__pycache__/Prep_Data.cpython-311.pyc differ diff --git a/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/__pycache__/agent_instruction_generator.cpython-311.pyc b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/__pycache__/agent_instruction_generator.cpython-311.pyc new file mode 100644 index 000000000..d8d27bd1c Binary files /dev/null and b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/__pycache__/agent_instruction_generator.cpython-311.pyc differ diff --git a/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/__pycache__/claude_3.cpython-311.pyc b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/__pycache__/claude_3.cpython-311.pyc new file mode 100644 index 000000000..dbec7cc83 Binary files /dev/null and b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/__pycache__/claude_3.cpython-311.pyc differ diff --git a/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/app.py b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/app.py index ac1109aa1..f7d914162 100644 --- a/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/app.py +++ b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/app.py @@ -1,31 +1,19 @@ -from aws_cdk import App, Stack, RemovalPolicy +from aws_cdk import App, Stack, RemovalPolicy, Duration from aws_cdk import aws_s3 as s3 from aws_cdk import aws_lambda as lambda_ from aws_cdk import aws_glue as glue from aws_cdk import aws_iam as iam -from aws_cdk.aws_s3_deployment import BucketDeployment, Source -import aws_cdk as cdk -from aws_cdk.aws_lambda import Runtime -from aws_cdk.aws_lambda_python_alpha import PythonFunction -from cdklabs.generative_ai_cdk_constructs import ( - bedrock) - -from cdklabs.generative_ai_cdk_constructs.bedrock import ( - Agent, ApiSchema, BedrockFoundationModel,AgentActionGroup -) from aws_cdk import aws_events as events from aws_cdk import aws_cloudformation as cfn - from aws_cdk import aws_events_targets as events_targets -# from aws_cdk import custom_resources -#from aws_cdk import core as cdk -from aws_cdk import Duration # Import Duration directly -from cdklabs.generative_ai_cdk_constructs.bedrock import ActionGroupExecutor - +from aws_cdk.aws_s3_deployment import BucketDeployment, Source +from aws_cdk.aws_lambda import Runtime +from aws_cdk.aws_lambda_python_alpha import PythonFunction +import aws_cdk as cdk +from cdklabs.generative_ai_cdk_constructs import bedrock from constructs import Construct -from agent_instruction_generator import analyze_csv_files,generate_instruction,invoke_claude_3_with_text - +from agent_instruction_generator import analyze_csv_files, generate_instruction, invoke_claude_3_with_text from Prep_Data import prep_data class MyStack(Stack): @@ -47,7 +35,7 @@ def __init__(self, scope: App, id: str,zip_file_name: str, region: str, **kwargs agent_role_name = f'AmazonBedrockExecutionRoleForAgents_{suffix}' lambda_name = f'{agent_name}-{suffix}' - foundation_model = BedrockFoundationModel('anthropic.claude-3-sonnet-20240229-v1:0', supports_agents=True) + foundation_model = bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V1_0 prep_data(data_folder_name) @@ -178,35 +166,36 @@ def __init__(self, scope: App, id: str,zip_file_name: str, region: str, **kwargs agent_instruction=invoke_claude_3_with_text(question) print(agent_instruction) - api_schema = ApiSchema.from_asset("./text_to_sql_openapi_schema.json") - agent = Agent( + api_schema = bedrock.ApiSchema.from_local_asset("./text_to_sql_openapi_schema.json") + agent = bedrock.Agent( self, "MyAgent", foundation_model=foundation_model, instruction=agent_instruction, description="Agent for performing sql queries.", - should_prepare_agent=True - + should_prepare_agent=True, + user_input_enabled=True, # Allow the agent to prompt for additional information + code_interpreter_enabled=False, # Disable code interpreter as it's not needed for SQL queries + ) - # agent.add_alias(self, 'ProdAlias', + # Create an agent alias for production use + # from cdklabs.generative_ai_cdk_constructs.bedrock import AgentAlias + # agent_alias = AgentAlias(self, 'ProdAlias', + # agent=agent, # alias_name='prod', - # agent_version='1' - # ) + # description="Production alias for the SQL query agent" + # ) - action_group = AgentActionGroup( - self, - "MyActionGroup", - action_group_name="QueryAthenaActionGroup", + action_group = bedrock.AgentActionGroup( + name="QueryAthenaActionGroup", description="Actions for getting the database schema and querying the Athena database for sample data or final query.", - action_group_executor=ActionGroupExecutor(lambda_=action_group_function), - action_group_state="ENABLED", + executor=bedrock.ActionGroupExecutor.fromlambda_function(action_group_function), + enabled=True, api_schema=api_schema ) - - agent.add_action_group(action_group) diff --git a/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/package-lock.json b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/package-lock.json index 8a9f8094f..d05598db4 100644 --- a/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/package-lock.json +++ b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/package-lock.json @@ -5,14 +5,14 @@ "packages": { "": { "dependencies": { - "aws-cdk-lib": "^2.166.0", + "aws-cdk-lib": "^2.101.1", "constructs": "^10.0.0" }, "devDependencies": { - "aws-cdk-lib": "2.166.0" + "aws-cdk-lib": "2.101.1" }, "peerDependencies": { - "aws-cdk-lib": "^2.166.0", + "aws-cdk-lib": "^2.101.1", "constructs": "^10.0.0" } }, @@ -34,45 +34,10 @@ "integrity": "sha512-7bY3J8GCVxLupn/kNmpPc5VJz8grx+4RKfnnJiO1LG+uxkZfANZG3RMHhE+qQxxwkyQ9/MfPtTpf748UhR425A==", "dev": true }, - "node_modules/@aws-cdk/cloud-assembly-schema": { - "version": "38.0.1", - "resolved": "https://registry.npmjs.org/@aws-cdk/cloud-assembly-schema/-/cloud-assembly-schema-38.0.1.tgz", - "integrity": "sha512-KvPe+NMWAulfNVwY7jenFhzhuLhLqJ/OPy5jx7wUstbjnYnjRVLpUHPU3yCjXFE0J8cuJVdx95BJ4rOs66Pi9w==", - "bundleDependencies": [ - "jsonschema", - "semver" - ], - "dev": true, - "dependencies": { - "jsonschema": "^1.4.1", - "semver": "^7.6.3" - } - }, - "node_modules/@aws-cdk/cloud-assembly-schema/node_modules/jsonschema": { - "version": "1.4.1", - "dev": true, - "inBundle": true, - "license": "MIT", - "engines": { - "node": "*" - } - }, - "node_modules/@aws-cdk/cloud-assembly-schema/node_modules/semver": { - "version": "7.6.3", - "dev": true, - "inBundle": true, - "license": "ISC", - "bin": { - "semver": "bin/semver.js" - }, - "engines": { - "node": ">=10" - } - }, "node_modules/aws-cdk-lib": { - "version": "2.166.0", - "resolved": "https://registry.npmjs.org/aws-cdk-lib/-/aws-cdk-lib-2.166.0.tgz", - "integrity": "sha512-FAsIz/CpczbMrcShgvTWNp3kcGN6IDojJWNLqHioTRsTekcyN3OPmKvQJXUNWL0fnhTd8biFXC2esg6kM19xZw==", + "version": "2.101.1", + "resolved": "https://registry.npmjs.org/aws-cdk-lib/-/aws-cdk-lib-2.101.1.tgz", + "integrity": "sha512-kKrJ0CcD82IyohjB3TRy34whf22GI6Y2bIrkBmui+fCb2t13+ToJb7zKBRmL6C090OsoiU/q+H6/WIZWOoYDvQ==", "bundleDependencies": [ "@balena/dockerignore", "case", @@ -83,25 +48,23 @@ "punycode", "semver", "table", - "yaml", - "mime-types" + "yaml" ], "dev": true, + "license": "Apache-2.0", "dependencies": { - "@aws-cdk/asset-awscli-v1": "^2.2.208", - "@aws-cdk/asset-kubectl-v20": "^2.1.3", - "@aws-cdk/asset-node-proxy-agent-v6": "^2.1.0", - "@aws-cdk/cloud-assembly-schema": "^38.0.1", + "@aws-cdk/asset-awscli-v1": "^2.2.200", + "@aws-cdk/asset-kubectl-v20": "^2.1.2", + "@aws-cdk/asset-node-proxy-agent-v6": "^2.0.1", "@balena/dockerignore": "^1.0.2", "case": "1.6.3", - "fs-extra": "^11.2.0", - "ignore": "^5.3.2", + "fs-extra": "^11.1.1", + "ignore": "^5.2.4", "jsonschema": "^1.4.1", - "mime-types": "^2.1.35", "minimatch": "^3.1.2", - "punycode": "^2.3.1", - "semver": "^7.6.3", - "table": "^6.8.2", + "punycode": "^2.3.0", + "semver": "^7.5.4", + "table": "^6.8.1", "yaml": "1.10.2" }, "engines": { @@ -118,15 +81,15 @@ "license": "Apache-2.0" }, "node_modules/aws-cdk-lib/node_modules/ajv": { - "version": "8.17.1", + "version": "8.12.0", "dev": true, "inBundle": true, "license": "MIT", "dependencies": { - "fast-deep-equal": "^3.1.3", - "fast-uri": "^3.0.1", + "fast-deep-equal": "^3.1.1", "json-schema-traverse": "^1.0.0", - "require-from-string": "^2.0.2" + "require-from-string": "^2.0.2", + "uri-js": "^4.2.2" }, "funding": { "type": "github", @@ -230,11 +193,10 @@ "node_modules/aws-cdk-lib/node_modules/fast-uri": { "version": "3.0.3", "dev": true, - "inBundle": true, "license": "BSD-3-Clause" }, "node_modules/aws-cdk-lib/node_modules/fs-extra": { - "version": "11.2.0", + "version": "11.1.1", "dev": true, "inBundle": true, "license": "MIT", @@ -254,7 +216,7 @@ "license": "ISC" }, "node_modules/aws-cdk-lib/node_modules/ignore": { - "version": "5.3.2", + "version": "5.2.4", "dev": true, "inBundle": true, "license": "MIT", @@ -304,25 +266,16 @@ "inBundle": true, "license": "MIT" }, - "node_modules/aws-cdk-lib/node_modules/mime-db": { - "version": "1.52.0", + "node_modules/aws-cdk-lib/node_modules/lru-cache": { + "version": "6.0.0", "dev": true, "inBundle": true, - "license": "MIT", - "engines": { - "node": ">= 0.6" - } - }, - "node_modules/aws-cdk-lib/node_modules/mime-types": { - "version": "2.1.35", - "dev": true, - "inBundle": true, - "license": "MIT", + "license": "ISC", "dependencies": { - "mime-db": "1.52.0" + "yallist": "^4.0.0" }, "engines": { - "node": ">= 0.6" + "node": ">=10" } }, "node_modules/aws-cdk-lib/node_modules/minimatch": { @@ -338,7 +291,7 @@ } }, "node_modules/aws-cdk-lib/node_modules/punycode": { - "version": "2.3.1", + "version": "2.3.0", "dev": true, "inBundle": true, "license": "MIT", @@ -356,10 +309,13 @@ } }, "node_modules/aws-cdk-lib/node_modules/semver": { - "version": "7.6.3", + "version": "7.5.4", "dev": true, "inBundle": true, "license": "ISC", + "dependencies": { + "lru-cache": "^6.0.0" + }, "bin": { "semver": "bin/semver.js" }, @@ -411,7 +367,7 @@ } }, "node_modules/aws-cdk-lib/node_modules/table": { - "version": "6.8.2", + "version": "6.8.1", "dev": true, "inBundle": true, "license": "BSD-3-Clause", @@ -427,7 +383,7 @@ } }, "node_modules/aws-cdk-lib/node_modules/universalify": { - "version": "2.0.1", + "version": "2.0.0", "dev": true, "inBundle": true, "license": "MIT", @@ -435,6 +391,21 @@ "node": ">= 10.0.0" } }, + "node_modules/aws-cdk-lib/node_modules/uri-js": { + "version": "4.4.1", + "dev": true, + "inBundle": true, + "license": "BSD-2-Clause", + "dependencies": { + "punycode": "^2.1.0" + } + }, + "node_modules/aws-cdk-lib/node_modules/yallist": { + "version": "4.0.0", + "dev": true, + "inBundle": true, + "license": "ISC" + }, "node_modules/aws-cdk-lib/node_modules/yaml": { "version": "1.10.2", "dev": true, diff --git a/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/package.json b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/package.json index f900aac69..b4ecb804e 100644 --- a/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/package.json +++ b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/package.json @@ -1,13 +1,25 @@ { + "name": "text-2-sql-agent-cdk-enhanced", + "version": "1.0.0", + "description": "Text to SQL Bedrock Agent with CDK Enhanced", + "main": "app.py", + "scripts": { + "build": "cdk synth", + "deploy": "cdk deploy", + "destroy": "cdk destroy" + }, + "engines": { + "node": ">=18.0.0" + }, "peerDependencies": { - "aws-cdk-lib": "^2.166.0", - "constructs": "^10.0.0" + "aws-cdk-lib": "^2.200.0", + "constructs": "^10.0.0" }, "devDependencies": { - "aws-cdk-lib": "2.166.0" + "aws-cdk-lib": "^2.200.0" }, "dependencies": { - "aws-cdk-lib": "^2.166.0", + "aws-cdk-lib": "^2.200.0", "constructs": "^10.0.0" - } - } \ No newline at end of file + } +} diff --git a/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/requirements.txt b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/requirements.txt index 78cc64da8..9a7b22916 100644 --- a/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/requirements.txt +++ b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/requirements.txt @@ -1,6 +1,8 @@ -bedrock-agent -boto3 -pandas -cdklabs.generative-ai-cdk-constructs -aws-cdk.aws-lambda-python-alpha -botocore +bedrock-agent==0.0.11 +boto3>=1.38.0 +pandas>=2.0.0 +cdklabs.generative-ai-cdk-constructs>=0.1.300 +aws-cdk.aws-lambda-python-alpha>=2.200.0a0 +botocore>=1.38.0 +aws-cdk-lib>=2.200.0 +constructs>=10.4.0 diff --git a/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/runtime.txt b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/runtime.txt new file mode 100644 index 000000000..67ebc4e9a --- /dev/null +++ b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/runtime.txt @@ -0,0 +1 @@ +python-3.11 diff --git a/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/setup.sh b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/setup.sh index 53ac36825..1fd7bcd78 100755 --- a/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/setup.sh +++ b/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/setup.sh @@ -1,14 +1,33 @@ #!/bin/bash +set -e # Exit on any error + +echo "Starting setup for Text to SQL Bedrock Agent CDK Enhanced..." + +# Check if Python 3.9+ is available +if ! command -v python3 &> /dev/null; then + echo "Error: Python 3 is required but not installed." + exit 1 +fi + +PYTHON_VERSION=$(python3 -c 'import sys; print(".".join(map(str, sys.version_info[:2])))') +REQUIRED_VERSION="3.9" + +if ! python3 -c "import sys; exit(0 if sys.version_info >= (3, 9) else 1)"; then + echo "Error: Python 3.9+ is required. Current version: $PYTHON_VERSION" + exit 1 +fi + +echo "Python version check passed: $PYTHON_VERSION" # Upgrade pip +echo "Upgrading pip..." pip install --upgrade pip -pip install --upgrade boto3 # Install nvm if it is not already installed if ! command -v nvm &> /dev/null; then echo "nvm not found, installing..." - curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash + curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash # Load nvm into the current shell session export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" @@ -20,17 +39,33 @@ export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" -# Install Node.js version 18 using nvm -nvm install 18 -nvm use 18 +# Install Node.js version 20 (LTS) using nvm +echo "Installing Node.js 20 (LTS)..." +nvm install 20 +nvm use 20 +# Verify Node.js installation +echo "Node.js version: $(node --version)" +echo "npm version: $(npm --version)" # Install and update AWS CDK globally +echo "Installing AWS CDK..." npm install -g aws-cdk@latest -npm update -g aws-cdk + +# Verify CDK installation +echo "CDK version: $(cdk --version)" # Install Node.js project dependencies +echo "Installing Node.js dependencies..." npm install # Install Python dependencies +echo "Installing Python dependencies..." pip install -r requirements.txt + +echo "Setup completed successfully!" +echo "" +echo "Next steps:" +echo "1. Make sure your AWS CLI is configured with appropriate credentials" +echo "2. Run: cdk bootstrap --profile YOUR_PROFILE --context zip_file_name=EV_WA.zip" +echo "3. Run: cdk deploy --profile YOUR_PROFILE --context zip_file_name=EV_WA.zip --context region=us-east-1"