This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| curl -X POST http://localhost:5000/api/sql-query \ | |
| -H "Content-Type: application/json" \ | |
| -d '{"query": "What were the total sales by product category last month?"}' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @app.route('/api/sql-query', methods=['POST', 'OPTIONS']) | |
| def natural_language_sql(): | |
| """Process a natural language query, convert it to SQL, and execute it""" | |
| # Get request data | |
| data = request.get_json() | |
| natural_language_query = data['query'] | |
| # Process natural language to SQL query and execute | |
| result = bedrock_service.natural_language_to_sql_result(natural_language_query) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def is_safe_query(self, query): | |
| """Check if the SQL query is safe (read-only)""" | |
| # Convert to lowercase for case-insensitive matching | |
| query_lower = query.lower() | |
| # Check for write operations keywords | |
| unsafe_keywords = ['insert', 'update', 'delete', 'drop', 'alter', 'create', 'truncate'] | |
| for keyword in unsafe_keywords: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def generate_sql_query(self, natural_language_query): | |
| """Generate SQL query from natural language instruction""" | |
| system_prompt = f""" | |
| You are an expert SQL assistant that helps users query a MySQL database. | |
| Your task is to generate a SQL query based on the user's natural language request. | |
| Here is the database schema information: | |
| {self.db_schema} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_database_schema(self): | |
| """Get database schema information to provide context to the model""" | |
| schema_info = [] | |
| with self.engine.connect() as connection: | |
| # Get all tables | |
| tables_result = connection.execute(text(""" | |
| SELECT table_name FROM information_schema.tables | |
| WHERE table_schema = :db_name | |
| """), {"db_name": self.db_config['db']}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class BedrockService: | |
| def __init__(self): | |
| # Model and AWS region configuration | |
| self.model_id = os.environ.get('BEDROCK_MODEL_ID', 'us.anthropic.claude-3-7-sonnet-20250219-v1:0') | |
| self.aws_region = os.environ.get('AWS_REGION', 'us-east-1') | |
| # Database configuration | |
| self.db_config = { | |
| 'host': os.environ.get('DB_HOST', ''), | |
| 'user': os.environ.get('DB_USER', ''), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | |
| # SPDX-License-Identifier: Apache-2.0 | |
| FROM --platform=linux/amd64 public.ecr.aws/lambda/python:3.12 | |
| LABEL name=lambda/python/clamav | |
| LABEL version=1.0 | |
| ARG CACHE_DATE=1 | |
| RUN dnf update -y \ | |
| && dnf -y install clamav clamav-update clamd \ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | |
| # SPDX-License-Identifier: Apache-2.0 | |
| from posixpath import join | |
| import time | |
| import boto3 | |
| import botocore | |
| import glob | |
| import json | |
| import logging |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | |
| # SPDX-License-Identifier: Apache-2.0 | |
| import boto3 | |
| import botocore | |
| import json | |
| import logging | |
| import os | |
| import pwd | |
| import re |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Transform: AWS::Serverless-2016-10-31 | |
| Resources: | |
| VPC: | |
| Type: "AWS::EC2::VPC" | |
| Properties: | |
| CidrBlock: 192.190.0.0/24 | |
| EnableDnsSupport: true | |
| EnableDnsHostnames: true | |
| Tags: | |
| - Key: Name |
NewerOlder