Crypto-Price-AI-Agent
This agent continuously watches the prices of the most popular cryptocurrencies. It automatically keeps track of which coins are currently among the top 50 by value and updates a database with the latest information. This solves the problem of needing to manually monitor the cryptocurrency market and collect data, saving time and effort. Traders, investors, and anyone interested in cryptocurrency trends would find this helpful. The agent is useful because it provides real-time data and automatically updates a central location, ensuring information is always current. It’s distinctive because it uses advanced technology to intelligently gather and analyze this data, all while running locally.
README
# Pydantic AI Crypto Price Agent
An intelligent cryptocurrency price monitoring agent that uses Pydantic-AI with local Llama 3.2 to fetch and analyze real-time crypto market data. The agent actively monitors the top 50 cryptocurrencies, detects market shifts, and updates your Supabase database automatically.
## Features
- 🤖 **AI-Powered Data Fetching**: Uses Pydantic-AI with local Llama 3.2 model
- 📊 **Real-time Market Monitoring**: Tracks top 50 cryptocurrencies by market cap
- 🔍 **Active Market Listening**: Detects when coins enter/exit the top 50
- 🗄️ **Database Integration**: Automatically updates Supabase with latest data
- ⚡ **Fast & Reliable**: Fetches data every 5 seconds with error handling
- 🔄 **Data Validation**: Uses Pydantic models for reliable data parsing
## Prerequisites
- Python 3.8 or higher
- Ollama (for running local Llama 3.2 model)
- Supabase account and project
- CoinGecko API key
- Internet connection
## Installation Guide
### 1. Install Ollama
Ollama allows you to run large language models locally on your machine.
#### Windows
1. Visit [ollama.com](https://ollama.com) and download the Windows installer
2. Run the installer and follow the setup wizard
3. Open Command Prompt or PowerShell to verify installation:
```cmd
ollama --version
```
#### macOS
```bash
# Using Homebrew (recommended)
brew install ollama
# Or download from ollama.com and run the installer
```
#### Linux
```bash
# Install via curl
curl -fsSL https://ollama.com/install.sh | sh
# Or using package managers:
# Ubuntu/Debian
sudo apt update && sudo apt install ollama
# Fedora
sudo dnf install ollama
# Arch Linux
sudo pacman -S ollama
```
### 2. Download and Run Llama 3.2 Model
After installing Ollama, download the Llama 3.2 model:
```bash
ollama pull llama3.2
```
**Start the Ollama service:**
#### Windows
```cmd
# Ollama typically starts automatically on Windows
# If not, run:
ollama serve
```
#### macOS
```bash
# Start Ollama service
ollama serve
```
#### Linux
```bash
# Start Ollama service
ollama serve
# Or if using systemd:
sudo systemctl start ollama
sudo systemctl enable ollama # To start on boot
```
**Verify Llama 3.2 is working:**
```bash
ollama run llama3.2
# You should see a prompt where you can chat with the model
# Type /bye to exit
```
### 3. Set Up Python Environment
#### Windows
```cmd
# Create virtual environment
python -m venv crypto_agent_env
crypto_agent_env\Scripts\activate
# Install dependencies
pip install pydantic-ai requests python-dotenv supabase pydantic
```
#### macOS/Linux
```bash
# Create virtual environment
python3 -m venv crypto_agent_env
source crypto_agent_env/bin/activate
# Install dependencies
pip install pydantic-ai requests python-dotenv supabase pydantic
```
### 4. Set Up Environment Variables
Create a `.env` file in your project directory:
```env
# Supabase Configuration
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_KEY=your_service_role_key_here
# CoinGecko API Key (get free key from coingecko.com)
CRYPTO_API_KEY=your_coingecko_api_key_here
```
**Get your API keys:**
1. **Supabase credentials**:
- Go to your Supabase project → Settings → API
- Copy the Project URL and service_role key
2. **CoinGecko API key**:
- Visit [coingecko.com](https://www.coingecko.com/en/api)
- Sign up for a free account
- Get your API key from the dashboard
### 5. Set Up Supabase Database
Create the cryptocurrencies table in your Supabase project:
```sql
CREATE TABLE cryptocurrencies (
id TEXT PRIMARY KEY,
symbol TEXT NOT NULL,
name TEXT NOT NULL,
current_price NUMERIC NOT NULL,
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create index for better performance
CREATE INDEX idx_cryptocurrencies_symbol ON cryptocurrencies(symbol);
CREATE INDEX idx_cryptocurrencies_last_updated ON cryptocurrencies(last_updated);
```
## Running the Agent
### 1. Start Ollama and Llama 3.2
**In Terminal/Command Prompt 1:**
```bash
ollama run llama3.2
```
Keep this running in the background. You can minimize this terminal.
### 2. Run the Price Agent
**In Terminal/Command Prompt 2:**
#### Windows
```cmd
# Navigate to your project directory
cd path\to\your\project
# Activate virtual environment
crypto_agent_env\Scripts\activate
# Run the agent
python priceAgent.py
```
#### macOS/Linux
```bash
# Navigate to your project directory
cd path/to/your/project
# Activate virtual environment
source crypto_agent_env/bin/activate
# Run the agent
python3 priceAgent.py
```
## Expected Output
When running successfully, you should see output like this:
```
Supabase client initialized.
Pydantic-AI agent initialized with Llama 3.2.
--- Starting new data cycle at 2024-01-15 10:30:45 UTC ---
Sending prompt to agent: 'Get the latest market data for the top 50 cryptocurrencies by market cap.'
🤖 AI Agent triggered tool: fetch_top_50_coins_data
✅ Successfully fetched and validated data for 50 coins.
Uploading 50 records to Supabase...
✅ Successfully upserted data to Supabase.
--- Cycle finished. Waiting for 5 seconds... ---
--- Starting new data cycle at 2024-01-15 10:30:50 UTC ---
📢 Market Shift Detected! New coins in top 50: dogecoin
📢 Market Shift Detected! Coins that dropped out of top 50: some-other-coin
```
## Configuration Options
You can modify these settings in `priceAgent.py`:
```python
# Change update frequency (in seconds)
update_interval_seconds = 5 # Default: 5 seconds
# Change number of coins to fetch (max 250 for free CoinGecko API)
"per_page": 50, # In the fetch_top_50_coins_data function
```
## Troubleshooting
### Common Error: "Connection refused" or "Ollama not responding"
**Cause**: Ollama service is not running or not accessible.
**Solutions**:
1. **Check if Ollama is running**:
```bash
curl http://localhost:11434/api/tags
```
2. **Start Ollama service**:
```bash
ollama serve
```
3. **Verify Llama 3.2 is installed**:
```bash
ollama list
# Should show llama3.2 in the list
```
### Common Error: "Model not found" or "llama3.2 not available"
**Solutions**:
1. **Pull the model again**:
```bash
ollama pull llama3.2
```
2. **Check available models**:
```bash
ollama list
```
3. **Try alternative model names**:
```python
# In priceAgent.py, try:
ollama_model = OpenAIModel(model_name='llama3.2:latest', provider=ollama_provider)
```
### Common Error: "CRYPTO_API_KEY not found"
**Solutions**:
1. **Check your .env file** exists and has the correct format
2. **Verify API key** is valid at coingecko.com
3. **Check file location** - .env should be in the same directory as priceAgent.py
### Common Error: "Supabase connection failed"
**Solutions**:
1. **Verify Supabase credentials** in your .env file
2. **Check database table exists** with correct schema
3. **Ensure service_role key** is used (not anon key)
### Performance Issues
**If the agent is slow**:
1. **Check internet connection** for API calls
2. **Increase update interval**:
```python
update_interval_seconds = 30 # Change from 5 to 30 seconds
```
3. **Reduce number of coins**:
```python
"per_page": 25, # Change from 50 to 25
```
### Ollama Service Management
#### Windows
```cmd
# Check if Ollama is running
tasklist | find "ollama"
# Kill Ollama if needed
taskkill /f /im ollama.exe
# Restart
ollama serve
```
#### macOS
```bash
# Check if Ollama is running
ps aux | grep ollama
# Kill Ollama if needed
pkill ollama
# Restart
ollama serve
```
#### Linux
```bash
# If using systemd
sudo systemctl status ollama
sudo systemctl restart ollama
# Manual process management
ps aux | grep ollama
pkill ollama
ollama serve
```
## File Structure
```
your-project/
├── priceAgent.py # Main agent script
├── .env # Environment variables (create this)
├── README.md # This guide
└── crypto_agent_env/ # Virtual environment directory
```
## Advanced Configuration
### Using Different Models
You can experiment with other Ollama models:
```bash
# Install other mo
[truncated…]PUBLIC HISTORY
IDENTITY
Identity inferred from code signals. No PROVENANCE.yml found.
Is this yours? Claim it →METADATA
README BADGE
Add to your README:
