Use LLMs to generate strongly-typed values

Overview

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.

You might also like...
Encode and decode dynamically constructed values of arbitrary shapes to/from SCALE bytes

scale-value · This crate provides a Value type, which is a runtime representation that is compatible with scale_info::TypeDef. It somewhat analogous t

A command line tool that resembles a debugger as well as Cheat Engine, to search for values in memory
A command line tool that resembles a debugger as well as Cheat Engine, to search for values in memory

Summary This is a small command-line tool designed to peek around memory of a running Linux process. It also provides filtering mechanisms similar to

Library uses file-based mmap to store key-values

Library uses file-based mmap to store key-values This is a Rust version of MMKV. By default, this lib uses CRC8 to check data integrity. If include fe

Macro to print variable(s) with values nicely (stripped from release builds)

log_macro Macro to print variable(s) with values nicely (stripped from release builds) Install cargo add log_macro Use Add this to top of file: #[mac

Microsoft Excel (XLSX) to Unicode Separated Values (USV) Rust crate

xlsx-to-usv Convert Microsoft Excel (XLSX) to Unicode Separated Values (USV). Built with the USV Rust crate. Syntax: stdin | xlsx-to-usv [options] | s

A statically typed language that can deeply improve the Python ecosystem

The Erg Programming Language This is the main source code repository for Erg. This contains the compiler and documentation. 日本語 | 简体中文 | 繁體中文 Erg can

dwarf is a typed, interpreted, language that shares syntax with Rust.
dwarf is a typed, interpreted, language that shares syntax with Rust.

The dwarf Programming Language dwarf is a programming language based heavily upon, and implemented in, Rust. The language is interpreted (and slow) wi

Fully-typed, async, reusable state management and synchronization for Dioxus 🧬

dioxus-query 🦀 ⚡ Fully-typed, async, reusable state management and synchronization for Dioxus 🧬. Inspired by TanStack Query. ⚠️ Work in progress ⚠️

Fully-typed global state management with a topics subscription system for Dioxus 🧬

dioxus-radio 📡🦀 ⚠️ Work in progress !! ⚠️ . Fully-typed global state management with a topics subscription system for Dioxus 🧬. Who is this for You

Owner
Grant Slatton
Grant Slatton
TUI interface for LLMs written in Rust 🔥

Tenere TUI interface for LLMs written in Rust ?? Demo ?? Supported LLMs Only ChatGPT is supported for the moment. But I'm planning to support more mod

BADR 22 Apr 22, 2023
Rust library for integrating local LLMs (with llama.cpp) and external LLM APIs.

Table of Contents About The Project Getting Started Roadmap Contributing License Contact A rust interface for the OpenAI API and Llama.cpp ./server AP

Shelby Jenkins 4 Dec 18, 2023
🛠 SmartGPT is an experimental program meant to provide LLMs

?? SmartGPT is an experimental program meant to provide LLMs (particularly GPT-3.5 and GPT-4) with the ability to complete complex tasks without user input by breaking them down into smaller problems, and collecting information using the internet and other external sources.

n0y0u 3 Feb 25, 2024
Padding/aligning values without heap allocation

zero-copy-pads Padding/aligning values without heap allocation. Cargo Features std (default feature): Disable #![no_std]. Enable features that require

Khải 5 Nov 29, 2021
Eventually consistent values for Rust

Eventuals give you the most up-to-date snapshots of some value. They are like Futures that update over time, continually resolving to an eventually co

Edge & Node 110 Dec 31, 2022
Rust TUI library - Clipping region is a set of min/max x/y values applied to the existing region

TinyBit Clipping region is a set of min/max x/y values applied to the existing region A TUI lib This is not yet production ready T O D O TODO: bugs: T

Togglebit 13 May 3, 2022
Generic extensions for tapping values in Rust.

tap Suffix-Position Pipeline Behavior This crate provides extension methods on all types that allow transparent, temporary, inspection/mutation (tappi

Alexander Payne 213 Dec 30, 2022
Quickly save and retrieve values for shell scripts.

Quickly save and retrieve values for shell scripts.

Alex Andrade 2 Dec 15, 2022
a crate to swap values between possibly-overlapping references

omniswap: a crate to swap values between possibly-overlapping references Motivating Example You cannot simply use std::mem::swap to replace values wit

Masaki Hara 21 Nov 30, 2022
🍅 A command-line tool to get and set values in toml files while preserving comments and formatting

tomato Get, set, and delete values in TOML files while preserving comments and formatting. That's it. That's the feature set. I wrote tomato to satisf

C J Silverio 15 Dec 23, 2022