1use clap::{Parser, Subcommand};
2
3pub const DEFAULT_CHAT_MODEL: &str = "llama3.2-3b-instruct";
9
10pub const DEFAULT_GENERATE_MODEL: &str = "llama3.2-1b-instruct";
12
13#[derive(Parser)]
14#[command(name = "kjarni")]
15#[command(about = "Kjarni: The SQLite of AI", long_about = None)]
16#[command(version)]
17pub struct Cli {
18 #[command(subcommand)]
19 pub command: Commands,
20
21 #[arg(short, long, action = clap::ArgAction::Count, global = true)]
23 pub verbose: u8,
24}
25
26#[derive(Subcommand, Debug, PartialEq)]
27pub enum Commands {
28 Model {
30 #[command(subcommand)]
31 action: ModelCommands,
32 },
33
34 Generate {
36 prompt: Option<String>,
38
39 #[arg(short, long, default_value = DEFAULT_GENERATE_MODEL)]
40 model: String,
41
42 #[arg(long)]
44 model_path: Option<String>,
45
46 #[arg(short = 'n', long, default_value_t = 100)]
48 max_tokens: usize,
49
50 #[arg(short, long, default_value_t = 0.7)]
52 temperature: f32,
53
54 #[arg(long)]
56 top_k: Option<usize>,
57
58 #[arg(long)]
60 top_p: Option<f32>,
61
62 #[arg(long)]
64 min_p: Option<f32>,
65
66 #[arg(long, default_value_t = 1.1)]
68 repetition_penalty: f32,
69
70 #[arg(long)]
72 greedy: bool,
73
74 #[arg(long)]
76 gpu: bool,
77
78 #[arg(long)]
80 no_stream: bool,
81
82 #[arg(short, long)]
84 quiet: bool,
85
86 #[arg(long)]
93 gguf: bool,
94
95 #[arg(long)]
99 draft: Option<String>,
100
101 #[arg(long, default_value_t = 4)]
104 draft_tokens: usize,
105
106 #[arg(long, value_name = "NGRAM")]
109 prompt_lookup: Option<usize>,
110 },
111
112 Summarize {
114 #[arg(short, long)]
116 input: Option<String>,
117
118 #[arg(short, long, default_value = "distilbart-cnn")]
120 model: String,
121
122 #[arg(long)]
125 model_path: Option<String>,
126
127 #[arg(long)]
129 min_length: Option<usize>,
130
131 #[arg(long)]
133 max_length: Option<usize>,
134
135 #[arg(long)]
137 num_beams: Option<usize>,
138
139 #[arg(long)]
141 length_penalty: Option<f32>,
142
143 #[arg(long)]
145 no_repeat_ngram: Option<usize>,
146
147 #[arg(long)]
149 greedy: bool,
150
151 #[arg(long)]
153 no_stream: bool,
154
155 #[arg(long)]
157 gpu: bool,
158
159 #[arg(short, long)]
161 quiet: bool,
162 },
163
164 Translate {
166 #[arg(short, long)]
168 input: Option<String>,
169
170 #[arg(short, long, default_value = "flan-t5-base")]
172 model: String,
173
174 #[arg(long)]
177 model_path: Option<String>,
178
179 #[arg(long)]
181 src: Option<String>,
182
183 #[arg(long)]
185 dst: Option<String>,
186
187 #[arg(long)]
189 max_length: Option<usize>,
190
191 #[arg(long)]
193 num_beams: Option<usize>,
194
195 #[arg(long)]
197 length_penalty: Option<f32>,
198
199 #[arg(long)]
201 no_repeat_ngram: Option<usize>,
202
203 #[arg(long)]
205 greedy: bool,
206
207 #[arg(long)]
209 no_stream: bool,
210
211 #[arg(long)]
213 gpu: bool,
214
215 #[arg(short, long)]
217 quiet: bool,
218 },
219
220 Inspect {
222 path: String,
224 },
225
226 Embed {
228 input: Option<String>,
230
231 #[arg(short, long, default_value = "minilm-l6-v2")]
233 model: String,
234
235 #[arg(long)]
237 model_path: Option<String>,
238
239 #[arg(long, default_value = "raw")]
241 format: String,
242
243 #[arg(long)]
245 normalize: bool,
246
247 #[arg(long, default_value = "mean")]
254 pooling: String,
255
256 #[arg(long)]
258 gpu: bool,
259
260 #[arg(short, long)]
262 quiet: bool,
263 },
264
265 Mcp,
268
269 Transcribe {
271 file: String,
273
274 #[arg(short, long, default_value = "whisper-small")]
276 model: String,
277
278 #[arg(long)]
281 model_path: Option<String>,
282
283 #[arg(short, long)]
285 language: Option<String>,
286
287 #[arg(long)]
289 translate: bool,
290
291 #[arg(short, long)]
293 timestamps: bool,
294
295 #[arg(long)]
297 max_tokens: Option<usize>,
298
299 #[arg(long)]
301 no_stream: bool,
302
303 #[arg(long)]
305 gpu: bool,
306
307 #[arg(short, long)]
309 quiet: bool,
310 },
311
312 Classify {
314 input: Vec<String>,
316
317 #[arg(short, long, default_value = "distilbert-sentiment")]
319 model: String,
320
321 #[arg(long, value_name = "PATH")]
323 model_path: Option<String>,
324
325 #[arg(long, value_name = "LABELS")]
328 labels: Option<String>,
329
330 #[arg(long, default_value = "5")]
332 top_k: usize,
333
334 #[arg(long)]
336 threshold: Option<f32>,
337
338 #[arg(long)]
340 max_length: Option<usize>,
341
342 #[arg(long)]
344 batch_size: Option<usize>,
345
346 #[arg(long)]
348 multi_label: bool,
349
350 #[arg(short, long, default_value = "text")]
352 format: String,
353
354 #[arg(long)]
356 gpu: bool,
357
358 #[arg(long)]
360 dtype: Option<String>,
361
362 #[arg(short, long)]
364 quiet: bool,
365 },
366
367 Rerank {
369 query: String,
371
372 documents: Vec<String>,
374
375 #[arg(short, long, default_value = "minilm-l6-v2-cross-encoder")]
376 model: String,
377
378 #[arg(long)]
380 model_path: Option<String>,
381
382 #[arg(short = 'k', long)]
384 top_k: Option<usize>,
385
386 #[arg(short, long, default_value = "text")]
388 format: String,
389
390 #[arg(long)]
392 gpu: bool,
393
394 #[arg(short, long)]
396 quiet: bool,
397 },
398
399 Chat {
401 #[arg(short, long, default_value = DEFAULT_CHAT_MODEL)]
402 model: String,
403
404 #[arg(long)]
406 model_path: Option<String>,
407
408 #[arg(short, long)]
410 system: Option<String>,
411
412 #[arg(short, long, default_value_t = 0.7)]
414 temperature: f32,
415
416 #[arg(short = 'n', long, default_value_t = 512)]
418 max_tokens: usize,
419
420 #[arg(long)]
422 gpu: bool,
423
424 #[arg(short, long)]
426 quiet: bool,
427
428 #[arg(long)]
435 gguf: bool,
436
437 #[arg(long)]
440 draft: Option<String>,
441
442 #[arg(long, default_value_t = 4)]
444 draft_tokens: usize,
445 },
446
447 Index {
449 #[command(subcommand)]
450 action: IndexCommands,
451 },
452
453 #[cfg(feature = "image-io")]
455 Image {
456 #[command(subcommand)]
457 action: ImageCommands,
458 },
459
460 Search {
462 index_path: String,
464
465 query: String,
467
468 #[arg(short = 'k', long, default_value_t = 10)]
470 top_k: usize,
471
472 #[arg(long, default_value = "hybrid")]
474 mode: String,
475
476 #[arg(short, long)]
479 model: Option<String>,
480
481 #[arg(long, default_value = None)]
485 rerank_model: Option<String>,
486
487 #[arg(short, long, default_value = "text")]
489 format: String,
490
491 #[arg(long)]
493 gpu: bool,
494
495 #[arg(short, long)]
497 quiet: bool,
498 },
499
500 Similarity {
502 text1: String,
504
505 text2: String,
507
508 #[arg(short, long, default_value = "minilm-l6-v2")]
510 model: String,
511
512 #[arg(long)]
514 gpu: bool,
515
516 #[arg(short, long)]
518 quiet: bool,
519 },
520}
521
522#[derive(Subcommand, Debug, PartialEq)]
523pub enum ModelCommands {
524 List {
526 #[arg(short, long)]
528 arch: Option<String>,
529
530 #[arg(short, long)]
532 task: Option<String>,
533
534 #[arg(short, long)]
536 downloaded: bool,
537 },
538
539 Download {
541 name: String,
542
543 #[arg(long)]
544 gguf: bool,
545
546 #[arg(short, long)]
547 quiet: bool,
548 },
549
550 Remove { name: String },
552
553 Info { name: String },
555
556 Search { query: String },
558}
559
560#[derive(Subcommand, Debug, PartialEq)]
561pub enum InspectCommands {
562 Model {
564 path: String,
566 },
567}
568
569#[cfg(feature = "image-io")]
575#[derive(Subcommand, Debug, PartialEq)]
576pub enum ImageCommands {
577 Index {
579 #[arg(required = true)]
581 inputs: Vec<String>,
582
583 #[arg(short, long, default_value = "images.json")]
585 output: String,
586
587 #[arg(short, long)]
589 quiet: bool,
590 },
591
592 Search {
594 index_path: String,
596
597 query: String,
599
600 #[arg(short = 'k', long, default_value_t = 5)]
602 top_k: usize,
603
604 #[arg(short, long)]
606 quiet: bool,
607 },
608}
609
610#[derive(Subcommand, Debug, PartialEq)]
611pub enum IndexCommands {
612 Create {
614 output: String,
616
617 #[arg(conflicts_with = "from_chunks")]
619 inputs: Vec<String>,
620
621 #[arg(long, conflicts_with = "inputs")]
623 from_chunks: Option<String>,
624
625 #[arg(long, default_value_t = 512)]
632 chunk_size: usize,
633
634 #[arg(long, default_value_t = 100)]
636 chunk_overlap: usize,
637
638 #[arg(short, long, default_value = "minilm-l6-v2")]
640 model: String,
641
642 #[arg(long)]
644 gpu: bool,
645
646 #[arg(short, long)]
648 quiet: bool,
649 },
650
651 Add {
653 index_path: String,
655
656 inputs: Vec<String>,
658
659 #[arg(long, default_value_t = 512)]
663 chunk_size: usize,
664
665 #[arg(long, default_value_t = 100)]
666 chunk_overlap: usize,
667
668 #[arg(short, long)]
670 model: Option<String>,
671
672 #[arg(long)]
674 gpu: bool,
675
676 #[arg(short, long)]
678 quiet: bool,
679 },
680
681 Remove {
683 index_path: String,
685
686 #[arg(required = true)]
688 paths: Vec<String>,
689
690 #[arg(short, long)]
692 quiet: bool,
693 },
694
695 Info {
697 index_path: String,
699 },
700}
701
702pub fn verbosity_to_log_level(verbose: u8) -> &'static str {
704 match verbose {
705 0 => "warn",
706 1 => "info",
707 2 => "debug",
708 _ => "trace",
709 }
710}
711
712#[cfg(test)]
713mod tests {
714 use super::*;
715 use clap::Parser;
716
717 #[test]
727 fn hardcoded_model_names_resolve() {
728 use kjarni::ModelType;
729
730 let mut bad = Vec::new();
731
732 for name in [DEFAULT_CHAT_MODEL, DEFAULT_GENERATE_MODEL] {
733 if ModelType::from_cli_name(name).is_none() {
734 bad.push(format!("CLI default '{name}'"));
735 }
736 }
737
738 for name in kjarni::chat::suggested_models() {
739 if ModelType::from_cli_name(name).is_none() {
740 bad.push(format!("chat suggestion '{name}'"));
741 }
742 }
743
744 assert!(
745 bad.is_empty(),
746 "these names are not in the model registry, so anyone who uses them \
747 gets an error:\n {}\n\
748 Run `kjarni model list` for the valid names.",
749 bad.join("\n ")
750 );
751 }
752
753 #[test]
758 fn default_chat_model_is_a_chat_model() {
759 assert!(
760 kjarni::chat::is_chat_model(DEFAULT_CHAT_MODEL).is_ok(),
761 "DEFAULT_CHAT_MODEL '{DEFAULT_CHAT_MODEL}' cannot be used for chat"
762 );
763 }
764 fn parse_args(args: &[&str]) -> Result<Cli, clap::Error> {
765 let mut full_args = vec!["kjarni"];
766 full_args.extend(args);
767 Cli::try_parse_from(full_args)
768 }
769
770 #[test]
771 fn test_verbosity_to_log_level_zero() {
772 assert_eq!(verbosity_to_log_level(0), "warn");
773 }
774
775 #[test]
776 fn test_verbosity_to_log_level_one() {
777 assert_eq!(verbosity_to_log_level(1), "info");
778 }
779
780 #[test]
781 fn test_verbosity_to_log_level_two() {
782 assert_eq!(verbosity_to_log_level(2), "debug");
783 }
784
785 #[test]
786 fn test_verbosity_to_log_level_three() {
787 assert_eq!(verbosity_to_log_level(3), "trace");
788 }
789
790 #[test]
791 fn test_verbosity_to_log_level_high() {
792 assert_eq!(verbosity_to_log_level(10), "trace");
793 assert_eq!(verbosity_to_log_level(255), "trace");
794 }
795
796 #[test]
797 fn test_generate_minimal() {
798 let cli = parse_args(&["generate"]).unwrap();
799
800 match cli.command {
801 Commands::Generate {
802 prompt,
803 model,
804 max_tokens,
805 temperature,
806 greedy,
807 gpu,
808 quiet,
809 ..
810 } => {
811 assert!(prompt.is_none());
812 assert_eq!(model, DEFAULT_GENERATE_MODEL);
813 assert_eq!(max_tokens, 100);
814 assert!((temperature - 0.7).abs() < 0.001);
815 assert!(!greedy);
816 assert!(!gpu);
817 assert!(!quiet);
818 }
819 _ => panic!("Expected Generate command"),
820 }
821 }
822
823 #[test]
824 fn test_generate_with_prompt() {
825 let cli = parse_args(&["generate", "Hello world"]).unwrap();
826
827 match cli.command {
828 Commands::Generate { prompt, .. } => {
829 assert_eq!(prompt, Some("Hello world".to_string()));
830 }
831 _ => panic!("Expected Generate command"),
832 }
833 }
834
835 #[test]
836 fn test_generate_with_model() {
837 let cli = parse_args(&["generate", "-m", "phi3.5-mini"]).unwrap();
838
839 match cli.command {
840 Commands::Generate { model, .. } => {
841 assert_eq!(model, "phi3.5-mini");
842 }
843 _ => panic!("Expected Generate command"),
844 }
845 }
846
847 #[test]
848 fn test_generate_with_max_tokens() {
849 let cli = parse_args(&["generate", "-n", "500"]).unwrap();
850
851 match cli.command {
852 Commands::Generate { max_tokens, .. } => {
853 assert_eq!(max_tokens, 500);
854 }
855 _ => panic!("Expected Generate command"),
856 }
857 }
858
859 #[test]
860 fn test_generate_with_temperature() {
861 let cli = parse_args(&["generate", "-t", "1.5"]).unwrap();
862
863 match cli.command {
864 Commands::Generate { temperature, .. } => {
865 assert!((temperature - 1.5).abs() < 0.001);
866 }
867 _ => panic!("Expected Generate command"),
868 }
869 }
870
871 #[test]
872 fn test_generate_with_sampling_params() {
873 let cli = parse_args(&[
874 "generate", "--top-k", "50", "--top-p", "0.9", "--min-p", "0.05",
875 ])
876 .unwrap();
877
878 match cli.command {
879 Commands::Generate {
880 top_k,
881 top_p,
882 min_p,
883 ..
884 } => {
885 assert_eq!(top_k, Some(50));
886 assert_eq!(top_p, Some(0.9));
887 assert_eq!(min_p, Some(0.05));
888 }
889 _ => panic!("Expected Generate command"),
890 }
891 }
892
893 #[test]
894 fn test_generate_with_greedy() {
895 let cli = parse_args(&["generate", "--greedy"]).unwrap();
896
897 match cli.command {
898 Commands::Generate { greedy, .. } => {
899 assert!(greedy);
900 }
901 _ => panic!("Expected Generate command"),
902 }
903 }
904
905 #[test]
906 fn test_generate_with_gpu() {
907 let cli = parse_args(&["generate", "--gpu"]).unwrap();
908
909 match cli.command {
910 Commands::Generate { gpu, .. } => {
911 assert!(gpu);
912 }
913 _ => panic!("Expected Generate command"),
914 }
915 }
916
917 #[test]
918 fn test_generate_with_no_stream() {
919 let cli = parse_args(&["generate", "--no-stream"]).unwrap();
920
921 match cli.command {
922 Commands::Generate { no_stream, .. } => {
923 assert!(no_stream);
924 }
925 _ => panic!("Expected Generate command"),
926 }
927 }
928
929 #[test]
930 fn test_generate_with_quiet() {
931 let cli = parse_args(&["generate", "-q"]).unwrap();
932
933 match cli.command {
934 Commands::Generate { quiet, .. } => {
935 assert!(quiet);
936 }
937 _ => panic!("Expected Generate command"),
938 }
939 }
940
941 #[test]
942 fn test_generate_all_options() {
943 let cli = parse_args(&[
944 "generate",
945 "test prompt",
946 "-m",
947 "llama3.2-3b-instruct",
948 "-n",
949 "256",
950 "-t",
951 "0.8",
952 "--top-k",
953 "40",
954 "--top-p",
955 "0.95",
956 "--repetition-penalty",
957 "1.2",
958 "--greedy",
959 "--gpu",
960 "--no-stream",
961 "-q",
962 ])
963 .unwrap();
964
965 match cli.command {
966 Commands::Generate {
967 prompt,
968 model,
969 max_tokens,
970 temperature,
971 top_k,
972 top_p,
973 repetition_penalty,
974 greedy,
975 gpu,
976 no_stream,
977 quiet,
978 ..
979 } => {
980 assert_eq!(prompt, Some("test prompt".to_string()));
981 assert_eq!(model, "llama3.2-3b-instruct");
982 assert_eq!(max_tokens, 256);
983 assert!((temperature - 0.8).abs() < 0.001);
984 assert_eq!(top_k, Some(40));
985 assert_eq!(top_p, Some(0.95));
986 assert!((repetition_penalty - 1.2).abs() < 0.001);
987 assert!(greedy);
988 assert!(gpu);
989 assert!(no_stream);
990 assert!(quiet);
991 }
992 _ => panic!("Expected Generate command"),
993 }
994 }
995
996 #[test]
997 fn test_chat_defaults() {
998 let cli = parse_args(&["chat"]).unwrap();
999
1000 match cli.command {
1001 Commands::Chat {
1002 model,
1003 system,
1004 temperature,
1005 max_tokens,
1006 gpu,
1007 quiet,
1008 ..
1009 } => {
1010 assert_eq!(model, DEFAULT_CHAT_MODEL);
1011 assert!(system.is_none());
1012 assert!((temperature - 0.7).abs() < 0.001);
1013 assert_eq!(max_tokens, 512);
1014 assert!(!gpu);
1015 assert!(!quiet);
1016 }
1017 _ => panic!("Expected Chat command"),
1018 }
1019 }
1020
1021 #[test]
1022 fn test_chat_with_system_prompt() {
1023 let cli = parse_args(&["chat", "-s", "You are a helpful assistant"]).unwrap();
1024
1025 match cli.command {
1026 Commands::Chat { system, .. } => {
1027 assert_eq!(system, Some("You are a helpful assistant".to_string()));
1028 }
1029 _ => panic!("Expected Chat command"),
1030 }
1031 }
1032
1033 #[test]
1034 fn test_chat_with_model() {
1035 let cli = parse_args(&["chat", "-m", "phi3.5-mini"]).unwrap();
1036
1037 match cli.command {
1038 Commands::Chat { model, .. } => {
1039 assert_eq!(model, "phi3.5-mini");
1040 }
1041 _ => panic!("Expected Chat command"),
1042 }
1043 }
1044
1045 #[test]
1046 fn test_classify_defaults() {
1047 let cli = parse_args(&["classify"]).unwrap();
1048
1049 match cli.command {
1050 Commands::Classify {
1051 input,
1052 model,
1053 top_k,
1054 format,
1055 multi_label,
1056 gpu,
1057 quiet,
1058 ..
1059 } => {
1060 assert!(input.is_empty());
1061 assert_eq!(model, "distilbert-sentiment");
1062 assert_eq!(top_k, 5);
1063 assert_eq!(format, "text");
1064 assert!(!multi_label);
1065 assert!(!gpu);
1066 assert!(!quiet);
1067 }
1068 _ => panic!("Expected Classify command"),
1069 }
1070 }
1071
1072 #[test]
1073 fn test_classify_with_input() {
1074 let cli = parse_args(&["classify", "This is great!"]).unwrap();
1075
1076 match cli.command {
1077 Commands::Classify { input, .. } => {
1078 assert_eq!(input, vec!["This is great!".to_string()]);
1079 }
1080 _ => panic!("Expected Classify command"),
1081 }
1082 }
1083
1084 #[test]
1085 fn test_classify_with_multiple_inputs() {
1086 let cli = parse_args(&["classify", "text one", "text two", "text three"]).unwrap();
1087
1088 match cli.command {
1089 Commands::Classify { input, .. } => {
1090 assert_eq!(input.len(), 3);
1091 assert_eq!(input[0], "text one");
1092 assert_eq!(input[1], "text two");
1093 assert_eq!(input[2], "text three");
1094 }
1095 _ => panic!("Expected Classify command"),
1096 }
1097 }
1098
1099 #[test]
1100 fn test_classify_with_labels() {
1101 let cli = parse_args(&["classify", "--labels", "bad,good"]).unwrap();
1102
1103 match cli.command {
1104 Commands::Classify { labels, .. } => {
1105 assert_eq!(labels, Some("bad,good".to_string()));
1106 }
1107 _ => panic!("Expected Classify command"),
1108 }
1109 }
1110
1111 #[test]
1112 fn test_classify_with_multi_label() {
1113 let cli = parse_args(&["classify", "--multi-label"]).unwrap();
1114
1115 match cli.command {
1116 Commands::Classify { multi_label, .. } => {
1117 assert!(multi_label);
1118 }
1119 _ => panic!("Expected Classify command"),
1120 }
1121 }
1122
1123 #[test]
1124 fn test_search_minimal() {
1125 let cli = parse_args(&["search", "./index", "my query"]).unwrap();
1126
1127 match cli.command {
1128 Commands::Search {
1129 index_path,
1130 query,
1131 top_k,
1132 mode,
1133 model,
1134 format,
1135 ..
1136 } => {
1137 assert_eq!(index_path, "./index");
1138 assert_eq!(query, "my query");
1139 assert_eq!(top_k, 10);
1140 assert_eq!(mode, "hybrid");
1141 assert_eq!(model, None);
1142 assert_eq!(format, "text");
1143 }
1144 _ => panic!("Expected Search command"),
1145 }
1146 }
1147
1148 #[test]
1149 fn test_search_with_top_k() {
1150 let cli = parse_args(&["search", "./index", "query", "-k", "20"]).unwrap();
1151
1152 match cli.command {
1153 Commands::Search { top_k, .. } => {
1154 assert_eq!(top_k, 20);
1155 }
1156 _ => panic!("Expected Search command"),
1157 }
1158 }
1159
1160 #[test]
1161 fn test_search_with_mode() {
1162 let cli = parse_args(&["search", "./index", "query", "--mode", "semantic"]).unwrap();
1163
1164 match cli.command {
1165 Commands::Search { mode, .. } => {
1166 assert_eq!(mode, "semantic");
1167 }
1168 _ => panic!("Expected Search command"),
1169 }
1170 }
1171
1172 #[test]
1173 fn test_search_with_rerank_model() {
1174 let cli = parse_args(&[
1175 "search",
1176 "./index",
1177 "query",
1178 "--rerank-model",
1179 "ms-marco-minilm",
1180 ])
1181 .unwrap();
1182
1183 match cli.command {
1184 Commands::Search { rerank_model, .. } => {
1185 assert_eq!(rerank_model, Some("ms-marco-minilm".to_string()));
1186 }
1187 _ => panic!("Expected Search command"),
1188 }
1189 }
1190
1191 #[test]
1192 fn test_similarity_minimal() {
1193 let cli = parse_args(&["similarity", "text one", "text two"]).unwrap();
1194
1195 match cli.command {
1196 Commands::Similarity {
1197 text1,
1198 text2,
1199 model,
1200 gpu,
1201 quiet,
1202 } => {
1203 assert_eq!(text1, "text one");
1204 assert_eq!(text2, "text two");
1205 assert_eq!(model, "minilm-l6-v2");
1206 assert!(!gpu);
1207 assert!(!quiet);
1208 }
1209 _ => panic!("Expected Similarity command"),
1210 }
1211 }
1212
1213 #[test]
1214 fn test_model_list_defaults() {
1215 let cli = parse_args(&["model", "list"]).unwrap();
1216
1217 match cli.command {
1218 Commands::Model {
1219 action:
1220 ModelCommands::List {
1221 arch,
1222 task,
1223 downloaded,
1224 },
1225 } => {
1226 assert!(arch.is_none());
1227 assert!(task.is_none());
1228 assert!(!downloaded);
1229 }
1230 _ => panic!("Expected Model List command"),
1231 }
1232 }
1233
1234 #[test]
1235 fn test_model_list_with_filters() {
1236 let cli = parse_args(&[
1237 "model",
1238 "list",
1239 "--arch",
1240 "bert",
1241 "--task",
1242 "embedding",
1243 "--downloaded",
1244 ])
1245 .unwrap();
1246
1247 match cli.command {
1248 Commands::Model {
1249 action:
1250 ModelCommands::List {
1251 arch,
1252 task,
1253 downloaded,
1254 },
1255 } => {
1256 assert_eq!(arch, Some("bert".to_string()));
1257 assert_eq!(task, Some("embedding".to_string()));
1258 assert!(downloaded);
1259 }
1260 _ => panic!("Expected Model List command"),
1261 }
1262 }
1263
1264 #[test]
1265 fn test_model_download() {
1266 let cli = parse_args(&["model", "download", "minilm-l6-v2"]).unwrap();
1267
1268 match cli.command {
1269 Commands::Model {
1270 action: ModelCommands::Download { name, gguf, quiet },
1271 } => {
1272 assert_eq!(name, "minilm-l6-v2");
1273 assert!(!gguf);
1274 assert!(!quiet);
1275 }
1276 _ => panic!("Expected Model Download command"),
1277 }
1278 }
1279
1280 #[test]
1281 fn test_model_download_gguf() {
1282 let cli = parse_args(&["model", "download", "llama3.2-1b", "--gguf"]).unwrap();
1283
1284 match cli.command {
1285 Commands::Model {
1286 action: ModelCommands::Download { name, gguf, .. },
1287 } => {
1288 assert_eq!(name, "llama3.2-1b");
1289 assert!(gguf);
1290 }
1291 _ => panic!("Expected Model Download command"),
1292 }
1293 }
1294
1295 #[test]
1296 fn test_model_info() {
1297 let cli = parse_args(&["model", "info", "phi3.5-mini"]).unwrap();
1298
1299 match cli.command {
1300 Commands::Model {
1301 action: ModelCommands::Info { name },
1302 } => {
1303 assert_eq!(name, "phi3.5-mini");
1304 }
1305 _ => panic!("Expected Model Info command"),
1306 }
1307 }
1308
1309 #[test]
1310 fn test_model_remove() {
1311 let cli = parse_args(&["model", "remove", "old-model"]).unwrap();
1312
1313 match cli.command {
1314 Commands::Model {
1315 action: ModelCommands::Remove { name },
1316 } => {
1317 assert_eq!(name, "old-model");
1318 }
1319 _ => panic!("Expected Model Remove command"),
1320 }
1321 }
1322
1323 #[test]
1324 fn test_model_search() {
1325 let cli = parse_args(&["model", "search", "llama"]).unwrap();
1326
1327 match cli.command {
1328 Commands::Model {
1329 action: ModelCommands::Search { query },
1330 } => {
1331 assert_eq!(query, "llama");
1332 }
1333 _ => panic!("Expected Model Search command"),
1334 }
1335 }
1336
1337 #[test]
1338 fn test_index_create_minimal() {
1339 let cli = parse_args(&["index", "create", "output.idx"]).unwrap();
1340
1341 match cli.command {
1342 Commands::Index {
1343 action:
1344 IndexCommands::Create {
1345 output,
1346 inputs,
1347 chunk_size,
1348 chunk_overlap,
1349 model,
1350 ..
1351 },
1352 } => {
1353 assert_eq!(output, "output.idx");
1354 assert!(inputs.is_empty());
1355 assert_eq!(chunk_size, 512);
1356 assert_eq!(chunk_overlap, 100);
1357 assert_eq!(model, "minilm-l6-v2");
1358 }
1359 _ => panic!("Expected Index Create command"),
1360 }
1361 }
1362
1363 #[test]
1364 fn test_index_create_with_inputs() {
1365 let cli = parse_args(&[
1366 "index",
1367 "create",
1368 "out.idx",
1369 "file1.txt",
1370 "file2.txt",
1371 "dir/",
1372 ])
1373 .unwrap();
1374
1375 match cli.command {
1376 Commands::Index {
1377 action: IndexCommands::Create { inputs, .. },
1378 } => {
1379 assert_eq!(inputs.len(), 3);
1380 assert_eq!(inputs[0], "file1.txt");
1381 assert_eq!(inputs[1], "file2.txt");
1382 assert_eq!(inputs[2], "dir/");
1383 }
1384 _ => panic!("Expected Index Create command"),
1385 }
1386 }
1387
1388 #[test]
1389 fn test_index_create_with_options() {
1390 let cli = parse_args(&[
1391 "index",
1392 "create",
1393 "out.idx",
1394 "--chunk-size",
1395 "500",
1396 "--chunk-overlap",
1397 "100",
1398 "-m",
1399 "nomic-embed-text",
1400 "--gpu",
1401 "-q",
1402 ])
1403 .unwrap();
1404
1405 match cli.command {
1406 Commands::Index {
1407 action:
1408 IndexCommands::Create {
1409 chunk_size,
1410 chunk_overlap,
1411 model,
1412 gpu,
1413 quiet,
1414 ..
1415 },
1416 } => {
1417 assert_eq!(chunk_size, 500);
1418 assert_eq!(chunk_overlap, 100);
1419 assert_eq!(model, "nomic-embed-text");
1420 assert!(gpu);
1421 assert!(quiet);
1422 }
1423 _ => panic!("Expected Index Create command"),
1424 }
1425 }
1426
1427 #[test]
1428 fn test_index_add() {
1429 let cli = parse_args(&["index", "add", "existing.idx", "newfile.txt"]).unwrap();
1430
1431 match cli.command {
1432 Commands::Index {
1433 action:
1434 IndexCommands::Add {
1435 index_path, inputs, ..
1436 },
1437 } => {
1438 assert_eq!(index_path, "existing.idx");
1439 assert_eq!(inputs, vec!["newfile.txt".to_string()]);
1440 }
1441 _ => panic!("Expected Index Add command"),
1442 }
1443 }
1444
1445 #[test]
1446 fn test_index_info() {
1447 let cli = parse_args(&["index", "info", "my.idx"]).unwrap();
1448
1449 match cli.command {
1450 Commands::Index {
1451 action: IndexCommands::Info { index_path },
1452 } => {
1453 assert_eq!(index_path, "my.idx");
1454 }
1455 _ => panic!("Expected Index Info command"),
1456 }
1457 }
1458
1459 #[test]
1460 fn test_verbose_zero() {
1461 let cli = parse_args(&["generate"]).unwrap();
1462 assert_eq!(cli.verbose, 0);
1463 }
1464
1465 #[test]
1466 fn test_verbose_one() {
1467 let cli = parse_args(&["-v", "generate"]).unwrap();
1468 assert_eq!(cli.verbose, 1);
1469 }
1470
1471 #[test]
1472 fn test_verbose_two() {
1473 let cli = parse_args(&["-vv", "generate"]).unwrap();
1474 assert_eq!(cli.verbose, 2);
1475 }
1476
1477 #[test]
1478 fn test_verbose_three() {
1479 let cli = parse_args(&["-vvv", "generate"]).unwrap();
1480 assert_eq!(cli.verbose, 3);
1481 }
1482
1483 #[test]
1484 fn test_verbose_long_form() {
1485 let cli = parse_args(&["--verbose", "--verbose", "generate"]).unwrap();
1486 assert_eq!(cli.verbose, 2);
1487 }
1488
1489 #[test]
1490 fn test_verbose_after_command() {
1491 let cli = parse_args(&["generate", "-v"]).unwrap();
1493 assert_eq!(cli.verbose, 1);
1494 }
1495
1496 #[test]
1497 fn test_missing_command() {
1498 let result = parse_args(&[]);
1499 assert!(result.is_err());
1500 }
1501
1502 #[test]
1503 fn test_unknown_command() {
1504 let result = parse_args(&["unknown"]);
1505 assert!(result.is_err());
1506 }
1507
1508 #[test]
1509 fn test_missing_required_arg() {
1510 let result = parse_args(&["transcribe"]);
1512 assert!(result.is_err());
1513 }
1514
1515 #[test]
1516 fn test_invalid_number() {
1517 let result = parse_args(&["generate", "-n", "not_a_number"]);
1518 assert!(result.is_err());
1519 }
1520
1521 #[test]
1522 fn test_invalid_float() {
1523 let result = parse_args(&["generate", "-t", "not_a_float"]);
1524 assert!(result.is_err());
1525 }
1526
1527 #[test]
1528 fn test_rerank_minimal() {
1529 let cli = parse_args(&["rerank", "my query"]).unwrap();
1530
1531 match cli.command {
1532 Commands::Rerank {
1533 query,
1534 documents,
1535 model,
1536 top_k,
1537 format,
1538 ..
1539 } => {
1540 assert_eq!(query, "my query");
1541 assert!(documents.is_empty());
1542 assert_eq!(model, "minilm-l6-v2-cross-encoder");
1543 assert!(top_k.is_none());
1544 assert_eq!(format, "text");
1545 }
1546 _ => panic!("Expected Rerank command"),
1547 }
1548 }
1549
1550 #[test]
1551 fn test_rerank_with_documents() {
1552 let cli = parse_args(&["rerank", "query", "doc1", "doc2", "doc3"]).unwrap();
1553
1554 match cli.command {
1555 Commands::Rerank {
1556 query, documents, ..
1557 } => {
1558 assert_eq!(query, "query");
1559 assert_eq!(documents.len(), 3);
1560 }
1561 _ => panic!("Expected Rerank command"),
1562 }
1563 }
1564
1565 #[test]
1566 fn test_rerank_with_top_k() {
1567 let cli = parse_args(&["rerank", "query", "-k", "5"]).unwrap();
1568
1569 match cli.command {
1570 Commands::Rerank { top_k, .. } => {
1571 assert_eq!(top_k, Some(5));
1572 }
1573 _ => panic!("Expected Rerank command"),
1574 }
1575 }
1576
1577 #[test]
1578 fn test_summarize_defaults() {
1579 let cli = parse_args(&["summarize"]).unwrap();
1580
1581 match cli.command {
1582 Commands::Summarize {
1583 input,
1584 model,
1585 min_length,
1586 max_length,
1587 num_beams,
1588 ..
1589 } => {
1590 assert!(input.is_none());
1591 assert_eq!(model, "distilbart-cnn");
1592 assert!(min_length.is_none());
1593 assert!(max_length.is_none());
1594 assert!(num_beams.is_none());
1595 }
1596 _ => panic!("Expected Summarize command"),
1597 }
1598 }
1599
1600 #[test]
1601 fn test_summarize_with_options() {
1602 let cli = parse_args(&[
1603 "summarize",
1604 "--input",
1605 "input.txt",
1606 "--min-length",
1607 "50",
1608 "--max-length",
1609 "200",
1610 "--num-beams",
1611 "4",
1612 "--length-penalty",
1613 "1.5",
1614 ])
1615 .unwrap();
1616
1617 match cli.command {
1618 Commands::Summarize {
1619 input,
1620 min_length,
1621 max_length,
1622 num_beams,
1623 length_penalty,
1624 ..
1625 } => {
1626 assert_eq!(input, Some("input.txt".to_string()));
1627 assert_eq!(min_length, Some(50));
1628 assert_eq!(max_length, Some(200));
1629 assert_eq!(num_beams, Some(4));
1630 assert_eq!(length_penalty, Some(1.5));
1631 }
1632 _ => panic!("Expected Summarize command"),
1633 }
1634 }
1635
1636 #[test]
1637 fn test_translate_defaults() {
1638 let cli = parse_args(&["translate"]).unwrap();
1639
1640 match cli.command {
1641 Commands::Translate {
1642 input,
1643 model,
1644 src,
1645 dst,
1646 ..
1647 } => {
1648 assert!(input.is_none());
1649 assert_eq!(model, "flan-t5-base");
1650 assert!(src.is_none());
1651 assert!(dst.is_none());
1652 }
1653 _ => panic!("Expected Translate command"),
1654 }
1655 }
1656
1657 #[test]
1658 fn test_translate_with_languages() {
1659 let cli = parse_args(&["translate", "--src", "en", "--dst", "is"]).unwrap();
1660
1661 match cli.command {
1662 Commands::Translate { src, dst, .. } => {
1663 assert_eq!(src, Some("en".to_string()));
1664 assert_eq!(dst, Some("is".to_string()));
1665 }
1666 _ => panic!("Expected Translate command"),
1667 }
1668 }
1669 #[test]
1670 fn test_transcribe_minimal() {
1671 let cli = parse_args(&["transcribe", "audio.wav"]).unwrap();
1672
1673 match cli.command {
1674 Commands::Transcribe {
1675 file,
1676 model,
1677 language,
1678 ..
1679 } => {
1680 assert_eq!(file, "audio.wav");
1681 assert_eq!(model, "whisper-small");
1682 assert!(language.is_none());
1683 }
1684 _ => panic!("Expected Transcribe command"),
1685 }
1686 }
1687
1688 #[test]
1689 fn test_classify_with_model() {
1690 let cli = parse_args(&["classify", "i hate mondays", "--model", "toxic-bert"]).unwrap();
1691 match cli.command {
1692 Commands::Classify { input, model, .. } => {
1693 assert_eq!(input, vec!["i hate mondays".to_string()]);
1694 assert_eq!(model, "toxic-bert");
1695 }
1696 _ => panic!("Expected Classify command"),
1697 }
1698 }
1699
1700 #[test]
1701 fn test_transcribe_with_options() {
1702 let cli = parse_args(&[
1703 "transcribe",
1704 "audio.mp3",
1705 "-m",
1706 "whisper-large-v3",
1707 "--language",
1708 "is",
1709 ])
1710 .unwrap();
1711
1712 match cli.command {
1713 Commands::Transcribe {
1714 file,
1715 model,
1716 language,
1717 ..
1718 } => {
1719 assert_eq!(file, "audio.mp3");
1720 assert_eq!(model, "whisper-large-v3");
1721 assert_eq!(language, Some("is".to_string()));
1722 }
1723 _ => panic!("Expected Transcribe command"),
1724 }
1725 }
1726}
1727
1728mod send_sync_tests {
1729 use kjarni::{
1730 Classifier, Embedder, Indexer, Reranker, Searcher, chat::Chat, generator::Generator,
1731 };
1732 const _: () = {
1734 const fn assert_send<T: Send>() {}
1735 const fn assert_sync<T: Sync>() {}
1736 assert_send::<Embedder>();
1737 assert_sync::<Embedder>();
1738
1739 assert_send::<Indexer>();
1740 assert_sync::<Indexer>();
1741
1742 assert_send::<Searcher>();
1743 assert_sync::<Searcher>();
1744
1745 assert_send::<Reranker>();
1746 assert_sync::<Reranker>();
1747
1748 assert_send::<Generator>();
1749 assert_sync::<Generator>();
1750
1751 assert_send::<Chat>();
1752 assert_sync::<Chat>();
1753
1754 assert_send::<Classifier>();
1755 assert_sync::<Classifier>();
1756 };
1757}