AI Powered Commit Message Normalization Tool
Project description
CMAI - AI-Powered Commit Message Normalizer
CMAI is an intelligent command-line tool that leverages AI to transform informal or colloquial commit messages into standardized, professional Git commit messages. It analyzes your staged changes and uses advanced language models to generate clear, concise, and conventional commit messages.
๐ Features
- AI-Powered Normalization: Converts informal commit descriptions into professional, standardized commit messages
- Git Integration: Automatically analyzes staged changes to provide context-aware suggestions
- Multiple AI Provider Support: Supports various AI providers including OpenAI-compatible APIs, Bailian (Qwen), DeepSeek, SiliconFlow, and local Ollama models
- Extensible Architecture: Easy to add new AI providers through the provider factory system
- Configurable: Customizable prompt templates and model settings
- Token Usage Tracking: Monitor AI token consumption for cost management
- Comprehensive Logging: Detailed logging for debugging and monitoring with stream output support
๐ Quick Start
Installation
Install CMAI using pip:
pip install cmai
Or install from source:
git clone https://github.com/yumuzhihan/cmai.git
cd cmai
pip install -e .
Quick Setup
- Configure your AI provider (this step is required):
Create ~/.config/cmai/settings.env:
# Example: Using OpenAI
PROVIDER=openai
API_KEY=your_openai_api_key
MODEL=gpt-4o-mini
# Or using Ollama locally
# PROVIDER=ollama
# OLLAMA_HOST=http://localhost:11434
# MODEL=qwen2.5:7b
- Set your API key (if using remote providers):
# For OpenAI
export OPENAI_API_KEY=your_api_key
# For Bailian
export DASHSCOPE_API_KEY=your_api_key
# For DeepSeek
export DEEPSEEK_API_KEY=your_api_key
- Test the installation:
# Stage some changes
git add .
# Generate a commit message
cmai "fixed a bug"
Basic Usage
- Stage your changes in Git:
git add .
- Use CMAI to generate a normalized commit message:
cmai "fix some bugs in user authentication"
- The tool will output a standardized commit message like:
Commit message: Fix authentication bugs in user login module
Tokens used: 45
โ ๏ธ Important Setup Reminder: Before using CMAI, you must configure at least two settings:
PROVIDER: The AI provider you want to use (e.g.,openai,bailian,ollama)MODEL: The specific model name (this is required for all providers)
Without these configurations, CMAI will fail to run. See the Configuration section for details.
๐ง Configuration
CMAI uses a configuration file located at ~/.config/cmai/settings.env. The configuration file will be automatically created on first run.
Environment Variables
Create or edit ~/.config/cmai/settings.env:
# AI Provider Configuration
PROVIDER=openai
API_BASE=https://api.openai.com/v1
API_KEY=your_api_key_here
MODEL=gpt-4o-mini
# For Bailian (Qwen) API
# PROVIDER=bailian
# API_BASE=https://dashscope.aliyuncs.com/compatible-mode/v1
# API_KEY=your_dashscope_api_key
# MODEL=qwen-turbo-latest
# For DeepSeek API
# PROVIDER=deepseek
# API_BASE=https://api.deepseek.com/v1
# API_KEY=your_deepseek_api_key
# MODEL=deepseek-chat
# For SiliconFlow API
# PROVIDER=siliconflow
# API_BASE=https://api.siliconflow.cn/v1
# API_KEY=your_siliconflow_api_key
# MODEL=Qwen/Qwen2.5-7B-Instruct
# For Ollama (local)
# PROVIDER=ollama
# OLLAMA_HOST=http://localhost:11434
# MODEL=qwen2.5:7b
# Logging Configuration
LOG_LEVEL=INFO
LOG_FILE_PATH=/path/to/logfile.log
# Prompt Template (optional customization)
PROMPT_TEMPLATE=Please generate a standardized commit message based on the user description: {user_input}. The changes include: {diff_content}. Respond only with the normalized commit message in English.
Supported AI Providers
CMAI supports multiple AI providers through its extensible architecture:
1. OpenAI-Compatible APIs
- OpenAI: Official OpenAI API
- Bailian (Qwen): Alibaba Cloud's Qwen models
- DeepSeek: DeepSeek's AI models
- SiliconFlow: SiliconFlow's AI services
- ChatGPT: OpenAI ChatGPT models
2. Local Models
- Ollama: Run models locally using Ollama
Important: Model Configuration
โ ๏ธ Important: You must specify a MODEL in your configuration. CMAI requires an explicit model name to function properly. Examples:
- OpenAI:
gpt-4o-mini,gpt-4o,gpt-3.5-turbo - Bailian:
qwen-turbo-latest,qwen-plus-latest,qwen-max-latest - DeepSeek:
deepseek-chat,deepseek-coder - SiliconFlow:
Qwen/Qwen2.5-7B-Instruct,deepseek-ai/DeepSeek-V2.5 - Ollama:
qwen2.5:7b,llama3.1:8b,codellama:7b
API Key Setup
Different providers require different API keys:
- OpenAI: Set
OPENAI_API_KEYorCMAI_API_KEYenvironment variable
export OPENAI_API_KEY=your_openai_api_key
- Bailian (Qwen): Set
DASHSCOPE_API_KEYenvironment variable
export DASHSCOPE_API_KEY=your_dashscope_api_key
- DeepSeek: Set
DEEPSEEK_API_KEYorCMAI_API_KEYenvironment variable
export DEEPSEEK_API_KEY=your_deepseek_api_key
- SiliconFlow: Set
SILICONFLOW_API_KEYorCMAI_API_KEYenvironment variable
export SILICONFLOW_API_KEY=your_siliconflow_api_key
-
Ollama: No API key required, but ensure Ollama is running locally
-
Configuration file: Add
API_KEY=your_api_key_hereto~/.config/cmai/settings.env -
Custom config file: Use the
--configoption to specify a different configuration file
๐ Usage Examples
Usage Examples
# Simple commit message normalization (uses configuration file settings)
cmai "updated readme file"
# Using a custom configuration file
cmai "fixed authentication bug" --config /path/to/custom/config.env
# Specifying a different repository
cmai "refactored user service" --repo /path/to/repo
Using Custom Configuration
# Use a specific configuration file
cmai "refactored auth system" --config /path/to/custom/config.env
Specifying Repository Path
# Analyze changes in a specific repository
cmai "fixed login bug" --repo /path/to/your/repo
Full Command Options
cmai [OPTIONS] MESSAGE
Arguments:
MESSAGE The informal commit message to be normalized [required]
Options:
-c, --config TEXT Path to configuration file
-r, --repo TEXT Git repository path
--help Show this message and exit
Note: Provider and model selection is configured through the configuration file or environment variables, not command-line arguments.
Configuration Examples for Different Providers
To use different AI providers, configure your ~/.config/cmai/settings.env file:
OpenAI
PROVIDER=openai
API_BASE=https://api.openai.com/v1
API_KEY=your_openai_api_key
MODEL=gpt-4o-mini
Bailian (Qwen)
PROVIDER=bailian
API_BASE=https://dashscope.aliyuncs.com/compatible-mode/v1
API_KEY=your_dashscope_api_key
MODEL=qwen-turbo-latest
DeepSeek
PROVIDER=deepseek
API_BASE=https://api.deepseek.com/v1
API_KEY=your_deepseek_api_key
MODEL=deepseek-chat
SiliconFlow
PROVIDER=siliconflow
API_BASE=https://api.siliconflow.cn/v1
API_KEY=your_siliconflow_api_key
MODEL=Qwen/Qwen2.5-7B-Instruct
Ollama (Local)
PROVIDER=ollama
OLLAMA_HOST=http://localhost:11434
MODEL=qwen2.5:7b
After configuring your preferred provider, simply use:
cmai "your commit message"
๐๏ธ Architecture
CMAI follows a modular architecture with the following components:
Core Components
cmai.main: Entry point and CLI interface using Clickcmai.core.normalizer: Core logic for commit message normalizationcmai.core.get_logger: Logging factory and configuration with stream supportcmai.config.settings: Configuration management using Pydantic
Provider System
cmai.providers.base: Abstract base class for AI providerscmai.providers.openai_provider: OpenAI-compatible API implementationcmai.providers.ollama_provider: Ollama local model implementationcmai.providers.provider_factory: Factory for creating and managing providerscmai.providers.bailian_provider: Legacy Bailian provider (deprecated)
Utilities
cmai.utils.git_staged_analyzer: Git repository analysis and diff extraction
Data Models
class AIResponse(BaseModel):
content: str # The normalized commit message
model: str # AI model used
provider: str # AI provider name
tokens_used: Optional[int] # Token consumption
Provider Factory System
CMAI uses a factory pattern for managing AI providers:
from cmai.providers.provider_factory import create_provider
# Create provider with default settings
provider = create_provider()
# Create specific provider with model
provider = create_provider("openai", model="gpt-4o-mini")
# Create Ollama provider
provider = create_provider("ollama", model="qwen2.5:7b")
๐ Extending CMAI
Adding New AI Providers
To add support for a new AI provider, create a class that inherits from BaseAIClient and register it with the provider factory:
from cmai.providers.base import BaseAIClient, AIResponse
from cmai.providers.provider_factory import register_custom_provider
class CustomProvider(BaseAIClient):
async def normalize_commit(self, prompt: str, **kwargs) -> AIResponse:
# Implement your provider logic here
# Must return AIResponse with content, model, provider, and tokens_used
pass
def validate_config(self) -> bool:
# Implement configuration validation
return True
# Register the provider
register_custom_provider("custom", CustomProvider)
Using the Provider Factory
The provider factory automatically manages different AI providers:
from cmai.providers.provider_factory import (
create_provider,
list_available_providers,
register_custom_provider
)
# List all available providers
providers = list_available_providers()
print(providers)
# Output: {'openai': 'OpenaiProvider', 'ollama': 'OllamaProvider', ...}
# Create provider instances
openai_provider = create_provider("openai", model="gpt-4o-mini")
ollama_provider = create_provider("ollama", model="qwen2.5:7b")
Custom Prompt Templates
You can customize the prompt template by modifying the PROMPT_TEMPLATE setting:
PROMPT_TEMPLATE=Generate a standardized commit message based on: {user_input}. Changes: {diff_content}. Use conventional commit format.
Available placeholders:
{user_input}: The user's informal commit message{diff_content}: Git diff information from staged changes
๐ ๏ธ Development
Setting Up Development Environment
- Clone the repository:
git clone https://github.com/yumuzhihan/cmai.git
cd cmai
- Create a virtual environment:
uv venv
# Activate virtual environment
# On Unix/macOS:
source .venv/bin/activate
# On Windows:
# .venv\Scripts\activate
- Install development dependencies:
# Install all dependencies including dev dependencies
uv sync --dev
# Or if you prefer to install only production dependencies:
uv sync
Project Structure
cmai/
โโโ cmai/
โ โโโ __init__.py
โ โโโ main.py # CLI entry point
โ โโโ config/
โ โ โโโ __init__.py
โ โ โโโ settings.py # Configuration management
โ โโโ core/
โ โ โโโ __init__.py
โ โ โโโ get_logger.py # Logging utilities
โ โ โโโ normalizer.py # Core normalization logic
โ โโโ providers/
โ โ โโโ __init__.py
โ โ โโโ base.py # Abstract provider interface
โ โ โโโ openai_provider.py # OpenAI-compatible implementation
โ โ โโโ ollama_provider.py # Ollama local model implementation
โ โ โโโ provider_factory.py # Provider factory system
โ โ โโโ bailian_provider.py # Legacy Bailian provider
โ โโโ utils/
โ โโโ __init__.py
โ โโโ git_staged_analyzer.py # Git utilities
โโโ tests/ # Test suite
โโโ docs/ # Documentation
โโโ scripts/ # Utility scripts
โโโ pyproject.toml # Project configuration
โโโ uv.lock # UV lock file
โโโ LICENSE # AGPL-3.0 License
โโโ README.md # This file
Running Tests
python -m pytest tests/
๐ Requirements
- Python 3.10 or higher
- Git (for repository analysis)
- Internet connection (for remote AI provider APIs)
- Ollama installation (for local AI models)
Dependencies
click>=8.2.1- Command-line interfaceopenai>=1.91.0- OpenAI-compatible API clientollama>=0.5.1- Ollama Python client for local modelspydantic>=2.11.7- Data validation and settingspydantic-settings>=2.10.0- Settings management
๐ค Contributing
We welcome contributions! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Add tests for new functionality
- Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Contribution Guidelines
- Follow PEP 8 coding standards
- Add type hints to all functions
- Write comprehensive tests
- Update documentation for new features
- Ensure backward compatibility
- When adding new providers, register them in the provider factory
Adding New Providers
When contributing new AI providers:
- Create a new file in
cmai/providers/(e.g.,custom_provider.py) - Implement the
BaseAIClientinterface - Register the provider in
provider_factory.py - Add configuration examples to the README
- Include tests for the new provider
๐ License
This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0). See the LICENSE file for details.
๐ Acknowledgments
- Built with Click for the CLI interface
- Uses Pydantic for configuration and data validation
- Supports multiple AI providers including OpenAI, Bailian, DeepSeek, SiliconFlow, and Ollama
- Inspired by conventional commit standards
- Uses UV for fast Python package management
๐ Support
If you encounter any issues or have questions:
- Check the Issues page
- Create a new issue with detailed information about your setup (provider, model, configuration)
- Review the documentation and configuration guide
- For provider-specific issues, include your provider and model information
Common Issues
- "Model must be specified": Ensure you have set the
MODELin your configuration file or passed it as a command-line argument - API key errors: Verify that your API key is correctly set for your chosen provider
- Connection errors: Check your internet connection and API endpoint URLs
- Ollama connection issues: Ensure Ollama is running locally and accessible at the configured host
Note: This tool requires access to AI language models. Please ensure you have appropriate API keys and understand the associated costs before using CMAI in production environments. For local usage, consider using Ollama with open-source models.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cmai-0.1.2.tar.gz.
File metadata
- Download URL: cmai-0.1.2.tar.gz
- Upload date:
- Size: 34.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
51d55aeda94e1a41efa454a8c703229fded84c2840d88c9f6b69a1854c0d0b70
|
|
| MD5 |
1ea01d91691c087be3d0b0494b9f989a
|
|
| BLAKE2b-256 |
4f0389a3188ab6142cf4f518488c564dc4930863451a95cc8ecac7a4e0c6d67e
|
File details
Details for the file cmai-0.1.2-py3-none-any.whl.
File metadata
- Download URL: cmai-0.1.2-py3-none-any.whl
- Upload date:
- Size: 32.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b14bac0ed0c5b0fa86aea7fc512dd6ec450f985d74107932f12f80f0d9cdf606
|
|
| MD5 |
3b2ab9a4f63f0f2620c7341a0d12554e
|
|
| BLAKE2b-256 |
0c4768598229de246742e53858005520d4a962ecef826e55c37720b36f630f03
|