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§
Sourcefn to_prompt_parts_with_mode(&self, mode: &str) -> Vec<PromptPart>
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.
Sourcefn to_prompt_with_mode(&self, mode: &str) -> String
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().
Sourcefn to_prompt_parts(&self) -> Vec<PromptPart>
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.
Sourcefn to_prompt(&self) -> String
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.
Sourcefn prompt_schema() -> String
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.