Skip to main content

Automate documentation generation using the LLM of your choice!

Project description

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.

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

llmdocgen-0.1.2.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

llmdocgen-0.1.2-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file llmdocgen-0.1.2.tar.gz.

File metadata

  • Download URL: llmdocgen-0.1.2.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for llmdocgen-0.1.2.tar.gz
Algorithm Hash digest
SHA256 300bac8bae16aa6e39311404cc7c816f2009998d0e7671aaa34fc76c5e3067ef
MD5 5364d489cb34b6623246d4f81da4b98a
BLAKE2b-256 074f11f5b8dfd4b8bf3f5693f02b9b3fb4da0dd467465a77491fef39a7dee063

See more details on using hashes here.

File details

Details for the file llmdocgen-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: llmdocgen-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for llmdocgen-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4f8423319ac6a486d1bbbace936f34119e385bbc8d817de8bb0ef877d88006d0
MD5 cada816d917675489345f285aa35a9f9
BLAKE2b-256 714edc5bb7704408d36f320a160731f4fb84a7954f9e4bb935b52c054365d153

See more details on using hashes here.

Supported by

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