Skip to content

Instantly share code, notes, and snippets.

View 4l3j4ndr0's full-sized avatar

Aelajandro Castañeda 4l3j4ndr0

View GitHub Profile
@4l3j4ndr0
4l3j4ndr0 / .sh
Last active April 7, 2025 13:49
Test Flask API natural language to SQL
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?"}'
@4l3j4ndr0
4l3j4ndr0 / .py
Created March 23, 2025 18:00
REST API for Natural Language Queries
@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)
@4l3j4ndr0
4l3j4ndr0 / .py
Created March 23, 2025 17:58
Security SQL Validation
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:
@4l3j4ndr0
4l3j4ndr0 / .py
Created March 23, 2025 17:57
Secure SQL Query Generation
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}
@4l3j4ndr0
4l3j4ndr0 / .py
Created March 23, 2025 17:50
Database Schema Extraction
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']})
@4l3j4ndr0
4l3j4ndr0 / .py
Created March 23, 2025 17:47
BedrockService
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', ''),
@4l3j4ndr0
4l3j4ndr0 / Dockerfile
Last active July 26, 2024 16:35
Dockerfile to build images with AWS Lambda capabilities.
# 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 \
@4l3j4ndr0
4l3j4ndr0 / lambda.py
Created July 26, 2024 16:30
virus-scan-clam-av - Lambda function to analyze malware on files from S3 storage.
# 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
@4l3j4ndr0
4l3j4ndr0 / lambda.py
Last active July 26, 2024 16:30
clam-av-database - Function to download the clam-av database definitions.
# 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
@4l3j4ndr0
4l3j4ndr0 / clam-av.yaml
Last active October 19, 2024 17:00
CloudFormation template to deploy a full serverless virus scan solution.
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