promptkit_rs_macros

Crates.iopromptkit_rs_macros
lib.rspromptkit_rs_macros
version0.1.0
sourcesrc
created_at2024-10-29 16:04:20.827976
updated_at2024-10-29 16:04:20.827976
descriptionLLM structured prompting library macros.
homepage
repository
max_upload_size
id1427160
size6,576
Sam Ragsdale (sragss)

documentation

README

promptkit-rs

Quick Start

use promptkit_rs::{
    executors::{Executor, OpenAI},
    Prompt, Renderable, rsx_2
};

#[derive(Clone)]
struct AuthorReviewPrompt {
    authors: Vec<String>,
}

#[derive(Clone, Serialize, Deserialize, JsonSchema)]
struct AuthorReviewResult {
    #[schemars(description = "...")]
    best_author_name: String,

    #[schemars(description = "Rationale for description in a short sentence.")]
    reason: String,
}

impl Prompt for AuthorReviewPrompt {
    type Output = AuthorReviewResult;


    fn render(&self) -> String {
        rsx_2! {
            "You're a turbo book worm. You live in a hole with nothing but books. "
            "I'm going to give you a list of authors and their books. "
            "Your job is to determine who is best."

            {self.authors}
        }
    }
}

fn main() {
    let input_data = AuthorReviewPrompt { authors: vec!["J.K. Rowling".to_string(), "George R.R. Martin".to_string()]};
    let author_review: AuthorReviewResult = OpenAI::execute(input_data).await.unwrap();

    // Will look something like this.
    // AuthorReviewResult {
    //     best_author_name: "George R.R. Martin",
    //     reason: "George R.R. Martin wins for his intricate world-building and morally complex characters, creating a narrative depth that keeps readers hooked."
    // }
}

You can also add sub components.

use promptkit_rs::{Renderable, rsx_2};

struct AuthorReviewPrompt {
    authors: Vec<Author>
}

struct Author {
    name: String,
    age: u64
}

impl Renderable for Author {
    fn render(&self) -> String {
        rsx_2! { 
            "name" {self.name}
            "age" {self.age.to_string()}
        }
    }
}

impl Prompt for AuthorReviewPrompt {
    type Output = AuthorReviewResult;

    fn render(&self) -> String {
        rsx_2! {
            "You're a turbo book worm. You live in a hole with nothing but books. "
            "I'm going to give you a list of authors and their books. "
            "Your job is to determine who is best."

            // Unfortunately we can only auto render Vec<String>
            {self.authors.iter().map(Renderable::render).collect::<Vec<String>>()}
        }
    }
}
Commit count: 0

cargo fmt