Repolode
retrage

retrage/gpt-macro

A Rust procedural macro that uses the ChatGPT API to generate function bodies and test cases at compile time.

Stars
667
Forks
8
Open issues
1
Last commit
2 months ago

What is it?

gpt-macro is a Rust procedural macro crate that sends a natural-language prompt and target code to the ChatGPT API during compilation, then substitutes the model's response into the source before the compiler continues. A function stub with an empty body can be left incomplete and filled in automatically, with the surrounding tests used as the specification.

The crate provides two macros. `auto_impl!{}` takes a prompt string followed by code, such as a function signature and its tests, and replaces the target with the generated implementation. `#[auto_test(...)]` attaches to a function and generates test cases for the named scenarios. Usage requires an OpenAI API key set in the `OPENAI_API_KEY` environment variable.

Because generation happens at build time, the output is compiled like ordinary handwritten code. This makes the project mainly an experiment in LLM-assisted code generation rather than a production build dependency.

Key features

  • 01Generates function bodies from a natural-language prompt with `auto_impl!{}`.
  • 02Generates test cases for a function with `#[auto_test(...)]`.
  • 03Runs the ChatGPT request during compilation, so generated code is compiled like handwritten code.
  • 04Reads the OpenAI API key from the `OPENAI_API_KEY` environment variable.

Use cases

  • Filling in simple, well-specified functions from their tests during prototyping.
  • Experimenting with LLM-assisted code generation inside a Rust build.
  • Generating extra test cases for an existing function.

Quick start

  1. 1

    Add the gpt-macro crate to your Rust project

  2. 2

    Get a ChatGPT API key and set it as the OPENAI_API_KEY environment variable

  3. 3

    Use auto_impl! or #[auto_test] in your code and build

Things to know

  • Requires a paid ChatGPT API key set in OPENAI_API_KEY.
  • Generated code comes from an external model and must be reviewed before use.

In collections

Topics

FAQ

What is gpt-macro?
A Rust procedural macro that uses the ChatGPT API to generate function bodies and test cases at compile time. gpt-macro is a Rust procedural macro crate that sends a natural-language prompt and target code to the ChatGPT API during compilation, then substitutes the model's response into the source before the compiler continues. A function stub with an empty body can be left incomplete and filled in automatically, with the surrounding tests used as the specification.
Is gpt-macro free to use? What license does it have?
gpt-macro is open source under the MIT license. Check the license terms before using it in commercial products.
Is gpt-macro still maintained?
Updated within 6 months. The last commit to gpt-macro was on Sep 14, 2026.
What can I use gpt-macro for?
Filling in simple, well-specified functions from their tests during prototyping. Experimenting with LLM-assisted code generation inside a Rust build. Generating extra test cases for an existing function.
How do I install gpt-macro?
Add the gpt-macro crate to your Rust project Get a ChatGPT API key and set it as the OPENAI_API_KEY environment variable Use auto_impl! or #[auto_test] in your code and build
Where can I get gpt-macro?
The source code and installation instructions are on GitHub: https://github.com/retrage/gpt-macro

Summary generated by AI from the project's README. GitHub stats refresh daily.

Related projects