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