Skip to content

swadhikar/code_review_assistant

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Code Review Assistant

An AI-powered code review system built with FastAPI and OpenAI, showcasing GenAI expertise, prompt engineering, and practical AI application development.

📁 Project Structure

code-review-assistant/
├── src/
│   ├── llm_handler.py          # OpenAI API integration
│   ├── prompt_templates.py     # Engineered prompts
│   └── utils.py                # Code validation utilities
├── test_prompts.py             # Test suite
├── requirements.txt            # Dependencies
├── .env.example               # Environment template
└── README.md                  # Documentation

1. Installation

# Clone the repository
git clone <your-repo-url>
cd code-review-assistant

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

2. Configuration

# Copy environment template
cp .env.example .env

# Edit .env and add your OpenAI API key
OPENAI_API_KEY=your_api_key_here

3. Test the Setup

python test_prompts.py

This will run several tests demonstrating:

  • Code validation and metrics
  • Comprehensive code review
  • Unit test generation
  • Different review types (security, performance, best practices)

Review Types Available

  1. Comprehensive Review: Complete code analysis covering all aspects
  2. Security Review: Focus on vulnerabilities and security issues
  3. Performance Review: Analyze complexity and optimization opportunities
  4. Best Practices: Check adherence to coding standards and patterns
  5. Documentation Review: Evaluate comments and documentation quality
  6. Bug Detection: Identify and fix potential bugs
  7. Complexity Analysis: Big O notation and scalability assessment

💡 Example Usage

from src.llm_handler import get_openai_handler
from src.prompt_templates import PromptTemplates, ReviewType

# Initialize handler
handler = get_openai_handler()

# Review code
code = """
def calculate_sum(numbers):
    total = 0
    for num in numbers:
        total += num
    return total
"""

# Generate review
system_prompt = get_system_prompt(ReviewType.PERFORMANCE)
user_prompt = PromptTemplates.get_code_review_prompt(
    code=code,
    review_type=ReviewType.PERFORMANCE
)

response = await handler.generate_completion(
    prompt=user_prompt,
    system_prompt=system_prompt
)

print(response["content"])

Prompts

  • Role-based system prompts for different expertise areas
  • Structured output formatting
  • Context-aware analysis
  • Specific, actionable recommendations

Code Analysis Utilities

  • Python syntax validation using AST
  • Function and class extraction
  • Line counting (code, comments, blank)
  • Token estimation for context management

LLM Handler

  • Async and sync completion methods
  • Streaming support (for real-time responses)
  • Usage tracking and logging
  • Error handling

Phase 3: FastAPI Endpoints

  • RESTful API for code review
  • Request/response models with Pydantic
  • File upload support
  • Rate limiting

Phase 4: Advanced Features

  • Multi-file analysis
  • RAG for code pattern recognition
  • Batch processing
  • Review history and caching

Tech Stack

  • Backend: FastAPI, Uvicorn
  • AI/ML: OpenAI GPT-4
  • Code Analysis: Python AST, Radon, Bandit
  • Async: asyncio, aiofiles
  • Config: python-dotenv

Environment Variables

Variable Description Default
OPENAI_API_KEY OpenAI API key Required
OPENAI_MODEL Model to use gpt-4-turbo-preview
TEMPERATURE Sampling temperature 0.3
MAX_TOKENS Max tokens per response 4000
MAX_CODE_LENGTH Max code length to analyze 10000

About

An AI-powered code review system built with FastAPI and OpenAI, showcasing GenAI expertise, prompt engineering, and practical AI application development.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages