...by making them LONGER!
A Rust procedural macro that generates method implementations using OpenAI's API.
This macro allows you to define trait implementations where the method bodies are generated automatically by OpenAI's language models. Simply provide the method signatures and optional hints, and the macro will call the OpenAI API to generate appropriate implementations.
Add this to your Cargo.toml:
[dependencies]
openai-macro = { path = "crates/openai-macro" }use openai_macro::openai_impl;
trait Calculator {
fn add(&self, a: i32, b: i32) -> i32;
fn multiply(&self, a: i32, b: i32) -> i32;
}
struct MyCalculator;
#[openai_impl(
model = "gpt-4o-mini",
prompt = "Implement basic arithmetic operations"
)]
impl Calculator for MyCalculator {
fn add(&self, a: i32, b: i32) -> i32 {
// Implementation generated by OpenAI
}
fn multiply(&self, a: i32, b: i32) -> i32 {
// Implementation generated by OpenAI
}
}OPENAI_API_KEY(required): Your OpenAI API keyOPENAI_BASE_URL(optional): Custom API endpoint (defaults to OpenAI's API)OPENAI_OFFLINE=1: Use only cached implementations, no network requests
model(optional): OpenAI model to use (default: "gpt-4o-mini")prompt(optional): Additional context or instructions for the AI
no-network: Compile-time flag to disable network requests and use only cached implementations
Generated implementations are automatically cached based on a hash of:
- Trait signatures
- Self type
- Model name
- Prompt hint
The cache is stored in your build output directory and persists across builds.
This is a Cargo workspace containing:
crates/openai-macro/: The main procedural macro crateexamples/abuse/: Example usage demonstrating the macro
cargo build# Set your API key
export OPENAI_API_KEY=your_api_key_here
# Build the example
cargo build -p abuse
# Or test in offline mode (uses cache only)
OPENAI_OFFLINE=1 cargo build -p abuseFor CI environments, use the no-network feature or OPENAI_OFFLINE=1 to avoid network calls:
cargo build --features no-networkLicensed under either of
- Apache License, Version 2.0
- MIT License
at your option.