pub struct Prompt {
pub request: ProviderRequest,
pub model: String,
pub provider: Provider,
pub version: String,
pub parameters: Vec<String>,
pub response_type: ResponseType,
}Fields§
§request: ProviderRequest§model: String§provider: Provider§version: String§parameters: Vec<String>§response_type: ResponseTypeImplementations§
Source§impl Prompt
impl Prompt
Sourcepub fn new(
py: Python<'_>,
messages: &Bound<'_, PyAny>,
model: &str,
provider: &Bound<'_, PyAny>,
system_instructions: Option<&Bound<'_, PyAny>>,
model_settings: Option<&Bound<'_, PyAny>>,
output_type: Option<&Bound<'_, PyAny>>,
) -> Result<Self, TypeError>
pub fn new( py: Python<'_>, messages: &Bound<'_, PyAny>, model: &str, provider: &Bound<'_, PyAny>, system_instructions: Option<&Bound<'_, PyAny>>, model_settings: Option<&Bound<'_, PyAny>>, output_type: Option<&Bound<'_, PyAny>>, ) -> Result<Self, TypeError>
Creates a new Prompt object. Main parsing logic is as follows:
- Extract model settings if provided, otherwise use provider default settings.
- Message and system instructions are expected to be a variant of MessageNum (OpenAIChatMessage, AnthropicMessage or GeminiContent).
- On instantiation, message will be check if is_instance_of pystring. If pystring, provider will be used to map to appropriate message Text type
- If message is a pylist, each item will be checked for is_instance_of pystring or MessageNum variant and converted accordingly.
- If message is a single MessageNum variant, it will be extracted and wrapped in a vec.
- After messages are parsed, a full provider request struct will by built using to_provider_request function.
§Arguments:
message: A single message or list of messages representing user input.model: The model identifier to use for the prompt.provider: The provider to use for the prompt.system_instruction: Optional system instruction message or list of messages.model_settings: Optional model settings to use for the prompt.output_type: Optional output type to enforce structured output.
pub fn model_settings<'py>( &self, py: Python<'py>, ) -> Result<Bound<'py, PyAny>, TypeError>
pub fn model_identifier(&self) -> String
pub fn save_prompt(&self, path: Option<PathBuf>) -> PyResult<PathBuf>
pub fn from_path(path: PathBuf) -> Result<Self, TypeError>
pub fn model_validate_json(json_string: String) -> Result<Self, TypeError>
pub fn model_dump_json(&self) -> String
pub fn __str__(&self) -> String
Sourcepub fn all_messages<'py>(
&self,
py: Python<'py>,
) -> Result<Bound<'py, PyList>, TypeError>
pub fn all_messages<'py>( &self, py: Python<'py>, ) -> Result<Bound<'py, PyList>, TypeError>
Returns all messages as Python objects, including system instructions, user messages, and assistant messages.
Sourcepub fn messages<'py>(
&self,
py: Python<'py>,
) -> Result<Bound<'py, PyList>, TypeError>
pub fn messages<'py>( &self, py: Python<'py>, ) -> Result<Bound<'py, PyList>, TypeError>
Returns User messages as Python objects. This means, system instructions are excluded.
Sourcepub fn message<'py>(
&self,
py: Python<'py>,
) -> Result<Bound<'py, PyAny>, TypeError>
pub fn message<'py>( &self, py: Python<'py>, ) -> Result<Bound<'py, PyAny>, TypeError>
Returns the last User message as a Python object. This means, system instructions are excluded.
Sourcepub fn openai_messages(&self) -> Result<OpenAIMessageList, TypeError>
pub fn openai_messages(&self) -> Result<OpenAIMessageList, TypeError>
Returns the messages as OpenAI ChatMessage Python objects This is a helper that provide strict typing when working with OpenAI prompts
Sourcepub fn openai_message(&self) -> Result<OpenAIChatMessage, TypeError>
pub fn openai_message(&self) -> Result<OpenAIChatMessage, TypeError>
Returns the last message as an OpenAI ChatMessage Python object This is a helper that provide strict typing when working with OpenAI prompts
Sourcepub fn gemini_messages(&self) -> Result<GeminiContentList, TypeError>
pub fn gemini_messages(&self) -> Result<GeminiContentList, TypeError>
Returns the messages as Google GeminiContent Python objects This is a helper that provide strict typing when working with Google/Gemini/Vertex prompts
Sourcepub fn gemini_message(&self) -> Result<GeminiContent, TypeError>
pub fn gemini_message(&self) -> Result<GeminiContent, TypeError>
Returns the last message as a Google GeminiContent Python object This is a helper that provide strict typing when working with Google/Gemini/Vertex prompts
pub fn anthropic_messages(&self) -> Result<AnthropicMessageList, TypeError>
Sourcepub fn anthropic_message(&self) -> Result<AnthropicMessage, TypeError>
pub fn anthropic_message(&self) -> Result<AnthropicMessage, TypeError>
Returns the last message as an Anthropic MessageParam Python object
pub fn system_instructions<'py>( &self, py: Python<'py>, ) -> Result<Bound<'py, PyList>, TypeError>
Sourcepub fn bind(
&self,
name: Option<&str>,
value: Option<&Bound<'_, PyAny>>,
kwargs: Option<&Bound<'_, PyDict>>,
) -> Result<Self, TypeError>
pub fn bind( &self, name: Option<&str>, value: Option<&Bound<'_, PyAny>>, kwargs: Option<&Bound<'_, PyDict>>, ) -> Result<Self, TypeError>
Binds a variable in the prompt to a value. This will return a new Prompt with the variable bound to the value. This will iterate over all user messages and bind the variable in each message.
§Arguments:
name: The name of the variable to bind.value: The value to bind the variable to.
§Returns:
Result<Self, PromptError>: Returns a new Prompt with the variable bound to the value.
Sourcepub fn bind_mut(
&mut self,
name: Option<&str>,
value: Option<&Bound<'_, PyAny>>,
kwargs: Option<&Bound<'_, PyDict>>,
) -> Result<(), TypeError>
pub fn bind_mut( &mut self, name: Option<&str>, value: Option<&Bound<'_, PyAny>>, kwargs: Option<&Bound<'_, PyDict>>, ) -> Result<(), TypeError>
Binds a variable in the prompt to a value. This will mutate the current Prompt and bind the variable in each user message.
§Arguments:
name: The name of the variable to bind.value: The value to bind the variable to.
§Returns:
Result<(), PromptError>: Returns Ok(()) on success or an error if the binding fails.
pub fn response_json_schema_pretty(&self) -> Option<String>
pub fn response_json_schema_py(&self) -> Option<String>
pub fn model_dump<'py>( &self, py: Python<'py>, ) -> Result<Bound<'py, PyAny>, TypeError>
Source§impl Prompt
impl Prompt
Sourcepub fn from_generic_config(
config: GenericPromptConfig,
) -> Result<Self, TypeError>
pub fn from_generic_config( config: GenericPromptConfig, ) -> Result<Self, TypeError>
Converts a generic prompt configuration to a Prompt instance. This handles the user-friendly YAML/JSON format parsing.
pub fn response_json_schema(&self) -> Option<&Value>
pub fn new_rs( messages: Vec<MessageNum>, model: &str, provider: Provider, system_instructions: Vec<MessageNum>, model_settings: Option<ModelSettings>, response_json_schema: Option<Value>, response_type: ResponseType, ) -> Result<Self, TypeError>
pub fn add_tools( &mut self, tools: Vec<AgentToolDefinition>, ) -> Result<(), TypeError>
pub fn extract_variables( messages: &[MessageNum], system_instructions: &[MessageNum], ) -> Vec<String>
pub fn model_dump_value(&self) -> Value
pub fn to_request_json(&self) -> Result<Value, TypeError>
pub fn set_response_json_schema( &mut self, response_json_schema: Option<Value>, response_type: ResponseType, )
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Prompt
impl<'de> Deserialize<'de> for Prompt
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<'py> IntoPyObject<'py> for Prompt
impl<'py> IntoPyObject<'py> for Prompt
Source§type Output = Bound<'py, <Prompt as IntoPyObject<'py>>::Target>
type Output = Bound<'py, <Prompt as IntoPyObject<'py>>::Target>
Source§fn into_pyobject(
self,
py: Python<'py>,
) -> Result<<Self as IntoPyObject<'_>>::Output, <Self as IntoPyObject<'_>>::Error>
fn into_pyobject( self, py: Python<'py>, ) -> Result<<Self as IntoPyObject<'_>>::Output, <Self as IntoPyObject<'_>>::Error>
Source§impl PyClassImpl for Prompt
impl PyClassImpl for Prompt
Source§const IS_BASETYPE: bool = false
const IS_BASETYPE: bool = false
Source§const IS_SUBCLASS: bool = false
const IS_SUBCLASS: bool = false
Source§const IS_MAPPING: bool = false
const IS_MAPPING: bool = false
Source§const IS_SEQUENCE: bool = false
const IS_SEQUENCE: bool = false
Source§const IS_IMMUTABLE_TYPE: bool = false
const IS_IMMUTABLE_TYPE: bool = false
Source§const RAW_DOC: &'static CStr = c"\x00"
const RAW_DOC: &'static CStr = c"\x00"
Source§const DOC: &'static CStr
const DOC: &'static CStr
text_signature if a constructor is defined. Read moreSource§type ThreadChecker = SendablePyClass<Prompt>
type ThreadChecker = SendablePyClass<Prompt>
Source§type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
Source§type BaseNativeType = PyAny
type BaseNativeType = PyAny
PyAny by default, and when you declare
#[pyclass(extends=PyDict)], it’s PyDict.fn items_iter() -> PyClassItemsIter
fn lazy_type_object() -> &'static LazyTypeObject<Self>
fn dict_offset() -> Option<isize>
fn weaklist_offset() -> Option<isize>
Source§impl PyClassNewTextSignature for Prompt
impl PyClassNewTextSignature for Prompt
const TEXT_SIGNATURE: &'static str = "(messages, model, provider, system_instructions=None, model_settings=None, output_type=None)"
Source§impl PyMethods<Prompt> for PyClassImplCollector<Prompt>
impl PyMethods<Prompt> for PyClassImplCollector<Prompt>
fn py_methods(self) -> &'static PyClassItems
Source§impl PyTypeInfo for Prompt
impl PyTypeInfo for Prompt
Source§fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
Source§fn type_object(py: Python<'_>) -> Bound<'_, PyType>
fn type_object(py: Python<'_>) -> Bound<'_, PyType>
impl DerefToPyAny for Prompt
impl ExtractPyClassWithClone for Prompt
impl StructuralPartialEq for Prompt
Auto Trait Implementations§
impl Freeze for Prompt
impl RefUnwindSafe for Prompt
impl Send for Prompt
impl Sync for Prompt
impl Unpin for Prompt
impl UnsafeUnpin for Prompt
impl UnwindSafe for Prompt
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<'a, 'py, T> FromPyObject<'a, 'py> for T
impl<'a, 'py, T> FromPyObject<'a, 'py> for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
Source§fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
self into an owned Python object, dropping type information.Source§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);