ToPrompt

Trait ToPrompt 

Source
pub trait ToPrompt {
    // Provided methods
    fn to_prompt_parts_with_mode(&self, mode: &str) -> Vec<PromptPart> { ... }
    fn to_prompt_with_mode(&self, mode: &str) -> String { ... }
    fn to_prompt_parts(&self) -> Vec<PromptPart> { ... }
    fn to_prompt(&self) -> String { ... }
    fn prompt_schema() -> String { ... }
}
Expand description

A trait for converting any type into a string suitable for an LLM prompt.

This trait provides a standard interface for converting various types into strings that can be used as prompts for language models.

§Example

use llm_toolkit::prompt::ToPrompt;

// Common types have ToPrompt implementations
let number = 42;
assert_eq!(number.to_prompt(), "42");

let text = "Hello, LLM!";
assert_eq!(text.to_prompt(), "Hello, LLM!");

§Custom Implementation

You can also implement ToPrompt directly for your own types:

use llm_toolkit::prompt::{ToPrompt, PromptPart};
use std::fmt;

struct CustomType {
    value: String,
}

impl fmt::Display for CustomType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.value)
    }
}

// By implementing ToPrompt directly, you can control the conversion.
impl ToPrompt for CustomType {
    fn to_prompt_parts(&self) -> Vec<PromptPart> {
        vec![PromptPart::Text(self.to_string())]
    }

    fn to_prompt(&self) -> String {
        self.to_string()
    }
}

let custom = CustomType { value: "custom".to_string() };
assert_eq!(custom.to_prompt(), "custom");

Provided Methods§

Source

fn to_prompt_parts_with_mode(&self, mode: &str) -> Vec<PromptPart>

Converts the object into a vector of PromptParts based on a mode.

This is the core method that derive(ToPrompt) will implement. The mode argument allows for different prompt representations, such as:

  • “full”: A comprehensive prompt with schema and examples.
  • “schema_only”: Just the data structure’s schema.
  • “example_only”: Just a concrete example.

The default implementation ignores the mode and calls to_prompt_parts for backward compatibility with manual implementations.

Source

fn to_prompt_with_mode(&self, mode: &str) -> String

Converts the object into a prompt string based on a mode.

This method extracts only the text portions from to_prompt_parts_with_mode().

Source

fn to_prompt_parts(&self) -> Vec<PromptPart>

Converts the object into a vector of PromptParts using the default “full” mode.

This method enables multimodal prompt generation by returning a collection of prompt parts that can include text, images, and other media types.

Source

fn to_prompt(&self) -> String

Converts the object into a prompt string using the default “full” mode.

This method provides backward compatibility by extracting only the text portions from to_prompt_parts() and joining them.

Source

fn prompt_schema() -> String

Returns a schema-level prompt for the type itself.

For enums, this returns all possible variants with their descriptions. For structs, this returns the field schema.

Unlike instance methods like to_prompt(), this is a type-level method that doesn’t require an instance.

§Examples
// Enum: get all variants
let schema = MyEnum::prompt_schema();

// Struct: get field schema
let schema = MyStruct::prompt_schema();

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl ToPrompt for &str

Source§

impl ToPrompt for bool

Source§

impl ToPrompt for char

Source§

impl ToPrompt for f32

Source§

impl ToPrompt for f64

Source§

impl ToPrompt for i8

Source§

impl ToPrompt for i16

Source§

impl ToPrompt for i32

Source§

impl ToPrompt for i64

Source§

impl ToPrompt for i128

Source§

impl ToPrompt for isize

Source§

impl ToPrompt for u8

Source§

impl ToPrompt for u16

Source§

impl ToPrompt for u32

Source§

impl ToPrompt for u64

Source§

impl ToPrompt for u128

Source§

impl ToPrompt for usize

Source§

impl ToPrompt for String

Source§

impl<T: ToPrompt> ToPrompt for Vec<T>

Implementors§