Struct TextGenerationPipeline

Source
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

Source

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
Hide additional 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}
Source

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§

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> 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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T