An AI-powered code review system built with FastAPI and OpenAI, showcasing GenAI expertise, prompt engineering, and practical AI application development.
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
# 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# Copy environment template
cp .env.example .env
# Edit .env and add your OpenAI API key
OPENAI_API_KEY=your_api_key_herepython test_prompts.pyThis will run several tests demonstrating:
- Code validation and metrics
- Comprehensive code review
- Unit test generation
- Different review types (security, performance, best practices)
- Comprehensive Review: Complete code analysis covering all aspects
- Security Review: Focus on vulnerabilities and security issues
- Performance Review: Analyze complexity and optimization opportunities
- Best Practices: Check adherence to coding standards and patterns
- Documentation Review: Evaluate comments and documentation quality
- Bug Detection: Identify and fix potential bugs
- Complexity Analysis: Big O notation and scalability assessment
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"])- Role-based system prompts for different expertise areas
- Structured output formatting
- Context-aware analysis
- Specific, actionable recommendations
- Python syntax validation using AST
- Function and class extraction
- Line counting (code, comments, blank)
- Token estimation for context management
- Async and sync completion methods
- Streaming support (for real-time responses)
- Usage tracking and logging
- Error handling
- RESTful API for code review
- Request/response models with Pydantic
- File upload support
- Rate limiting
- Multi-file analysis
- RAG for code pattern recognition
- Batch processing
- Review history and caching
- Backend: FastAPI, Uvicorn
- AI/ML: OpenAI GPT-4
- Code Analysis: Python AST, Radon, Bandit
- Async: asyncio, aiofiles
- Config: python-dotenv
| 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 |