11 releases

0.2.2 Feb 12, 2024
0.2.1 Feb 5, 2024
0.1.7 Feb 4, 2024

#624 in Machine learning

Download history 1/week @ 2024-09-17 15/week @ 2024-09-24 6/week @ 2024-10-01

97 downloads per month

MIT license

45KB
726 lines

Magic Instantiate

Quickstart

use openai_magic_instantiate::*;

#[derive(MagicInstantiate)]
struct Person {
    // Descriptions can help the LLM understand how to generate the value
    #[magic(description = "The person's name without any titles or honorifics")]
    name: String,
    // Validators can be used to enforce constraints on the generated value
    #[magic(validator = Min(1800))]
    #[magic(validator = Max(2100))]
    year_of_birth: u32,
}

let client = async_openai::Client::new();
let person: Person = client.instantiate("The president of the USA in 1954").await?;

// For even more ergonomics, use the `make_magic` macro to create the `magic!` macro
make_magic!(client);

let person: Person = magic!("The prime minister of the UK in 1954");

Descriptions and validators can be applied at the field level, or the struct/enum level.

Some basic validators are provided, but you can also define your own by implementing the Validator trait.

What happens here is the derived MagicInstantiate trait allows this struct to be represented as a TypeScript type definition.

This type definition plus a few instructions are used as a prompt to the LLM. The output of the LLM is validated and marshalled back into the Rust type. Attempts are made to re-prompt the LLM to fix any validation errors.

With this simple mechanism, you can write entire programs infused with AI.

Dependencies

~10–22MB
~320K SLoC