Skip to main content

LLM Code Documentation Generator

llmdocgen is a library for automatically documenting code using large language models (LLMs). It takes code files as input, sends them to an LLM to generate documentation comments, and returns the code with the generated documentation added.

LLMDocGen will not just document python files but also other languages like JavaScript, Java, C++, etc. It can be used as a library or run as a command line tool.

Features

  • Automatically documents code using LLMs
  • Supports a wide variety of programming languages out of the box
  • Easily configurable to add support for additional file types
  • Can be used as a library or run as a command line tool
  • Integrates with LiteLLM to work with OpenAI, Anthropic, and other LLM providers

Installation

Install from PyPI using pip:

pip install llmdocgen

Setup

You can use the default environment file path (~/.llmdocgen) or specify a custom environment file path by setting the LLMDOCGEN_ENV_PATH environment variable.

llmdocgen requires a few environment variables to be set in order to authenticate with the LLM provider. The easiest way to set these variables is to create your environment file with the following variables:

# Required for LiteLLM

# For Anthropic
LITELLM_MODEL=claude-3-opus-20240229  # or another supported model
ANTHROPIC_API_KEY=your_api_key

# For OpenAI
OPENAI_API_KEY=your_api_key
LITELLM_MODEL=gpt-4-turbo  # or another supported model

# llmdocgen settings
PROMPT_FILE=default_prompt.txt
ESCAPE_CHARACTERS=%"%"%"

# See settings.py for additional optional variables

See the LiteLLM docs for more details on authenticating.

Usage

There are two main ways to use llmdocgen:

As a library

from llmdocgen import enrich
import pathlib

file_path = pathlib.Path("path/to/file.py")
completion = enrich.parsed_file_completion(file_path)
print(completion)

To run and save completion to the original file:

from llmdocgen import executor
import pathlib

file_path = pathlib.Path("path/to/file.py")
executor.inline_document_file(file_path, save=True)

To run and save to a different file:

from llmdocgen import executor
import pathlib

original_file_path = pathlib.Path("path/to/file.py")
new_file_path = pathlib.Path("path/to/new_file.py")
executor.inline_document_file(original_file_path, save=True, new_file_path=new_file_path)

This will print out the code from file_path with documentation comments added.

As a command line tool

You can also run llmdocgen as a module:

python -m llmdocgen.main /path/to/directory --recursive

When running as a module, you can also pass in extra prompt context and change the model used:

python -m llmdocgen.main /path/to/directory -r --model claude-3-opus-20240229 -e "Make sure you use C# conventions!"

This will process all supported files in /path/to/directory. Pass --recursive to also process subdirectories.

Configuration

Supported File Types

By default, llmdocgen supports a wide variety of file types (see settings.DEFAULT_EXTENSIONS). You can add additional file types by setting the LLMDOCGEN_EXTRA_EXTENSIONS environment variable to a comma-separated list of extensions. To override the default list entirely, set the LLMDOCGEN_EXTENSIONS environment variable.

Prompts

llmdocgen uses a prompt defined in default_prompt.txt to instruct the LLM on how to generate the documentation. You can modify this prompt or provide a different file by setting the PROMPT_FILE environment variable. The prompt should include %"%"% where the generated code should be inserted. llmdocgen will use this to extract the generated code from the LLM's response.

Authenticating with LLMs

As mentioned above, llmdocgen uses LiteLLM to integrate with LLM providers. All authentication happens through LiteLLM by setting the appropriate environment variables. See settings.LITELLM for the full list of supported settings. These map directly to arguments to LiteLLM's litellm.completion() function.

File Types

llmdocgen supports a wide variety of file types out of the box. The supported file types are defined in the settings.DEFAULT_EXTENSIONS dictionary. Each key is a file extension, and each value is a list of languages that llmdocgen will use to generate documentation for that file type.

If you would like to include additional file types, you can set the LLMDOCGEN_EXTRA_EXTENSIONS environment variable to a comma-separated list of file extensions.

If you want to override the default list entirely, you can set the LLMDOCGEN_EXTENSIONS environment variable to a comma-separated list of file extensions. This would allow you to only document specific file types.

All of these can be set in your environment file (see the Setup section above) so you do not have to set them every time you run llmdocgen.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on the GitHub repository.

License

llmdocgen is released under the MIT License. See LICENSE for more information.

Metadata

Release files for llmdocgen 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llmdocgen 0.1.2
File Size Uploaded
llmdocgen-0.1.2.tar.gz 9.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for llmdocgen 0.1.2
File Interpreter ABI Platform
llmdocgen-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 18.4 kB

Release files / llmdocgen-0.1.2.tar.gz

Download URL llmdocgen-0.1.2.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
300bac8bae16aa6e39311404cc7c816f2009998d0e7671aaa34fc76c5e3067ef
BLAKE2b-256 checksum
How to use checksums
074f11f5b8dfd4b8bf3f5693f02b9b3fb4da0dd467465a77491fef39a7dee063
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.19

Release files / llmdocgen-0.1.2-py3-none-any.whl

Download URL llmdocgen-0.1.2-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4f8423319ac6a486d1bbbace936f34119e385bbc8d817de8bb0ef877d88006d0
BLAKE2b-256 checksum
How to use checksums
714edc5bb7704408d36f320a160731f4fb84a7954f9e4bb935b52c054365d153
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.19

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page