Code Monkey home page Code Monkey logo

gpt3_macro's Introduction

gpt3_macro

GitHub Workflow Status Crates.io Lines of code Crates.io Crates.io

Rust macro that uses GPT3 codex to generate code at compiletime.

Just describe what you want the function to do and (optionally) define a function header. The macro will generate the sourcecode for you at compiletime.

It can also generate tests for you. (See example 3)

Example 1

create_function!("checks if number is prime" fn is_prime(num: u64) -> bool);

will (usually) expand to something like:

// A rust function that checks if number is prime
fn is_prime(num: u64) -> bool {
    if num == 2 {
        return true;
    }
    if num % 2 == 0 {
        return false;
    }
    let mut i = 3;
    while i * i <= num {
        if num % i == 0 {
            return false;
        }
        i += 2;
    }
    true
}

Example 2

create_function!("prints n elements of the fibonnacci sequence to stdout" fn fib(n: u64));

sometimes expands to:

// prints n elements of the fibonnacci sequence to stdout
fn fib (n : u64) {
    let mut a = 0;
    let mut b = 1;
    let mut c = 0;
    for _ in 0..n {
        c = a + b;
        a = b;
        b = c;
        println!("{}", c);
    }
}

Example 3: Code and test generation

create_function_and_tests!("fizzbuzz", fn fizzbuzz(n: u64) -> String)

will often expand to:

// A rust function that fizzbuzz
fn fizzbuzz (n : u64) -> String {
    if n % 15 == 0 {
        "FizzBuzz".to_string()
    } else if n % 3 == 0 {
        "Fizz".to_string()
    } else if n % 5 == 0 {
        "Buzz".to_string()
    } else {
        n.to_string()
    }
}
// 5 tests for the function
#[test]
fn test_fizzbuzz_1() {
    assert_eq!(fizzbuzz(1), "1");
}
#[test]
fn test_fizzbuzz_2() {
    assert_eq!(fizzbuzz(2), "2");
}
#[test]
fn test_fizzbuzz_3() {
    assert_eq!(fizzbuzz(3), "Fizz");
}
#[test]
fn test_fizzbuzz_4() {
    assert_eq!(fizzbuzz(4), "4");
}
#[test]
fn test_fizzbuzz_5() {
    assert_eq!(fizzbuzz(5), "Buzz");
}

Pros and Cons

Pros Cons
Spend less time coding simple utility functions and save your brainpower for the big problems Compilation takes way longer
Create more readable sourcecode — the documentation IS the sourcode. You need to be part of the GPT3 Codex private beta
A little nondeterminism during compilation is fun! GPT3 Codex will not always be free :(

Installation

Generate an OpenAI API key at the OpenAI Account Page and set the $OPENAI_KEY environment variable

Then execute

cargo add gpt3_macro

or manually add

gpt3_macro = "0.3.1"

to your Cargo.toml

gpt3_macro's People

Contributors

vongaisberg avatar sagudev avatar

Stargazers

Amr Elsayyad avatar Tyler Critchlow avatar  avatar AYOUB EL MHAMDI avatar Takafumi Yano avatar Onur 001 avatar  avatar 椰格 avatar surgit avatar Itzik avatar chris west avatar Arijit Basu avatar Sal Sal avatar Orhun Parmaksız avatar chaoticpogo avatar Marcus Otterström avatar Sematre avatar Jeff Carpenter avatar axyie avatar Tom Niget avatar Luis Galán avatar  avatar Tamme Schichler avatar toks avatar kaioh33 avatar Miraculous Owonubi avatar Evgeny avatar orange soeur avatar Jonathan Chan Kwan Yin avatar  avatar Tyrving avatar Hung-I Wang avatar Ryota Sakai avatar LXY avatar ForkΨKillet avatar  avatar zbv avatar HaoLin avatar Peihao Yang avatar  avatar Brad Pillow avatar Quentin Chateau avatar Liam avatar James Clarke avatar Orvar Segerström avatar Hector Orellana avatar Spike O'Carroll avatar Thomas Pearson avatar Kit avatar Jules Bertholet avatar Austin avatar  avatar Meadowsys avatar  avatar  avatar Kento Nishi avatar vaaaaanquish avatar yvt avatar Sergey Melnychuk avatar Ronak Badhe avatar Daniel Prilik avatar Pema Malling avatar  avatar Bilal Khan avatar  avatar Angelo Kontaxis avatar Zack Radisic avatar Marc Espin avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

sagudev isgasho

gpt3_macro's Issues

Cache generated code to reduce API usage

Currently, on incremental builds, an Api request would be made. Having some sort of local cache would help prevent running into any API limits usage for the stated case. Something like cacache alongside the description (and maybe an optional source path) as the cache key could serve some good :)

And then maybe exposing an optional feature flag to invalidate/not utilize the cache during build wouldn't be bad also.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.