gemini_ai/
format.rs

1use crate::Pair;
2
3pub fn json(instruction: &str, text: &str, properties: &str, max_len: u64) -> String {
4    let format: String = format!(
5        r#"{{
6    "system_instruction": {{
7            "parts":
8              {{ "text": "{}"}}
9    }},
10    "contents": [{{
11      "parts":[
12        {{"text": "{}"}}
13        ]
14}}],
15    "generationConfig": {{
16        "response_mime_type": "application/json",
17        "response_schema": {{
18          "type": "ARRAY",
19          "items": {{
20            "type": "OBJECT",
21            "properties": {{{
22              
23            }}}
24        }},
25        
26}}
27}}
28}}"#,
29        instruction, text, properties
30    );
31    format
32}
33
34pub fn text(system_instruction: &str, text: &str, max_len: u64) -> String {
35    let content = format!(
36        "{{ \"system_instruction\": {{
37            \"parts\":
38              {{ \"text\": \"{}\"}}
39    }},
40            \"contents\": {{
41              \"parts\": {{
42                \"text\": \"{}\"
43                }}
44    }},
45    \"generationConfig\": {{
46    \"maxOutputTokens\": {}
47    }}  
48    }}",
49        system_instruction, text, max_len
50    );
51    // println!("{}", content);
52    content
53}
54
55pub fn transcribe(prompt: &str, video_path: &str, max_len: u64) -> String {
56    let response = format!("  {{\"contents\": 
57    [
58    {{
59    \"parts\":[
60      {{\"text\": \"Transcribe the audio from this video, giving timestamps for salient events in the video. Also provide visual descriptions.and {}\"
61      }},
62      {{
63            \"inline_data\": {{
64              \"mime_type\":\"video/mp4\",
65              \"data\": \"{}\"
66          }}
67        }}
68      }}
69      ]
70    }}
71    ],
72          \"generationConfig\": {{
73    \"maxOutputTokens\": {}
74    }} 
75      }}",prompt,video_path,max_len);
76
77    // println!("{}", response);
78    response
79}
80
81pub fn schema(
82    instruction: &str,
83    prompt: &str,
84    mime_type: &str,
85    source: &str,
86    max_len: u64,
87) -> String {
88    let rag = format!(
89        r#"{{
90        "system_instruction": {{
91        "parts":{{ "text": "{}"}}
92}},
93"contents": [{{
94"parts":[
95  {{"text": "{}"}},
96  {{
97    "inline_data": {{
98      "mime_type":"{}",
99      "data": "{}"
100  }}
101}}
102  ]
103}}],
104      "generationConfig": {{
105"maxOutputTokens": {}
106}} 
107}}
108"#,
109        instruction, prompt, mime_type, &source, max_len
110    );
111    // println!("{}", rag);
112    rag
113}
114
115pub fn memory_schema(prompt: &str, parts: &str, max_len: u64) -> String {
116    let response = format!(
117        "{{ \"system_instruction\": {{
118            \"parts\":
119              {{ \"text\": \"{}\"}}
120    }},
121            \"contents\": [
122    {{
123      \"role\": \"user\",
124      \"parts\": [
125    {}
126      ]
127    }}
128    ],
129    \"generationConfig\": {{
130    \"maxOutputTokens\": {}
131    }}  
132    }}",
133        prompt, parts, max_len
134    );
135    response
136}
137pub fn search(instruction: &str, prompt: &str) -> String {
138    let search = format!(
139        r#"{{
140    "system_instruction": {{
141            "parts":{{ "text": "{}"}}
142    }},
143 "contents":
144        [{{
145          "parts":
146           [{{ 
147            "text": "{}" 
148            }}]
149        }}],
150      "tools": [{{
151      "google_search_retrieval": {{
152                  "dynamic_retrieval_config": {{
153                    "mode": "MODE_DYNAMIC",
154                    "dynamic_threshold": 1,
155        }}
156        }}
157        }}]
158    }}
159    "#,
160        instruction, prompt
161    );
162    search
163}
164
165pub fn training_model(
166    tuningmodename: &str,
167    model: &str,
168    batch: u64,
169    learning_rate: f64,
170    epoch: u64,
171    example: &str,
172) -> String {
173    let response = format!(
174        r#"
175    {{
176        "display_name": "{}",
177        "base_model": "{}",
178        "tuning_task": {{
179          "hyperparameters": {{
180            "batch_size": {},
181            "learning_rate": {},
182            "epoch_count":{},
183    }},
184          "training_data": {{
185            "examples": {{
186              "examples": [
187                {}
188              ]
189    }}
190    }}
191    }}
192    }}
193    "#,
194        tuningmodename, model, batch, learning_rate, epoch, example
195    );
196    response
197}
198
199pub fn key(key: &str, r#type: &str) -> String {
200    let key = format!("\"{}\":{{\"type\":\"{}\"}}", key, r#type);
201    key
202}
203pub fn nested(key: &str, pair: &[Pair]) -> String {
204    let mut pairs = Vec::new();
205    pairs.push(pair);
206
207    let mut parr = String::new();
208    for par in pairs.iter() {
209        for par in par.iter() {
210            let pari = format!("\"{}\":{{\"type\":\"{}\"}},", par.key, par.r#type);
211            parr.push_str(&pari);
212        }
213    }
214    let parameter = parr.trim_end_matches(",");
215    // println!("{}", parr);
216    let pp = format!(
217        "\"{}\":{{
218  \"type\":\"OBJECT\",
219  \"properties\": {{{
220
221  }}}
222  }}",
223        key, parameter
224    );
225    // println!("{}", pp);
226    pp
227}
228
229pub struct Train {
230    train: Function,
231}
232pub struct Function {}
233
234#[derive(Debug)]
235pub struct Parameters {}
236pub struct Properties {}
237
238pub struct PropertiesParameter {}
239
240pub fn function_call_format(instruction: &str, format: &str, text: &str) -> String {
241    let response = format!(
242        r#"{{
243            "system_instruction": {{
244              "parts": {{
245                "text": "{}"
246                }}
247            }},
248            "tools": [{{
249          "function_declarations": [
250          {}
251          ]
252        }}],
253        
254            "tool_config": {{
255              "function_calling_config": {{"mode": "none"}}
256            }},
257        
258            "contents": {{
259              "role": "user",
260              "parts": {{
261                "text": "{}"
262            }}
263        }}
264    }}
265                "#,
266        instruction, format, text
267    );
268    response
269}
270
271fn function_calling() {
272    let response = format!(
273        "{{
274    \"function_declarations\": [
275      {}
276    ]
277    }}",
278        ""
279    );
280}
281
282//  {{
283//   \"name\": \"set_light_color\",
284//   \"description\": \"Set the light color. Lights must be enabled for this to work.\",
285//   \"parameters\": {{
286//     \"type\": \"object\",
287//     \"properties\": {{
288//       \"rgb_hex\": {{
289//         \"type\": \"string\",
290//         \"description\": \"The light color as a 6-digit hex string, e.g. ff0000 for red.\"
291// }}
292// }},
293//     \"required\": [
294//       \"rgb_hex\"
295//     ]
296// }}
297// }},
298
299impl PropertiesParameter {
300    pub fn new(key: &str, r#type: &str, description: &str) -> String {
301        let mut keys = vec![];
302        let mut types = vec![];
303        let mut desrciptions = vec![];
304
305        keys.push(key);
306        types.push(r#type);
307        desrciptions.push(description);
308
309        let mut response = String::new();
310
311        for (i, key) in keys.iter().enumerate() {
312            let response_ = format!(
313                "{{
314                  \"{}\":{{
315                  \"type\":\"{}\",
316                  \"description\":\"{}\"
317                    }}
318                }}",
319                keys[i], types[i], desrciptions[i]
320            );
321            response.push_str(&response_);
322        }
323        response
324    }
325}
326
327impl Properties {
328    pub fn parameter(r#type: &str, parameter: &str, required: Option<&[&str]>) -> String {
329        let mut types = vec![];
330        let mut propertiess = vec![];
331        let mut requireds = vec![];
332        types.push(r#type);
333        propertiess.push(parameter);
334        requireds.push(required);
335
336        let mut responses = String::new();
337        for (i, types) in types.iter().enumerate() {
338            match requireds[i] {
339                None => {
340                    let responsee = format!(
341                        "{{
342                      \"type\":\"{}\",
343                      \"properties\": {}
344                    }}",
345                        types, propertiess[i]
346                    );
347                    responses.push_str(&responsee);
348                }
349                Some(response) => {
350                    // for response in re
351                    let responsee = format!(
352                        "{{
353                  \"type\":\"object\",
354                  \"properties\": {},
355                  \"required\":{:?}
356                }}",
357                        propertiess[i], response
358                    );
359                    responses.push_str(&responsee);
360                }
361            }
362        }
363        responses
364    }
365}
366
367impl Parameters {
368    pub fn parameter(name: &str, description: &str, parameter: &str) -> String {
369        let mut parameters: Vec<&str> = vec![];
370        let mut names = vec![];
371        let mut descriptions = vec![];
372
373        parameters.push(parameter);
374        names.push(name);
375        descriptions.push(r#description);
376
377        let mut response = String::new();
378
379        for (i, key) in names.iter().enumerate() {
380            let respons = format!(
381                "{{
382            \"name\":\"{}\",
383            \"description\":\"{}\",
384            \"parameters\":{}
385            }}",
386                names[i], descriptions[i], parameters[i]
387            );
388            response.push_str(&respons);
389        }
390        response
391    }
392}
393
394impl Function {
395    pub fn new(parameters: &[String]) -> String {
396        let mut response = String::new();
397        for parameter in parameters {
398            let responsee = format!("{},", parameter);
399            response.push_str(&responsee);
400        }
401        let parameters = response.trim_end_matches(",");
402        let responsee = format!(
403            "{{
404        \"function_declarations\": [
405          {}
406        ]
407        }}",
408            parameters
409        );
410        responsee
411        // println!("{}", responsee);
412    }
413}