Skip to main content

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

ai_code_commenter_vraj0703-0.0.1.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ai_code_commenter_vraj0703-0.0.1-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file ai_code_commenter_vraj0703-0.0.1.tar.gz.

File metadata

File hashes

Hashes for ai_code_commenter_vraj0703-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d3edf016a246e3fa41ea9b11dc55e7fae17f0bed04fb14950cc64460aaf2c0c3
MD5 12ee5357078fe8352edb76151b1bd64a
BLAKE2b-256 d8e3b367baf89bd6355ebea3069de2114960f6e66f3546b8205d9982fb552148

See more details on using hashes here.

File details

Details for the file ai_code_commenter_vraj0703-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for ai_code_commenter_vraj0703-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 648018428f87163ba497146b1a52b6b760015c156623df0e43b87adea698d051
MD5 a439eccde01773396488172b9e84c0af
BLAKE2b-256 893b1621dc7f60325d52349e69acc73ffe8ad502b2f44ef386ac0a28338069c6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page