Skip to content

aws-samples/sample-data-preparation-for-fraud-models-ai-agents

FSI Fraud Detection with Generative AI

This solution provides an advanced financial fraud detection system that leverages generative AI and AWS services. It focuses on preprocessing and transforming financial transaction data to enhance fraud detection capabilities. The system utilizes a series of data transformation steps, implemented as AWS Lambda functions, and orchestrated through an Amazon Bedrock agent.

Key Features

  • Data preprocessing pipeline for fraud detection datasets
  • Generation of a data report to inform you about the input dataset
  • Utilization of AWS Lambda for scalable data transformations
  • Integration with Amazon Bedrock for intelligent orchestration
  • Amazon Kendra integration with Bedrock for searching fraud-related documentation (optional due to regional availbility; will have to uncomment in the backend-stack.ts if you want to deploy it)
  • Customizable data transformation steps including:
    • Dropping unnecessary columns
    • Converting text to lowercase
    • Cleaning entity IDs
    • Converting timestamps
    • Encoding categorical variables
    • One-hot encoding of fraud indicators
    • Adding event timestamps Feel free to add more tailored to the data that you are planning to input.

Architecture

Architecture Diagram

WebSocket Architecture

  1. WebSocket API Gateway

    • WebSockets: enables real-time bidirectional communication between clients and servers through a persistent connection, ideal for applications requiring live updates like chat or gaming.
    • Timeout Handling: API Gateway has integration timeouts, so async functions like the message handler are used to manage longer-running operations like chat requests
    • Benefits: Real-time streaming responses, better user experience, reduced latency, persistent connections
  2. WebSocket Lambda Handlers (backend/lambda/websocket/)

    • Connect Handler (connect_handler.py): Manages WebSocket connections and initialization
    • Disconnect Handler (disconnect_handler.py): Handles cleanup and state management on disconnection
    • Message Handler (message_handler.py): Routes incoming messages and dispatches to appropriate handlers
    • Streaming Chat Handler (streaming_chat_handler.py): Processes Bedrock agent requests and streams responses in real-time chunks
  3. Real-time Streaming Capabilities

    • Chunked streaming responses as they're generated by Bedrock
    • Benefits: Progressive content delivery, better perceived performance, real-time trace information
  4. Unified Message Routing

    • Message-based routing through single WebSocket connection
    • Benefits: Support for multiple message types (chat, list operations), extensible architecture

WebSocket Communication Flow

  1. Chat Flow: User sends message via WebSocket → Message Handler routes → Streaming Chat Handler processes → Bedrock Supervisor Agent → Responses streamed back in real-time chunks
  2. List Operations: Frontend sends list request via WebSocket → Message Handler routes to appropriate list function → Response sent back through WebSocket
  3. Connection Management: Persistent WebSocket connections with proper lifecycle management (connect/disconnect handlers)

The WebSocket implementation maintains session continuity and provides real-time bidirectional communication while supporting both chat interactions and data listing operations through a unified interface.

Introduction

The FSI Fraud Detection solution leverages AWS services and generative AI to:

  • Process and analyze financial transactions in real-time
  • Detect potential fraudulent patterns using machine learning
  • Provide interactive fraud investigation capabilities through a React-based web interface
  • Secure access through AWS Cognito authentication
  • Deploy infrastructure as code using AWS CDK

Prerequisites

Before deploying this solution, ensure you have the following:

  1. AWS Account Access

    • An AWS account with administrative permissions
    • AWS CLI installed and configured with your credentials
  2. Development Tools

    • Node.js 18.x or later
    • npm 8.x or later
    • AWS CDK CLI (can be installed via npm install -g aws-cdk)
    • Git (for cloning the repository)
  3. AWS Configuration

    • Code currently only supports us-east-1

Deployment Instructions

Follow these steps to deploy the solution in your AWS account:

  1. Clone the Repository

    git clone <repository-url>
    cd scale-fsi-fraud-detection-gen-ai
  2. Install Dependencies

    # Install backend dependencies
    cd backend
    npm install
  3. Deploy the Backend Stack

    # Ensure you're in the backend directory
    cdk bootstrap
    cdk synth
    cdk deploy 

    During deployment, CDK will:

    • Create necessary IAM roles and permissions
    • Deploy Lambda functions
    • Set up API Gateway endpoints
    • Configure Cognito user pool
    • Create required DynamoDB tables
  4. Configure Environment Variables After the stack deployment completes, you'll receive various outputs from CloudFormation. Update the .env file in the chatbot directory with these values:

     REACT_APP_AWS_REGION=us-east-1
     REACT_APP_COGNITO_USER_POOL_ID=<TODO>
     REACT_APP_COGNITO_CLIENT_ID=<TODO>
     REACT_APP_COGNITO_DOMAIN=<TODO>
     REACT_APP_REDIRECT_SIGNIN=http://localhost:3000/
     REACT_APP_REDIRECT_SIGNOUT=http://localhost:3000/
     REACT_APP_API_GATEWAY_ENDPOINT=<TODO>
     REACT_APP_WEBSOCKET_ENDPOINT=<TODO>
    
  5. Verify Deployment

    • Check the AWS Console to ensure all resources are created correctly
    • Test the API Gateway endpoints using the provided URLs
    • Verify Cognito user pool configuration

Post-Deployment Steps

  1. Access the Application

    cd chatbot-app
    npm install
    npm start
    • Create user on sign up
    • Verify all functionality is working as expected

Cleanup

To avoid incurring unnecessary AWS charges, you can remove the deployed resources:

cd backend
cdk destroy

Double check that all resources have been destroyed especially the S3 bucket.

Support and Contributing

For support, please open an issue in the repository. Contributions are welcome! Please read our contributing guidelines before submitting pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Authors

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •