mistralrs 0.8.1

Fast, flexible LLM inference.
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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
use std::{collections::HashMap, fmt::Display, sync::Arc};

use super::*;
use either::Either;
use image::DynamicImage;
use indexmap::IndexMap;
use serde_json::{json, Value};

/// A type which can be used as a chat request.
///
/// Implemented by [`TextMessages`], [`MultimodalMessages`], and [`RequestBuilder`].
pub trait RequestLike {
    /// Borrow the current list of chat messages.
    fn messages_ref(&self) -> &[IndexMap<String, MessageContent>];
    /// Borrow the current list of images (empty for text-only requests).
    fn images_ref(&self) -> &[DynamicImage];
    /// Take ownership of the messages, converting them into a [`RequestMessage`].
    fn take_messages(&mut self) -> RequestMessage;
    /// Take any custom logits processors, if configured.
    fn take_logits_processors(&mut self) -> Option<Vec<Arc<dyn CustomLogitsProcessor>>>;
    /// Take any active adapter names (LoRA / X-LoRA), if configured.
    fn take_adapters(&mut self) -> Option<Vec<String>>;
    /// Whether log-probabilities should be returned.
    fn return_logprobs(&self) -> bool;
    /// Whether web search should be enabled for this request.
    fn enable_search(&self) -> Option<bool>;
    /// Take the generation constraint (regex, JSON schema, grammar, or none).
    fn take_constraint(&mut self) -> Constraint;
    /// Take the tools and tool-choice policy, if any.
    fn take_tools(&mut self) -> Option<(Vec<Tool>, ToolChoice)>;
    /// Take the sampling parameters.
    fn take_sampling_params(&mut self) -> SamplingParams;
    /// Take web search options, if configured.
    fn take_web_search_options(&mut self) -> Option<WebSearchOptions>;
    /// Whether to silently truncate prompts that exceed the model's context length.
    fn truncate_sequence(&self) -> bool {
        false
    }
    /// Apply any deferred model-specific media prefixes.
    ///
    /// Called automatically by [`Model`](crate::Model) before sending the request.
    /// The default implementation is a no-op.
    fn resolve_pending_prefixes(&mut self, _category: &ModelCategory) {}
}

#[derive(Debug, Clone, PartialEq)]
/// Plain text (chat) messages.
///
/// No constraints, logits processors, logprobs, tools, or adapters.
///
/// Sampling is deterministic.
pub struct TextMessages {
    messages: Vec<IndexMap<String, MessageContent>>,
    enable_thinking: Option<bool>,
}

impl From<TextMessages> for Vec<IndexMap<String, MessageContent>> {
    fn from(value: TextMessages) -> Self {
        value.messages
    }
}

#[derive(Debug, Clone, PartialEq)]
/// A chat message role.
pub enum TextMessageRole {
    /// The human user.
    User,
    /// The model / assistant.
    Assistant,
    /// System prompt providing instructions to the model.
    System,
    /// Output from a tool call.
    Tool,
    /// A custom role string.
    Custom(String),
}

impl Display for TextMessageRole {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::User => write!(f, "user"),
            Self::Assistant => write!(f, "assistant"),
            Self::System => write!(f, "system"),
            Self::Tool => write!(f, "tool"),
            Self::Custom(c) => write!(f, "{c}"),
        }
    }
}

impl Default for TextMessages {
    fn default() -> Self {
        Self::new()
    }
}

impl TextMessages {
    /// Create an empty text message list.
    pub fn new() -> Self {
        Self {
            messages: Vec::new(),
            enable_thinking: None,
        }
    }

    /// Append a message with the given role and text content.
    pub fn add_message(mut self, role: TextMessageRole, text: impl ToString) -> Self {
        self.messages.push(IndexMap::from([
            ("role".to_string(), Either::Left(role.to_string())),
            ("content".to_string(), Either::Left(text.to_string())),
        ]));
        self
    }

    /// Remove all messages.
    pub fn clear(mut self) -> Self {
        self.messages.clear();
        self
    }

    /// Enable extended thinking (chain-of-thought) for models that support it.
    pub fn enable_thinking(mut self, enable_thinking: bool) -> Self {
        self.enable_thinking = Some(enable_thinking);
        self
    }
}

impl RequestLike for TextMessages {
    fn messages_ref(&self) -> &[IndexMap<String, MessageContent>] {
        &self.messages
    }
    fn images_ref(&self) -> &[DynamicImage] {
        &[]
    }
    fn take_messages(&mut self) -> RequestMessage {
        let mut other = Vec::new();
        std::mem::swap(&mut other, &mut self.messages);
        RequestMessage::Chat {
            messages: other,
            enable_thinking: self.enable_thinking,
            reasoning_effort: None,
        }
    }
    fn enable_search(&self) -> Option<bool> {
        None
    }
    fn take_logits_processors(&mut self) -> Option<Vec<Arc<dyn CustomLogitsProcessor>>> {
        None
    }
    fn take_adapters(&mut self) -> Option<Vec<String>> {
        None
    }
    fn return_logprobs(&self) -> bool {
        false
    }
    fn take_constraint(&mut self) -> Constraint {
        Constraint::None
    }
    fn take_tools(&mut self) -> Option<(Vec<Tool>, ToolChoice)> {
        None
    }
    fn take_sampling_params(&mut self) -> SamplingParams {
        SamplingParams::deterministic()
    }
    fn take_web_search_options(&mut self) -> Option<WebSearchOptions> {
        None
    }
}

impl From<TextMessages> for MultimodalMessages {
    fn from(text: TextMessages) -> Self {
        Self {
            messages: text.messages,
            images: Vec::new(),
            audios: Vec::new(),
            videos: Vec::new(),
            enable_thinking: text.enable_thinking,
            pending_prefixes: Vec::new(),
        }
    }
}

/// Tracks a message whose text has not yet been prefixed with model-specific
/// media tokens. Resolved automatically at send-time.
#[derive(Debug, Clone, PartialEq)]
struct PendingMediaPrefix {
    /// Index into the owning struct's `messages` vec.
    message_index: usize,
    /// Global image indices that belong to this message.
    image_indices: Vec<usize>,
    /// Global audio indices that belong to this message.
    audio_indices: Vec<usize>,
    /// Global video indices that belong to this message.
    video_indices: Vec<usize>,
}

#[derive(Debug, Clone, PartialEq)]
/// Text (chat) messages with images and/or audios.
///
/// No constraints, logits processors, logprobs, tools, or adapters.
///
/// Sampling is deterministic.
pub struct MultimodalMessages {
    messages: Vec<IndexMap<String, MessageContent>>,
    images: Vec<DynamicImage>,
    audios: Vec<AudioInput>,
    videos: Vec<VideoInput>,
    enable_thinking: Option<bool>,
    pending_prefixes: Vec<PendingMediaPrefix>,
}

impl Default for MultimodalMessages {
    fn default() -> Self {
        Self::new()
    }
}

impl MultimodalMessages {
    /// Create an empty multimodal message list.
    pub fn new() -> Self {
        Self {
            images: Vec::new(),
            messages: Vec::new(),
            audios: Vec::new(),
            videos: Vec::new(),
            enable_thinking: None,
            pending_prefixes: Vec::new(),
        }
    }

    /// Append a text-only message with the given role and content.
    pub fn add_message(mut self, role: TextMessageRole, text: impl ToString) -> Self {
        self.messages.push(IndexMap::from([
            ("role".to_string(), Either::Left(role.to_string())),
            ("content".to_string(), Either::Left(text.to_string())),
        ]));
        self
    }

    /// Append a message containing images.
    ///
    /// Model-specific prefix tokens are applied automatically when the
    /// request is sent via [`Model::send_chat_request`](crate::Model::send_chat_request)
    /// or [`Model::stream_chat_request`](crate::Model::stream_chat_request).
    pub fn add_image_message(
        self,
        role: TextMessageRole,
        text: impl ToString,
        images: Vec<DynamicImage>,
    ) -> Self {
        self.add_multimodal_message(role, text, images, vec![], vec![])
    }

    /// Append a message containing audio.
    ///
    /// Model-specific prefix tokens are applied automatically when the
    /// request is sent via [`Model::send_chat_request`](crate::Model::send_chat_request)
    /// or [`Model::stream_chat_request`](crate::Model::stream_chat_request).
    pub fn add_audio_message(
        self,
        role: TextMessageRole,
        text: impl ToString,
        audios: Vec<AudioInput>,
    ) -> Self {
        self.add_multimodal_message(role, text, vec![], audios, vec![])
    }

    /// Append a message containing video.
    ///
    /// Model-specific prefix tokens are applied automatically when the
    /// request is sent via [`Model::send_chat_request`](crate::Model::send_chat_request)
    /// or [`Model::stream_chat_request`](crate::Model::stream_chat_request).
    pub fn add_video_message(
        self,
        role: TextMessageRole,
        text: impl ToString,
        videos: Vec<VideoInput>,
    ) -> Self {
        self.add_multimodal_message(role, text, vec![], vec![], videos)
    }

    /// Append a message containing a mix of text, images, audio, and/or video.
    ///
    /// Model-specific prefix tokens are applied automatically when the
    /// request is sent via [`Model::send_chat_request`](crate::Model::send_chat_request)
    /// or [`Model::stream_chat_request`](crate::Model::stream_chat_request).
    pub fn add_multimodal_message(
        mut self,
        role: TextMessageRole,
        text: impl ToString,
        images: Vec<DynamicImage>,
        audios: Vec<AudioInput>,
        videos: Vec<VideoInput>,
    ) -> Self {
        // Images
        let n_added_images = images.len();
        let image_indices: Vec<usize> =
            (self.images.len()..self.images.len() + n_added_images).collect();
        self.images.extend(images);

        // Audios
        let n_added_audios = audios.len();
        let audio_indices: Vec<usize> =
            (self.audios.len()..self.audios.len() + n_added_audios).collect();
        self.audios.extend(audios);

        // Videos
        let n_added_videos = videos.len();
        let video_indices: Vec<usize> =
            (self.videos.len()..self.videos.len() + n_added_videos).collect();
        self.videos.extend(videos);

        if n_added_images > 0 || n_added_audios > 0 || n_added_videos > 0 {
            let mut content_vec: Vec<IndexMap<String, Value>> = Vec::new();
            for _ in 0..n_added_images {
                content_vec.push(IndexMap::from([(
                    "type".to_string(),
                    Value::String("image".to_string()),
                )]));
            }
            for _ in 0..n_added_audios {
                content_vec.push(IndexMap::from([(
                    "type".to_string(),
                    Value::String("audio".to_string()),
                )]));
            }
            for _ in 0..n_added_videos {
                content_vec.push(IndexMap::from([(
                    "type".to_string(),
                    Value::String("video".to_string()),
                )]));
            }
            // Store raw (unprefixed) text, prefixing happens at send-time
            content_vec.push(IndexMap::from([
                ("type".to_string(), Value::String("text".to_string())),
                ("text".to_string(), Value::String(text.to_string())),
            ]));

            let message_index = self.messages.len();
            self.messages.push(IndexMap::from([
                ("role".to_string(), Either::Left(role.to_string())),
                ("content".to_string(), Either::Right(content_vec)),
            ]));

            self.pending_prefixes.push(PendingMediaPrefix {
                message_index,
                image_indices,
                audio_indices,
                video_indices,
            });
        } else {
            self.messages.push(IndexMap::from([
                ("role".to_string(), Either::Left(role.to_string())),
                ("content".to_string(), Either::Left(text.to_string())),
            ]));
        }
        self
    }

    /// Remove all messages, images, audio, and video.
    pub fn clear(mut self) -> Self {
        self.messages.clear();
        self.images.clear();
        self.audios.clear();
        self.videos.clear();
        self.pending_prefixes.clear();
        self
    }

    /// Enable extended thinking (chain-of-thought) for models that support it.
    pub fn enable_thinking(mut self, enable_thinking: bool) -> Self {
        self.enable_thinking = Some(enable_thinking);
        self
    }
}

impl RequestLike for MultimodalMessages {
    fn messages_ref(&self) -> &[IndexMap<String, MessageContent>] {
        &self.messages
    }
    fn images_ref(&self) -> &[DynamicImage] {
        &self.images
    }
    fn resolve_pending_prefixes(&mut self, category: &ModelCategory) {
        resolve_pending(category, &mut self.messages, &mut self.pending_prefixes);
    }
    fn take_messages(&mut self) -> RequestMessage {
        let mut other_messages = Vec::new();
        std::mem::swap(&mut other_messages, &mut self.messages);
        let mut other_images = Vec::new();
        std::mem::swap(&mut other_images, &mut self.images);
        let mut other_audios = Vec::new();
        std::mem::swap(&mut other_audios, &mut self.audios);
        let mut other_videos = Vec::new();
        std::mem::swap(&mut other_videos, &mut self.videos);
        RequestMessage::MultimodalChat {
            images: other_images,
            messages: other_messages,
            audios: other_audios,
            videos: other_videos,
            enable_thinking: self.enable_thinking,
            reasoning_effort: None,
        }
    }
    fn enable_search(&self) -> Option<bool> {
        None
    }
    fn take_logits_processors(&mut self) -> Option<Vec<Arc<dyn CustomLogitsProcessor>>> {
        None
    }
    fn take_adapters(&mut self) -> Option<Vec<String>> {
        None
    }
    fn return_logprobs(&self) -> bool {
        false
    }
    fn take_constraint(&mut self) -> Constraint {
        Constraint::None
    }
    fn take_tools(&mut self) -> Option<(Vec<Tool>, ToolChoice)> {
        None
    }
    fn take_sampling_params(&mut self) -> SamplingParams {
        SamplingParams::deterministic()
    }
    fn take_web_search_options(&mut self) -> Option<WebSearchOptions> {
        None
    }
}

#[derive(Clone)]
/// A way to add messages with finer control given.
///
/// This includes control over:
/// - Logits processors
/// - Constraints
/// - Logprobs
/// - Tools
/// - Sampling
/// - Enable thinking for models that support the configuration
pub struct RequestBuilder {
    messages: Vec<IndexMap<String, MessageContent>>,
    images: Vec<DynamicImage>,
    audios: Vec<AudioInput>,
    videos: Vec<VideoInput>,
    logits_processors: Vec<Arc<dyn CustomLogitsProcessor>>,
    adapters: Vec<String>,
    return_logprobs: bool,
    constraint: Constraint,
    tools: Vec<Tool>,
    tool_choice: ToolChoice,
    sampling_params: SamplingParams,
    web_search_options: Option<WebSearchOptions>,
    enable_thinking: Option<bool>,
    truncate_sequence: bool,
    pending_prefixes: Vec<PendingMediaPrefix>,
}

impl Default for RequestBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl From<TextMessages> for RequestBuilder {
    fn from(value: TextMessages) -> Self {
        Self {
            messages: value.messages,
            images: Vec::new(),
            audios: Vec::new(),
            videos: Vec::new(),
            logits_processors: Vec::new(),
            adapters: Vec::new(),
            return_logprobs: false,
            constraint: Constraint::None,
            tools: Vec::new(),
            tool_choice: ToolChoice::Auto,
            sampling_params: SamplingParams::deterministic(),
            web_search_options: None,
            enable_thinking: None,
            truncate_sequence: false,
            pending_prefixes: Vec::new(),
        }
    }
}

impl From<MultimodalMessages> for RequestBuilder {
    fn from(value: MultimodalMessages) -> Self {
        Self {
            messages: value.messages,
            images: value.images,
            audios: value.audios,
            videos: value.videos,
            logits_processors: Vec::new(),
            adapters: Vec::new(),
            return_logprobs: false,
            constraint: Constraint::None,
            tools: Vec::new(),
            tool_choice: ToolChoice::Auto,
            sampling_params: SamplingParams::deterministic(),
            web_search_options: None,
            enable_thinking: None,
            truncate_sequence: false,
            pending_prefixes: value.pending_prefixes,
        }
    }
}

impl RequestBuilder {
    /// Create an empty request builder with deterministic sampling defaults.
    pub fn new() -> Self {
        Self {
            messages: Vec::new(),
            images: Vec::new(),
            audios: Vec::new(),
            videos: Vec::new(),
            logits_processors: Vec::new(),
            adapters: Vec::new(),
            return_logprobs: false,
            constraint: Constraint::None,
            tools: Vec::new(),
            tool_choice: ToolChoice::Auto,
            sampling_params: SamplingParams::deterministic(),
            web_search_options: None,
            enable_thinking: None,
            truncate_sequence: false,
            pending_prefixes: Vec::new(),
        }
    }

    /// Enable web search with the given options.
    pub fn with_web_search_options(mut self, web_search_options: WebSearchOptions) -> Self {
        self.web_search_options = Some(web_search_options);
        self
    }

    /// Add a message to the request.
    ///
    /// For messages with tool calls, use [`Self::add_message_with_tool_call`].
    /// For messages with tool outputs, use [`Self::add_tool_message`].
    pub fn add_message(mut self, role: TextMessageRole, text: impl ToString) -> Self {
        self.messages.push(IndexMap::from([
            ("role".to_string(), Either::Left(role.to_string())),
            ("content".to_string(), Either::Left(text.to_string())),
        ]));
        self
    }

    /// Add a message with the output of a tool call.
    pub fn add_tool_message(mut self, tool_content: impl ToString, tool_id: impl ToString) -> Self {
        self.messages.push(IndexMap::from([
            (
                "role".to_string(),
                Either::Left(TextMessageRole::Tool.to_string()),
            ),
            (
                "content".to_string(),
                Either::Left(tool_content.to_string()),
            ),
            (
                "tool_call_id".to_string(),
                Either::Left(tool_id.to_string()),
            ),
        ]));
        self
    }

    /// Append an assistant message that includes tool call results.
    pub fn add_message_with_tool_call(
        mut self,
        role: TextMessageRole,
        text: impl ToString,
        tool_calls: Vec<ToolCallResponse>,
    ) -> Self {
        let tool_messages = tool_calls
            .iter()
            .map(|t| {
                IndexMap::from([
                    ("id".to_string(), Value::String(t.id.clone())),
                    ("type".to_string(), Value::String(t.tp.to_string())),
                    (
                        "function".to_string(),
                        json!({
                            "name": t.function.name,
                            "arguments": t.function.arguments,
                        }),
                    ),
                ])
            })
            .collect();
        self.messages.push(IndexMap::from([
            ("role".to_string(), Either::Left(role.to_string())),
            ("content".to_string(), Either::Left(text.to_string())),
            ("function".to_string(), Either::Right(tool_messages)),
        ]));
        self
    }

    /// Append a message containing images.
    ///
    /// Model-specific prefix tokens are applied automatically when the
    /// request is sent via [`Model::send_chat_request`](crate::Model::send_chat_request)
    /// or [`Model::stream_chat_request`](crate::Model::stream_chat_request).
    pub fn add_image_message(
        self,
        role: TextMessageRole,
        text: impl ToString,
        images: Vec<DynamicImage>,
    ) -> Self {
        self.add_multimodal_message(role, text, images, vec![], vec![])
    }

    /// Append a message containing audio.
    ///
    /// Model-specific prefix tokens are applied automatically when the
    /// request is sent via [`Model::send_chat_request`](crate::Model::send_chat_request)
    /// or [`Model::stream_chat_request`](crate::Model::stream_chat_request).
    pub fn add_audio_message(
        self,
        role: TextMessageRole,
        text: impl ToString,
        audios: Vec<AudioInput>,
    ) -> Self {
        self.add_multimodal_message(role, text, vec![], audios, vec![])
    }

    /// Append a message containing video.
    ///
    /// Model-specific prefix tokens are applied automatically when the
    /// request is sent via [`Model::send_chat_request`](crate::Model::send_chat_request)
    /// or [`Model::stream_chat_request`](crate::Model::stream_chat_request).
    pub fn add_video_message(
        self,
        role: TextMessageRole,
        text: impl ToString,
        videos: Vec<VideoInput>,
    ) -> Self {
        self.add_multimodal_message(role, text, vec![], vec![], videos)
    }

    /// Append a message containing a mix of text, images, audio, and/or video.
    ///
    /// Model-specific prefix tokens are applied automatically when the
    /// request is sent via [`Model::send_chat_request`](crate::Model::send_chat_request)
    /// or [`Model::stream_chat_request`](crate::Model::stream_chat_request).
    pub fn add_multimodal_message(
        mut self,
        role: TextMessageRole,
        text: impl ToString,
        images: Vec<DynamicImage>,
        audios: Vec<AudioInput>,
        videos: Vec<VideoInput>,
    ) -> Self {
        // Images
        let n_added_images = images.len();
        let image_indices: Vec<usize> =
            (self.images.len()..self.images.len() + n_added_images).collect();
        self.images.extend(images);

        // Audios
        let n_added_audios = audios.len();
        let audio_indices: Vec<usize> =
            (self.audios.len()..self.audios.len() + n_added_audios).collect();
        self.audios.extend(audios);

        // Videos
        let n_added_videos = videos.len();
        let video_indices: Vec<usize> =
            (self.videos.len()..self.videos.len() + n_added_videos).collect();
        self.videos.extend(videos);

        if n_added_images > 0 || n_added_audios > 0 || n_added_videos > 0 {
            let mut content_vec: Vec<IndexMap<String, Value>> = Vec::new();
            for _ in 0..n_added_images {
                content_vec.push(IndexMap::from([(
                    "type".to_string(),
                    Value::String("image".to_string()),
                )]));
            }
            for _ in 0..n_added_audios {
                content_vec.push(IndexMap::from([(
                    "type".to_string(),
                    Value::String("audio".to_string()),
                )]));
            }
            for _ in 0..n_added_videos {
                content_vec.push(IndexMap::from([(
                    "type".to_string(),
                    Value::String("video".to_string()),
                )]));
            }
            // Store raw (unprefixed) text, prefixing happens at send-time
            content_vec.push(IndexMap::from([
                ("type".to_string(), Value::String("text".to_string())),
                ("text".to_string(), Value::String(text.to_string())),
            ]));

            let message_index = self.messages.len();
            self.messages.push(IndexMap::from([
                ("role".to_string(), Either::Left(role.to_string())),
                ("content".to_string(), Either::Right(content_vec)),
            ]));

            self.pending_prefixes.push(PendingMediaPrefix {
                message_index,
                image_indices,
                audio_indices,
                video_indices,
            });
        } else {
            self.messages.push(IndexMap::from([
                ("role".to_string(), Either::Left(role.to_string())),
                ("content".to_string(), Either::Left(text.to_string())),
            ]));
        }
        self
    }

    /// Add a custom logits processor applied during token sampling.
    pub fn add_logits_processor(mut self, processor: Arc<dyn CustomLogitsProcessor>) -> Self {
        self.logits_processors.push(processor);
        self
    }

    /// Activate the given LoRA/X-LoRA adapter layers by name.
    pub fn set_adapters(mut self, adapters: Vec<String>) -> Self {
        self.adapters = adapters;
        self
    }

    /// The default tool choice is auto.
    pub fn set_tools(mut self, tools: Vec<Tool>) -> Self {
        self.tools = tools;
        self
    }

    /// Control how the model selects tools (auto, required, none, or a specific tool).
    pub fn set_tool_choice(mut self, tool_choice: ToolChoice) -> Self {
        self.tool_choice = tool_choice;
        self
    }

    /// Request log-probabilities for each generated token.
    pub fn return_logprobs(mut self, return_logprobs: bool) -> Self {
        self.return_logprobs = return_logprobs;
        self
    }

    /// Apply a generation constraint (regex, JSON schema, or grammar).
    pub fn set_constraint(mut self, constraint: Constraint) -> Self {
        self.constraint = constraint;
        self
    }

    /// Set the sampling parameters as given.
    pub fn set_sampling(mut self, params: SamplingParams) -> Self {
        self.sampling_params = params;
        self
    }

    /// Set the sampling parameters for deterministic generation.
    /// This sets up the parameters so that there is:
    /// - No temperature, topk, topp, minp
    /// - No penalties, stop tokens, or logit bias
    /// - No maximum length
    pub fn set_deterministic_sampler(mut self) -> Self {
        self.sampling_params = SamplingParams::deterministic();
        self
    }

    /// Set the sampling temperature. Higher values increase randomness.
    pub fn set_sampler_temperature(mut self, temperature: f64) -> Self {
        self.sampling_params.temperature = Some(temperature);
        self
    }

    /// Limit sampling to the top-k most probable tokens.
    pub fn set_sampler_topk(mut self, topk: usize) -> Self {
        self.sampling_params.top_k = Some(topk);
        self
    }

    /// Nucleus sampling: only consider tokens whose cumulative probability exceeds this threshold.
    pub fn set_sampler_topp(mut self, topp: f64) -> Self {
        self.sampling_params.top_p = Some(topp);
        self
    }

    /// Min-p sampling: filter tokens below this fraction of the top token's probability.
    pub fn set_sampler_minp(mut self, minp: f64) -> Self {
        self.sampling_params.min_p = Some(minp);
        self
    }

    /// Return the top-n log-probabilities per token position.
    pub fn set_sampler_topn_logprobs(mut self, top_n_logprobs: usize) -> Self {
        self.sampling_params.top_n_logprobs = top_n_logprobs;
        self
    }

    /// Penalize tokens proportionally to how often they have appeared so far.
    pub fn set_sampler_frequency_penalty(mut self, frequency_penalty: f32) -> Self {
        self.sampling_params.frequency_penalty = Some(frequency_penalty);
        self
    }

    /// Penalize tokens that have appeared at all, regardless of frequency.
    pub fn set_sampler_presence_penalty(mut self, presence_penalty: f32) -> Self {
        self.sampling_params.presence_penalty = Some(presence_penalty);
        self
    }

    /// Set stop tokens that terminate generation when produced.
    pub fn set_sampler_stop_toks(mut self, stop_toks: StopTokens) -> Self {
        self.sampling_params.stop_toks = Some(stop_toks);
        self
    }

    /// Set the maximum number of tokens to generate.
    pub fn set_sampler_max_len(mut self, max_len: usize) -> Self {
        self.sampling_params.max_len = Some(max_len);
        self
    }

    /// Apply a bias to specific token IDs during sampling.
    pub fn set_sampler_logits_bias(mut self, logits_bias: HashMap<u32, f32>) -> Self {
        self.sampling_params.logits_bias = Some(logits_bias);
        self
    }

    /// Generate multiple independent completions for the same prompt.
    pub fn set_sampler_n_choices(mut self, n_choices: usize) -> Self {
        self.sampling_params.n_choices = n_choices;
        self
    }

    /// Configure DRY (Don't Repeat Yourself) sampling parameters.
    pub fn set_sampler_dry_params(mut self, dry_params: DrySamplingParams) -> Self {
        self.sampling_params.dry_params = Some(dry_params);
        self
    }

    /// Enable extended thinking (chain-of-thought) for models that support it.
    pub fn enable_thinking(mut self, enable_thinking: bool) -> Self {
        self.enable_thinking = Some(enable_thinking);
        self
    }

    /// Truncate prompts that exceed the model's maximum context length.
    pub fn with_truncate_sequence(mut self, truncate_sequence: bool) -> Self {
        self.truncate_sequence = truncate_sequence;
        self
    }
}

impl RequestLike for RequestBuilder {
    fn messages_ref(&self) -> &[IndexMap<String, MessageContent>] {
        &self.messages
    }

    fn images_ref(&self) -> &[DynamicImage] {
        &self.images
    }

    fn resolve_pending_prefixes(&mut self, category: &ModelCategory) {
        resolve_pending(category, &mut self.messages, &mut self.pending_prefixes);
    }

    fn take_messages(&mut self) -> RequestMessage {
        if self.images.is_empty() && self.audios.is_empty() && self.videos.is_empty() {
            let mut other = Vec::new();
            std::mem::swap(&mut other, &mut self.messages);
            RequestMessage::Chat {
                messages: other,
                enable_thinking: self.enable_thinking,
                reasoning_effort: None,
            }
        } else {
            let mut other_messages = Vec::new();
            std::mem::swap(&mut other_messages, &mut self.messages);
            let mut other_images = Vec::new();
            std::mem::swap(&mut other_images, &mut self.images);
            let mut other_audios = Vec::new();
            std::mem::swap(&mut other_audios, &mut self.audios);
            let mut other_videos = Vec::new();
            std::mem::swap(&mut other_videos, &mut self.videos);
            RequestMessage::MultimodalChat {
                images: other_images,
                messages: other_messages,
                audios: other_audios,
                videos: other_videos,
                enable_thinking: self.enable_thinking,
                reasoning_effort: None,
            }
        }
    }

    fn enable_search(&self) -> Option<bool> {
        self.web_search_options.as_ref().map(|_| true)
    }

    fn take_logits_processors(&mut self) -> Option<Vec<Arc<dyn CustomLogitsProcessor>>> {
        if self.logits_processors.is_empty() {
            None
        } else {
            let mut other = Vec::new();
            std::mem::swap(&mut other, &mut self.logits_processors);
            Some(other)
        }
    }

    fn take_adapters(&mut self) -> Option<Vec<String>> {
        if self.adapters.is_empty() {
            None
        } else {
            let mut other = Vec::new();
            std::mem::swap(&mut other, &mut self.adapters);
            Some(other)
        }
    }

    fn return_logprobs(&self) -> bool {
        self.return_logprobs
    }

    fn take_constraint(&mut self) -> Constraint {
        let mut other = Constraint::None;
        std::mem::swap(&mut other, &mut self.constraint);
        other
    }

    fn take_tools(&mut self) -> Option<(Vec<Tool>, ToolChoice)> {
        if self.tools.is_empty() {
            None
        } else {
            let mut other_ts = Vec::new();
            std::mem::swap(&mut other_ts, &mut self.tools);
            let mut other_tc = ToolChoice::Auto;
            std::mem::swap(&mut other_tc, &mut self.tool_choice);
            Some((other_ts, other_tc))
        }
    }

    fn take_sampling_params(&mut self) -> SamplingParams {
        let mut other = SamplingParams::deterministic();
        std::mem::swap(&mut other, &mut self.sampling_params);
        other
    }

    fn take_web_search_options(&mut self) -> Option<WebSearchOptions> {
        let mut other = None;
        std::mem::swap(&mut other, &mut self.web_search_options);
        other
    }

    fn truncate_sequence(&self) -> bool {
        self.truncate_sequence
    }
}

/// Shared implementation: apply a model-specific prefixer to all pending messages.
fn resolve_pending(
    category: &ModelCategory,
    messages: &mut [IndexMap<String, MessageContent>],
    pending: &mut Vec<PendingMediaPrefix>,
) {
    let prefixer = match category {
        ModelCategory::Multimodal { prefixer } => prefixer,
        _ => {
            // Not a multimodal model, nothing to prefix.
            pending.clear();
            return;
        }
    };

    for entry in pending.drain(..) {
        let Some(msg) = messages.get_mut(entry.message_index) else {
            continue;
        };
        let Some(Either::Right(content_vec)) = msg.get_mut("content") else {
            continue;
        };
        // Find the text part and apply prefixing
        for part in content_vec.iter_mut() {
            let is_text = part
                .get("type")
                .is_some_and(|v| v == &Value::String("text".to_string()));
            if !is_text {
                continue;
            }
            if let Some(Value::String(text)) = part.get_mut("text") {
                if !entry.image_indices.is_empty() {
                    *text = prefixer.prefix_image(entry.image_indices.clone(), text);
                }
                if !entry.audio_indices.is_empty() {
                    *text = prefixer.prefix_audio(entry.audio_indices.clone(), text);
                }
                if !entry.video_indices.is_empty() {
                    *text = prefixer.prefix_video(entry.video_indices.clone(), text);
                }
            }
            break;
        }
    }
}

#[derive(Clone, Debug)]
/// An individual embedding input.
pub enum EmbeddingRequestInput {
    /// Raw text prompt that will be tokenized.
    Prompt(String),
    /// Pre-tokenized input.
    Tokens(Vec<u32>),
}

impl EmbeddingRequestInput {
    /// Convert this input into a [`RequestMessage`] suitable for the engine.
    pub fn into_request_message(self) -> RequestMessage {
        match self {
            Self::Prompt(prompt) => RequestMessage::Embedding { prompt },
            Self::Tokens(prompt) => RequestMessage::EmbeddingTokens { prompt },
        }
    }
}

#[derive(Clone, Debug)]
/// A validated embedding request constructed via [`EmbeddingRequestBuilder`].
pub struct EmbeddingRequest {
    /// The embedding inputs (text prompts or pre-tokenized sequences).
    pub inputs: Vec<EmbeddingRequestInput>,
    /// Whether to truncate inputs that exceed the model's maximum context length.
    pub truncate_sequence: bool,
}

impl EmbeddingRequest {
    /// Create a new builder for an embedding request.
    pub fn builder() -> EmbeddingRequestBuilder {
        EmbeddingRequestBuilder::new()
    }
}

/// Builder for configuring embedding requests.
#[derive(Clone, Debug, Default)]
pub struct EmbeddingRequestBuilder {
    inputs: Vec<EmbeddingRequestInput>,
    truncate_sequence: bool,
}

impl EmbeddingRequestBuilder {
    /// Create an empty builder. You must add at least one input before using it.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a single text prompt.
    pub fn add_prompt(mut self, prompt: impl Into<String>) -> Self {
        self.inputs
            .push(EmbeddingRequestInput::Prompt(prompt.into()));
        self
    }

    /// Add multiple text prompts at once.
    pub fn add_prompts<I, S>(mut self, prompts: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.inputs.extend(
            prompts
                .into_iter()
                .map(|prompt| EmbeddingRequestInput::Prompt(prompt.into())),
        );
        self
    }

    /// Add a single pre-tokenized prompt.
    pub fn add_tokens(mut self, tokens: impl Into<Vec<u32>>) -> Self {
        self.inputs
            .push(EmbeddingRequestInput::Tokens(tokens.into()));
        self
    }

    /// Add multiple pre-tokenized prompts.
    pub fn add_tokens_batch<I>(mut self, batches: I) -> Self
    where
        I: IntoIterator<Item = Vec<u32>>,
    {
        self.inputs
            .extend(batches.into_iter().map(EmbeddingRequestInput::Tokens));
        self
    }

    /// Control whether prompts longer than the model context are truncated.
    pub fn with_truncate_sequence(mut self, truncate: bool) -> Self {
        self.truncate_sequence = truncate;
        self
    }

    /// Validate and build the [`EmbeddingRequest`]. Returns an error if no inputs were added.
    pub fn build(self) -> anyhow::Result<EmbeddingRequest> {
        if self.inputs.is_empty() {
            anyhow::bail!("Embedding request must contain at least one input.");
        }

        Ok(EmbeddingRequest {
            inputs: self.inputs,
            truncate_sequence: self.truncate_sequence,
        })
    }
}