11 releases

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

#492 in Machine learning

Download history 6/week @ 2024-02-02 10/week @ 2024-02-09 140/week @ 2024-02-16 78/week @ 2024-02-23 12/week @ 2024-03-01 14/week @ 2024-03-08 6/week @ 2024-03-15 59/week @ 2024-03-29 11/week @ 2024-04-05

70 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–23MB
~348K SLoC