Automatic generation and improvement of docstrings using LLMs
Python
2
32 commits
updated Feb 3, 2025
This project develops an end-to-end system for the automatic generation and improvement of docstrings in Python files using Large Language Models (LLMs). The aim is to add missing docstrings to existing scripts as well as to optimize the content of existing docstrings.
The repository is organized as follows:
Data:
Contains the raw data (Python scripts), filtered scripts, and training and test data for fine-tuning.
Code:
Contains the following scripts and notebooks for dataset processing, analysis, and fine-tuning:
create_finetune_dataset.py:docstring_analysis.py:docstring_generator_app.py:finetune_LLM_docstring.ipynb:Results:
Contains test and evaluation files, including:
inference_finetuned_LLM_docstring.ipynb:Icon:
Contains the application icon that is displayed in the dock when the program is executed.
Models:
Contains the final checkpoint of the fine-tuned model, which is used in the application for docstring generation.
In software development, docstrings play a decisive role in the readability and maintainability of code. However, manual creation is often neglected. With the increasing availability of LLMs, this process can be automated. This project aims to automatically generate missing docstrings in Python functions and to improve the quality of existing docstrings.
Source Code Corpus (~25,000 GitHub repositories, Python scripts):
filter_sample_scripts.py script (minimally adapted) was used with adjusted conditions to filter the
original ~25,000 scripts down to about ~5,000 scripts. The adapted conditions:
# Define conditions for filtering
conditions = {
"lines": lambda x: 20 <= x <= 2000, # between 20 and 2000 lines
"functions": lambda x: x >= 2, # at least two functions
"functions_with_docstring": lambda x: x >= 2, # at least two functions with a docstring
"average_line_length": lambda x: x <= 120, # max. 120 characters per line
}
A typical entry in the final dataset looks like this:
[Function] def function(a, b):\n ... \nreturn list [Docstring] Do X and return a list. [EOS]
In this project, the model CodeLlama from Hugging Face is fine-tuned using LoRA (Low-Rank Adaptation) on an extracted and filtered dataset.
Dataset Preparation:
Model Setup:
meta-llama/CodeLlama-7b-Python-hf) is loaded from Hugging Face.Fine-Tuning Configuration:
wandb and mlflow are used for experiment tracking and monitoring.Evaluation:
Inference:
The script requires Python 3.10 and uses the libraries pandas (BSD 3-Clause
License), matplotlib (Matplotlib License), and the standard Python libraries json, os.
Clone the repository and install the required dependencies as follows:
git clone https://github.com/mbernahr/Docstring_generation.git
cd Docstring_generation/Code
python3 -m venv venv
source venv/bin/activate
python -m venv venv
venv\Scripts\activate
pip install -r ..\requirements.txt
Before the first use, create an account at https://huggingface.co and create a token. Next, login with the token from huggingface:
huggingface-cli login
Run the script as follows:
python docstring_generator_app.py
The updated file is saved with the suffix _updated.py.
Automatic generation and improvement of docstrings using LLMs
Python
2
32 commits
updated Feb 3, 2025
This project develops an end-to-end system for the automatic generation and improvement of docstrings in Python files using Large Language Models (LLMs). The aim is to add missing docstrings to existing scripts as well as to optimize the content of existing docstrings.
The repository is organized as follows:
Data:
Contains the raw data (Python scripts), filtered scripts, and training and test data for fine-tuning.
Code:
Contains the following scripts and notebooks for dataset processing, analysis, and fine-tuning:
create_finetune_dataset.py:docstring_analysis.py:docstring_generator_app.py:finetune_LLM_docstring.ipynb:Results:
Contains test and evaluation files, including:
inference_finetuned_LLM_docstring.ipynb:Icon:
Contains the application icon that is displayed in the dock when the program is executed.
Models:
Contains the final checkpoint of the fine-tuned model, which is used in the application for docstring generation.
In software development, docstrings play a decisive role in the readability and maintainability of code. However, manual creation is often neglected. With the increasing availability of LLMs, this process can be automated. This project aims to automatically generate missing docstrings in Python functions and to improve the quality of existing docstrings.
Source Code Corpus (~25,000 GitHub repositories, Python scripts):
filter_sample_scripts.py script (minimally adapted) was used with adjusted conditions to filter the
original ~25,000 scripts down to about ~5,000 scripts. The adapted conditions:
# Define conditions for filtering
conditions = {
"lines": lambda x: 20 <= x <= 2000, # between 20 and 2000 lines
"functions": lambda x: x >= 2, # at least two functions
"functions_with_docstring": lambda x: x >= 2, # at least two functions with a docstring
"average_line_length": lambda x: x <= 120, # max. 120 characters per line
}
A typical entry in the final dataset looks like this:
[Function] def function(a, b):\n ... \nreturn list [Docstring] Do X and return a list. [EOS]
In this project, the model CodeLlama from Hugging Face is fine-tuned using LoRA (Low-Rank Adaptation) on an extracted and filtered dataset.
Dataset Preparation:
Model Setup:
meta-llama/CodeLlama-7b-Python-hf) is loaded from Hugging Face.Fine-Tuning Configuration:
wandb and mlflow are used for experiment tracking and monitoring.Evaluation:
Inference:
The script requires Python 3.10 and uses the libraries pandas (BSD 3-Clause
License), matplotlib (Matplotlib License), and the standard Python libraries json, os.
Clone the repository and install the required dependencies as follows:
git clone https://github.com/mbernahr/Docstring_generation.git
cd Docstring_generation/Code
python3 -m venv venv
source venv/bin/activate
python -m venv venv
venv\Scripts\activate
pip install -r ..\requirements.txt
Before the first use, create an account at https://huggingface.co and create a token. Next, login with the token from huggingface:
huggingface-cli login
Run the script as follows:
python docstring_generator_app.py
The updated file is saved with the suffix _updated.py.