Git Product home page Git Product logo

gpt-macro's Introduction

gpt-macro

ChatGPT powered Rust proc macro that generates code at compile-time.

Implemented Macros

  • auto_impl!{}
  • #[auto_test(...)]

Usage

Get ChatGPT API key and set it to your environment variable OPENAI_API_KEY before run.

auto_impl!{}

Syntax:

auto_impl! {
    $STR_LIT
    $TOKEN_STREAM
}

where $STR_LIT is a prompt string literal, and $TOKEN_STREAM is target code.

Example:

use gpt_macro::auto_impl;

auto_impl! {
    "Return fizz if the number is divisible by 3, buzz if the number is divisible by 5, and fizzbuzz if the number is divisible by both 3 and 5."
    fn fizzbuzz(n: u32) -> String {
    }

    #[test]
    fn test_fizzbuzz() {
        assert_eq!(fizzbuzz(3), "fizz");
        assert_eq!(fizzbuzz(5), "buzz");
        assert_eq!(fizzbuzz(15), "fizzbuzz");
        assert_eq!(fizzbuzz(1), "1");
    }
}

As you can see, the fizzbuzz() implementation is incomplete, so the build fails without auto_impl!{}. The macro parses given prompt and target code, and asks ChatGPT to fill the code when expanding the macro. It replaces the target with code extracted from ChatGPT response. Then Rust compiler continues compiling the code.

Response Example:

fn fizzbuzz(n: u32) -> String {
    if n % 3 == 0 && n % 5 == 0 {
        return String::from("fizzbuzz");
    } else if n % 3 == 0 {
        return String::from("fizz");
    } else if n % 5 == 0 {
        return String::from("buzz");
    } else {
        return n.to_string();
    }
}

#[test]
fn test_fizzbuzz() {
    assert_eq!(fizzbuzz(3), "fizz");
    assert_eq!(fizzbuzz(5), "buzz");
    assert_eq!(fizzbuzz(15), "fizzbuzz");
    assert_eq!(fizzbuzz(1), "1");
}

#[auto_test]

See this example:

use gpt_macro::auto_test;

#[auto_test(test_valid, test_div_by_zero)]
fn div_u32(a: u32, b: u32) -> u32 {
    if b == 0 {
        panic!("attempt to divide by zero");
    }
    a / b
}

Supported Models

  • ChatGPT: gpt-3.5-turbo (default)
  • Text Completion: text-davinci-003 (Specify davinci feature to enable it)

License

gpt-macro is released under the MIT license.

gpt-macro's People

Contributors

retrage avatar v0y4g3r avatar

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.