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
mod chat;
mod error;
mod prompt;

use std::collections::HashMap;

pub use chat::*;
pub use error::*;
pub use prompt::*;
use serde_json::Value;

use crate::schemas::{messages::Message, prompt::PromptValue};

// pub type PromptArgs<'a> = HashMap<&'a str, &'a str>;
pub type PromptArgs = HashMap<String, Value>;
pub trait PromptFromatter: Send + Sync {
    fn template(&self) -> String;
    fn variables(&self) -> Vec<String>;
    fn format(&self, input_variables: PromptArgs) -> Result<String, PromptError>;
}
impl<PA> From<PA> for Box<dyn PromptFromatter>
where
    PA: PromptFromatter + 'static,
{
    fn from(prompt: PA) -> Self {
        Box::new(prompt)
    }
}

/// Represents a generic template for formatting messages.
pub trait MessageFormatter: Send + Sync {
    fn format_messages(&self, input_variables: PromptArgs) -> Result<Vec<Message>, PromptError>;

    /// Returns a list of required input variable names for the template.
    fn input_variables(&self) -> Vec<String>;
}
impl<MF> From<MF> for Box<dyn MessageFormatter>
where
    MF: MessageFormatter + 'static,
{
    fn from(prompt: MF) -> Self {
        Box::new(prompt)
    }
}

pub trait FormatPrompter: Send + Sync {
    fn format_prompt(&self, input_variables: PromptArgs) -> Result<PromptValue, PromptError>;
    fn get_input_variables(&self) -> Vec<String>;
}
impl<FP> From<FP> for Box<dyn FormatPrompter>
where
    FP: FormatPrompter + 'static,
{
    fn from(prompt: FP) -> Self {
        Box::new(prompt)
    }
}