Skip to main content

An OpenAI LLM based CLI coding assistant.

Project description

llm-code

PyPi Coverage Status


An OpenAI LLM based CLI coding assistant.

llm-code is inspired by Simon Wilson's llm package. It takes a similar approach of developing a simple tool to create an LLM based assistant that helps write code.

Installation

pipx install llm-code

Configuration

llm-code requires an OpenAI API key. You can get one from OpenAI.

You can set the key in a few different ways, depending on your preference:

  1. Set the OPENAI_API_KEY environment variable.
export OPENAI_API_KEY = sk-...
  1. Use an env file in ~/.llm_code/env
mkdir -p ~/.llm_code
echo "OPENAI_API_KEY=sk-..." > ~/.llm_code/env

Usage

llm-code is meant to be simple to use. The default prompts should be good enough. There are two broad modes:

  1. Generage some code from scratch.
llm-code write a function that takes a list of numbers and returns the sum of the numbers in python. Add type hints.
  1. Give in some input files and ask for changes.
llm-code -i my_file.py add docstrings to all python functions.
llm-code --help
Usage: llm-code [OPTIONS] [INSTRUCTIONS]...

  Ask for code completion from OpenAI's chat models.

Options:
  -i, --inputs TEXT  Glob of input files.
  --help             Show this message and exit.

Examples

Simple hello world.

llm-code write hello world in rust
fn main() {
    println!("Hello, world!");
}

Sum of two numbers with type hints.

llm-code write a function that takes a list of numbers and returns the sum of the numbers in python. Add type hints.
from typing import List

def sum_numbers(numbers: List[int]) -> int:
    return sum(numbers)

Lets assume that we stuck the output of the previous call in out.py. We can now say:

llm-code -i out.py add appropriate docstrings
from typing import List

def sum_numbers(numbers: List[int]) -> int:
    """Return the sum of the given list of numbers."""
    return sum(numbers)

Or we could write some unit tests.

llm-code -i out.py write a complete unit test file using pytest.
import pytest

from typing import List
from my_module import sum_numbers


def test_sum_numbers():
    assert sum_numbers([1, 2, 3]) == 6
    assert sum_numbers([-1, 0, 1]) == 0
    assert sum_numbers([]) == 0

TODO

  • Add a simple cache to replay the same query.
  • Add an --exec option to execute the generated code.
  • Add a --stats option to output token counts.
  • Add logging to a local sqllite db.

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

llm_code-0.3.tar.gz (5.8 kB view hashes)

Uploaded Source

Built Distribution

llm_code-0.3-py3-none-any.whl (7.5 kB view hashes)

Uploaded Python 3

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