Skip to content

grantslatton/openai-magic-instantiate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Use LLMs to generate strongly-typed values

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages