llm_chain::prompt

Struct ChatMessageCollection

Source
pub struct ChatMessageCollection<Body> { /* private fields */ }
Expand description

A collection of chat messages with various roles (e.g., user, assistant, system).

Implementations§

Source§

impl<Body> ChatMessageCollection<Body>

Source

pub fn new() -> Self

Creates a new empty ChatMessageCollection.

Source

pub fn for_vector(messages: Vec<ChatMessage<Body>>) -> Self

Creates a ChatMessageCollection from a given vector of ChatMessage.

§Arguments
  • messages - A vector of ChatMessage instances to be included in the collection.
Source

pub fn with_system(self, body: Body) -> Self

Adds a system message to the collection with the given body.

§Arguments
  • body - The message body to be added as a system message.
Source

pub fn with_user(self, body: Body) -> Self

Adds a user message to the collection with the given body.

§Arguments
  • body - The message body to be added as a user message.
Source

pub fn with_assistant(self, body: Body) -> Self

Adds an assistant message to the collection with the given body.

§Arguments
  • body - The message body to be added as an assistant message.
Source

pub fn append(&mut self, other: ChatMessageCollection<Body>)

Appends another ChatMessageCollection to this one

§Arguments
  • other - The other ChatMessageCollection to append to this one
Source

pub fn add_message(&mut self, message: ChatMessage<Body>)

Appends a ChatMessage to the collection.

§Arguments
  • message - The ChatMessage instance to be added to the collection.
Source

pub fn remove_first_message(&mut self) -> Option<ChatMessage<Body>>

Removes the first message from the collection and returns it, or None if the collection is empty.

Source

pub fn len(&self) -> usize

Returns the number of messages in the collection.

Source

pub fn is_empty(&self) -> bool

Returns true if the collection contains no messages.

Source

pub fn get_message(&self, index: usize) -> Option<&ChatMessage<Body>>

Returns a reference to the message at the specified index, or None if the index is out of bounds.

§Arguments
  • index - The index of the desired message in the collection.
Source

pub fn iter(&self) -> Iter<'_, ChatMessage<Body>>

Returns an iterator over the messages in the collection.

Source

pub fn map<U, F>(&self, f: F) -> ChatMessageCollection<U>
where F: FnMut(&ChatMessage<Body>) -> ChatMessage<U>,

Creates a new ChatMessageCollection with the results of applying a function to each ChatMessage.

§Arguments
  • f - The function to apply to each ChatMessage.
Source

pub fn try_map<U, E, F: Fn(&Body) -> Result<U, E>>( &self, f: F, ) -> Result<ChatMessageCollection<U>, E>

Creates a new ChatMessageCollection by applying a fallible function to each message body in the current collection. Returns an error if the function fails for any message.

§Arguments
  • f - The fallible function to apply to each message body.
Source

pub fn trim_to_max_messages(&mut self, max_number_of_messages: usize)

Trims the conversation to the specified number of messages by removing the oldest messages.

§Arguments
  • max_number_of_messages - The desired number of messages to keep in the conversation.
Source§

impl ChatMessageCollection<String>

Implementation of ChatMessageCollection for String.

Source

pub fn trim_context<Tok>( &mut self, tokenizer: &Tok, max_tokens: i32, ) -> Result<(), TokenizerError>
where Tok: Tokenizer,

Trims the conversation context by removing the oldest messages in the collection until the total number of tokens in the remaining messages is less than or equal to the specified max_tokens limit.

§Arguments
  • tokenizer - An instance of a Tokenizer that is used to tokenize the chat message bodies.
  • max_tokens - The maximum number of tokens allowed in the trimmed conversation context.
§Returns

A Result<(), TokenizerError> indicating success or failure.

Source

pub fn with_user_template( self, body: &str, parameters: &Parameters, ) -> Result<Self, StringTemplateError>

Adds a user message to the conversation by templating the specified template string and parameters.

§Arguments
  • body - A template string representing the message body.
  • parameters - Parameters used to template the message body
§Returns

Result<Self, StringTemplateError> If Ok() A Result containing a modified ChatMessageCollection with the new user message added on success, or an error if the body couldn’t be templated

Source

pub fn with_system_template( self, body: &str, parameters: &Parameters, ) -> Result<Self, StringTemplateError>

Adds a system message to the conversation by templating the specified template string and parameters.

§Arguments
  • body - A template string representing the message body.
  • parameters - Parameters used to template the message body
§Returns

Result<Self, StringTemplateError> If Ok() A Result containing a modified ChatMessageCollection with the new system message added on success, or an error if the body couldn’t be templated

Source

pub fn with_assistant_template( self, body: &str, parameters: &Parameters, ) -> Result<Self, StringTemplateError>

Adds a assistant message to the conversation by templating the specified template string and parameters.

§Arguments
  • body - A template string representing the message body.
  • parameters - Parameters used to template the message body
§Returns

Result<Self, StringTemplateError> If Ok() A Result containing a modified ChatMessageCollection with the new assistant message added on success, or an error if the body couldn’t be templated

Source§

impl ChatMessageCollection<StringTemplate>

Implementation of ChatMessageCollection for StringTemplate.

Source

pub fn with_user_template(self, body: &str) -> Self

Adds a user message to the conversation using the specified template string.

§Arguments
  • body - A template string representing the message body.
§Returns

A modified ChatMessageCollection with the new user message added.

Source

pub fn with_system_template(self, body: &str) -> Self

Adds a system message to the conversation using the specified template string.

§Arguments
  • body - A template string representing the message body.
§Returns

A modified ChatMessageCollection with the new system message added.

Source

pub fn with_assistant_template(self, body: &str) -> Self

Adds an assistant message to the conversation using the specified template string.

§Arguments
  • body - A template string representing the message body.
§Returns

A modified ChatMessageCollection with the new assistant message added.

Trait Implementations§

Source§

impl<Body: Clone> Clone for ChatMessageCollection<Body>

Source§

fn clone(&self) -> ChatMessageCollection<Body>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Body: Debug> Debug for ChatMessageCollection<Body>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Body> Default for ChatMessageCollection<Body>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de, Body> Deserialize<'de> for ChatMessageCollection<Body>
where Body: Deserialize<'de>,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T: Display> Display for ChatMessageCollection<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> From<ChatMessageCollection<T>> for Data<T>

Source§

fn from(chat: ChatMessageCollection<T>) -> Self

Converts to this type from the input type.
Source§

impl<Body> Serialize for ChatMessageCollection<Body>
where Body: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<Body> Freeze for ChatMessageCollection<Body>

§

impl<Body> RefUnwindSafe for ChatMessageCollection<Body>
where Body: RefUnwindSafe,

§

impl<Body> Send for ChatMessageCollection<Body>
where Body: Send,

§

impl<Body> Sync for ChatMessageCollection<Body>
where Body: Sync,

§

impl<Body> Unpin for ChatMessageCollection<Body>
where Body: Unpin,

§

impl<Body> UnwindSafe for ChatMessageCollection<Body>
where Body: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,