Skip to content

Repository files navigation

Ethereum On-Chain Data AI Analyzer

AI-powered natural language interface for Ethereum blockchain analytics using LangChain, Perplexity, and ChromaDB.

Features

  • 🔍 Natural language queries for blockchain data
  • 📊 Real-time gas price and network utilization analysis
  • 🤖 AI-powered insights using Perplexity LLM
  • 💾 Vector database for semantic search
  • 🌐 REST API with FastAPI
  • ⚡ Fast data retrieval via Alchemy

Tech Stack

  • Backend: Python, FastAPI
  • Blockchain: Web3.py, Alchemy API
  • AI/ML: LangChain, Perplexity API
  • Vector DB: ChromaDB
  • API: REST with OpenAPI docs

Installation

"I'm going to walk you through my Ethereum AI Analyzer project, which is a full-stack application combining blockchain technology, machine learning, and API design.

Problem Statement Ethereum has incredibly rich on-chain data—blocks, transactions, gas prices, smart contract interactions. But this data is typically accessed through low-level RPC calls or specialized dashboards. There's no intuitive way for non-technical users to query blockchain data using natural language.

My Approach I built a Retrieval-Augmented Generation (RAG) system that combines:

Real-time blockchain data fetching

Semantic search via vector embeddings

LLM-powered natural language responses

Architecture (High Level) text User Query (Natural Language) ↓ FastAPI Endpoint ↓ Query Classifier (What type? Gas/Contracts/MEV?) ↓ Ethereum Data Fetcher (Web3.py + Alchemy) ↓ Vector Store (ChromaDB) - Semantic Search ↓ LangChain + Perplexity LLM ↓ AI Response with Context

🎯 PART 1: WHAT IS THIS PROJECT? Simple Explanation: Imagine you want to know:

"What's the price of gas on Ethereum?"

"Which transactions cost the most?"

"Is the network busy right now?"

Normally, you'd have to:

Go to a website

Find technical data

Read confusing numbers

Figure out what it means

Our Project lets you just ask a question in English, and the computer gives you a smart answer.

🔧 PART 2: THE TOOLS WE USE Tool 1: Python (Programming Language) What is it? A programming language (like English, but for computers)

Why we use it? Easy to learn, lots of libraries for blockchain and AI

Simple analogy: Python is like giving instructions to a robot in a language it understands

Tool 2: FastAPI (Web Server) What is it? A tool that lets people access your program through the internet

Why we use it?

Fast (hence the name)

Easy to use

Automatically creates documentation

Simple analogy: Like a restaurant waiter taking orders from customers and bringing them food

Example:

text Customer (You): "Give me gas price information" ↓ FastAPI (Waiter): "OK, let me get that for you" ↓ Backend (Chef): "Here's the data" ↓ FastAPI (Waiter): Brings back the answer to you Tool 3: Web3.py (Blockchain Connection) What is it? A Python library that talks to blockchain networks

Why we use it? It's the bridge between our program and Ethereum

Simple analogy: Like a telephone that lets you call the Ethereum network

What it does:

python

Get the latest block

latest_block = w3.eth.block_number

Get gas price

gas_price = w3.eth.gas_price

It's that simple!

Tool 4: Alchemy API (Blockchain Data Provider) What is it? A service that gives you access to Ethereum blockchain data

Why we use it instead of running our own node?

Your own node: Takes 500GB storage, needs powerful computer, slow

Alchemy: They run powerful servers, you just ask them

It's like calling a library instead of having your own library

How it works:

text Our Program → Asks Alchemy → Alchemy checks Ethereum → Sends back data Alchemy URL looks like:

text https://eth-mainnet.g.alchemy.com/v2/YOUR_KEY Think of it as an address to Alchemy's service + a key that identifies you.

Tool 5: ChromaDB (Vector Database) What is it? A database that stores information in a smart way (using numbers/vectors)

Why we use it? For "semantic search" - finding similar things intelligently

Simple explanation:

Normal database:

text Q: "Show me blocks with high gas" Result: Nothing found (too specific) ChromaDB (Vector Database):

text Q: "Show me blocks with high gas" ChromaDB thinks: "Ah, you want blocks with high utilization!" Result: Found 10 similar blocks! Real example:

python

Add data

chromadb.add( documents=["Block 100 had 50% utilization"], ids=["block_100"] )

Search (finds similar meaning, not exact words)

chromadb.query("blocks that are congested")

Returns: Block 100 (because 50% is high utilization = congestion)

Tool 6: LangChain (AI Orchestration Tool) What is it? A framework that connects AI models with data sources

Why we use it? Makes it easy to build AI systems step by step

How it works:

text User Question ↓ Step 1: Get data from blockchain ↓ Step 2: Search vector DB for similar patterns ↓ Step 3: Feed all this to AI ↓ Step 4: AI generates answer Without LangChain: You'd write 500+ lines of code With LangChain: 20 lines of code!

Tool 7: Perplexity AI (Language Model) What is it? An AI that understands and generates human language

Why we use it?

Understands your question

Generates intelligent answers

Cheaper than GPT-4

Designed for analytical tasks

How it works:

python perplexity = PerplexityLLM(api_key="your_key")

Give it information

context = "Current gas: 0.149 gwei, Network: 60% utilized"

Ask it to generate an answer

answer = perplexity("What does this mean?")

Output: "The network is moderately busy but not congested..."

🏗️ PART 3: HOW EVERYTHING WORKS TOGETHER The Complete Flow: text ┌─────────────────────────────────────────────────────────────┐ │ USER ASKS: "What's the current gas price?" │ └────────────────────┬────────────────────────────────────────┘ │ ▼ ┌──────────────────────────┐ │ FastAPI receives request │ └────────────┬─────────────┘ │ ▼ ┌──────────────────────────────┐ │ Classify question type │ │ (gas? contract? transaction?)│ └────────────┬─────────────────┘ │ ▼ ┌────────────────────────────────┐ │ Web3.py + Alchemy API fetch │ │ ETHEREUM MAINNET │ │ ↓ │ │ Latest block: 23846600 │ │ Gas price: 4.25 gwei │ │ Network util: 60% │ └────────────┬───────────────────┘ │ ▼ ┌────────────────────────────────┐ │ Store in ChromaDB │ │ (for future pattern matching) │ └────────────┬───────────────────┘ │ ▼ ┌────────────────────────────────┐ │ Search ChromaDB for similar │ │ historical patterns │ └────────────┬───────────────────┘ │ ▼ ┌────────────────────────────────────┐ │ Package data for AI: │ │ • Current data from blockchain │ │ • Historical patterns from DB │ │ • User's original question │ └────────────┬─────────────────────┘ │ ▼ ┌────────────────────────────────┐ │ Perplexity AI generates answer │ │ "Gas is 4.25 gwei, network is │ │ 60% utilized, meaning..." │ └────────────┬───────────────────┘ │ ▼ ┌────────────────────────────────┐ │ FastAPI sends response to user │ └────────────┬───────────────────┘ │ ▼ ┌─────────────────────────────────────────────────────────────┐ │ USER GETS: Detailed, intelligent answer about gas price │ └─────────────────────────────────────────────────────────────┘ 📁 PART 4: PROJECT FILES EXPLAINED File 1: ethereum_data_fetcher.py What does it do? Gets blockchain data

Uses: Web3.py, Alchemy API

Example functions:

python get_latest_block() # Gets newest block get_current_gas_price() # Gets gas price get_top_gas_consumers() # Gets expensive transactions File 2: vector_store.py What does it do? Stores and searches blockchain data intelligently

Uses: ChromaDB

Example:

python vector_store.add_block_data(block_data) # Save data vector_store.query_blocks("blocks with high gas") # Search it File 3: perplexity_llm.py What does it do? Connects to Perplexity AI

Uses: Perplexity API, LangChain

Example:

python llm = PerplexityLLM(api_key="key") answer = llm("What does this data mean?") File 4: ai_query_engine.py What does it do? Puts everything together

Uses: All the above + LangChain

Does:

Understands user question

Fetches blockchain data

Searches vector DB

Calls Perplexity AI

Returns answer

File 5: app.py What does it do? Makes the API available on the internet

Uses: FastAPI

Creates endpoints like:

text GET /health - System health POST /query - Ask questions GET /gas-price - Get gas price GET /latest-block - Get latest block 🔑 PART 5: API KEYS - WHY DO WE NEED THEM? API Key = Access Pass Imagine you want to use a gym:

You = Our program

Gym = Alchemy/Perplexity

API Key = Your gym membership card

Without it: "Sorry, I don't know who you are!"

With it: "Welcome! Here's your data!"

Our 3 API Keys:

  1. Alchemy API Key

text Why: To access Ethereum blockchain What it gives: Real-time blockchain data Without it: Can't get block info, transactions, gas prices 2. Perplexity API Key

text Why: To access AI model What it gives: Smart question answering Without it: Can't generate intelligent responses 3. Etherscan API Key (Optional)

text Why: Backup data source What it gives: Alternative blockchain data if Alchemy is down Without it: Alchemy still works fine ⚙️ PART 6: WHY THESE SPECIFIC TOOLS? Comparison: Why Alchemy vs Other Options? Option Pro Con Alchemy Fast, reliable, free tier generous Need API key Running own node Complete control 500GB storage, slow, expensive Infura Also reliable More expensive Etherscan Simple, free Limited data Winner: Alchemy ✅

Comparison: Why ChromaDB vs SQL Database? Option Best For ChromaDB (Vector DB) Finding SIMILAR patterns, semantic search PostgreSQL (SQL DB) Finding EXACT matches, structured data We need semantic search (finding similar gas usage patterns), so ChromaDB wins! ✅

Comparison: Why Perplexity vs Other LLMs? Option Cost Speed Best For Perplexity $$ Fast Analytics (our use case) ✅ GPT-4 $$$$$ Slower General purpose Claude $$$ Medium Long context Llama (Local) Free Depends on hardware Privacy Winner for this project: Perplexity ✅

📊 PART 7: DATA FLOW EXAMPLE Real Example: User Asks "Show me top gas consumers" text STEP 1: User types question Input: "Show me top gas consumers"

STEP 2: FastAPI receives it Code: app.py receives POST request

STEP 3: Classify question type ai_query_engine.py determines: "This is a top_gas_consumers query"

STEP 4: Fetch blockchain data ethereum_data_fetcher.py calls:

  • Alchemy API
  • Web3.py connects
  • Gets latest block: 23846600
  • Analyzes all 250 transactions in that block
  • Ranks by gas used × gas price
  • Top 5: • Tx 1: 15,494,447.98 Gwei • Tx 2: 15,440,453.84 Gwei • Tx 3: 15,440,453.84 Gwei ... etc

STEP 5: Store in vector DB vector_store.py saves: "Top gas consuming transactions in block 23846600" (encoded as vectors for future pattern matching)

STEP 6: Search vector DB for context Finds similar historical queries about expensive transactions

STEP 7: Package for AI Create context: Current data: [5 top transactions with costs] Historical: [Similar patterns from past] Question: [User's original question]

STEP 8: Call Perplexity AI perplexity_llm.py sends: "Here's data about expensive transactions. What insights can you give?"

STEP 9: Perplexity generates response AI Output: "The top 5 gas-consuming transactions show:

  • Average cost: 15M Gwei (extremely high)
  • All are smart contract calls
  • This indicates complex operations
  • Likely DeFi protocol interactions
  • Network is 99% utilized during these blocks"

STEP 10: FastAPI returns response JSON Response: { "query": "Show me top gas consumers", "query_type": "top_gas_consumers", "ai_response": "[AI-generated analysis above]", "blockchain_data": [list of top 5 transactions] }

STEP 11: Frontend displays it User sees: Beautiful card with the answer! 🎯 PART 8: SIMPLE ANALOGY - THE WHOLE PROJECT Think of it like a Restaurant: text CUSTOMER (User) | | Asks: "What should I eat?" | ▼ WAITER (FastAPI) | Routes order to kitchen | ▼ MENU PLANNER (ai_query_engine.py) | Classifies: Italian? Indian? Vegetarian? | ▼ SUPPLIERS (Web3.py + Alchemy) | Get fresh ingredients from market | ▼ PANTRY (ChromaDB) | Store ingredients, remember what worked before | ▼ HEAD CHEF (Perplexity AI) | "Based on available ingredients and past recipes, | I recommend: Pasta with these specific seasonings" | ▼ WAITER (FastAPI) | Brings beautiful dish to customer | ▼ CUSTOMER gets: Delicious, intelligent answer! ✅ RECAP: What We Built Component Technology Why Does What Data Source Alchemy API Fast, reliable Gets Ethereum data Data Connection Web3.py Standard in blockchain Talks to Ethereum Data Storage ChromaDB Semantic search Stores patterns Data Processing Python Flexible, easy Processes blockchain data API Server FastAPI Fast, documented Makes it accessible online AI Model Perplexity Cost-effective, analytical Generates insights AI Framework LangChain Simplifies AI integration Connects everything Frontend HTML/JS Simple, fast User interface

About

Built an LLM-powered tool that pulls Ethereum blockchain data and lets users query it in natural language.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages