gemini_ai/
format.rs

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
use crate::Pair;

pub fn json(instruction: &str, text: &str, properties: &str, max_len: u64) -> String {
    let format: String = format!(
        r#"{{
    "system_instruction": {{
            "parts":
              {{ "text": "{}"}}
    }},
    "contents": [{{
      "parts":[
        {{"text": "{}"}}
        ]
}}],
    "generationConfig": {{
        "response_mime_type": "application/json",
        "response_schema": {{
          "type": "ARRAY",
          "items": {{
            "type": "OBJECT",
            "properties": {
              
            },
            "maxOutputTokens":{}
}}
}}
}}
}}"#,
        instruction, text, properties, max_len
    );
    format
}

pub fn text(system_instruction: &str, text: &str, max_len: u64) -> String {
    let content = format!(
        "{{ \"system_instruction\": {{
            \"parts\":
              {{ \"text\": \"{}\"}}
    }},
            \"contents\": {{
              \"parts\": {{
                \"text\": \"{}\"
                }}
    }},
    \"generationConfig\": {{
    \"maxOutputTokens\": {}
    }}  
    }}",
        system_instruction, text, max_len
    );
    println!("{}", content);
    content
}

pub fn image(instruction: &str, prompt: &str, image: &str, max_len: u64) -> String {
    let iamge_response = format!(
        "{{
    \"system_instruction\": {{
            \"parts\":
              {{ \"text\": \"{}\"}}
    }},
      \"contents\": [{{
        \"parts\":[
          {{\"text\": \"{}\"}},
          {{
            \"inline_data\": {{
              \"mime_type\":\"image/jpeg\",
              \"data\": \"{}\"
          }}
        }}
        ]
      }}],
      \"generationConfig\": {{
    \"maxOutputTokens\": {}
    }} 
    }}",
        instruction, prompt, image, max_len
    );
    iamge_response
}

pub fn video(prompt: &str, video_path: &str, mime_type: &str) -> String {
    let response = format!(
        "  {{\"contents\": [{{
        \"parts\":[
          {{\"text\": \"{}\"}},
          {{\"file_data\":{{\"mime_type\": \"{}\", \"file_uri\": \"{}\"}}}}
          ]
          }}]
          }}",
        prompt, mime_type, video_path
    );
    response
}

pub fn transcribe(prompt: &str, video_path: &str) -> String {
    let response = format!("  {{\"contents\": 
    [
    {{
    \"parts\":[
      {{\"text\": \"Transcribe the audio from this video, giving timestamps for salient events in the video. Also provide visual descriptions.and {}\"
      }},
      {{\"file_data\":{{\"mime_type\": \"video/mp4\", \"file_uri\": \"{}\"}}
      }}
      ]
    }}
    ]
      }}",prompt,video_path);

    // println!("{}", response);
    response
}

pub fn pdf(prompt: &str, path: &str) -> String {
    let pdf = format!(
        "{{
      \"contents\": [{{
        \"parts\":[
          {{\"text\": \"{}\"}},
          {{\"file_data\":{{\"mime_type\": \"application/pdf\", \"file_uri\": \"{}\"}}}}
          ]
}}]
}}",
        prompt, path
    );
    println!("{}", pdf);
    pdf
}

pub fn key(key: &str, r#type: &str) -> String {
    let key = format!("\"{}\":{{\"type\":\"{}\"}}", key, r#type);
    key
}
pub fn nested(key: &str, pair: &[Pair]) -> String {
    let mut pairs = Vec::new();
    pairs.push(pair);

    let mut parr = String::new();
    for par in pairs.iter() {
        for par in par.iter() {
            let pari = format!("\"{}\":{{\"type\":\"{}\"}},", par.key, par.r#type);
            parr.push_str(&pari);
        }
    }
    let parameter = parr.trim_end_matches(",");
    // println!("{}", parr);
    let pp = format!(
        "\"{}\":{{
  \"type\":\"OBJECT\",
  \"properties\": {{{

  }}}
  }}",
        key, parameter
    );
    // println!("{}", pp);
    pp
}

pub struct Train {
    train: Function,
}
pub struct Function {}

#[derive(Debug)]
pub struct Parameters {}
pub struct Properties {}

pub struct PropertiesParameter {}

pub fn function_call_format(instruction: &str, format: &str, text: &str) -> String {
    let response = format!(
        "{{
    \"system_instruction\": {{
      \"parts\": {{
        \"text\": \"{}\"
}}
}},
    \"tools\": [{}],

    \"tool_config\": {{
      \"function_calling_config\": {{\"mode\": \"none\"}}
}},

    \"contents\": {{
      \"role\": \"user\",
      \"parts\": {{
        \"text\": \"{}\"
}}
}}
}}",
        instruction, format, text
    );
    response
}

fn function_calling() {
    let response = format!(
        "{{
    \"function_declarations\": [
      {}
    ]
    }}",
        ""
    );
}

//  {{
//   \"name\": \"set_light_color\",
//   \"description\": \"Set the light color. Lights must be enabled for this to work.\",
//   \"parameters\": {{
//     \"type\": \"object\",
//     \"properties\": {{
//       \"rgb_hex\": {{
//         \"type\": \"string\",
//         \"description\": \"The light color as a 6-digit hex string, e.g. ff0000 for red.\"
// }}
// }},
//     \"required\": [
//       \"rgb_hex\"
//     ]
// }}
// }},

impl PropertiesParameter {
    pub fn new(key: &str, r#type: &str, description: &str) -> String {
        let mut keys = vec![];
        let mut types = vec![];
        let mut desrciptions = vec![];

        keys.push(key);
        types.push(r#type);
        desrciptions.push(description);

        let mut response = String::new();

        for (i, key) in keys.iter().enumerate() {
            let response_ = format!(
                "{{
                  \"{}\":{{
                  \"type\":\"{}\",
                  \"description\":\"{}\"
                    }}
                }}",
                keys[i], types[i], desrciptions[i]
            );
            response.push_str(&response_);
        }
        response
    }
}

impl Properties {
    pub fn parameter(r#type: &str, parameter: &str, required: Option<&[&str]>) -> String {
        let mut types = vec![];
        let mut propertiess = vec![];
        let mut requireds = vec![];
        types.push(r#type);
        propertiess.push(parameter);
        requireds.push(required);

        let mut responses = String::new();
        for (i, types) in types.iter().enumerate() {
            match requireds[i] {
                None => {
                    let responsee = format!(
                        "{{
                      \"type\":\"{}\",
                      \"properties\": {}
                    }}",
                        types, propertiess[i]
                    );
                    responses.push_str(&responsee);
                }
                Some(response) => {
                    // for response in re
                    let responsee = format!(
                        "{{
                  \"type\":\"object\",
                  \"properties\": {},
                  \"required\":{:?}
                }}",
                        propertiess[i], response
                    );
                    responses.push_str(&responsee);
                }
            }
        }
        responses
    }
}

impl Parameters {
    pub fn parameter(name: &str, description: &str, parameter: &str) -> String {
        let mut parameters: Vec<&str> = vec![];
        let mut names = vec![];
        let mut descriptions = vec![];

        parameters.push(parameter);
        names.push(name);
        descriptions.push(r#description);

        let mut response = String::new();

        for (i, key) in names.iter().enumerate() {
            let respons = format!(
                "{{
            \"name\":\"{}\",
            \"description\":\"{}\",
            \"parameters\":{}
            }}",
                names[i], descriptions[i], parameters[i]
            );
            response.push_str(&respons);
        }
        response
    }
}

impl Function {
    pub fn new(parameters: &[String]) -> String {
        let mut response = String::new();
        for parameter in parameters {
            let responsee = format!("{},", parameter);
            response.push_str(&responsee);
        }
        let parameters = response.trim_end_matches(",");
        let responsee = format!(
            "{{
        \"function_declarations\": [
          {}
        ]
        }}",
            parameters
        );
        responsee
        // println!("{}", responsee);
    }
}