Skip to content

Commit 626a2e0

Browse files
committed
Time for version 2
1 parent bb6afd3 commit 626a2e0

6 files changed

+112
-20
lines changed

.env.example

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
DB_HOST=localhost
2+
DB_USER=username
3+
DB_PASSWORD=password
4+
DB_DATABASE=nfl_games
5+
OLLAMA_MODEL=qwen2.5-coder:14b
6+
OLLAMA_URL=http://localhost:11434/api/generate

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
nfl_env/
2-
venv/
2+
venv/
3+
.env

README.md

+83-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,91 @@
11
# MySQL AI with Python and Ollama
22

3-
## Description
3+
## Description
44
This WIP project is a simple python script that connects to a DB and an Ollama instance to generate
55
an SQL query, fetch results from DB, and return the results. Accuracy depends on LLM!
66

7-
## Features
7+
## Features
88
- Uses an LLM through Ollama to generate an SQL query with a constructed prompt with the DB schema
9-
and then analyses the results to output in a human readable response
9+
- Analyzes results to output in a human readable response
10+
- Supports NFL statistics queries
1011

11-
## Contact
12+
## Example Usage
13+
```bash
14+
Enter your NFL analysis question (or 'quit' to exit): How many games did the Patriots win in the 2024 NFL season?
15+
16+
Beginning analysis...
17+
18+
Generated query: SELECT COUNT(*) FROM games WHERE nfl_team_name_winner = 'Patriots' AND season = 2024;
19+
20+
Executing query...
21+
Sending results to Ollama for analysis...
22+
23+
Analysis:
24+
The Patriots won 4 games in the 2024 NFL season.
25+
```
26+
27+
## Prerequisites
28+
- Python 3.8+
29+
- MySQL Server
30+
- Ollama installed and running
31+
- Required Python packages (see Installation)
32+
33+
## Installation
34+
```bash
35+
# Clone the repository
36+
git clone https://github.com/yourusername/mysql-ai-script.git
37+
cd mysql-ai-script
38+
39+
# Create a virtual environment
40+
python -m venv venv
41+
source venv/bin/activate
42+
43+
# Install dependencies
44+
pip install -r requirements.txt
45+
46+
# Set up environment variables
47+
cp .env.example .env
48+
# Edit .env with your database credentials
49+
```
50+
51+
## Database Setup
52+
1. Create a MySQL database named `nfl_games`
53+
2. Run the schema creation script:
54+
```bash
55+
mysql -u username -p nfl_games < tables.sql
56+
```
57+
3. Populate data using either:
58+
- [nfl_data_py](https://github.com/nflverse/nfl_data_py)
59+
- [nflscraPy](https://github.com/blnkpagelabs/nflscraPy)
60+
61+
## Usage
62+
```bash
63+
# Start Ollama (if not running)
64+
systemctl start ollama
65+
66+
# Run the script
67+
python mysql-ai.py
68+
```
69+
70+
Example queries:
71+
- "How many games did the Eagles win in 2023?"
72+
- "What was the average rushing yards for the Chiefs in 2024?"
73+
- "Show me all games where the Bills scored over 30 points"
74+
75+
## Configuration
76+
The script can be configured through environment variables:
77+
- `DB_HOST`: MySQL host (default: localhost)
78+
- `DB_USER`: Database username
79+
- `DB_PASS`: Database password
80+
- `DB_NAME`: Database name (default: nfl_games)
81+
- `OLLAMA_MODEL`: LLM model to use (default: qwen2.5-coder:14b)
82+
83+
## Contact
1284
Developed by [Ryan Murney](https://ryanmurney.ca)
85+
86+
## Acknowledgments
87+
- NFL data providers: nfl_data_py and nflscraPy
88+
89+
## Project Status
90+
Currently a WIP side project for fun:
91+
- Query complexity is limited by whichever LLM you use and or database complexity, can halluciante massively

mysql-ai.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1+
from dotenv import load_dotenv
12
import json
23
import mysql.connector
4+
import os
35
import re
46
import requests
57

8+
load_dotenv()
9+
610
DB_CONFIG = {
7-
"host": "localhost",
8-
"user": "username",
9-
"password": "username",
10-
"database": "nfl_games"
11+
"host": os.getenv('DB_HOST'),
12+
"user": os.getenv('DB_USER'),
13+
"password": os.getenv('DB_PASSWORD'),
14+
"database": os.getenv('DB_DATABASE')
1115
}
1216

17+
OLLAMA_MODEL = os.getenv('OLLAMA_MODEL')
18+
OLLAMA_URL = os.getenv('OLLAMA_URL')
19+
1320
def connect_to_db():
1421
return mysql.connector.connect(**DB_CONFIG)
1522

@@ -186,8 +193,6 @@ def generate_sql(question):
186193
db = connect_to_db()
187194
schema = get_db_schema(db)
188195

189-
print(schema)
190-
191196
close_db_connection(db)
192197

193198
prompt = f"""
@@ -216,9 +221,9 @@ def generate_sql(question):
216221
"""
217222

218223
response = requests.post(
219-
"http://localhost:11434/api/generate",
224+
OLLAMA_URL,
220225
json={
221-
"model": "qwen2.5-coder:14b",
226+
"model": OLLAMA_MODEL,
222227
"prompt": prompt,
223228
"stream": False
224229
}
@@ -259,9 +264,9 @@ def analyze_with_ollama(results, question):
259264
"""
260265

261266
response = requests.post(
262-
"http://localhost:11434/api/generate",
267+
OLLAMA_URL,
263268
json={
264-
"model": "qwen2.5-coder:14b",
269+
"model": OLLAMA_MODEL,
265270
"prompt": prompt,
266271
"stream": False
267272
}

requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ numpy==1.26.4
1818
ollama==0.4.7
1919
packaging==24.2
2020
pandas==1.5.3
21-
pydantic==2.10.6
2221
pydantic_core==2.27.2
22+
pydantic==2.10.6
2323
python-dateutil==2.9.0.post0
24+
python-dotenv==1.0.1
2425
pytz==2025.1
2526
requests==2.32.3
2627
six==1.17.0

tables.sql

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
-- games
1+
CREATE DATABASE `nfl_games`;
2+
23
CREATE TABLE `games` (
34
`game_id` int NOT NULL AUTO_INCREMENT,
45
`status` varchar(50) DEFAULT NULL,
@@ -18,9 +19,8 @@ CREATE TABLE `games` (
1819
`score_loser` int DEFAULT NULL,
1920
`boxscore_stats_link` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,
2021
PRIMARY KEY (`game_id`)
21-
) ENGINE=InnoDB AUTO_INCREMENT=6733 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
22+
) ENGINE=InnoDB AUTO_INCREMENT=6733 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
2223

23-
-- game_stats
2424
CREATE TABLE `game_stats` (
2525
`stat_id` int NOT NULL AUTO_INCREMENT,
2626
`game_id` int NOT NULL,
@@ -60,4 +60,4 @@ CREATE TABLE `game_stats` (
6060
PRIMARY KEY (`stat_id`),
6161
KEY `fk_game_stats_game` (`game_id`),
6262
CONSTRAINT `fk_game_stats_game` FOREIGN KEY (`game_id`) REFERENCES `games` (`game_id`) ON DELETE CASCADE
63-
) ENGINE=InnoDB AUTO_INCREMENT=13461 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
63+
) ENGINE=InnoDB AUTO_INCREMENT=13461 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

0 commit comments

Comments
 (0)