Struct llm_chain::prompt::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>
impl<Body> ChatMessageCollection<Body>
sourcepub fn for_vector(messages: Vec<ChatMessage<Body>>) -> Self
pub fn for_vector(messages: Vec<ChatMessage<Body>>) -> Self
Creates a ChatMessageCollection from a given vector of ChatMessage.
Arguments
messages- A vector ofChatMessageinstances to be included in the collection.
sourcepub fn with_system(self, body: Body) -> Self
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.
sourcepub fn with_user(self, body: Body) -> Self
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.
sourcepub fn with_assistant(self, body: Body) -> Self
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.
sourcepub fn append(&mut self, other: ChatMessageCollection<Body>)
pub fn append(&mut self, other: ChatMessageCollection<Body>)
Appends another ChatMessageCollection to this one
Arguments
other- The other ChatMessageCollection to append to this one
sourcepub fn add_message(&mut self, message: ChatMessage<Body>)
pub fn add_message(&mut self, message: ChatMessage<Body>)
Appends a ChatMessage to the collection.
Arguments
message- TheChatMessageinstance to be added to the collection.
sourcepub fn remove_first_message(&mut self) -> Option<ChatMessage<Body>>
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.
sourcepub fn get_message(&self, index: usize) -> Option<&ChatMessage<Body>>
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.
sourcepub fn iter(&self) -> Iter<'_, ChatMessage<Body>>
pub fn iter(&self) -> Iter<'_, ChatMessage<Body>>
Returns an iterator over the messages in the collection.
sourcepub fn map<U, F>(&self, f: F) -> ChatMessageCollection<U>where
F: FnMut(&ChatMessage<Body>) -> ChatMessage<U>,
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 eachChatMessage.
sourcepub fn try_map<U, E, F: Fn(&Body) -> Result<U, E>>(
&self,
f: F
) -> Result<ChatMessageCollection<U>, E>
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.
sourcepub fn trim_to_max_messages(&mut self, max_number_of_messages: usize)
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>
impl ChatMessageCollection<String>
Implementation of ChatMessageCollection for String.
sourcepub fn trim_context<Tok, TT: Clone>(
&mut self,
tokenizer: &Tok,
max_tokens: i32
) -> Result<(), TokenizerError>where
Tok: Tokenizer<TT>,
pub fn trim_context<Tok, TT: Clone>( &mut self, tokenizer: &Tok, max_tokens: i32 ) -> Result<(), TokenizerError>where Tok: Tokenizer<TT>,
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 aTokenizerthat 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§impl ChatMessageCollection<StringTemplate>
impl ChatMessageCollection<StringTemplate>
Implementation of ChatMessageCollection for StringTemplate.
sourcepub fn with_user_template(self, body: &str) -> Self
pub fn with_user_template(self, body: &str) -> Self
sourcepub fn with_system_template(self, body: &str) -> Self
pub fn with_system_template(self, body: &str) -> Self
sourcepub fn with_assistant_template(self, body: &str) -> Self
pub fn with_assistant_template(self, body: &str) -> Self
Trait Implementations§
source§impl<Body: Clone> Clone for ChatMessageCollection<Body>
impl<Body: Clone> Clone for ChatMessageCollection<Body>
source§fn clone(&self) -> ChatMessageCollection<Body>
fn clone(&self) -> ChatMessageCollection<Body>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl<Body: Debug> Debug for ChatMessageCollection<Body>
impl<Body: Debug> Debug for ChatMessageCollection<Body>
source§impl<Body> Default for ChatMessageCollection<Body>
impl<Body> Default for ChatMessageCollection<Body>
source§impl<'de, Body> Deserialize<'de> for ChatMessageCollection<Body>where
Body: Deserialize<'de>,
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>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,
source§impl<T: Display> Display for ChatMessageCollection<T>
impl<T: Display> Display for ChatMessageCollection<T>
source§impl<T> From<ChatMessageCollection<T>> for Data<T>
impl<T> From<ChatMessageCollection<T>> for Data<T>
source§fn from(chat: ChatMessageCollection<T>) -> Self
fn from(chat: ChatMessageCollection<T>) -> Self
Auto Trait Implementations§
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> 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> 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<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request