File size: 5,064 Bytes
2532605 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | # KRONECTOR API Quick Start
## Installation
Requirements already in `requirements.txt`:
- fastapi>=0.109.0
- uvicorn[standard]>=0.27.0
- pydantic (included with fastapi)
No additional installs needed!
---
## Configuration
Set these environment variables in `.env`:
```bash
GROQ_API_KEY=your-groq-key # For natural language parsing
KRONECTOR_MODEL_RUN_ID=abc123 # MLflow run ID for trained model
```
Optional:
```bash
KRONECTOR_TEST_RUN_ID=abc123 # For integration tests
```
---
## Start the Server
```bash
python -m uvicorn api.main:app --reload
```
Or with custom host/port:
```bash
python -m uvicorn api.main:app --host 0.0.0.0 --port 8000 --reload
```
**Output:**
```
INFO: Application startup complete
INFO: Uvicorn running on http://127.0.0.1:8000
```
---
## API Documentation
### Interactive Docs (Swagger UI)
Visit: **http://localhost:8000/docs**
### ReDoc
Visit: **http://localhost:8000/redoc**
---
## Endpoints
### 1. Predict Race Outcome
**Endpoint:**
```
POST /predict/f1
```
**Request:**
```json
{
"query": "What's Max Verstappen's win probability at Monaco 2023?"
}
```
**Response:**
```json
{
"win_probability": 0.87,
"metadata": {
"season": 2023,
"round": 6,
"driver_id": "VER",
"driver_name": "Max Verstappen",
"team": "Red Bull Racing",
"grid_position": 1.0
},
"shap_values": {
"grid_position": 0.45,
"sector_1_time": 0.12,
"team_pit_speed": -0.05
}
}
```
**cURL:**
```bash
curl -X POST http://localhost:8000/predict/f1 \
-H "Content-Type: application/json" \
-d '{"query": "Verstappen Monaco 2023"}'
```
---
### 2. List Drivers
**Endpoint:**
```
GET /drivers
GET /drivers?season=2023
```
**Response:**
```json
[
{
"driver_id": "VER",
"driver_name": "Max Verstappen",
"team": "Red Bull Racing"
},
{
"driver_id": "HAM",
"driver_name": "Lewis Hamilton",
"team": "Mercedes"
}
]
```
**cURL:**
```bash
curl http://localhost:8000/drivers
curl http://localhost:8000/drivers?season=2023
```
---
### 3. List Races
**Endpoint:**
```
GET /races/{season}
```
**Response:**
```json
[
{
"season": 2023,
"round": 1,
"name": "Bahrain Grand Prix"
},
{
"season": 2023,
"round": 2,
"name": "Saudi Arabian Grand Prix"
}
]
```
**cURL:**
```bash
curl http://localhost:8000/races/2023
```
---
### 4. Health Check
**Endpoint:**
```
GET /health
```
**Response:**
```json
{
"status": "healthy",
"model_loaded": true,
"data_available": true,
"version": "1.0.0"
}
```
**cURL:**
```bash
curl http://localhost:8000/health
```
---
## Error Responses
### 400 Bad Request
Invalid query or missing race data.
```json
{
"detail": "No matching data for query. Season 2099, round 999"
}
```
### 503 Service Unavailable
Model or data not loaded.
```json
{
"detail": "Model not loaded. Set KRONECTOR_MODEL_RUN_ID."
}
```
### 422 Unprocessable Entity
Validation error (e.g., query too short).
```json
{
"detail": [
{
"loc": ["body", "query"],
"msg": "ensure this value has at least 3 characters",
"type": "value_error.string.min_length"
}
]
}
```
---
## Python Client Example
```python
import requests
BASE_URL = "http://localhost:8000"
# Predict win probability
response = requests.post(
f"{BASE_URL}/predict/f1",
json={"query": "What's Lewis' chance at Silverstone 2023?"}
)
prediction = response.json()
print(f"Win probability: {prediction['win_probability']:.1%}")
print(f"Driver: {prediction['metadata']['driver_name']}")
# List drivers
drivers = requests.get(f"{BASE_URL}/drivers").json()
print(f"Total drivers: {len(drivers)}")
# List races
races = requests.get(f"{BASE_URL}/races/2023").json()
print(f"Races in 2023: {len(races)}")
# Health check
health = requests.get(f"{BASE_URL}/health").json()
print(f"API Status: {health['status']}")
```
---
## Running Tests
```bash
# All API tests
python -m pytest tests/test_api_endpoints.py -v
# With output
python -m pytest tests/test_api_endpoints.py -v -s
# Specific test
python -m pytest tests/test_api_endpoints.py::test_health_endpoint -v
```
---
## Performance Notes
- **First prediction**: ~2-3 seconds (model inference)
- **Subsequent predictions**: ~1 second (cached model)
- **Driver/race list**: <100ms
- **Health check**: <10ms
---
## Troubleshooting
### "Model not loaded"
- Set `KRONECTOR_MODEL_RUN_ID` environment variable
- Verify MLflow run exists: `mlflow runs list --experiment-id 0`
### "Race data not loaded"
- Verify `data_output/fastf1_races.parquet` exists
- Run data pipeline first: `python -m data.fastf1_pipeline`
### "Query parsing failed"
- Set `GROQ_API_KEY` environment variable
- Query must be 3+ characters
### Port already in use
```bash
python -m uvicorn api.main:app --port 8001
```
---
## Files
- [api/main.py](../api/main.py) โ FastAPI application
- [api/schemas.py](../api/schemas.py) โ Pydantic models
- [tests/test_api_endpoints.py](../tests/test_api_endpoints.py) โ Tests
---
**Status:** โ
Production-ready API
Ready to predict! ๐
|