Crates.io | openai-magic-instantiate |
lib.rs | openai-magic-instantiate |
version | 0.2.2 |
source | src |
created_at | 2024-02-04 02:05:16.706589 |
updated_at | 2024-02-12 03:37:33.641004 |
description | Use LLMs to instantiate well-typed values |
homepage | https://github.com/grantslatton/openai-magic-instantiate |
repository | https://github.com/grantslatton/openai-magic-instantiate |
max_upload_size | |
id | 1125994 |
size | 41,230 |
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.