Skip to content

BhukyaKishore/Code-Generator-Agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Code Wizard - Multi-Language AI Code Generator

Generate production-ready code in 6 programming languages with AI-powered self-consistency prompting


πŸ“‹ Table of Contents

  1. Overview
  2. Architecture
  3. System Components
  4. AI Model & Methodology
  5. Installation & Setup
  6. Usage Guide
  7. Security & Guardrails
  8. Logging System
  9. API Documentation
  10. Technical Workflow
  11. Performance & Optimization
  12. Troubleshooting

🎯 Overview

Code Wizard is an intelligent code generation platform that leverages advanced AI models to generate production-ready code across multiple programming languages. It combines sophisticated prompting techniques with strict security guardrails to ensure both quality and safety.

Key Features

βœ… Multi-Language Support: Python, JavaScript, Java, C++, C, SQL βœ… Self-Consistency Prompting: Generates 9 different solutions and picks the best βœ… Real-time Progress Tracking: Visual progress bar during generation βœ… Timestamped Logging: Detailed logs for every execution βœ… Security Guardrails: Prevents malicious code patterns βœ… Beautiful UI: 3D animated interface with modern design βœ… One-Click Copy: Easy code copying to clipboard βœ… Language-Specific Bot Names: Unique personality for each language


πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    FRONTEND (Browser)                        β”‚
β”‚           Beautiful UI with 3D Animations                    β”‚
β”‚                    index.html                                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚ HTTP/REST
                           β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              FASTAPI BACKEND (main.py)                       β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚  API Routes                                         β”‚    β”‚
β”‚  β”‚  β€’ /api/generate        - Code generation          β”‚    β”‚
β”‚  β”‚  β€’ /health              - Health check             β”‚    β”‚
β”‚  β”‚  β€’ /api/languages       - Supported languages      β”‚    β”‚
β”‚  β”‚  β€’ /api/guardrails      - Security rules           β”‚    β”‚
β”‚  β”‚  β€’ /api/logs            - Recent log files         β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚  Middleware & Validation                            β”‚    β”‚
β”‚  β”‚  β€’ CORS Configuration                              β”‚    β”‚
β”‚  β”‚  β€’ Request Validation                              β”‚    β”‚
β”‚  β”‚  β€’ Security Filtering                              β”‚    β”‚
β”‚  β”‚  β€’ Error Handling                                  β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚  Logging System                                     β”‚    β”‚
β”‚  β”‚  β€’ Timestamped log files (logs/)                    β”‚    β”‚
β”‚  β”‚  β€’ Console output                                  β”‚    β”‚
β”‚  β”‚  β€’ Request/Response tracking                       β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚
                           β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           CODE GENERATION AGENT (agent.py)                  β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚  Language Configurations                            β”‚    β”‚
β”‚  β”‚  β€’ Language-specific system prompts                 β”‚    β”‚
β”‚  β”‚  β€’ Syntax validators                               β”‚    β”‚
β”‚  β”‚  β€’ Code quality scorers                            β”‚    β”‚
β”‚  β”‚  β€’ Fallback templates                              β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚  Self-Consistency Engine                            β”‚    β”‚
β”‚  β”‚  β€’ Generate 9 solutions with different temps        β”‚    β”‚
β”‚  β”‚  β€’ Score each solution (0-10+)                      β”‚    β”‚
β”‚  β”‚  β€’ Return best solution                            β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚  LLM Interface (Qwen2.5-Coder-7B)                   β”‚    β”‚
β”‚  β”‚  β€’ Local GGUF model loading                         β”‚    β”‚
β”‚  β”‚  β€’ Inference with llama-cpp-python                  β”‚    β”‚
β”‚  β”‚  β€’ Multiple sampling strategies                     β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚
                           β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚            LOCAL LLM MODEL (Qwen2.5-Coder-7B)              β”‚
β”‚            Quantized GGUF Format (4-5GB)                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ”§ System Components

1. Frontend (index.html)

Technology Stack:

  • Pure HTML5, CSS3, JavaScript (No frameworks)
  • WebGL-inspired 3D animated backgrounds
  • Responsive design (Mobile, Tablet, Desktop)

Key Features:

  • Language selection buttons (6 languages)
  • Textarea for prompt input
  • Real-time progress bar with gradient animation
  • Code output with syntax highlighting
  • One-click copy functionality
  • Error message display
  • Security guardrails display

Animations:

  • Floating blob animations (3D effect)
  • Button hover effects
  • Smooth transitions
  • Loading spinner
  • Progress bar with glow effect

2. Backend (main.py)

Framework: FastAPI (Async Python Web Framework)

Components:

main.py
β”œβ”€β”€ Logging Setup
β”‚   β”œβ”€β”€ Timestamped log files (logs/codewizard_YYYYMMDD_HHMMSS.log)
β”‚   β”œβ”€β”€ File handler (DEBUG level)
β”‚   └── Console handler (INFO level)
β”‚
β”œβ”€β”€ API Routes
β”‚   β”œβ”€β”€ GET /                 - Serve index.html
β”‚   β”œβ”€β”€ GET /health          - Health check endpoint
β”‚   β”œβ”€β”€ POST /api/generate   - Code generation
β”‚   β”œβ”€β”€ GET /api/languages   - Language info
β”‚   β”œβ”€β”€ GET /api/guardrails  - Security rules
β”‚   └── GET /api/logs        - Log file listing
β”‚
β”œβ”€β”€ Validation Layer
β”‚   β”œβ”€β”€ Prompt validation (max 1000 chars)
β”‚   β”œβ”€β”€ Language validation
β”‚   β”œβ”€β”€ Security pattern detection
β”‚   └── Sanitization
β”‚
β”œβ”€β”€ Error Handling
β”‚   β”œβ”€β”€ HTTP exception handlers
β”‚   β”œβ”€β”€ Graceful error messages
β”‚   └── Detailed logging
β”‚
└── Middleware
    β”œβ”€β”€ CORS configuration
    β”œβ”€β”€ Request logging
    └── Response handling

Security Guardrails (16 patterns):

  • SQL injection patterns (DROP TABLE, DELETE FROM, TRUNCATE)
  • Code execution (eval, exec, system, os.system)
  • Dangerous imports (import)
  • Credential exposure (password, api_key, secret)
  • System commands (rm -rf, chmod 777, sudo)
  • Network exploitation (curl exec, wget exec)

3. Agent (agent.py)

Core Technology: Qwen2.5-Coder-7B (7 Billion Parameters)

Key Components:

Self-Consistency Prompting

Algorithm:
1. Generate 9 different code solutions
2. Use varying temperatures (0.1 to 0.9) for diversity
3. Score each solution based on quality metrics
4. Return the highest-scoring solution

Why 9 samples?
- Statistically significant diversity
- Computationally efficient (vs 16-25 samples)
- Good balance between quality and speed

Temperature Settings

Temperatures Used: [0.1, 0.2, 0.3, 0.4, 0.5, 0.5, 0.7, 0.9, 0.9]

- Low (0.1-0.3): Conservative, focused code
- Medium (0.4-0.5): Balanced exploration
- High (0.7-0.9): Creative variations

Benefit: Generates diverse solutions to select best

Code Scoring System

Scoring Criteria:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Criteria                        β”‚ Points β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Optimal length (50-1000 chars)  β”‚  2.0  β”‚
β”‚ Function/Class definition       β”‚  3.0  β”‚
β”‚ Return statement                β”‚  2.0  β”‚
β”‚ Documentation/Comments          β”‚  1.0  β”‚
β”‚ Type hints                      β”‚  1.0  β”‚
β”‚ Logic keywords present          β”‚  2.0  β”‚
β”‚ Keyword matching (from prompt)  β”‚ 0.5x  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Penalties:                      β”‚        β”‚
β”‚ - TODO/FIXME patterns           β”‚ -5.0  β”‚
β”‚ - Incomplete code               β”‚ -5.0  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Max Score: ~15-20 points

Language-Specific Prompts

Each language has a custom system prompt:

# Python Prompt includes:
- Type hints examples
- Python idioms
- No markdown requirement
- Standard library usage

# JavaScript Prompt includes:
- ES6+ syntax
- Async/await patterns
- Modern JavaScript conventions

# Java Prompt includes:
- Class structure
- Java naming conventions
- Proper OOP patterns

# SQL Prompt includes:
- Query optimization tips
- GROUP BY patterns
- JOIN examples

Code Validation Pipeline

Raw LLM Output
    ↓
[1] Remove Markdown (```, ```python, etc.)
    ↓
[2] Extract Code Section
    ↓
[3] Remove Explanatory Text
    ↓
[4] Check Minimum Length (>15 chars)
    ↓
[5] Detect Bad Patterns (TODO, FIXME, pass)
    ↓
[6] Syntax Validation (Python: ast.parse)
    ↓
[7] Return Valid Code or Empty String

Fallback System

When LLM fails (model not available):

  • Pattern-matched fallback templates
  • Language-specific boilerplate
  • Proper structure and syntax
  • Ready-to-run code

πŸ€– AI Model & Methodology

Model Information

Model Name: Qwen2.5-Coder-7B-Instruct

  • Parameters: 7 Billion
  • Quantization: Q5_K_M (GGUF format)
  • Size: ~4.7 GB
  • Architecture: Transformer-based
  • Training Data: Code + general knowledge
  • Context Window: 4096 tokens

Inference Configuration

MODEL_PARAMS = {
    "model_path": "./models/qwen2.5-coder-7b-instruct-q5_k_m.gguf",
    "n_ctx": 4096,              # Context window size
    "n_threads": 8,             # CPU threads
    "n_gpu_layers": 0,          # 0 = CPU only
    "verbose": False            # Suppress debug output
}

INFERENCE_SETTINGS = {
    "max_tokens": 1500,         # Maximum output length
    "temperature": 0.1-0.9,     # Varies by sample
    "top_p": 0.9,              # Nucleus sampling
    "repeat_penalty": 1.15,    # Avoid repetition
    "stop": ["Prompt:", "\n\n\n\n", "Output:"]
}

Why Self-Consistency Prompting?

Traditional Approach:

Prompt β†’ Model β†’ Single Output
         (Deterministic)

Self-Consistency Approach:

Prompt β†’ Model β†’ Solution 1 (Score: 8.5)
      β†˜           Solution 2 (Score: 7.2)
       β†˜          Solution 3 (Score: 9.1) ← Selected
        β†’ Model β†’ Solution 4 (Score: 6.8)
                  ...

Benefits:

  • βœ… Higher quality through selection
  • βœ… Diversity reduces errors
  • βœ… Objective scoring eliminates bias
  • βœ… Fallback for failed attempts

Token Usage Estimation

Input Prompt:  ~100-200 tokens
System Prompt: ~400-500 tokens
Output Code:   ~200-600 tokens
─────────────────────────────
Per Sample:    ~700-1300 tokens
9 Samples:     ~6,300-11,700 tokens

Total Generation: ~11,700 tokens (average)

πŸ’» Installation & Setup

Prerequisites

  • Python 3.8+
  • 5GB free disk space (for model)
  • 8GB+ RAM recommended
  • Modern web browser

Step 1: Clone/Download Project

cd code-wizard

Step 2: Install Dependencies

# Create virtual environment
python -m venv venv

# Activate virtual environment
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate

# Install Python packages
pip install fastapi uvicorn pydantic llama-cpp-python

Step 3: Download AI Model

# Create models directory
mkdir models

# Download Qwen2.5-Coder-7B (requires git-lfs)
# Option 1: Using huggingface-cli
pip install huggingface-hub
huggingface-cli download Qwen/Qwen2.5-Coder-7B-Instruct-GGUF \
  qwen2.5-coder-7b-instruct-q5_k_m.gguf \
  --local-dir ./models

# Option 2: Manual download from Hugging Face
# Visit: https://huggingface.co/Qwen/Qwen2.5-Coder-7B-Instruct-GGUF
# Download: qwen2.5-coder-7b-instruct-q5_k_m.gguf
# Place in: ./models/

Step 4: Verify Installation

# Check model file
ls -lh models/qwen2.5-coder-7b-instruct-q5_k_m.gguf

# Should show ~4.7GB file

Step 5: Run Application

# Start FastAPI server
python main.py

# Server will start at http://localhost:8000
# API Docs: http://localhost:8000/docs

Step 6: Access Application

Open browser and navigate to:
http://localhost:8000

πŸ“– Usage Guide

For End Users

  1. Open Application

    • Navigate to http://localhost:8000
    • See beautiful dashboard with animations
  2. Select Language

    • Click on one of 6 language buttons
    • Button will highlight with gradient
  3. Describe Your Code

    • Type natural language description
    • Examples:
      • "Write a function to check if a number is prime"
      • "Create a user login system"
      • "Build a SQL query to get top 10 products"
  4. Generate Code

    • Click "Generate Code" button
    • Watch progress bar animate
    • Code appears in right panel
  5. Copy Code

    • Click "Copy Code" button
    • Code is copied to clipboard
    • Confirmation message appears
  6. Clear & Repeat

    • Click "Clear" to reset
    • Start new code generation

For Developers

API Request Example

curl -X POST http://localhost:8000/api/generate \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Write a function to count vowels in a string",
    "language": "python"
  }'

API Response Example

{
  "code": "def count_vowels(text: str) -> int:\n    vowels = \"aeiouAEIOU\"\n    return sum(1 for c in text if c in vowels)",
  "language": "python",
  "prompt": "Write a function to count vowels in a string",
  "timestamp": "2024-01-15T10:30:45.123456",
  "bot_name": "PyWizard",
  "status": "success",
  "generation_time": 2.34
}

Check API Health

curl http://localhost:8000/health

Get Supported Languages

curl http://localhost:8000/api/languages

πŸ”’ Security & Guardrails

Security Architecture

User Input
    ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 1. Length Check                 β”‚ Max 1000 chars
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 2. Pattern Matching             β”‚ 16 regex patterns
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 3. Keyword Detection            β”‚ Dangerous keywords
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 4. Validation Response          β”‚ Pass/Reject
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    ↓
[Approved] β†’ Code Generation
[Rejected] β†’ Error Message to User

Blocked Patterns (16 Security Rules)

SQL Injection:
- DROP TABLE
- DELETE FROM
- TRUNCATE TABLE

Code Execution:
- eval()
- exec()
- system()
- os.system()

Dangerous Imports:
- __import__

Credential Exposure:
- password =
- api_key =
- secret =

System Commands:
- rm -rf
- chmod 777
- sudo
- curl ... exec
- wget ... exec

Response Status Codes

200 OK               - Code generated successfully
400 Bad Request      - Invalid language or failed validation
403 Forbidden        - Security pattern detected
503 Service Error    - Agent not initialized
500 Server Error     - Unexpected error

πŸ“ Logging System

Log File Structure

logs/
β”œβ”€β”€ codewizard_20240115_090000.log
β”œβ”€β”€ codewizard_20240115_091530.log
β”œβ”€β”€ codewizard_20240115_092245.log
└── codewizard_20240115_095010.log

Log Format

2024-01-15 09:00:00 - [INFO] - __main__ - πŸš€ CODE WIZARD API - APPLICATION STARTUP
2024-01-15 09:00:01 - [INFO] - __main__ - βœ… FastAPI application initialized
2024-01-15 09:00:15 - [INFO] - __main__ - πŸ“₯ NEW CODE GENERATION REQUEST
2024-01-15 09:00:15 - [INFO] - __main__ - πŸ”€ Language: python
2024-01-15 09:00:15 - [INFO] - __main__ - πŸ“ Prompt: Write a function to count vowels...
2024-01-15 09:00:15 - [INFO] - __main__ - βœ… Prompt validation passed
2024-01-15 09:00:15 - [INFO] - __main__ - πŸš€ Starting code generation for python...
2024-01-15 09:02:45 - [INFO] - agent.py - βœ… Code generated successfully in 2.34s
2024-01-15 09:02:45 - [INFO] - __main__ - βœ… Code generated successfully in 2.34s
2024-01-15 09:02:45 - [INFO] - __main__ - πŸ“Š Generated code length: 234 characters

Log Levels

DEBUG   - Detailed diagnostic information
INFO    - General informational messages
WARNING - Warning messages for suspicious activity
ERROR   - Error messages with stack traces

Accessing Logs

# View recent logs
tail -f logs/codewizard_*.log

# Get specific run logs
cat logs/codewizard_20240115_090000.log

# List all log files
ls -lh logs/

πŸ”Œ API Documentation

Base URL

http://localhost:8000

Endpoints

1. Generate Code

POST /api/generate
Content-Type: application/json

Request:
{
  "prompt": string,      // Max 1000 characters
  "language": string     // python|javascript|java|cpp|c|sql
}

Response:
{
  "code": string,
  "language": string,
  "prompt": string,
  "timestamp": string,
  "bot_name": string,
  "status": string,
  "generation_time": float
}

Examples:
Prompt: "count vowels in string"
Language: "python"

Response Status:
200 - Success
400 - Invalid input
403 - Restricted pattern
500 - Server error

2. Health Check

GET /health

Response:
{
  "status": "healthy",
  "service": "Code Wizard API",
  "timestamp": string,
  "uptime": string
}

3. List Languages

GET /api/languages

Response:
{
  "languages": ["python", "javascript", "java", "cpp", "c", "sql"],
  "bots": {
    "python": "PyWizard",
    "javascript": "ScriptMaster",
    ...
  },
  "count": 6
}

4. Security Guardrails

GET /api/guardrails

Response:
{
  "guardrails": [string],
  "max_prompt_length": 1000,
  "security_patterns_count": 16
}

5. Log Files

GET /api/logs

Response:
{
  "logs": [string],
  "total": number
}

πŸ”„ Technical Workflow

Complete Request-Response Flow

User Interface (Browser)
    ↓
    └─→ [User Input]
        β€’ Language selection
        β€’ Code prompt description
        β€’ Click "Generate"
    ↓
HTTP POST /api/generate
    ↓
FastAPI Route Handler (main.py)
    β”œβ”€ Log request details
    β”œβ”€ Validate language
    β”œβ”€ Validate prompt
    β”‚   β”œβ”€ Check length (<1000 chars)
    β”‚   β”œβ”€ Check security patterns
    β”‚   └─ Return error if invalid
    β”œβ”€ Call CodeGeneratorAgent
    ↓
Agent Initialization (agent.py)
    β”œβ”€ Load Qwen2.5-Coder LLM
    β”œβ”€ Setup language-specific prompt
    ↓
Self-Consistency Generation Loop
    β”œβ”€ Iteration 1 (temp=0.1)
    β”‚   β”œβ”€ LLM inference
    β”‚   β”œβ”€ Extract code
    β”‚   β”œβ”€ Score: 8.5
    β”œβ”€ Iteration 2 (temp=0.2)
    β”‚   β”œβ”€ LLM inference
    β”‚   β”œβ”€ Extract code
    β”‚   β”œβ”€ Score: 7.2
    β”œβ”€ ...
    β”œβ”€ Iteration 9 (temp=0.9)
    β”‚   β”œβ”€ LLM inference
    β”‚   β”œβ”€ Extract code
    β”‚   β”œβ”€ Score: 6.5
    ↓
Select Best Solution
    β”œβ”€ Find max score (9.1)
    β”œβ”€ Return associated code
    ↓
Return Response
    β”œβ”€ Serialize to JSON
    β”œβ”€ Include metadata
    β”œβ”€ Log generation time
    β”œβ”€ Log code stats
    ↓
HTTP 200 Response
    {
      "code": "...",
      "language": "python",
      "generation_time": 2.34,
      ...
    }
    ↓
Browser Display
    β”œβ”€ Stop progress bar
    β”œβ”€ Show generated code
    β”œβ”€ Enable copy button
    β”œβ”€ Hide loading spinner
    ↓
User Action
    β”œβ”€ Copy code (one-click)
    β”œβ”€ Clear and try again
    β”œβ”€ Refine prompt

⚑ Performance & Optimization

Generation Time Breakdown

Total Time: ~2-4 seconds (average)

Breakdown:
β”œβ”€ API Round-trip:     100-200ms
β”œβ”€ Validation:         50-100ms
β”œβ”€ Model Loading:      500-1000ms (first run only)
β”œβ”€ 9x LLM Inference:   1000-2000ms (most time)
└─ Post-processing:    100-200ms

Memory Usage

At Rest:
β”œβ”€ Python processes: ~100-150MB
β”œβ”€ LLM Model: ~4.7GB (loaded once)
└─ Total: ~4.8GB

During Generation:
β”œβ”€ Base overhead: ~500MB
β”œβ”€ Generation buffers: ~200MB
└─ Peak: ~5.5GB

Optimization Techniques

Model Quantization:

  • Q5_K_M format reduces model size by 60%
  • Minimal quality loss vs FP32
  • Faster inference speed

Temperature Diversity:

  • Varying temperatures (0.1-0.9) prevents overfitting
  • Reduces similar solution duplicates
  • Better coverage of solution space

Async Processing:

  • FastAPI handles multiple concurrent requests
  • Non-blocking I/O operations
  • Scalable to many users

Code Scoring:

  • Avoids manual review
  • Objective selection criteria
  • Consistent quality

πŸ› οΈ Troubleshooting

Issue: Model Not Found

Error: Model not found at ./models/qwen2.5-coder-7b-instruct-q5_k_m.gguf

Solution:
1. Download model from Hugging Face
2. Place in ./models/ directory
3. Verify file size (~4.7GB)
4. Restart application

Issue: Out of Memory

Error: RuntimeError: CUDA out of memory or RAM full

Solution:
1. Close other applications
2. Ensure 8GB+ available RAM
3. Set n_gpu_layers=0 (CPU only)
4. Reduce context window (n_ctx=2048)
5. Use GPU if available (n_gpu_layers > 0)

Issue: Slow Generation

Typical: 2-4 seconds
Slow: >10 seconds

Causes:
- Low system RAM (swap usage)
- High CPU usage from other apps
- Slow disk I/O

Solutions:
1. Close background apps
2. Increase available RAM
3. Use SSD for better I/O
4. Monitor logs for errors

Issue: Port Already in Use

Error: Address already in use (port 8000)

Solution 1: Use different port
python main.py --port 8001

Solution 2: Kill existing process
# Windows:
netstat -ano | findstr :8000
taskkill /PID <PID> /F

# macOS/Linux:
lsof -i :8000
kill -9 <PID>

Issue: Invalid Response from API

Error: JSON decode error or malformed response

Solution:
1. Check logs for error messages
2. Verify prompt validation (max 1000 chars)
3. Check for security pattern issues
4. Restart FastAPI server
5. Clear browser cache

Issue: Code Quality Issues

Generated code seems incomplete or incorrect

Possible Causes:
1. Prompt is ambiguous
2. Language not well-suited for task
3. Model hallucinates (rare)

Solutions:
1. Rephrase prompt more specifically
2. Try different language
3. Check logs for generation score
4. Try again (different temperature sampling)

πŸ“Š Monitoring & Debugging

Enable Debug Logging

# In main.py, change logging level
logging.basicConfig(level=logging.DEBUG)

Monitor Generation Stats

# Check log file for generation metrics
logs/codewizard_*.log

# Key metrics to watch:
- Generation time (goal: 2-4s)
- Code length (optimal: 100-500 chars)
- Solution scores (goal: >8.0)

Check System Resources

# Monitor CPU/Memory during generation
top                    # macOS/Linux
taskmgr               # Windows GUI
wsl-manager           # WSL

# Expected:
- CPU: 60-90%
- RAM: 70-90% (during generation)

πŸš€ Future Enhancements

Potential improvements:

  1. Model Upgrades

    • Qwen2.5-Coder-32B (larger, better quality)
    • Specialized models for each language
    • Fine-tuned models for specific domains
  2. Feature Additions

    • Code explanation/documentation
    • Automated code testing
    • Performance optimization suggestions
    • Integration with IDEs (VS Code plugin)
  3. UI Improvements

    • Dark/Light theme toggle
    • Code syntax highlighting
    • Multiple tabs for different languages
    • Prompt history
  4. API Enhancements

    • WebSocket for streaming responses
    • Batch code generation
    • Code comparison tool
    • Rating/feedback system
  5. Security

    • Rate limiting per IP
    • API key authentication
    • Usage analytics
    • Abuse detection

πŸ“„ License

This project uses open-source components:

  • FastAPI: MIT License
  • Qwen2.5-Coder: Qwen License (Commercial use allowed)
  • llama-cpp-python: MIT License

🀝 Contributing

To contribute improvements:

  1. Test changes locally
  2. Verify logging works
  3. Check security guardrails
  4. Update documentation
  5. Submit improvements

πŸ“ž Support

For issues or questions:

  1. Check logs: logs/codewizard_*.log
  2. Review troubleshooting section
  3. Check API documentation: /docs
  4. Monitor health: /health endpoint

πŸŽ“ Learning Resources

Understand the technology:


Made with ❀️ by Code Wizard Team Last Updated: January 2024# Code-Generator-Agent

Code-Generator-Agent

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors