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 ofChatMessage
instances 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
- TheChatMessage
instance 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>
pub fn map<U, F>(&self, f: F) -> ChatMessageCollection<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>
Implementation of ChatMessageCollection
for String
.
impl ChatMessageCollection<String>
Implementation of ChatMessageCollection
for String
.
Sourcepub fn trim_context<Tok>(
&mut self,
tokenizer: &Tok,
max_tokens: i32,
) -> Result<(), TokenizerError>where
Tok: Tokenizer,
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 aTokenizer
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.
Sourcepub fn with_user_template(
self,
body: &str,
parameters: &Parameters,
) -> Result<Self, StringTemplateError>
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
Sourcepub fn with_system_template(
self,
body: &str,
parameters: &Parameters,
) -> Result<Self, StringTemplateError>
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
Sourcepub fn with_assistant_template(
self,
body: &str,
parameters: &Parameters,
) -> Result<Self, StringTemplateError>
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
.
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 more