Skip to content

Latest commit

Β 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

JSON AI Code Generator

A production-ready Docker application that processes JSON problem descriptions and generates Python solutions using Google's Gemini AI. The application reads coding problems from JSON files, generates Python code solutions, executes them, and validates the output against expected results.

🎯 What It Does

  1. Reads JSON Files: Processes all .json files from the input directory
  2. AI Code Generation: Uses Gemini AI to generate Python solutions for each problem
  3. Code Execution: Runs the generated code with test inputs
  4. Validation: Compares actual output with expected results
  5. Solution Storage: Saves all generated solutions to files
  6. Rate Limiting: Implements intelligent delays to avoid API limits
  7. Error Handling: Robust retry logic for API failures

πŸ—οΈ Architecture

Core Components

  • main.py: Main application orchestrating the entire workflow
  • jsonLoader.py: JSON file parsing and loading utility
  • dockerfile: Multi-stage production Docker image
  • docker-compose.yml: Container orchestration configuration

Docker Configuration

Multi-Stage Build

# Builder stage: Install dependencies
FROM python:3.11-slim AS builder
# ... build dependencies and virtual environment

# Production stage: Minimal runtime image
FROM python:3.11-slim AS production
# ... copy only necessary files and run as non-root user

Security Features

  • βœ… Non-root execution: Runs as appuser for security
  • βœ… Read-only filesystem: Container filesystem is read-only
  • βœ… No new privileges: Security option prevents privilege escalation
  • βœ… Resource limits: Memory (1GB) and CPU (0.5 cores) constraints
  • βœ… Health monitoring: Built-in health checks

Production Optimizations

  • βœ… Minimal image size: Multi-stage build reduces final image size
  • βœ… Dependency isolation: Virtual environment for clean dependencies
  • βœ… Log management: Structured logging with rotation (10MB max, 3 files)
  • βœ… Restart policy: on-failure - restarts only on errors, not after completion

πŸ“ Project Structure

json-ai-code-generator/
β”œβ”€β”€ 🐳 Docker Configuration
β”‚   β”œβ”€β”€ dockerfile                 # Multi-stage production Docker image
β”‚   β”œβ”€β”€ docker-compose.yml         # Container orchestration
β”‚   └── .dockerignore              # Docker build exclusions
β”œβ”€β”€ 🎯 Application Code
β”‚   β”œβ”€β”€ main.py                    # Main processing application
β”‚   β”œβ”€β”€ jsonLoader.py              # JSON file parsing utility
β”‚   └── requirements.txt           # Python dependencies
β”œβ”€β”€ πŸš€ Deployment Scripts
β”‚   β”œβ”€β”€ deploy.ps1                 # Windows PowerShell deployment
β”‚   β”œβ”€β”€ deploy.sh                  # Linux/macOS Bash deployment
β”‚   └── Makefile                   # Linux/macOS Make commands
β”œβ”€β”€ βš™οΈ Configuration
β”‚   β”œβ”€β”€ .env.example               # Environment variables template
β”‚   └── .gitignore                 # Git exclusions
β”œβ”€β”€ πŸ“‚ Input/Output
β”‚   β”œβ”€β”€ json/                      # Input JSON problem files
β”‚   └── solutions/                 # Generated Python solutions
└── πŸ“š Documentation
    └── README.md                  # This file

πŸš€ Quick Start

1. Environment Setup

# Copy environment template
cp .env.example .env

# Edit .env file and add your Gemini API key
# GEMINI_API_KEY=your_actual_api_key_here

2. Prepare Input Files

Create JSON files in the json/ directory with this format:

{
  "query": "Write a Python program that reads an integer and prints 'YES' if it's even, 'NO' if it's odd.",
  "test_input": 7,
  "test_output": "NO"
}

3. Deploy and Run

Windows (PowerShell)

# Deploy and run
.\deploy.ps1

# Monitor progress
.\deploy.ps1 -Action logs

# Check status
.\deploy.ps1 -Action status

Linux/macOS (Bash)

# Make script executable
chmod +x deploy.sh

# Deploy and run
./deploy.sh

# Monitor progress
./deploy.sh logs

# Check status
./deploy.sh status

Linux/macOS (Make)

# Deploy and run
make deploy

# Monitor progress
make logs

# Check status
make status

πŸ“‹ Deployment Instructions

Prerequisites

  1. Docker & Docker Compose: Ensure both are installed and running
  2. Gemini API Key: Get your API key from Google AI Studio
  3. Environment File: Configure .env with your API key

Production Deployment

Step 1: Clone and Configure

git clone <repository-url>
cd json-ai-code-generator
cp .env.example .env
# Edit .env and add GEMINI_API_KEY=your_key_here

Step 2: Prepare Input Data

# Add your JSON problem files to the json/ directory
# Each file should contain: query, test_input, test_output

Step 3: Deploy

Windows:

# Full deployment
.\deploy.ps1

# Alternative: Manual Docker commands
docker build -t json-processor:latest .
docker-compose up -d

Linux/macOS:

# Using deployment script
./deploy.sh

# Using Make
make deploy

# Alternative: Manual Docker commands
docker build -t json-processor:latest .
docker-compose up -d

Step 4: Monitor Execution

# Real-time logs
docker-compose logs -f

# Check container status
docker-compose ps

# View health status
docker inspect json-processor-prod --format='{{.State.Health.Status}}'

Step 5: Retrieve Results

# Generated solutions will be in ./solutions/ directory
ls -la solutions/

# Example files:
# solution_1.py, solution_2.py, etc.

Development vs Production

Feature Development Production
Restart Policy no on-failure
Resource Limits None 1GB RAM, 0.5 CPU
Security Basic Non-root, read-only filesystem
Logging Console Structured with rotation
Health Checks Disabled Enabled (30s intervals)
Volume Mounts Read-write Read-only for inputs

Environment Variables

Variable Required Description Example
GEMINI_API_KEY βœ… Google Gemini API key AIza...
OPENAI_API_KEY βž– OpenAI API key (if using OpenAI) sk-...
OPENROUTER_API_KEY βž– OpenRouter API key (if using OpenRouter) sk-or-...
PYTHONUNBUFFERED βž– Disable Python buffering 1
PYTHONDONTWRITEBYTECODE βž– Don't create .pyc files 1

πŸ€– AI Provider Configuration

The application supports multiple AI providers. By default, it uses Google Gemini, but you can easily switch to OpenAI or OpenRouter.

Current Default (Gemini)

# In main.py - Current configuration
llm = ChatOpenAI(
    api_key=os.getenv("GEMINI_API_KEY"),
    base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
    model="gemini-2.5-flash",
)

Switch to OpenAI

1. Update Environment

# In .env file
OPENAI_API_KEY=sk-your-openai-api-key-here

2. Modify main.py

# Replace the LLM configuration in main.py
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    api_key=os.getenv("OPENAI_API_KEY"),
    model="gpt-5",  # or "gpt-5", "gpt-5-mini", etc.
    temperature=0.1,
)

3. Update Requirements

# Add to requirements.txt (if not already present)
openai>=1.0.0

Switch to OpenRouter

1. Update Environment

# In .env file
OPENROUTER_API_KEY=sk-or-your-openrouter-api-key-here

2. Modify main.py

# Replace the LLM configuration in main.py
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    api_key=os.getenv("OPENROUTER_API_KEY"),
    base_url="https://openrouter.ai/api/v1",
    model="anthropic/claude-sonnet-4.5",  # or other available models
    temperature=0.1,
    extra_headers={
        "HTTP-Referer": "https://your-site.com",  # Optional
        "X-Title": "JSON AI Code Generator",      # Optional
    }
)

3. Update Requirements

# Add to requirements.txt (if not already present)
openai>=1.0.0

Popular Model Options

OpenAI Models

  • gpt-5 - Most capable, higher cost
  • gpt-5-mini - Fast, small and capable
  • gpt-4o - Capable last gen multimodal model

OpenRouter Models

  • anthropic/claude-sonnet-4.5 - Excellent for coding
  • meta-llama/llama-3.1-70b-instruct - Open source, good performance
  • google/gemini-2.5-pro - Google's model via OpenRouter
  • z-ai/glm-4.6 - Open source, top of oss benchmarks
  • mistralai/mixtral-8x7b-instruct - Good balance of speed/quality

Configuration Examples

Multi-Provider Setup

# main.py - Support multiple providers
import os
from langchain_openai import ChatOpenAI

def get_llm_provider():
    """Select LLM provider based on available API keys"""
    if os.getenv("OPENAI_API_KEY"):
        return ChatOpenAI(
            api_key=os.getenv("OPENAI_API_KEY"),
            model="gpt-5",
            temperature=0.1,
        )
    elif os.getenv("OPENROUTER_API_KEY"):
        return ChatOpenAI(
            api_key=os.getenv("OPENROUTER_API_KEY"),
            base_url="https://openrouter.ai/api/v1",
            model="z-ai/glm-4.6",
            temperature=0.1,
        )
    elif os.getenv("GEMINI_API_KEY"):
        return ChatOpenAI(
            api_key=os.getenv("GEMINI_API_KEY"),
            base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
            model="gemini-2.5-flash",
        )
    else:
        raise ValueError("No API key found. Please set one of: OPENAI_API_KEY, OPENROUTER_API_KEY, GEMINI_API_KEY")

# Use the function
llm = get_llm_provider()

Environment-Based Selection

# .env file - Set your preferred provider
AI_PROVIDER=openai
OPENAI_API_KEY=sk-your-key-here

# Or
AI_PROVIDER=openrouter
OPENROUTER_API_KEY=sk-or-your-key-here

# Or
AI_PROVIDER=gemini
GEMINI_API_KEY=your-key-here
# main.py - Environment-based selection
import os
from langchain_openai import ChatOpenAI

def create_llm():
    provider = os.getenv("AI_PROVIDER", "gemini").lower()
    
    if provider == "openai":
        return ChatOpenAI(
            api_key=os.getenv("OPENAI_API_KEY"),
            model=os.getenv("OPENAI_MODEL", "gpt-5"),
            temperature=float(os.getenv("AI_TEMPERATURE", "0.1")),
        )
    elif provider == "openrouter":
        return ChatOpenAI(
            api_key=os.getenv("OPENROUTER_API_KEY"),
            base_url="https://openrouter.ai/api/v1",
            model=os.getenv("OPENROUTER_MODEL", "z-ai/glm-4.6"),
            temperature=float(os.getenv("AI_TEMPERATURE", "0.1")),
        )
    elif provider == "gemini":
        return ChatOpenAI(
            api_key=os.getenv("GEMINI_API_KEY"),
            base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
            model=os.getenv("GEMINI_MODEL", "gemini-2.5-flash"),
        )
    else:
        raise ValueError(f"Unsupported AI provider: {provider}")

llm = create_llm()

Rate Limiting by Provider

Different providers have different rate limits. You may need to adjust the delays:

# In main.py, adjust these values based on your provider:

# For OpenAI (adjust based on your tier)
retry_delay = 60      # Longer delay for OpenAI
time.sleep(10)        # Shorter delay between problems

# For OpenRouter (varies by model)
retry_delay = 30      # Standard delay
time.sleep(15)        # Medium delay between problems

# For Gemini (current default)
retry_delay = 30      # Standard delay
time.sleep(20)        # Longer delay between problems

Cost Considerations

Provider Model Cost (approx) Speed Quality
OpenAI gpt-5 $$$ Medium Excellent
OpenAI gpt-5-mini $ Fast Good
OpenRouter claude-4.5-sonnet $$ Medium Excellent
OpenRouter llama-3.1-70b $ Fast Good
Gemini gemini-2.5-flash $ Fast Good

Deployment with Different Providers

After modifying the configuration:

# Rebuild and deploy with new AI provider
docker build -t json-processor:latest .
docker-compose up -d

# Monitor logs to ensure new provider works
docker-compose logs -f

JSON Input Format

Each JSON file should contain:

{
  "query": "Problem description for the AI",
  "test_input": "Input data for testing (string, number, or array)",
  "test_output": "Expected output for validation"
}

Examples:

{
  "query": "Write a program to check if a number is prime",
  "test_input": 17,
  "test_output": "YES"
}
{
  "query": "Write a program to reverse a string",
  "test_input": "hello",
  "test_output": "olleh"
}

Troubleshooting

Common Issues

  1. API Rate Limiting

    • Symptom: 503 - model is overloaded errors
    • Solution: App has built-in retry logic with 30s delays
  2. No JSON Files Found

    • Symptom: Found 0 problems to process
    • Solution: Add .json files to the json/ directory
  3. Container Exits Immediately

    • Symptom: Container status shows Exited (0)
    • Solution: This is normal! Container completes and exits cleanly
  4. Permission Errors

    • Symptom: Cannot write to solutions directory
    • Solution: Check Docker volume mount permissions

Debugging Commands

# Check container logs
docker-compose logs --tail=100

# Access container shell (if running)
docker exec -it json-processor-prod /bin/bash

# Check Docker system resources
docker system df

# Clean up Docker resources
docker system prune -f

Performance Tuning

Rate Limiting Configuration

# In main.py, adjust these values:
retry_delay = 30      # Seconds between retries
time.sleep(20)        # Seconds between problems
max_retries = 3       # Maximum retry attempts

Resource Limits

# In docker-compose.yml:
deploy:
  resources:
    limits:
      memory: 1G        # Adjust based on needs
      cpus: '0.5'       # Adjust based on needs

πŸ”§ Management Commands

Container Management

Action Windows PowerShell Linux/macOS Bash Linux/macOS Make
Deploy .\deploy.ps1 ./deploy.sh make deploy
Status .\deploy.ps1 -Action status ./deploy.sh status make status
Logs .\deploy.ps1 -Action logs ./deploy.sh logs make logs
Stop .\deploy.ps1 -Action stop ./deploy.sh stop make stop
Restart .\deploy.ps1 -Action stop && .\deploy.ps1 ./deploy.sh stop && ./deploy.sh make restart

Direct Docker Commands

# Build image
docker build -t json-processor:latest .

# Run container
docker-compose up -d

# View logs
docker-compose logs -f

# Stop container
docker-compose down

# Remove everything
docker-compose down --volumes --remove-orphans

πŸ›‘οΈ Security Features

  • πŸ”’ Non-root execution: Container runs as unprivileged appuser
  • πŸ“– Read-only filesystem: Container filesystem is read-only for security
  • 🚫 No new privileges: Prevents privilege escalation attacks
  • 🎯 Minimal attack surface: Multi-stage build with minimal runtime dependencies
  • πŸ“Š Resource constraints: Memory and CPU limits prevent resource exhaustion
  • πŸ₯ Health monitoring: Regular health checks ensure container integrity

πŸ“Š Monitoring & Observability

  • πŸ“ˆ Resource Usage: Built-in CPU and memory monitoring
  • πŸ₯ Health Checks: Automated health status verification
  • πŸ“ Structured Logging: JSON-formatted logs with rotation
  • πŸ“Š Progress Tracking: Real-time progress indicators
  • ⚠️ Error Handling: Comprehensive error reporting and retry logic

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test with Docker deployment
  5. Submit a pull request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages