pub struct TextGenerationPipeline { /* private fields */ }
Expand description
A ready-to-use pipeline for generating text using a quantized model.
After building with TextGenerationPipelineBuilder
, call
generate_text(prompt, max_length)
to produce text completions.
Example:
use transformers::pipelines::text_generation_pipeline::{TextGenerationPipelineBuilder, ModelOptions, Gemma3Size};
let pipeline = TextGenerationPipelineBuilder::new(
ModelOptions::Gemma3(Gemma3Size::Size1B),
)
.temperature(0.7)
.build().unwrap();
let output = pipeline.generate_text("What is the meaning of life?", 5).unwrap();
println!("{}", output);
Implementations§
Source§impl TextGenerationPipeline
impl TextGenerationPipeline
Sourcepub fn prompt_completion(
&self,
prompt: &str,
max_length: usize,
) -> Result<String>
pub fn prompt_completion( &self, prompt: &str, max_length: usize, ) -> Result<String>
Examples found in repository?
examples/text_generation_pipeline.rs (line 20)
4fn main() -> Result<()> {
5 println!("Building pipeline...");
6
7 // 1. Create the pipeline, using the builder to configure the model
8 let pipeline = TextGenerationPipelineBuilder::new(ModelOptions::Qwen3(Qwen3Size::Size1_7B))
9 .temperature(0.7)
10 .build()?;
11 println!("Pipeline built successfully.");
12
13 // 2. Define prompt and max length
14 let prompt = "Explain the concept of Large Language Models in simple terms.";
15 let max_length = 250; // Maximum number of tokens to generate
16
17 println!("Generating text for prompt: '{}'", prompt);
18
19 // 3. Generate text
20 let generated_text = pipeline.prompt_completion(prompt, max_length)?;
21
22 println!("\n--- Generated Text ---");
23 println!("{}", generated_text);
24 println!("--- End of Text ---\n");
25
26 let second_prompt = "Explain the fibonacci sequence in simple terms. /no_think";
27
28 let generated_text = pipeline.prompt_completion(second_prompt, max_length)?;
29
30 println!("\n--- Generated Text 2 ---");
31 println!("{}", generated_text);
32 println!("--- End of Text 2 ---");
33
34 Ok(())
35}
More examples
examples/test_multi.rs (line 74)
4fn main() -> Result<()> {
5 println!("Building pipeline 1...");
6 // 1. Create the pipeline, using the builder to configure the model
7 let pipeline_1 = TextGenerationPipelineBuilder::new(ModelOptions::Qwen3(Qwen3Size::Size1_7B))
8 .temperature(0.7)
9 .build()?;
10 println!("Pipeline 1 built successfully.");
11
12 println!("Building pipeline 2...");
13 let pipeline_2 = TextGenerationPipelineBuilder::new(ModelOptions::Qwen3(Qwen3Size::Size1_7B))
14 .temperature(0.7)
15 .build()?;
16 println!("Pipeline 2 built successfully.");
17
18 println!("Building pipeline 3...");
19 let pipeline_3 = TextGenerationPipelineBuilder::new(ModelOptions::Qwen3(Qwen3Size::Size1_7B))
20 .temperature(0.7)
21 .build()?;
22 println!("Pipeline 3 built successfully.");
23
24 println!("Building pipeline 4...");
25 let pipeline_4 = TextGenerationPipelineBuilder::new(ModelOptions::Qwen3(Qwen3Size::Size1_7B))
26 .temperature(0.7)
27 .build()?;
28 println!("Pipeline 4 built successfully.");
29
30 println!("Building pipeline 5...");
31 let pipeline_5 = TextGenerationPipelineBuilder::new(ModelOptions::Qwen3(Qwen3Size::Size1_7B))
32 .temperature(0.7)
33 .build()?;
34 println!("Pipeline 5 built successfully.");
35
36 println!("Building pipeline 6...");
37 let pipeline_6 = TextGenerationPipelineBuilder::new(ModelOptions::Qwen3(Qwen3Size::Size1_7B))
38 .temperature(0.7)
39 .build()?;
40 println!("Pipeline 6 built successfully.");
41
42 println!("Building pipeline 7...");
43 let pipeline_7 = TextGenerationPipelineBuilder::new(ModelOptions::Qwen3(Qwen3Size::Size1_7B))
44 .temperature(0.7)
45 .build()?;
46 println!("Pipeline 7 built successfully.");
47
48 println!("Building pipeline 8...");
49 let pipeline_8 = TextGenerationPipelineBuilder::new(ModelOptions::Qwen3(Qwen3Size::Size1_7B))
50 .temperature(0.7)
51 .build()?;
52 println!("Pipeline 8 built successfully.");
53
54 println!("Building pipeline 9...");
55 let pipeline_9 = TextGenerationPipelineBuilder::new(ModelOptions::Qwen3(Qwen3Size::Size1_7B))
56 .temperature(0.7)
57 .build()?;
58 println!("Pipeline 9 built successfully.");
59
60 println!("Building pipeline 10...");
61 let pipeline_10 = TextGenerationPipelineBuilder::new(ModelOptions::Qwen3(Qwen3Size::Size1_7B))
62 .temperature(0.7)
63 .build()?;
64 println!("Pipeline 10 built successfully.");
65
66 /*
67
68 Prompting all of them
69
70 */
71
72 let prompt = "Hello, world!";
73
74 let response_1 = pipeline_1.prompt_completion(prompt, 10)?;
75 println!("Response 1: {}", response_1);
76
77 let response_2 = pipeline_2.prompt_completion(prompt, 10)?;
78 println!("Response 2: {}", response_2);
79
80 let response_3 = pipeline_3.prompt_completion(prompt, 10)?;
81 println!("Response 3: {}", response_3);
82
83 let response_4 = pipeline_4.prompt_completion(prompt, 10)?;
84 println!("Response 4: {}", response_4);
85
86 let response_5 = pipeline_5.prompt_completion(prompt, 10)?;
87 println!("Response 5: {}", response_5);
88
89 let response_6 = pipeline_6.prompt_completion(prompt, 10)?;
90 println!("Response 6: {}", response_6);
91
92 let response_7 = pipeline_7.prompt_completion(prompt, 10)?;
93 println!("Response 7: {}", response_7);
94
95 let response_8 = pipeline_8.prompt_completion(prompt, 10)?;
96 println!("Response 8: {}", response_8);
97
98 let response_9 = pipeline_9.prompt_completion(prompt, 10)?;
99 println!("Response 9: {}", response_9);
100
101 let response_10 = pipeline_10.prompt_completion(prompt, 100)?;
102 println!("Response 10: {}", response_10);
103
104 Ok(())
105}
Sourcepub fn message_completion(
&self,
messages: Vec<Message>,
max_length: usize,
) -> Result<String>
pub fn message_completion( &self, messages: Vec<Message>, max_length: usize, ) -> Result<String>
Examples found in repository?
examples/text_generation_pipeline_with_messages.rs (line 32)
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::Qwen3(Qwen3Size::Size0_6B))
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 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.",
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}
Auto Trait Implementations§
impl !Freeze for TextGenerationPipeline
impl !RefUnwindSafe for TextGenerationPipeline
impl !Send for TextGenerationPipeline
impl !Sync for TextGenerationPipeline
impl Unpin for TextGenerationPipeline
impl !UnwindSafe for TextGenerationPipeline
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> 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