pub struct Message { /* private fields */ }
Expand description
An individual message in a chat.
Implementations§
Source§impl Message
impl Message
Sourcepub fn system(content: &str) -> Self
pub fn system(content: &str) -> Self
Create a new system message.
System messages are used to provide instructions to the model. It’s not recommended to use more than one of these in a given chat.
Examples found in repository?
examples/text_generation_pipeline_with_messages.rs (line 16)
5fn main() -> Result<()> {
6 println!("Building pipeline...");
7
8 // 1. Create the pipeline, using the builder to configure the model
9 let pipeline = TextGenerationPipelineBuilder::new(ModelOptions::Phi4(Phi4Size::Size14B))
10 .temperature(0.7)
11 .build()?;
12 println!("Pipeline built successfully.");
13
14 // 2. Define messages and max length
15 let mut messages = vec![
16 Message::system("You are a helpful pirate assistant."),
17 Message::user("Explain the concept of Large Language Models in simple terms."),
18 ];
19 let max_length = 500; // Maximum number of tokens to generate
20
21 // Get the last user message in the messages vector
22 let prompt = messages
23 .iter()
24 .rev()
25 .find(|message| message.role() == "user")
26 .unwrap()
27 .content();
28
29 println!("Generating text for prompt: '{}'", prompt);
30
31 // 3. Generate text
32 let generated_text = pipeline.message_completion(messages.clone(), max_length)?;
33
34 println!("\n--- Generated Text ---");
35 println!("{}", generated_text);
36 println!("--- End of Text ---\n");
37
38 // Add the models response to the messages
39 messages.push(Message::assistant(generated_text.as_str()));
40
41 messages.push(Message::user(
42 "Explain the fibonacci sequence in simple terms. /no_think",
43 ));
44
45 let generated_text = pipeline.message_completion(messages, max_length)?;
46
47 println!("\n--- Generated Text 2 ---");
48 println!("{}", generated_text);
49 println!("--- End of Text 2 ---");
50
51 Ok(())
52}
Sourcepub fn user(content: &str) -> Self
pub fn user(content: &str) -> Self
Create a new user message.
User messages are used to send messages from the user to the model.
Examples found in repository?
examples/text_generation_pipeline_with_messages.rs (line 17)
5fn main() -> Result<()> {
6 println!("Building pipeline...");
7
8 // 1. Create the pipeline, using the builder to configure the model
9 let pipeline = TextGenerationPipelineBuilder::new(ModelOptions::Phi4(Phi4Size::Size14B))
10 .temperature(0.7)
11 .build()?;
12 println!("Pipeline built successfully.");
13
14 // 2. Define messages and max length
15 let mut messages = vec![
16 Message::system("You are a helpful pirate assistant."),
17 Message::user("Explain the concept of Large Language Models in simple terms."),
18 ];
19 let max_length = 500; // Maximum number of tokens to generate
20
21 // Get the last user message in the messages vector
22 let prompt = messages
23 .iter()
24 .rev()
25 .find(|message| message.role() == "user")
26 .unwrap()
27 .content();
28
29 println!("Generating text for prompt: '{}'", prompt);
30
31 // 3. Generate text
32 let generated_text = pipeline.message_completion(messages.clone(), max_length)?;
33
34 println!("\n--- Generated Text ---");
35 println!("{}", generated_text);
36 println!("--- End of Text ---\n");
37
38 // Add the models response to the messages
39 messages.push(Message::assistant(generated_text.as_str()));
40
41 messages.push(Message::user(
42 "Explain the fibonacci sequence in simple terms. /no_think",
43 ));
44
45 let generated_text = pipeline.message_completion(messages, max_length)?;
46
47 println!("\n--- Generated Text 2 ---");
48 println!("{}", generated_text);
49 println!("--- End of Text 2 ---");
50
51 Ok(())
52}
Sourcepub fn assistant(content: &str) -> Self
pub fn assistant(content: &str) -> Self
Create a new assistant message.
Assistant messages are used to store responses from the model.
Examples found in repository?
examples/text_generation_pipeline_with_messages.rs (line 39)
5fn main() -> Result<()> {
6 println!("Building pipeline...");
7
8 // 1. Create the pipeline, using the builder to configure the model
9 let pipeline = TextGenerationPipelineBuilder::new(ModelOptions::Phi4(Phi4Size::Size14B))
10 .temperature(0.7)
11 .build()?;
12 println!("Pipeline built successfully.");
13
14 // 2. Define messages and max length
15 let mut messages = vec![
16 Message::system("You are a helpful pirate assistant."),
17 Message::user("Explain the concept of Large Language Models in simple terms."),
18 ];
19 let max_length = 500; // Maximum number of tokens to generate
20
21 // Get the last user message in the messages vector
22 let prompt = messages
23 .iter()
24 .rev()
25 .find(|message| message.role() == "user")
26 .unwrap()
27 .content();
28
29 println!("Generating text for prompt: '{}'", prompt);
30
31 // 3. Generate text
32 let generated_text = pipeline.message_completion(messages.clone(), max_length)?;
33
34 println!("\n--- Generated Text ---");
35 println!("{}", generated_text);
36 println!("--- End of Text ---\n");
37
38 // Add the models response to the messages
39 messages.push(Message::assistant(generated_text.as_str()));
40
41 messages.push(Message::user(
42 "Explain the fibonacci sequence in simple terms. /no_think",
43 ));
44
45 let generated_text = pipeline.message_completion(messages, max_length)?;
46
47 println!("\n--- Generated Text 2 ---");
48 println!("{}", generated_text);
49 println!("--- End of Text 2 ---");
50
51 Ok(())
52}
Sourcepub fn role(&self) -> &str
pub fn role(&self) -> &str
Get the role of the message.
Examples found in repository?
examples/text_generation_pipeline_with_messages.rs (line 25)
5fn main() -> Result<()> {
6 println!("Building pipeline...");
7
8 // 1. Create the pipeline, using the builder to configure the model
9 let pipeline = TextGenerationPipelineBuilder::new(ModelOptions::Phi4(Phi4Size::Size14B))
10 .temperature(0.7)
11 .build()?;
12 println!("Pipeline built successfully.");
13
14 // 2. Define messages and max length
15 let mut messages = vec![
16 Message::system("You are a helpful pirate assistant."),
17 Message::user("Explain the concept of Large Language Models in simple terms."),
18 ];
19 let max_length = 500; // Maximum number of tokens to generate
20
21 // Get the last user message in the messages vector
22 let prompt = messages
23 .iter()
24 .rev()
25 .find(|message| message.role() == "user")
26 .unwrap()
27 .content();
28
29 println!("Generating text for prompt: '{}'", prompt);
30
31 // 3. Generate text
32 let generated_text = pipeline.message_completion(messages.clone(), max_length)?;
33
34 println!("\n--- Generated Text ---");
35 println!("{}", generated_text);
36 println!("--- End of Text ---\n");
37
38 // Add the models response to the messages
39 messages.push(Message::assistant(generated_text.as_str()));
40
41 messages.push(Message::user(
42 "Explain the fibonacci sequence in simple terms. /no_think",
43 ));
44
45 let generated_text = pipeline.message_completion(messages, max_length)?;
46
47 println!("\n--- Generated Text 2 ---");
48 println!("{}", generated_text);
49 println!("--- End of Text 2 ---");
50
51 Ok(())
52}
Sourcepub fn content(&self) -> &str
pub fn content(&self) -> &str
Get the content of the message.
Examples found in repository?
examples/text_generation_pipeline_with_messages.rs (line 27)
5fn main() -> Result<()> {
6 println!("Building pipeline...");
7
8 // 1. Create the pipeline, using the builder to configure the model
9 let pipeline = TextGenerationPipelineBuilder::new(ModelOptions::Phi4(Phi4Size::Size14B))
10 .temperature(0.7)
11 .build()?;
12 println!("Pipeline built successfully.");
13
14 // 2. Define messages and max length
15 let mut messages = vec![
16 Message::system("You are a helpful pirate assistant."),
17 Message::user("Explain the concept of Large Language Models in simple terms."),
18 ];
19 let max_length = 500; // Maximum number of tokens to generate
20
21 // Get the last user message in the messages vector
22 let prompt = messages
23 .iter()
24 .rev()
25 .find(|message| message.role() == "user")
26 .unwrap()
27 .content();
28
29 println!("Generating text for prompt: '{}'", prompt);
30
31 // 3. Generate text
32 let generated_text = pipeline.message_completion(messages.clone(), max_length)?;
33
34 println!("\n--- Generated Text ---");
35 println!("{}", generated_text);
36 println!("--- End of Text ---\n");
37
38 // Add the models response to the messages
39 messages.push(Message::assistant(generated_text.as_str()));
40
41 messages.push(Message::user(
42 "Explain the fibonacci sequence in simple terms. /no_think",
43 ));
44
45 let generated_text = pipeline.message_completion(messages, max_length)?;
46
47 println!("\n--- Generated Text 2 ---");
48 println!("{}", generated_text);
49 println!("--- End of Text 2 ---");
50
51 Ok(())
52}
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Message
impl<'de> Deserialize<'de> for Message
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>,
Deserialize this value from the given Serde deserializer. Read more
impl Eq for Message
impl StructuralPartialEq for Message
Auto Trait Implementations§
impl Freeze for Message
impl RefUnwindSafe for Message
impl Send for Message
impl Sync for Message
impl Unpin for Message
impl UnwindSafe for Message
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more