llama-runner 1.1.1

A straightforward Rust library for running llama.cpp models locally on device
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
use std::{
    io::IsTerminal,
    marker::PhantomData,
    num::NonZeroU32,
    path::{Path, PathBuf},
    str::FromStr,
    sync::LazyLock,
};

use encoding_rs::{Decoder, UTF_8};
use hf_hub::api::tokio::ApiBuilder;
use llama_cpp_2::{
    LlamaContextLoadError,
    context::{LlamaContext, params::LlamaContextParams},
    llama_backend::LlamaBackend,
    llama_batch::LlamaBatch,
    model::{AddBos, LlamaChatTemplate, LlamaModel},
    mtmd::{self, MtmdBitmap, MtmdContext, MtmdInputText},
    sampling::LlamaSampler,
    token::LlamaToken,
};
use strum::Display;

use crate::{
    error::{CreateLlamaCppRunnerError, RunnerError},
    sample::{LlguidanceSamplingParams, SimpleSamplingParams},
    template::{ChatTemplate, ModelChatTemplate},
};

pub const QWEN_3D5_4B_GUFF_MODEL_ID: &str = "unsloth/Qwen3.5-4B-GGUF";
pub const QWEN_3D5_4B_GUFF_MODDEL_FILENAME: &str = "Qwen3.5-4B-Q4_K_M.gguf";
pub const QWEN_3D5_4B_GUFF_MULTIMODEL_FILENAME: &str = "mmproj-F16.gguf";

pub const GEMMA_3_1B_GUFF_MODEL_ID: &str = "google/gemma-3-1b-it-qat-q4_0-gguf";
pub const GEMMA_3_1B_GUFF_MODEL_FILENAME: &str = "gemma-3-1b-it-q4_0.gguf";

pub trait TextLmRunner<'s, 'req> {
    type Response: Iterator<
        Item = Result<String, RunnerError<<Self::Template as ChatTemplate>::Error>>,
    >;
    type Template: ChatTemplate;
    fn stream_lm_response(
        &'s self,
        request: GenericTextLmRequest<'req, Self::Template>,
    ) -> Self::Response;
}

pub trait VisionLmRunner<'s, 'req> {
    type Response: Iterator<
        Item = Result<String, RunnerError<<Self::Template as ChatTemplate>::Error>>,
    >;
    type Template: ChatTemplate;
    fn stream_vlm_response(
        &'s self,
        request: GenericVisionLmRequest<'req, Self::Template>,
    ) -> Self::Response;
}

#[derive(Debug, Clone)]
pub struct RunnerRequest<MsgCt, Tmpl> {
    pub messages: Vec<(MessageRole, MsgCt)>,
    pub sampling: SimpleSamplingParams,
    pub llguidance: Option<LlguidanceSamplingParams>,
    pub max_seq: usize,
    pub prefill: Option<String>,
    pub tmpl: Tmpl,
}

impl<M, T> Default for RunnerRequest<M, T>
where
    T: Default,
{
    fn default() -> Self {
        Self {
            messages: vec![],
            sampling: Default::default(),
            llguidance: None,
            max_seq: usize::MAX,
            prefill: None,
            tmpl: Default::default(),
        }
    }
}

pub type GenericTextLmRequest<'a, Tmpl> = RunnerRequest<&'a str, Tmpl>;
pub type GenericVisionLmRequest<'a, Tmpl> = RunnerRequest<ImageOrText<'a>, Tmpl>;

pub type TextLmRequest<'a> = RunnerRequest<&'a str, ModelChatTemplate>;
pub type VisionLmRequest<'a> = RunnerRequest<ImageOrText<'a>, ModelChatTemplate>;

pub trait TextLmRunnerExt<'s, 'req, Tmpl, TmplErr> {
    fn get_lm_response(
        &'s self,
        request: GenericTextLmRequest<'req, Tmpl>,
    ) -> Result<String, RunnerError<TmplErr>>;
}

pub trait VisionLmRunnerExt<'s, 'req, Tmpl, TmplErr> {
    fn get_vlm_response(
        &'s self,
        request: GenericVisionLmRequest<'req, Tmpl>,
    ) -> Result<String, RunnerError<TmplErr>>;
}

impl<'s, 'req, TextRunner, Tmpl> TextLmRunnerExt<'s, 'req, Tmpl, Tmpl::Error> for TextRunner
where
    Tmpl: ChatTemplate,
    TextRunner: TextLmRunner<'s, 'req, Template = Tmpl>,
{
    fn get_lm_response(
        &'s self,
        request: GenericTextLmRequest<'req, Tmpl>,
    ) -> Result<String, RunnerError<Tmpl::Error>> {
        self.stream_lm_response(request)
            .collect::<Result<String, _>>()
    }
}

impl<'s, 'req, VisionRunner, Tmpl> VisionLmRunnerExt<'s, 'req, Tmpl, Tmpl::Error> for VisionRunner
where
    Tmpl: ChatTemplate,
    VisionRunner: VisionLmRunner<'s, 'req, Template = Tmpl>,
{
    fn get_vlm_response(
        &'s self,
        request: GenericVisionLmRequest<'req, Tmpl>,
    ) -> Result<String, RunnerError<Tmpl::Error>> {
        self.stream_vlm_response(request)
            .collect::<Result<String, _>>()
    }
}

#[derive(Debug, Clone, Display, PartialEq, Eq)]
pub enum MessageRole {
    #[strum(to_string = "assistant")]
    Assistant,
    #[strum(to_string = "user")]
    User,
    #[strum(to_string = "system")]
    System,
    #[strum(to_string = "{0}")]
    Custom(&'static str),
}

#[derive(Debug, Clone)]
pub enum ImageOrText<'a> {
    Text(&'a str),
    Image(&'a image::DynamicImage),
}

pub struct Gemma3TextRunner<Tmpl> {
    model: LlamaModel,
    llama_template: LlamaChatTemplate,
    ctx_size: NonZeroU32,
    _tmpl: PhantomData<Tmpl>,
}

impl<Tmpl> Gemma3TextRunner<Tmpl> {
    pub async fn new(
        model_id: impl ToString,
        model_file: impl AsRef<str>,
        ctx_size: NonZeroU32,
    ) -> Result<Self, CreateLlamaCppRunnerError> {
        let repo = build_hf_api()?.model(model_id.to_string());
        Self::from_file(repo.get(model_file.as_ref()).await?, ctx_size)
    }

    pub async fn default() -> Result<RunnerWithRecommendedSampling<Self>, CreateLlamaCppRunnerError>
    {
        let inner = Self::new(
            GEMMA_3_1B_GUFF_MODEL_ID,
            GEMMA_3_1B_GUFF_MODEL_FILENAME,
            32_000.try_into().unwrap(),
        )
        .await?;
        Ok(RunnerWithRecommendedSampling {
            inner,
            default_sampling: Self::recommend_sampling(),
        })
    }

    pub fn recommend_sampling() -> SimpleSamplingParams {
        SimpleSamplingParams {
            top_p: Some(0.95f32),
            top_k: Some(64),
            temperature: Some(1f32),
            ..Default::default()
        }
    }

    pub fn from_file(
        model_file: impl AsRef<Path>,
        ctx_size: NonZeroU32,
    ) -> Result<Self, CreateLlamaCppRunnerError> {
        let model = LlamaModel::load_from_file(&LLAMA_BACKEND, model_file, &Default::default())?;

        let chat_template = model.chat_template(None)?;
        Ok(Self {
            model,
            llama_template: chat_template,
            ctx_size,
            _tmpl: PhantomData,
        })
    }
}

impl<'s, 'req, Tmpl> TextLmRunner<'s, 'req> for Gemma3TextRunner<Tmpl>
where
    Tmpl: ChatTemplate + 's,
{
    type Response = Gemma3Stream<'s, &'req str, Self, Tmpl>;
    type Template = Tmpl;

    fn stream_lm_response(&'s self, request: GenericTextLmRequest<'req, Tmpl>) -> Self::Response {
        let ctx = self
            .model
            .new_context(
                &LLAMA_BACKEND,
                LlamaContextParams::default().with_n_ctx(Some(self.ctx_size)),
            )
            .map_err(|err| RunnerError::from(err));
        Gemma3Stream::new(ctx, request, self, &self.model)
    }
}

pub struct Gemma3VisionRunner<Tmpl> {
    model: LlamaModel,
    chat_template: LlamaChatTemplate,
    mtmd_ctx: MtmdContext,
    ctx_size: NonZeroU32,
    _tmpl: PhantomData<Tmpl>,
}

static LLAMA_BACKEND: LazyLock<LlamaBackend> = LazyLock::new(|| {
    llama_cpp_2::send_logs_to_tracing(llama_cpp_2::LogOptions::default());
    LlamaBackend::init().unwrap()
});

impl<Tmpl> Gemma3VisionRunner<Tmpl> {
    pub async fn new(
        repo_id: impl ToString,
        model_file: impl AsRef<str>,
        multimodel_file: impl AsRef<str>,
        ctx_size: NonZeroU32,
    ) -> Result<Self, CreateLlamaCppRunnerError> {
        let repo = build_hf_api()?.model(repo_id.to_string());
        let model = LlamaModel::load_from_file(
            &LLAMA_BACKEND,
            repo.get(model_file.as_ref()).await?,
            &Default::default(),
        )?;

        let mtmd_ctx = MtmdContext::init_from_file(
            repo.get(multimodel_file.as_ref()).await?.to_str().unwrap(),
            &model,
            &Default::default(),
        )?;

        let chat_template = model.chat_template(None)?;

        Ok(Self {
            model,
            mtmd_ctx,
            chat_template,
            ctx_size,
            _tmpl: PhantomData,
        })
    }

    pub async fn default() -> Result<RunnerWithRecommendedSampling<Self>, CreateLlamaCppRunnerError>
    {
        let inner = Self::new(
            QWEN_3D5_4B_GUFF_MODEL_ID,
            QWEN_3D5_4B_GUFF_MODDEL_FILENAME,
            QWEN_3D5_4B_GUFF_MULTIMODEL_FILENAME,
            16384u32.try_into().unwrap(),
        )
        .await?;
        Ok(RunnerWithRecommendedSampling {
            inner: inner,
            default_sampling: SimpleSamplingParams {
                top_p: Some(0.8f32),
                top_k: Some(20),
                temperature: Some(0.7f32),
                presence_penalty: Some(1.5),
                repetition_penalty: Some(1.0),
                seed: None,
            },
        })
    }

    pub fn from_files(
        model_file: impl AsRef<Path>,
        multimodel_file: impl AsRef<Path>,
        ctx_size: NonZeroU32,
    ) -> Result<Self, CreateLlamaCppRunnerError> {
        let model = LlamaModel::load_from_file(&LLAMA_BACKEND, model_file, &Default::default())?;
        let mtmd_ctx = MtmdContext::init_from_file(
            multimodel_file.as_ref().as_os_str().to_str().unwrap(),
            &model,
            &Default::default(),
        )?;

        let chat_template = model.chat_template(None)?;

        Ok(Self {
            model,
            mtmd_ctx,
            chat_template,
            ctx_size,
            _tmpl: PhantomData,
        })
    }

    fn new_context_window(&self) -> Result<LlamaContext<'_>, LlamaContextLoadError> {
        self.model.new_context(
            &LLAMA_BACKEND,
            LlamaContextParams::default().with_n_ctx(Some(self.ctx_size)),
        )
    }
}

impl<'s, 'req, Tmpl> VisionLmRunner<'s, 'req> for Gemma3VisionRunner<Tmpl>
where
    Tmpl: ChatTemplate + 's,
{
    type Response = Gemma3Stream<'s, ImageOrText<'req>, Self, Tmpl>;
    type Template = Tmpl;

    fn stream_vlm_response(
        &'s self,
        request: GenericVisionLmRequest<'req, Tmpl>,
    ) -> Self::Response {
        let ctx = self
            .new_context_window()
            .map_err(|err| RunnerError::from(err));
        Gemma3Stream::new(ctx, request, self, &self.model)
    }
}

impl<'s, 'req, Tmpl> TextLmRunner<'s, 'req> for Gemma3VisionRunner<Tmpl>
where
    Tmpl: ChatTemplate + 's,
{
    type Response = <Self as VisionLmRunner<'s, 'req>>::Response;
    type Template = <Self as VisionLmRunner<'s, 'req>>::Template;

    fn stream_lm_response(&'s self, request: GenericTextLmRequest<'req, Tmpl>) -> Self::Response {
        self.stream_vlm_response(request.into())
    }
}

impl<'a, Tmpl> From<GenericTextLmRequest<'a, Tmpl>> for GenericVisionLmRequest<'a, Tmpl> {
    fn from(value: GenericTextLmRequest<'a, Tmpl>) -> Self {
        Self {
            messages: value
                .messages
                .into_iter()
                .map(|(role, text)| (role, ImageOrText::Text(text)))
                .collect(),
            sampling: value.sampling,
            llguidance: value.llguidance,
            max_seq: value.max_seq,
            prefill: value.prefill,
            tmpl: value.tmpl,
        }
    }
}

pub struct Gemma3Stream<'a, Message, Runner, Tmpl: ChatTemplate> {
    ctx_source: Option<Result<LlamaContext<'a>, RunnerError<Tmpl::Error>>>,
    ctx: Option<LlamaContext<'a>>,
    req: RunnerRequest<Message, Tmpl>,
    runner: &'a Runner,
    model: &'a LlamaModel,
    runtime: Option<Runtime<'a>>,
    done: bool,
}

struct Runtime<'a> {
    sampler: LlamaSampler,
    decoder: Decoder,
    batch: LlamaBatch<'a>,
    n_past: i32,
    step: usize,
}

trait PrepareRun<TmplErr> {
    fn prepare(&mut self) -> Result<(), RunnerError<TmplErr>>;
}

impl<Tmpl> PrepareRun<Tmpl::Error>
    for Gemma3Stream<'_, ImageOrText<'_>, Gemma3VisionRunner<Tmpl>, Tmpl>
where
    Tmpl: ChatTemplate,
{
    fn prepare(&mut self) -> Result<(), RunnerError<Tmpl::Error>> {
        // Preprocess the message, flattening media
        let media_marker = mtmd::mtmd_default_marker();
        let messages = self
            .req
            .messages
            .iter()
            .fold(
                Vec::<(MessageRole, String)>::new(),
                |mut acc, (role, message)| {
                    let text = match message {
                        ImageOrText::Text(text) => text,
                        ImageOrText::Image(_) => media_marker,
                    };
                    if let Some(last) = acc.last()
                        && last.0 == *role
                    {
                        // merge adjacent
                        let (_, adj) = acc.remove(acc.len() - 1);
                        acc.push((role.clone(), format!("{0}\n{text}", adj)));
                        acc
                    } else {
                        acc.push((role.clone(), text.to_string()));
                        acc
                    }
                },
            )
            .into_iter()
            .collect::<Vec<_>>();
        log::debug!(target: "gemma", "preprocessed messages: {messages:?}");

        // apply custom template
        let formatted_prompt = self
            .req
            .tmpl
            .apply_template(self.model, &self.runner.chat_template, &messages)
            .map_err(RunnerError::ApplyChatTemplate)?;

        // Aggregate images
        let bitmaps = self
            .req
            .messages
            .iter()
            .filter_map(|msg| match &msg.1 {
                ImageOrText::Image(image) => Some(image),
                _ => None,
            })
            .enumerate()
            .map(|(idx, im)| {
                MtmdBitmap::from_image_data(
                    im.width(),
                    im.height(),
                    im.to_rgb8().to_vec().as_slice(),
                )
                .expect(format!("image#{} has corrupted RGB data", idx).as_str())
            })
            .collect::<Vec<_>>();
        let bitmap_refs = bitmaps.iter().collect::<Vec<_>>();
        let chunks = self.runner.mtmd_ctx.tokenize(
            MtmdInputText {
                text: formatted_prompt,
                add_special: true,
                parse_special: true,
            },
            &bitmap_refs,
        )?;
        log::debug!(target: "gemma", "tokenization resulted in {} chunks", chunks.len());
        let n_past = chunks.eval_chunks(
            &self.runner.mtmd_ctx,
            self.ctx.as_ref().unwrap(),
            0,
            0,
            1,
            true,
        )?;

        // Generate preparation
        let mut preparation = Runtime {
            sampler: self.req.sampling.to_llama(),
            decoder: UTF_8.new_decoder(),
            batch: LlamaBatch::new(self.runner.ctx_size.get() as usize, 1),
            n_past,
            step: 0,
        };
        if let Some(llguidance) = &self.req.llguidance {
            let llg_sampler = llguidance.to_llama(&self.runner.model)?;
            preparation.sampler = LlamaSampler::chain_simple([llg_sampler, preparation.sampler]);
        }
        self.runtime = Some(preparation);

        Ok(())
    }
}

impl<S: AsRef<str>, Tmpl> PrepareRun<Tmpl::Error>
    for Gemma3Stream<'_, S, Gemma3TextRunner<Tmpl>, Tmpl>
where
    Tmpl: ChatTemplate,
{
    fn prepare(&mut self) -> Result<(), RunnerError<Tmpl::Error>> {
        // Preprocess the message
        let messages = self
            .req
            .messages
            .iter()
            .fold(
                Vec::<(MessageRole, String)>::new(),
                |mut acc, (role, message)| {
                    if let Some(last) = acc.last()
                        && last.0 == *role
                    {
                        // merge adjacent
                        let (_, adj) = acc.remove(acc.len() - 1);
                        acc.push((role.clone(), format!("{0}\n{1}", adj, message.as_ref())));
                        acc
                    } else {
                        acc.push((role.clone(), message.as_ref().to_string()));
                        acc
                    }
                },
            )
            .into_iter()
            .collect::<Vec<_>>();
        log::debug!(target: "gemma", "preprocessed messages: {messages:?}");

        // apply custom template
        let formatted_prompt = self
            .req
            .tmpl
            .apply_template(self.model, &self.runner.llama_template, &messages)
            .map_err(RunnerError::ApplyChatTemplate)?;

        // Aggregate images
        let token_list = self.model.str_to_token(&formatted_prompt, AddBos::Always)?;
        let mut batch = LlamaBatch::new(self.runner.ctx_size.get() as usize, 1);
        let token_list_len = token_list.len();
        for (i, token) in token_list.into_iter().enumerate() {
            batch.add(token, i as i32, &[0], i == token_list_len - 1)?;
        }
        self.ctx.as_mut().unwrap().decode(&mut batch)?;

        // Generate preparation
        let mut preparation = Runtime {
            sampler: self.req.sampling.to_llama(),
            decoder: UTF_8.new_decoder(),
            batch,
            n_past: token_list_len as i32,
            step: 0,
        };
        if let Some(llguidance) = &self.req.llguidance {
            let llg_sampler = llguidance.to_llama(&self.runner.model)?;
            preparation.sampler = LlamaSampler::chain_simple([llg_sampler, preparation.sampler]);
        }
        self.runtime = Some(preparation);

        Ok(())
    }
}

impl<'a, Message, Runner, Tmpl> Iterator for Gemma3Stream<'a, Message, Runner, Tmpl>
where
    Tmpl: ChatTemplate,
    Self: PrepareRun<Tmpl::Error>,
{
    type Item = Result<String, RunnerError<Tmpl::Error>>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.done {
            return None;
        }

        if let Some(result) = self.ctx_source.take() {
            match result {
                Ok(ctx) => self.ctx = Some(ctx),
                Err(err) => {
                    self.done = true;
                    return Some(Err(err));
                }
            }
        }

        if self.runtime.is_none()
            && let Err(err) = self.prepare()
        {
            self.done = true;
            return Some(Err(err));
        }
        let Runtime {
            sampler,
            decoder,
            batch,
            n_past,
            step,
        } = self.runtime.as_mut().unwrap();

        if *step >= self.req.max_seq {
            self.done = true;
            return None;
        }

        // Sample response token
        let ctx = self.ctx.as_mut().unwrap();
        let model = self.model;
        let sample_idx = batch.n_tokens() - 1;
        let mut sample = |token: LlamaToken,
                          sampler: &mut LlamaSampler,
                          ctx: &mut LlamaContext<'a>,
                          step: usize|
         -> Result<Option<String>, RunnerError<Tmpl::Error>> {
            sampler.accept(token);
            if model.is_eog_token(token) {
                return Ok(None);
            }
            batch.clear();
            batch.add(token, *n_past + (step as i32), &[0], true)?;

            ctx.decode(batch)?;

            let piece = model.token_to_piece(token, decoder, true, None)?;
            Ok(Some(piece))
        };
        if let Some(prefill) = self.req.prefill.take() {
            log::debug!(target: "gemma", "prefill: {}", prefill);
            let tokens = match model.str_to_token(&prefill, AddBos::Never) {
                Ok(tokens) => tokens,
                Err(err) => {
                    return Some(Err(err.into()));
                }
            };
            log::debug!(target: "gemma", "prefill tokens: {:?}", tokens.iter().map(|t| t.0).collect::<Vec<_>>());
            for token in tokens {
                match sample(token, sampler, ctx, *step) {
                    Ok(_) => {}
                    Err(err) => return Some(Err(err.into())),
                }
                *step += 1;
            }
            Some(Ok(prefill))
        } else {
            let token = sampler.sample(ctx, sample_idx);
            match sample(token, sampler, ctx, *step) {
                Ok(Some(piece)) => {
                    *step += 1;
                    return Some(Ok(piece));
                }
                Ok(None) => {
                    self.done = true;
                    return None;
                }
                Err(err) => {
                    self.done = true;
                    return Some(Err(err));
                }
            }
        }
    }
}

impl<'s, Message, Runner, Tmpl> Gemma3Stream<'s, Message, Runner, Tmpl>
where
    Tmpl: ChatTemplate,
{
    fn new(
        source: Result<LlamaContext<'s>, RunnerError<Tmpl::Error>>,
        req: RunnerRequest<Message, Tmpl>,
        runner: &'s Runner,
        model: &'s LlamaModel,
    ) -> Self {
        Self {
            ctx_source: Some(source),
            ctx: None,
            req,
            runner,
            model,
            runtime: None,
            done: false,
        }
    }
}

pub struct RunnerWithRecommendedSampling<Inner> {
    pub inner: Inner,
    pub default_sampling: SimpleSamplingParams,
}

impl<'a, Inner> RunnerWithRecommendedSampling<Inner> {
    fn get_preprocessed_simple_sampling(
        &self,
        sampling: SimpleSamplingParams,
    ) -> SimpleSamplingParams {
        let mut sampling = sampling;
        if sampling.top_k.is_none() {
            sampling.top_k = self.default_sampling.top_k;
        }
        if sampling.top_p.is_none() {
            sampling.top_p = self.default_sampling.top_p;
        }
        if sampling.temperature.is_none() {
            sampling.temperature = self.default_sampling.temperature;
        }
        sampling
    }
}

impl<'s, 'req, Inner, Tmpl> VisionLmRunner<'s, 'req> for RunnerWithRecommendedSampling<Inner>
where
    Tmpl: ChatTemplate,
    Inner: VisionLmRunner<'s, 'req, Template = Tmpl>,
{
    type Response = <Inner as VisionLmRunner<'s, 'req>>::Response;
    type Template = Tmpl;

    fn stream_vlm_response(
        &'s self,
        mut request: GenericVisionLmRequest<'req, Tmpl>,
    ) -> Self::Response {
        request.sampling = self.get_preprocessed_simple_sampling(request.sampling);
        self.inner.stream_vlm_response(request)
    }
}

impl<'s, 'req, Inner, Tmpl> TextLmRunner<'s, 'req> for RunnerWithRecommendedSampling<Inner>
where
    Tmpl: ChatTemplate,
    Inner: TextLmRunner<'s, 'req, Template = Tmpl>,
{
    type Response = <Inner as TextLmRunner<'s, 'req>>::Response;
    type Template = Tmpl;

    fn stream_lm_response(
        &'s self,
        mut request: GenericTextLmRequest<'req, Tmpl>,
    ) -> Self::Response {
        request.sampling = self.get_preprocessed_simple_sampling(request.sampling);
        self.inner.stream_lm_response(request)
    }
}

impl<Inner> From<Inner> for RunnerWithRecommendedSampling<Inner> {
    fn from(value: Inner) -> Self {
        Self {
            inner: value,
            default_sampling: SimpleSamplingParams::default(),
        }
    }
}

fn build_hf_api() -> Result<hf_hub::api::tokio::Api, hf_hub::api::tokio::ApiError> {
    let mut api = ApiBuilder::new()
        .with_progress(std::io::stdin().is_terminal())
        .with_token(std::env::var("HF_TOKEN").ok())
        .with_chunk_size(Some(2 << 28));
    if let Ok(endpoint) = std::env::var("HF_ENDPOINT") {
        api = api.with_endpoint(endpoint);
    }
    if let Ok(cache) = std::env::var("HF_HOME") {
        api = api.with_cache_dir(
            PathBuf::from_str(&cache).expect("HF_HOME env var is not a valid path"),
        );
    }
    api.build()
}