ToPrompt

Trait ToPrompt 

Source
pub trait ToPrompt {
    // Required method
    fn to_prompt_parts(&self) -> Vec<PromptPart>;

    // Provided method
    fn to_prompt(&self) -> 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");

Required Methods§

Source

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

Converts the object into a vector of PromptParts.

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

Provided Methods§

Source

fn to_prompt(&self) -> String

Converts the object into a prompt string.

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

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

Implementors§