An AI-powered tool to automatically comment code using local Ollama models.
Project description
AI Code Commenter using Ollama
This Python script automatically generates and prepends comments to your source code files using a locally running Ollama model. It can process a single file or recursively walk through an entire directory, making it easy to document a whole codebase.
The comment style is fully customizable through a simple text file.
Features
🤖 AI-Powered Commenting: Leverages local LLMs via Ollama to understand code and generate meaningful comments.
📂 Batch Processing: Can run on an entire folder (your codebase) to comment on all supported files recursively.
📄 Single File Mode: Can also target a single file for quick commenting.
🎨 Customizable Format: You define the exact format of the comment block using a format.txt template.
🔌 Simple & Local: No need for external APIs or paid services. It all runs on your machine.
Prerequisites
Before you begin, ensure you have the following installed and running:
Python 3.6+: The script is written in Python.
Ollama: You must have Ollama installed and the service running on your machine.
An Ollama Model: You need at least one model pulled. For code-related tasks, we recommend llama3 for quality or phi3:mini for speed.
Bash
Pull a high-quality model (slower, needs ~8GB RAM)
ollama pull llama3
Or pull a fast, smaller model (needs ~4GB RAM)
ollama pull phi3:mini
Setup
Download Files: Place the commenter.py script and the format.txt file in the same directory.
Configure the Model (Optional): Open commenter.py and change the OLLAMA_MODEL variable if you want to use a different model than the default (llama3).
Python
In commenter.py
OLLAMA_MODEL = "phi3:mini" # Example: Changed to a faster model Customize the Comment Format: Open format.txt and edit it to define your desired comment structure. The script will instruct the AI to follow this format precisely. The default format is:
""" Author: AI Assistant Date: <CURRENT_DATE> Description: <A high-level summary of the file's purpose in one or two sentences.>
Functions:
- <function_name_1>:
- <function_name_2>: """
How to Run on Your Codebase
You can run the script from your terminal.
To Comment on an Entire Codebase (Folder): Provide the path to the root folder of your project. The script will automatically find and process all supported code files (e.g., .py, .js, .dart, etc.) inside it and all its subdirectories.
Bash
python commenter.py /path/to/your/project_folder/ Example:
Bash
python commenter.py C:\Users\YourUser\Documents\MyWebApp
To Comment on a Single File:
Provide the direct path to the specific file you want to comment.
Bash
python commenter.py /path/to/your/file.py Example:
Bash
python commenter.py C:\Users\YourUser\Documents\MyWebApp\src\utils.py
Troubleshooting
Timeout Errors If you see an error like Error: The 'ollama' command timed out..., it means the AI model is taking too long to respond. This usually happens the first time you run it.
Solution 1 (Warm-up): Run a simple command in your terminal before using the script to load the model into memory: ollama run llama3 "Hello!".
Solution 2 (Increase Timeout): Open commenter.py and increase the timeout=300 value to a higher number (e.g., 600 for 10 minutes).
Solution 3 (Use a Smaller Model): A smaller model like phi3:mini is much faster. See the setup section to change the model.
Project details
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 ai_code_commenter_vraj0703-0.0.2.tar.gz.
File metadata
- Download URL: ai_code_commenter_vraj0703-0.0.2.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8df115db63ad84da7033fd200fd2eddb86aa272b99c7dede920ae12b42a17a15
|
|
| MD5 |
048f468adda416750b448bf4665c8160
|
|
| BLAKE2b-256 |
e59b64241e539334303ffc60198352b10e62aa474079dea15acb7710b75dc06e
|
File details
Details for the file ai_code_commenter_vraj0703-0.0.2-py3-none-any.whl.
File metadata
- Download URL: ai_code_commenter_vraj0703-0.0.2-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b452fd65e00d1ee964b1eaeff607bee8614277d8315cdd5d9d209a870b21f61
|
|
| MD5 |
52a09d073ba8c29a6a1e38ad00bb6209
|
|
| BLAKE2b-256 |
f85c175a2faae53db0fe5a3883d2c79fe302bfa54c8d440e77470987374a0ee1
|