cognis 0.2.0

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

use std::collections::HashMap;

use async_trait::async_trait;
use serde_json::Value;

use cognis_core::embeddings::Embeddings;
use cognis_core::error::{CognisError, Result};
use cognis_core::runnables::base::Runnable;
use cognis_core::runnables::config::RunnableConfig;

/// A single example represented as a map of field names to values.
pub type Example = HashMap<String, String>;

/// Trait for dynamically selecting examples based on the input.
pub trait ExampleSelector: Send + Sync {
    /// Select the most relevant examples for the given input.
    fn select_examples(&self, input: &str) -> Vec<Example>;

    /// Add an example to the selector's pool.
    fn add_example(&mut self, example: Example);
}

/// Selects examples by semantic similarity using an embeddings model.
///
/// Stores examples alongside their pre-computed embedding vectors and returns
/// the top-k most similar examples to the query at format time.
pub struct SemanticSimilaritySelector {
    embeddings: Box<dyn Embeddings>,
    examples: Vec<Example>,
    example_embeddings: Vec<Vec<f32>>,
    /// The key in each example whose value is embedded for comparison.
    input_key: String,
    k: usize,
    /// Cached runtime for blocking on async embed calls within the sync trait.
    runtime: tokio::runtime::Runtime,
}

impl SemanticSimilaritySelector {
    /// Create a new builder for `SemanticSimilaritySelector`.
    pub fn builder(embeddings: Box<dyn Embeddings>) -> SemanticSimilaritySelectorBuilder {
        SemanticSimilaritySelectorBuilder {
            embeddings,
            examples: Vec::new(),
            input_key: "input".to_string(),
            k: 3,
        }
    }
}

/// Builder for [`SemanticSimilaritySelector`].
pub struct SemanticSimilaritySelectorBuilder {
    embeddings: Box<dyn Embeddings>,
    examples: Vec<Example>,
    input_key: String,
    k: usize,
}

impl SemanticSimilaritySelectorBuilder {
    /// Set the number of examples to return.
    pub fn k(mut self, k: usize) -> Self {
        self.k = k;
        self
    }

    /// Set the key in each example whose value is used for embedding comparison.
    pub fn input_key(mut self, key: impl Into<String>) -> Self {
        self.input_key = key.into();
        self
    }

    /// Provide initial examples.
    pub fn examples(mut self, examples: Vec<Example>) -> Self {
        self.examples = examples;
        self
    }

    /// Build the selector, computing embeddings for all initial examples.
    pub fn build(self) -> SemanticSimilaritySelector {
        let runtime = tokio::runtime::Runtime::new()
            .expect("failed to create tokio runtime for SemanticSimilaritySelector");

        let texts: Vec<String> = self
            .examples
            .iter()
            .map(|ex| ex.get(&self.input_key).cloned().unwrap_or_default())
            .collect();

        let example_embeddings = if texts.is_empty() {
            Vec::new()
        } else {
            runtime
                .block_on(self.embeddings.embed_documents(texts))
                .unwrap_or_default()
        };

        SemanticSimilaritySelector {
            embeddings: self.embeddings,
            examples: self.examples,
            example_embeddings,
            input_key: self.input_key,
            k: self.k,
            runtime,
        }
    }
}

/// Compute cosine similarity between two vectors.
fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
    let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
    let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
    let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();
    if norm_a == 0.0 || norm_b == 0.0 {
        return 0.0;
    }
    dot / (norm_a * norm_b)
}

impl ExampleSelector for SemanticSimilaritySelector {
    fn select_examples(&self, input: &str) -> Vec<Example> {
        let query_embedding = self
            .runtime
            .block_on(self.embeddings.embed_query(input))
            .unwrap_or_default();

        let mut scored: Vec<(usize, f32)> = self
            .example_embeddings
            .iter()
            .enumerate()
            .map(|(i, emb)| (i, cosine_similarity(&query_embedding, emb)))
            .collect();

        scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        scored
            .into_iter()
            .take(self.k)
            .map(|(i, _)| self.examples[i].clone())
            .collect()
    }

    fn add_example(&mut self, example: Example) {
        let text = example.get(&self.input_key).cloned().unwrap_or_default();
        let embedding = self
            .runtime
            .block_on(self.embeddings.embed_query(&text))
            .unwrap_or_default();
        self.examples.push(example);
        self.example_embeddings.push(embedding);
    }
}

/// Selects examples that fit within a character-length budget.
///
/// Examples are added in order until the cumulative formatted length would
/// exceed the configured maximum.
pub struct LengthBasedSelector {
    examples: Vec<Example>,
    max_length: usize,
    /// Template used to format each example for length measurement.
    example_template: String,
}

impl LengthBasedSelector {
    /// Create a new builder for `LengthBasedSelector`.
    pub fn builder() -> LengthBasedSelectorBuilder {
        LengthBasedSelectorBuilder {
            examples: Vec::new(),
            max_length: 1000,
            example_template: String::new(),
        }
    }

    /// Format a single example using the example template.
    fn format_example(&self, example: &Example) -> String {
        if self.example_template.is_empty() {
            // Fallback: concatenate key=value pairs
            example
                .iter()
                .map(|(k, v)| format!("{}: {}", k, v))
                .collect::<Vec<_>>()
                .join("\n")
        } else {
            format_template_simple(&self.example_template, example)
        }
    }
}

/// Builder for [`LengthBasedSelector`].
pub struct LengthBasedSelectorBuilder {
    examples: Vec<Example>,
    max_length: usize,
    example_template: String,
}

impl LengthBasedSelectorBuilder {
    /// Set the maximum character length budget.
    pub fn max_length(mut self, max_length: usize) -> Self {
        self.max_length = max_length;
        self
    }

    /// Provide initial examples.
    pub fn examples(mut self, examples: Vec<Example>) -> Self {
        self.examples = examples;
        self
    }

    /// Set the template used to format examples for length measurement.
    pub fn example_template(mut self, template: impl Into<String>) -> Self {
        self.example_template = template.into();
        self
    }

    /// Build the selector.
    pub fn build(self) -> LengthBasedSelector {
        LengthBasedSelector {
            examples: self.examples,
            max_length: self.max_length,
            example_template: self.example_template,
        }
    }
}

impl ExampleSelector for LengthBasedSelector {
    fn select_examples(&self, _input: &str) -> Vec<Example> {
        let mut selected = Vec::new();
        let mut total_length = 0;

        for example in &self.examples {
            let formatted = self.format_example(example);
            let new_length = total_length + formatted.len();
            if new_length > self.max_length {
                break;
            }
            total_length = new_length;
            selected.push(example.clone());
        }

        selected
    }

    fn add_example(&mut self, example: Example) {
        self.examples.push(example);
    }
}

/// A few-shot prompt template that formats examples into the prompt.
///
/// Supports both static examples and dynamic selection via an
/// [`ExampleSelector`]. The final prompt is assembled as:
///
/// ```text
/// {prefix}
/// {example_separator}
/// {formatted_example_1}
/// {example_separator}
/// {formatted_example_2}
/// {example_separator}
/// {suffix}  (with input variables substituted)
/// ```
pub struct FewShotPromptTemplate {
    /// Text before the examples.
    pub prefix: String,
    /// Text after the examples (usually contains `{input}` or similar placeholders).
    pub suffix: String,
    /// Template for formatting each individual example.
    pub example_template: String,
    /// Separator placed between formatted examples.
    pub example_separator: String,
    /// Dynamic example selector (takes precedence over static examples).
    pub selector: Option<Box<dyn ExampleSelector>>,
    /// Static examples (used when no selector is set).
    pub examples: Option<Vec<Example>>,
    /// The key used to look up the input text for the selector.
    pub input_key: String,
}

impl FewShotPromptTemplate {
    /// Create a new builder for `FewShotPromptTemplate`.
    pub fn builder() -> FewShotPromptTemplateBuilder {
        FewShotPromptTemplateBuilder {
            prefix: String::new(),
            suffix: String::new(),
            example_template: String::new(),
            example_separator: "\n\n".to_string(),
            selector: None,
            examples: None,
            input_key: "input".to_string(),
        }
    }

    /// Format the few-shot prompt with the given input variables.
    pub fn format(&self, input_variables: &HashMap<String, String>) -> Result<String> {
        let examples = self.get_examples(input_variables)?;
        let formatted_examples: Vec<String> = examples
            .iter()
            .map(|ex| format_template_simple(&self.example_template, ex))
            .collect();

        let mut parts = Vec::new();

        if !self.prefix.is_empty() {
            parts.push(self.prefix.clone());
        }

        if !formatted_examples.is_empty() {
            parts.push(formatted_examples.join(&self.example_separator));
        }

        if !self.suffix.is_empty() {
            let rendered_suffix = format_template_simple(&self.suffix, input_variables);
            parts.push(rendered_suffix);
        }

        Ok(parts.join(&self.example_separator))
    }

    /// Get the examples to use, either from the selector or the static list.
    fn get_examples(&self, input_variables: &HashMap<String, String>) -> Result<Vec<Example>> {
        if let Some(ref selector) = self.selector {
            let input_text = input_variables
                .get(&self.input_key)
                .cloned()
                .unwrap_or_default();
            Ok(selector.select_examples(&input_text))
        } else if let Some(ref examples) = self.examples {
            Ok(examples.clone())
        } else {
            Ok(Vec::new())
        }
    }
}

#[async_trait]
impl Runnable for FewShotPromptTemplate {
    fn name(&self) -> &str {
        "FewShotPromptTemplate"
    }

    async fn invoke(&self, input: Value, _config: Option<&RunnableConfig>) -> Result<Value> {
        let kwargs: HashMap<String, String> = match input {
            Value::Object(map) => map
                .into_iter()
                .map(|(k, v)| {
                    let s = match v {
                        Value::String(s) => s,
                        other => other.to_string(),
                    };
                    (k, s)
                })
                .collect(),
            _ => {
                return Err(CognisError::TypeMismatch {
                    expected: "Object".into(),
                    got: "non-Object".into(),
                });
            }
        };
        let text = self.format(&kwargs)?;
        Ok(Value::String(text))
    }
}

/// Builder for [`FewShotPromptTemplate`].
pub struct FewShotPromptTemplateBuilder {
    prefix: String,
    suffix: String,
    example_template: String,
    example_separator: String,
    selector: Option<Box<dyn ExampleSelector>>,
    examples: Option<Vec<Example>>,
    input_key: String,
}

impl FewShotPromptTemplateBuilder {
    /// Set the prefix text that appears before examples.
    pub fn prefix(mut self, prefix: impl Into<String>) -> Self {
        self.prefix = prefix.into();
        self
    }

    /// Set the suffix text that appears after examples.
    pub fn suffix(mut self, suffix: impl Into<String>) -> Self {
        self.suffix = suffix.into();
        self
    }

    /// Set the template for formatting each example.
    pub fn example_template(mut self, template: impl Into<String>) -> Self {
        self.example_template = template.into();
        self
    }

    /// Set the separator placed between formatted examples.
    pub fn example_separator(mut self, separator: impl Into<String>) -> Self {
        self.example_separator = separator.into();
        self
    }

    /// Set a dynamic example selector.
    pub fn selector(mut self, selector: Box<dyn ExampleSelector>) -> Self {
        self.selector = Some(selector);
        self
    }

    /// Set static examples.
    pub fn examples(mut self, examples: Vec<Example>) -> Self {
        self.examples = Some(examples);
        self
    }

    /// Set the input key used to look up the query text for the selector.
    pub fn input_key(mut self, key: impl Into<String>) -> Self {
        self.input_key = key.into();
        self
    }

    /// Build the `FewShotPromptTemplate`.
    pub fn build(self) -> FewShotPromptTemplate {
        FewShotPromptTemplate {
            prefix: self.prefix,
            suffix: self.suffix,
            example_template: self.example_template,
            example_separator: self.example_separator,
            selector: self.selector,
            examples: self.examples,
            input_key: self.input_key,
        }
    }
}

/// Simple template string formatting: replaces `{key}` with values from the map.
/// Unknown placeholders are left as-is.
fn format_template_simple(template: &str, variables: &HashMap<String, String>) -> String {
    let mut result = String::with_capacity(template.len());
    let mut chars = template.chars().peekable();

    while let Some(ch) = chars.next() {
        if ch == '{' {
            if chars.peek() == Some(&'{') {
                chars.next();
                result.push('{');
                continue;
            }
            let mut name = String::new();
            for inner in chars.by_ref() {
                if inner == '}' {
                    break;
                }
                name.push(inner);
            }
            if let Some(value) = variables.get(&name) {
                result.push_str(value);
            } else {
                // Leave placeholder as-is if not found
                result.push('{');
                result.push_str(&name);
                result.push('}');
            }
        } else if ch == '}' {
            if chars.peek() == Some(&'}') {
                chars.next();
                result.push('}');
            } else {
                result.push('}');
            }
        } else {
            result.push(ch);
        }
    }
    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use cognis_core::embeddings_fake::DeterministicFakeEmbedding;

    fn make_example(input: &str, output: &str) -> Example {
        let mut ex = HashMap::new();
        ex.insert("input".to_string(), input.to_string());
        ex.insert("output".to_string(), output.to_string());
        ex
    }

    // ---------- Static examples formatting ----------

    #[test]
    fn test_static_examples_formatting() {
        let template = FewShotPromptTemplate::builder()
            .prefix("Translate English to French:")
            .suffix("Input: {input}\nOutput:")
            .example_template("Input: {input}\nOutput: {output}")
            .examples(vec![
                make_example("hello", "bonjour"),
                make_example("goodbye", "au revoir"),
            ])
            .build();

        let mut vars = HashMap::new();
        vars.insert("input".to_string(), "thank you".to_string());

        let result = template.format(&vars).unwrap();
        assert!(result.contains("Translate English to French:"));
        assert!(result.contains("Input: hello\nOutput: bonjour"));
        assert!(result.contains("Input: goodbye\nOutput: au revoir"));
        assert!(result.contains("Input: thank you\nOutput:"));
    }

    // ---------- Dynamic example selection with SemanticSimilaritySelector ----------

    #[test]
    fn test_semantic_similarity_selector() {
        let embeddings = Box::new(DeterministicFakeEmbedding::new(64));

        let examples = vec![
            make_example("cat", "chat"),
            make_example("dog", "chien"),
            make_example("house", "maison"),
            make_example("car", "voiture"),
            make_example("tree", "arbre"),
        ];

        let selector = SemanticSimilaritySelector::builder(embeddings)
            .k(2)
            .examples(examples)
            .build();

        let selected = selector.select_examples("cat");
        assert_eq!(selected.len(), 2);
        // The first result for "cat" input should be the "cat" example itself
        // since it has identical embedding.
        assert_eq!(selected[0].get("input").unwrap(), "cat");
    }

    #[test]
    fn test_semantic_selector_with_few_shot_template() {
        let embeddings = Box::new(DeterministicFakeEmbedding::new(64));

        let examples = vec![
            make_example("hello", "bonjour"),
            make_example("goodbye", "au revoir"),
            make_example("thank you", "merci"),
            make_example("please", "s'il vous plait"),
        ];

        let selector = SemanticSimilaritySelector::builder(embeddings)
            .k(2)
            .examples(examples)
            .build();

        let template = FewShotPromptTemplate::builder()
            .prefix("Translate English to French:")
            .suffix("Input: {input}\nOutput:")
            .example_template("Input: {input}\nOutput: {output}")
            .selector(Box::new(selector))
            .build();

        let mut vars = HashMap::new();
        vars.insert("input".to_string(), "hello".to_string());

        let result = template.format(&vars).unwrap();
        assert!(result.contains("Translate English to French:"));
        // Should contain the "hello" example since it matches exactly
        assert!(result.contains("Input: hello\nOutput: bonjour"));
        assert!(result.contains("Input: hello\nOutput:"));
    }

    // ---------- LengthBasedSelector respects budget ----------

    #[test]
    fn test_length_based_selector_respects_budget() {
        let examples = vec![
            make_example("a", "1"),       // "Input: a\nOutput: 1" = 20 chars
            make_example("bb", "22"),     // "Input: bb\nOutput: 22" = 22 chars
            make_example("ccc", "333"),   // "Input: ccc\nOutput: 333" = 24 chars
            make_example("dddd", "4444"), // won't fit
        ];

        let selector = LengthBasedSelector::builder()
            .examples(examples)
            .example_template("Input: {input}\nOutput: {output}")
            .max_length(66)
            .build();

        let selected = selector.select_examples("anything");
        assert_eq!(selected.len(), 3);
        assert_eq!(selected[0].get("input").unwrap(), "a");
        assert_eq!(selected[1].get("input").unwrap(), "bb");
        assert_eq!(selected[2].get("input").unwrap(), "ccc");
    }

    #[test]
    fn test_length_based_selector_empty_when_budget_zero() {
        let examples = vec![make_example("a", "1")];

        let selector = LengthBasedSelector::builder()
            .examples(examples)
            .example_template("Input: {input}\nOutput: {output}")
            .max_length(0)
            .build();

        let selected = selector.select_examples("anything");
        assert!(selected.is_empty());
    }

    // ---------- Custom example template ----------

    #[test]
    fn test_custom_example_template() {
        let template = FewShotPromptTemplate::builder()
            .prefix("Examples:")
            .suffix("Q: {input}\nA:")
            .example_template("Q: {input}\nA: {output}")
            .examples(vec![
                make_example("What is 1+1?", "2"),
                make_example("What is 2+2?", "4"),
            ])
            .build();

        let mut vars = HashMap::new();
        vars.insert("input".to_string(), "What is 3+3?".to_string());

        let result = template.format(&vars).unwrap();
        assert!(result.contains("Q: What is 1+1?\nA: 2"));
        assert!(result.contains("Q: What is 2+2?\nA: 4"));
        assert!(result.contains("Q: What is 3+3?\nA:"));
    }

    // ---------- Custom separator ----------

    #[test]
    fn test_custom_separator() {
        let template = FewShotPromptTemplate::builder()
            .prefix("Start")
            .suffix("End")
            .example_template("{input} -> {output}")
            .example_separator("\n---\n")
            .examples(vec![make_example("a", "1"), make_example("b", "2")])
            .build();

        let vars = HashMap::new();
        let result = template.format(&vars).unwrap();
        assert_eq!(result, "Start\n---\na -> 1\n---\nb -> 2\n---\nEnd");
    }

    // ---------- Prefix and suffix formatting ----------

    #[test]
    fn test_prefix_and_suffix_formatting() {
        let template = FewShotPromptTemplate::builder()
            .prefix("You are a helpful assistant.")
            .suffix("User: {input}\nAssistant:")
            .example_template("User: {input}\nAssistant: {output}")
            .examples(vec![make_example("Hi", "Hello!")])
            .build();

        let mut vars = HashMap::new();
        vars.insert("input".to_string(), "How are you?".to_string());

        let result = template.format(&vars).unwrap();
        assert!(result.starts_with("You are a helpful assistant."));
        assert!(result.ends_with("User: How are you?\nAssistant:"));
    }

    // ---------- Empty examples ----------

    #[test]
    fn test_empty_examples() {
        let template = FewShotPromptTemplate::builder()
            .prefix("Prefix")
            .suffix("Suffix: {input}")
            .example_template("{input} -> {output}")
            .examples(vec![])
            .build();

        let mut vars = HashMap::new();
        vars.insert("input".to_string(), "test".to_string());

        let result = template.format(&vars).unwrap();
        assert_eq!(result, "Prefix\n\nSuffix: test");
    }

    #[test]
    fn test_no_examples_no_selector() {
        let template = FewShotPromptTemplate::builder()
            .prefix("Prefix")
            .suffix("Suffix")
            .example_template("{input}")
            .build();

        let vars = HashMap::new();
        let result = template.format(&vars).unwrap();
        assert_eq!(result, "Prefix\n\nSuffix");
    }

    // ---------- Runnable trait implementation ----------

    #[tokio::test]
    async fn test_runnable_trait_implementation() {
        let template = FewShotPromptTemplate::builder()
            .prefix("Examples:")
            .suffix("Input: {input}\nOutput:")
            .example_template("Input: {input}\nOutput: {output}")
            .examples(vec![make_example("hi", "hello")])
            .build();

        let result = template
            .invoke(serde_json::json!({"input": "hey"}), None)
            .await
            .unwrap();

        match result {
            Value::String(s) => {
                assert!(s.contains("Examples:"));
                assert!(s.contains("Input: hi\nOutput: hello"));
                assert!(s.contains("Input: hey\nOutput:"));
            }
            _ => panic!("Expected string output from Runnable::invoke"),
        }
    }

    #[tokio::test]
    async fn test_runnable_invoke_type_error() {
        let template = FewShotPromptTemplate::builder()
            .prefix("P")
            .suffix("S")
            .example_template("{input}")
            .build();

        let result = template
            .invoke(serde_json::json!("not an object"), None)
            .await;
        assert!(result.is_err());
    }

    // ---------- Add example to selector ----------

    #[test]
    fn test_add_example_to_selector() {
        let embeddings = Box::new(DeterministicFakeEmbedding::new(32));

        let mut selector = SemanticSimilaritySelector::builder(embeddings)
            .k(5)
            .examples(vec![make_example("cat", "chat")])
            .build();

        assert_eq!(selector.examples.len(), 1);

        selector.add_example(make_example("dog", "chien"));
        assert_eq!(selector.examples.len(), 2);
        assert_eq!(selector.example_embeddings.len(), 2);

        let selected = selector.select_examples("dog");
        assert!(!selected.is_empty());
    }

    #[test]
    fn test_add_example_to_length_selector() {
        let mut selector = LengthBasedSelector::builder()
            .max_length(1000)
            .example_template("{input} -> {output}")
            .build();

        selector.add_example(make_example("a", "1"));
        selector.add_example(make_example("b", "2"));

        let selected = selector.select_examples("anything");
        assert_eq!(selected.len(), 2);
    }

    // ---------- Few-shot with input variables in suffix ----------

    #[test]
    fn test_suffix_with_multiple_input_variables() {
        let template = FewShotPromptTemplate::builder()
            .prefix("Translate {source_lang} to {target_lang}:")
            .suffix("Input ({source_lang}): {input}\nOutput ({target_lang}):")
            .example_template("Input: {input}\nOutput: {output}")
            .examples(vec![make_example("hello", "bonjour")])
            .build();

        let mut vars = HashMap::new();
        vars.insert("input".to_string(), "goodbye".to_string());
        vars.insert("source_lang".to_string(), "English".to_string());
        vars.insert("target_lang".to_string(), "French".to_string());

        let result = template.format(&vars).unwrap();
        // Prefix doesn't get variable substitution (it's a plain string),
        // but suffix does.
        assert!(result.contains("Input (English): goodbye"));
        assert!(result.contains("Output (French):"));
    }

    // ---------- Builder default separator ----------

    #[test]
    fn test_default_separator_is_double_newline() {
        let template = FewShotPromptTemplate::builder()
            .prefix("P")
            .suffix("S")
            .example_template("{input}")
            .examples(vec![make_example("a", "1")])
            .build();

        assert_eq!(template.example_separator, "\n\n");
    }
}