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
- Author
- Sudhanva Narayana
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
- Clone the repository locally.
- Install dependencies with
pip install -r requirements.txt. - Configure credentials by creating a
.envfile with yourOPENAI_API_KEY. - Run the generator by passing
--taskand--languageflags tomain.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
- Load API credentials from
.envviapython-dotenv. - Initialize the
OpenAILLM wrapper fromlangchain.llms. - Define two
PromptTemplateinstances (one for implementation code and a second that consumes{code}to produce tests). - Wrap each prompt in its own
LLMChain(code_chainandtest_chain). - Pipe the first chain’s output into the second using
SequentialChain. - 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
- GitHub Repository: PyCode on GitHub
- License: MIT License.
Related work
Production ML context
See how this topic connects to production ML systems, infrastructure, and inference.