Blog #ai #openai #portfolio

Generic Code Generator

PyCode is a sequential LangChain CLI utility that generates implementation code and matching unit tests in any target programming language.

Published
Updated
Reading
1 min

Overview

PyCode is a small command-line utility I built to test multi-step prompt chaining in LangChain. Given a short task description and a target programming language, it runs a two-stage SequentialChain. The first LLM call writes the implementation, and the second call takes that generated code as input to write a matching unit test suite.

Get started

  1. Clone the repository locally.
  2. Install dependencies with pip install -r requirements.txt.
  3. Configure credentials by creating a .env file with your OPENAI_API_KEY.
  4. Run the generator by passing --task and --language flags to main.py.

Usage

  • Command-Line Arguments:
    • --task: What the function or module should do (for example, "return a list of numbers").
    • --language: Target programming language (for example, "python" or "java").
  • Example:
python main.py --task "calculate the factorial of a number" --language "java"

Code Generation Process

  1. Load API credentials from .env via python-dotenv.
  2. Initialize the OpenAI LLM wrapper from langchain.llms.
  3. Define two PromptTemplate instances (one for implementation code and a second that consumes {code} to produce tests).
  4. Wrap each prompt in its own LLMChain (code_chain and test_chain).
  5. Pipe the first chain’s output into the second using SequentialChain.
  6. Execute the combined chain with the CLI arguments and print both the implementation and the generated test cases to stdout.

Contribute

Issues and pull requests are welcome on GitHub.

Additional Information

Related work

Production ML context

See how this topic connects to production ML systems, infrastructure, and inference.