A production-ready multi-agent system for intelligent code generation, quality analysis, and optimization using AutoGen and Chainlit.
This system demonstrates advanced multi-agent collaboration patterns for code development workflows, featuring:
- π§βπ» Code Generator Agent: Creates high-quality code from natural language requirements
- π Code Quality Analyzer Agent: Performs comprehensive static analysis and security checks
- β‘ Code Optimizer Agent: Automatically fixes issues and applies optimizations
- π€ User Proxy Agent: Handles approval workflows with intuitive web interface
- Multi-language Support: Python, JavaScript, TypeScript, Java, Go
- Comprehensive Analysis: Static analysis, complexity metrics, security scanning
- Automated Optimization: Style fixing, performance improvements, refactoring
- Quality Assurance: Built-in validation and testing recommendations
- User Approval Workflows: Interactive approval process with Chainlit UI
- pylint: Python static analysis and code quality checks
- flake8: Style guide enforcement and error detection
- bandit: Security vulnerability detection
- radon: Code complexity and maintainability metrics
- black + isort: Automated code formatting
- ηΎηΌε€§ζ¨‘ε (Bailian/DashScope): Default model provider
- OpenAI GPT: Alternative model support
- Custom Models: Configurable model backends
- Python 3.9+ (recommended: Python 3.11)
- Virtual Environment (recommended)
- ηΎηΌε€§ζ¨‘ε API Key or other supported model API
# Clone the repository
git clone <repository-url>
cd context-engineering-intro
# Create and activate 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 with your API keys and settings
nano .env # or use your preferred editor
Required environment variables:
# ηΎηΌε€§ζ¨‘ε Configuration
BAILIAN_API_KEY=bsk-your-api-key-here
BAILIAN_MODEL=qwen-max
BAILIAN_ENDPOINT=https://dashscope.aliyuncs.com/api/v1/
# Application Settings
CHAINLIT_PORT=8000
CHAINLIT_HOST=localhost
DEBUG_MODE=true
Create or update model_config.yaml
:
# ηΎηΌε€§ζ¨‘ε Configuration
provider: autogen_ext.models.openai.OpenAIChatCompletionClient
config:
model: qwen-max
api_key: ${BAILIAN_API_KEY}
base_url: ${BAILIAN_ENDPOINT}
# Start the Chainlit application
chainlit run app.py -h
# Visit http://localhost:8000 in your browser
- Start a Request: Choose a starter prompt or describe your code requirements
- Review Generation: The Code Generator creates initial code with self-validation
- Quality Analysis: The Code Reviewer performs comprehensive analysis
- Optimization: The Code Optimizer applies improvements and fixes
- User Approval: Review and approve the final code
Create a Python function to calculate fibonacci numbers with error handling
Build a Python Flask REST API for user authentication with:
- JWT token management
- Password hashing with bcrypt
- Rate limiting
- Comprehensive error handling
- Unit tests
Implement a binary search tree in Python with:
- Insert, delete, search operations
- Tree balancing
- In-order traversal
- Comprehensive docstrings and type hints
During the process, you'll see action buttons:
- β Approve Final Code: Accept and complete the workflow
- π Approve & Continue: Accept current step and proceed
- π Request Changes: Specify modifications needed
- β Reject: Reject current output
help
or/help
: Show usage instructionshistory
or/history
: View code generation historyreset
or/reset
: Clear session data
βββ agents/ # Specialized agent implementations
β βββ code_generator.py # Code generation with validation
β βββ code_reviewer.py # Quality analysis and reporting
β βββ code_optimizer.py # Automated fixing and optimization
β βββ user_proxy.py # User interaction and approval
βββ tools/ # Analysis and utility tools
β βββ code_analysis.py # Static analysis integration
β βββ complexity_analyzer.py # Complexity metrics
β βββ security_scanner.py # Security vulnerability detection
β βββ code_formatter.py # Code formatting utilities
βββ models/ # Pydantic data models
β βββ code_request.py # Code generation requests
β βββ analysis_result.py # Analysis results and metrics
β βββ optimization_result.py # Optimization outcomes
βββ prompts/ # Agent system messages
β βββ code_generator_prompts.py
β βββ code_reviewer_prompts.py
β βββ code_optimizer_prompts.py
βββ config/ # Configuration management
β βββ settings.py # Environment and tool settings
βββ app.py # Main Chainlit application
The system uses AutoGen's RoundRobinGroupChat
for agent coordination:
- Code Generator β Creates initial code with self-validation
- Code Reviewer β Analyzes quality, security, complexity
- Code Optimizer β Applies fixes and improvements
- User Proxy β Handles approval and feedback
# Install development dependencies
pip install -r requirements.txt
# Install pre-commit hooks (optional)
pre-commit install
# Run code quality checks
ruff check .
mypy .
black --check .
# Run all tests
pytest tests/ -v
# Run with coverage
pytest tests/ -v --cov=agents --cov=tools --cov-report=term-missing
# Run specific test
pytest tests/test_code_generator.py -v
- Style: PEP8 compliance with black formatting
- Type Safety: Comprehensive type hints with mypy
- Documentation: Google-style docstrings for all functions
- Testing: Minimum 80% test coverage
- Security: Bandit security scanning
Create optional configuration files:
# Pylint configuration
config/pylint.rc
# Flake8 configuration
config/setup.cfg
# Bandit configuration
config/bandit.yaml
Customize quality thresholds in .env
:
MIN_MAINTAINABILITY_INDEX=70.0
MAX_COMPLEXITY_SCORE=5.0
MAX_SECURITY_ISSUES=0
MAX_STYLE_VIOLATIONS=5
Current support includes:
- Python (Full support)
- JavaScript/TypeScript (Basic support)
- Java (Basic support)
- Go (Basic support)
β Configuration Error - Model configuration file not found
Solution: Ensure model_config.yaml
exists with valid configuration
β Authentication failed
Solution: Check your API key in .env
file and ensure it's valid
β Pylint execution failed
Solution: Ensure analysis tools are installed: pip install pylint flake8 bandit radon
β Port 8000 is already in use
Solution: Change port in .env
or kill existing process
For better performance:
- Use faster model variants (e.g.,
qwen-turbo
vsqwen-max
) - Adjust quality thresholds for faster analysis
- Enable caching for repeated requests
- Use virtual environment for better dependency isolation
Enable debug mode in .env
:
DEBUG_MODE=true
Check application logs for detailed error information.
Modify prompts in prompts/
directory to customize agent behavior:
code_generator_prompts.py
- Code generation instructionscode_reviewer_prompts.py
- Quality analysis criteriacode_optimizer_prompts.py
- Optimization strategies
Use the analysis tools in your CI/CD pipeline:
# Quality gate script
python -c "
from tools.code_analysis import analyze_code_quality
import asyncio
result = asyncio.run(analyze_code_quality(open('code.py').read()))
exit(0 if result['passed'] else 1)
"
Add support for custom models by modifying model_config.yaml
:
provider: custom_provider.CustomChatClient
config:
model: custom-model
api_key: ${CUSTOM_API_KEY}
endpoint: ${CUSTOM_ENDPOINT}
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit changes (
git commit -m 'Add amazing feature'
) - Push to branch (
git push origin feature/amazing-feature
) - Open a Pull Request
- Follow PEP8 style guidelines
- Add comprehensive tests for new features
- Update documentation for API changes
- Ensure all quality checks pass
- Add type hints for all functions
This project is licensed under the MIT License - see the LICENSE file for details.
- AutoGen Team - For the excellent multi-agent framework
- Chainlit Team - For the intuitive web interface framework
- Alibaba Cloud - For ηΎηΌε€§ζ¨‘ε API services
- Python Community - For the amazing analysis tools ecosystem
- Documentation: Check this README and inline code documentation
- Issues: Report bugs and feature requests via GitHub Issues
- Community: Join discussions in GitHub Discussions
- API Reference: See AutoGen and Chainlit official documentation
Built with β€οΈ using AutoGen, Chainlit, and ηΎηΌε€§ζ¨‘ε