This project implements a multi-agent system for detecting fraud in blockchain transactions using crewAI. The system utilizes various agents and custom tools to mine contract data, detect potential fraud, ensure ethical considerations, and monitor performance.
The results is published as a journal paper. If you happened to use this case, please cite this paper:
Chen, Y. J., & Madisetti, V. K. (2024). Information Security,
Ethics, and Integrity in LLM Agent Interaction.
Journal of Information Security, 16(1), 184-196.
DOI: 10.4236/jis.2025.161010
The Blockchain Fraud Detection System consists of the following key components:
- Contract Miner Agent: Extracts contract data and creates balanced datasets.
- Investigative Agents (A, B, C): Detect potential fraud using different algorithms.
- AI Ethics Expert Agent: Ensures fair and unbiased fraud detection by evaluating results.
- Performance Analyst Agent: Tracks and analyzes the performance of Investigative Agents and the overall workflow.
- Workflow Manager Agent: Oversees and manages the hierarchical process.
The system is designed with a focus on modularity, configuration, logging, and robust error handling, moving towards a production-grade architecture.
The project is organized into several files to separate concerns:
── Multiagent_blockchain
├── main.py # Main entry point to run the workflow
├── config.py # Handles application configuration and environment variables
├── logging_setup.py # Configures the application's logging system
├── MAS/ . # Modules load data, set up a language model, define CrewAI tools,
agents, and tasks, and manage the CrewAI crew.
|── baseline_models/ # Two traditional ML model for fraud detection
└── temp_datasets/ # Directory for temporary datasets generated by ContractMinerTool.py files for SampleTool and other data generateion tools
-
Clone the Repository:
git clone <https://github.com/hydrogeohc/MultiAgent_BlockChain> # Replace with your repository URL
-
Navigate to the project directory:
cd <MultiAgent_Blockchain>
-
Set up a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate`
-
Install dependencies: The project requires
crewai
,langchain-community
(or specific model libraries),pandas
,numpy
,python-dotenv
, andpydantic
.pip install -r requirements.txt # You'll need to create this file
Note: Create a
requirements.txt
file listing the necessary libraries:crewai langchain-community pandas numpy python-dotenv pydantic scikit-learn # Needed for EthicsCheckerTool metrics imblearn # If using SMOTE logic from baselines in ContractMinerTool web3 # If using web3 in tool logic
-
Set up Environment Variables: Create a
.env
file in the project root directory and add your configuration. This includes API keys for your LLM provider and the path to your dataset.# .env file OPENAI_API_KEY="your_openai_api_key" # If using OpenAI models ANTHROPIC_API_KEY="your_anthropic_api_key" # If using Anthropic models # Specify the LLM model to use (e.g., "gpt-4o", "anthropic/claude-3-5-sonnet-20240620") LLM_MODEL_NAME="gpt-4o" # or your preferred model # Path to your dataset file DATA_FILE_PATH="./forta_hacked_address_features.csv" # Update if your file path is different # Optional: Adjust LLM temperature (default is 0.1) # LLM_TEMPERATURE=0.1 # Optional: Adjust Crew process (default is hierarchical) # CREW_PROCESS="hierarchical" # or "sequential" # Optional: Configure Ethics Checker Tool parameters # ETHICS_FLAG_COLUMN="FLAG" # ETHICS_FRAUD_LABEL=1 # ETHICS_NORMAL_LABEL=0 # ETHICS_HIGH_CORRELATION_THRESHOLD=0.8 # ETHICS_TRANSPARENCY_QUANTILES='{"max value received": 0.95, "total transactions (including tnx to create contract": 0.95}' # Optional: Configure Contract Miner Tool parameters # MINER_FLAG_COLUMN="FLAG" # MINER_RANDOM_STATE=42 # MINER_FRAUD_LABEL=1 # MINER_NORMAL_LABEL=0 # Optional: LLM Request Timeout in seconds # LLM_REQUEST_TIMEOUT=60.0
-
Place your dataset: Ensure your dataset file (e.g.,
forta_hacked_address_features.csv
) is located at the path specified by theDATA_FILE_PATH
environment variable. The original file wasforta_hacked_address_features.csv
.
To run the multi-agent workflow, execute the main.py
script:
python main.py
The script will load configuration, initialize the LLM and tools,
create agents and tasks, set up the CrewAI crew, and kick off
the process. Progress and output will be logged to the console.
Components
ContractMiningSpecialist Agent
Role: Contract Mining Specialist
Goal: Mine and prepare balanced contract datasets.
Backstory: Expert in blockchain data extraction and dataset creation.
Investigative Agents (A, B, C)
Roles: Fraud Detection Specialist (Algorithm A/B/C)
Goals: Apply specific algorithms to detect fraud and report results including predictions and feature importances.
Backstory: Specialists in using various fraud detection algorithms.
AI Ethics Expert Agent
Role: AI Ethics Expert
Goal: Evaluate the fairness, bias, and performance of fraud detection results.
Backstory: Expert in AI ethics and model evaluation.
Performance Analyst Agent
Role: Performance Analyst
Goal: Monitor and analyze the performance of agent tasks and outputs.
Backstory: Data analyst specializing in workflow performance optimization.
Workflow Manager Agent
Role: Workflow Manager
Goal: Oversee and coordinate the hierarchical workflow.
Backstory: Responsible for managing the entire process.
Tools
Custom tools wrap the core logic for mining, detection, ethics checking, and performance monitoring. They are designed to accept and return structured JSON data.
ProductionContractMinerTool: Handles dataset creation based on fraud percentage.
ProductionFraudDetectionTool: Runs specific fraud detection algorithms.
ProductionEthicsCheckerTool: Evaluates detection results for fairness, bias, and performance.
ProductionPerformanceMonitorTool: Analyzes workflow performance based on agent outputs.
Note: The core implementation logic within the _run methods of the Production*Tool classes are placeholders and need to be replaced with your actual, robust code (e.g., integrating the logic from the revised ContractMinerTool.py and EthicsCheckerTool.py files).
Data
The system uses a dataset (specified by DATA_FILE_PATH) containing blockchain transaction data. Ensure this file exists and contains the necessary columns, including the FLAG column for ground truth labels used in evaluation.
Contributing
Contributions are welcome! If you find any issues or have ideas for improvements, feel free to open a pull request.
License
This project is licensed under the MIT License - see the MIT LICENSE file for details.