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
use std::sync::Arc;

use sllm::message::{MessageBuilder, PromptMessageGroup};

pub mod sync;
pub mod units;

mod error;
mod pipeline_net;
mod prompt_manager;
mod traits;

pub use error::Error;
pub use pipeline_net::PipelineNet;
pub use prompt_manager::PromptManager;
pub use sllm::Backend;
pub use traits::*;

pub trait ToKeywordString {
    fn to_keyword_string() -> String;
}

pub mod prelude {
    pub use super::ToKeywordString;
    pub use ai_agent_macro::*;
    pub use sllm::message::{MessageBuilder, PromptMessageGroup};
}

#[derive(Debug, Clone)]
pub enum ModuleParam {
    Str(String),
    MessageBuilders(Vec<PromptMessageGroup>),
    None,
}

impl ModuleParam {
    pub fn is_none(&self) -> bool {
        match self {
            Self::None => true,
            _ => false,
        }
    }

    pub fn into_message_group(self) -> Option<Vec<PromptMessageGroup>> {
        match self {
            Self::MessageBuilders(group) => Some(group),
            _ => None,
        }
    }

    pub fn into_string(self) -> Option<String> {
        match self {
            Self::Str(s) => Some(s),
            _ => None,
        }
    }

    pub fn as_message_group(&self) -> Option<&Vec<PromptMessageGroup>> {
        match self {
            Self::MessageBuilders(group) => Some(group),
            _ => None,
        }
    }

    pub fn as_string(&self) -> Option<&String> {
        match self {
            Self::Str(s) => Some(s),
            _ => None,
        }
    }
}

impl Default for ModuleParam {
    fn default() -> Self {
        Self::None
    }
}

impl From<&str> for ModuleParam {
    fn from(val: &str) -> Self {
        ModuleParam::Str(val.into())
    }
}

impl From<Vec<PromptMessageGroup>> for ModuleParam {
    fn from(val: Vec<PromptMessageGroup>) -> Self {
        ModuleParam::MessageBuilders(val)
    }
}

impl From<String> for ModuleParam {
    fn from(val: String) -> Self {
        ModuleParam::Str(val)
    }
}

//
// Model Wrapper
#[derive(Debug, Clone)]
pub struct Model {
    model: Arc<sync::Mutex<sllm::Model>>,
}

impl Model {
    pub fn new(backend: Backend) -> Result<Self, Error> {
        let model = sllm::Model::new(backend)?;
        Ok(Self {
            model: Arc::new(sync::Mutex::new(model)),
        })
    }

    pub async fn set_temperature(&self, temperature: f64) {
        let mut model = self.model.lock().await;
        model.set_temperature(temperature);
    }

    pub async fn generate_response<T>(&self, input: T) -> Result<String, Error>
    where
        T: IntoIterator + Send,
        T::Item: MessageBuilder + Send,
    {
        let model = self.model.lock().await;
        let result = model.generate_response(input).await?;
        Ok(result)
    }
}

// pub use sllm;

#[cfg(test)]
mod tests {
    use super::Model;

    pub fn get_model() -> Model {
        dotenv::dotenv().ok();
        Model::new(sllm::Backend::ChatGPT {
            api_key: std::env::var("OPEN_API_KEY").unwrap(),
            model: "gpt-3.5-turbo".into(),
        })
        .unwrap()
    }

    use super::ToKeywordString;
    use ai_agent_macro::KeywordString;

    #[allow(dead_code)]
    #[derive(KeywordString)]
    struct SubStruct {
        prop1: i32,
        prop2: f32,
        prop3: String,
    }

    #[allow(dead_code)]
    #[derive(KeywordString)]
    struct TestStruct {
        sub: SubStruct,
        prop: Vec<SubStruct>,
    }

    #[ignore]
    #[test]
    fn test_print_keyword() {
        assert_eq!(
            TestStruct::to_keyword_string(),
            "{sub{prop1, prop2, prop3}, prop[{prop1, prop2, prop3}]}"
        );
    }
}