llmclient 0.3.2

Rust LLM client - Gemini, OpenAI, Claude, Mistral, DeepSeek, Groq
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
use serde_derive::Deserialize;
use reqwest::Client;
use reqwest::header::{HeaderMap, HeaderValue};
use crate::gemini::GeminiCompletion;
use crate::gpt::GptCompletion;
use crate::mistral::MistralCompletion;
use crate::claude::ClaudeCompletion;
use crate::deepseek::DeepseekCompletion;
use crate::groq::GroqCompletion;
use crate::functions::{Function, get_function_json};

#[allow(non_camel_case_types)]
#[derive(Debug, Clone, PartialEq)]
pub enum LlmType  {
    GEMINI,
    GPT,
    CLAUDE,
    MISTRAL,
    DEEPSEEK,
    GROQ,
    GEMINI_ERROR,
    GPT_ERROR,
    CLAUDE_ERROR,
    MISTRAL_ERROR,
    DEEPSEEK_ERROR,
    GROQ_ERROR,
    GEMINI_TOOLS,
    GPT_TOOLS,
    CLAUDE_TOOLS,
    MISTRAL_TOOLS,
    DEEPSEEK_TOOLS,
    GROQ_TOOLS,
}

pub type Triple = (usize, usize, usize);

impl std::fmt::Display for LlmType {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            LlmType::GEMINI => write!(f, "GEMINI"),
            LlmType::GPT => write!(f, "GPT"),
            LlmType::CLAUDE => write!(f, "CLAUDE"),
            LlmType::MISTRAL => write!(f, "MISTRAL"),
            LlmType::DEEPSEEK => write!(f, "DEEPSEEK"),
            LlmType::GROQ => write!(f, "GROQ"),
            LlmType::GEMINI_ERROR => write!(f, "GEMINI_ERROR"),
            LlmType::GPT_ERROR => write!(f, "GPT_ERROR"),
            LlmType::CLAUDE_ERROR => write!(f, "CLAUDE_ERROR"),
            LlmType::MISTRAL_ERROR => write!(f, "MISTRAL_ERROR"),
            LlmType::DEEPSEEK_ERROR => write!(f, "DEEPSEEK_ERROR"),
            LlmType::GROQ_ERROR => write!(f, "GROQ_ERROR"),
            LlmType::GEMINI_TOOLS => write!(f, "GEMINI_TOOLS"),
            LlmType::GPT_TOOLS => write!(f, "GPT_TOOLS"),
            LlmType::CLAUDE_TOOLS => write!(f, "CLAUDE_TOOLS"),
            LlmType::MISTRAL_TOOLS => write!(f, "MISTRAL_TOOLS"),
            LlmType::DEEPSEEK_TOOLS => write!(f, "DEEPSEEK_TOOLS"),
            LlmType::GROQ_TOOLS => write!(f, "GROQ_TOOLS"),
        }
    }
}

#[derive(Debug, Clone)]
pub struct LlmReturn {
    pub llm_type: LlmType,
    pub text: String,
    pub finish_reason: String,
    pub usage: Triple,
    pub timing: f64,
    pub citations: Option<String>,
    pub safety_ratings: Option<Vec<String>>,
}

impl LlmReturn {
    pub fn new(llm_type: LlmType, text: String, finish_reason: String, usage: Triple, timing: f64, citations: Option<String>, safety_ratings: Option<Vec<String>>) -> Self {
        LlmReturn { llm_type, text, finish_reason, usage, timing, citations, safety_ratings }
    }
}

#[allow(clippy::print_in_format_impl)]
impl std::fmt::Display for LlmReturn {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        println!("---------- {} ----------", self.llm_type);
        let _ = writeln!(f, "{}", self.text);
        if !self.finish_reason.is_empty() && self.finish_reason != "STOP" {
            println!("Finish Reason: {}", self.finish_reason);
        }
        println!("Tokens: Input: {} + Output: {} -> Total: {}",
                 self.usage.0, self.usage.1, self.usage.2);
        println!("Timing: {:.4} secs", self.timing);
        if let Some(ref citations) = self.citations {
            println!("Citations:\n{}", citations);
        }
        if let Some(ref safety_ratings) = self.safety_ratings {
            println!("Safety Settings: {:?}", safety_ratings);
        }

        Ok(())
    }
}

pub trait LlmCompletion {
    /// Set temperature
    fn set_temperature(&mut self, temperature: f32);

    /// If applicable set output to be json. Hint in prompt still necessary.
    fn set_json(&mut self, _is_json: bool) {
        // not applicable for all models
    }

    /// Supply single role and single part text
    fn add_text(&mut self, role: &str, content: &str);

    /// Supply single role with multi-string for iparts with single content
    fn add_many_text(&mut self, role: &str, prompt: &[String]);

    /// Supply simple, 'system' content
    fn add_system(&mut self, system_prompt: &str);

    /// Supply multi-parts and single 'system' content
    fn add_multi_part_system(&mut self, system_prompts: &[String]);

    /// Supply multi-context 'system' content
    fn add_systems(&mut self, system_prompts: &[String]);

    /// Supply multi-String content with user and llm alternating
    fn dialogue(&mut self, prompts: &[String], has_system: bool);

    /// Truncate messages
    fn truncate_messages(&mut self, len: usize);

    /// Return String of Object
    fn debug(&self) -> String;

    /// Create and call llm by supplying data and common parameters
    fn call(system: &str, user: &[String], temperature: f32, _is_json: bool, is_chat: bool) -> impl std::future::Future<Output = Result<LlmReturn, Box<dyn std::error::Error + Send>>> + Send;

    /// Create and call llm by supplying model, data and common parameters
    fn call_model(model: &str, system: &str, user: &[String], temperature: f32, _is_json: bool, is_chat: bool) -> impl std::future::Future<Output = Result<LlmReturn, Box<dyn std::error::Error + Send>>> + Send;

    /// Create and call llm by supplying model, function, data and common parameters
    fn call_model_function(model: &str, system: &str, user: &[String], temperature: f32, _is_json: bool, is_chat: bool, function: Option<Vec<Function>>) -> impl std::future::Future<Output = Result<LlmReturn, Box<dyn std::error::Error + Send>>> + Send;
}

pub trait LlmMessage {
    /// Supply single role and single part text
    fn text(role: &str, content: &str) -> Self
        where Self: Sized;

    /// Supply single role with multi-string for iparts with single content
    fn many_text(role: &str, prompt: &[String]) -> Self
        where Self: Sized;

    /// Supply simple, 'system' content
    fn system(system_prompt: &str) -> Vec<Self>
        where Self: Sized;

    /// Supply multi-parts and single 'system' content
    fn multi_part_system(system_prompts: &[String]) -> Vec<Self>
        where Self: Sized;

    /// Supply multi-context 'system' content
    fn systems(system_prompts: &[String]) -> Vec<Self>
        where Self: Sized;

    /// Supply multi-String content with user and model alternating
    fn dialogue(prompts: &[String], has_system: bool) -> Vec<Self>
        where Self: Sized;

    /// Return String of Object
    fn debug(&self) -> String;
}

// Lowest common denominator error message!
#[derive(Debug, Deserialize)]
pub struct LlmError {
    pub error: LlmErrorMessage
}

#[derive(Debug, Deserialize)]
pub struct LlmErrorMessage {
    pub message: String
}

impl std::fmt::Display for LlmErrorMessage {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "message: {}", self.message)
    }
}

/// Call named LLM and model to call functions
pub async fn call_function_llm_model(llm: &str, model: &str, user: &[String], function: &[&str]) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {
    call_llm_model_function(llm, model, "", user, 0.2, false, false, function).await
}

/// Call named LLM and default model to call functions
pub async fn call_function_llm(llm: &str, user: &[String], function: &[&str]) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {
    let model = get_model(llm);

    call_function_llm_model(llm, &model, user, function).await
}

/// Call default LLM and model to call functions
pub async fn call_function(user: &[String], function: &[&str]) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {
    let llm: &str = &std::env::var("LLM_TO_USE").map_err(|_| "groq".to_string()).unwrap();
    let model = get_model(llm);

    call_function_llm_model(llm, &model, user, function).await
}

/// Call named LLM and model with common parameters supplied
#[allow(clippy::too_many_arguments)]
pub async fn call_llm_model_function(llm: &str, model: &str, system: &str, user: &[String], temperature: f32, is_json: bool, is_chat: bool, function: &[&str]) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {
//println!("{:?}", function);
    let function: Option<Vec<Function>> = get_function_json(llm, function);

    match llm {
        "google" | "gemini" => {
            GeminiCompletion::call_model_function(model, system, user, temperature, is_json, is_chat, function).await
        },
        "openai" | "gpt" => {
            GptCompletion::call_model_function(model, system, user, temperature, is_json, is_chat, function).await
        },
        "mistral" => {
            MistralCompletion::call_model_function(model, system, user, temperature, is_json, is_chat, function).await
        },
        "anthropic" | "claude" => {
            ClaudeCompletion::call_model_function(model, system, user, temperature, is_json, is_chat, function).await
        },
        "deepseek" => {
            DeepseekCompletion::call_model_function(model, system, user, temperature, is_json, is_chat, function).await
        },
        _ => {
            GroqCompletion::call_model_function(model, system, user, temperature, is_json, is_chat, function).await
        },
    }
}

/// Call default named LLM with common parameters supplied
pub async fn call_llm_model(llm: &str, model: &str, system: &str, user: &[String], temperature: f32, is_json: bool, is_chat: bool) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {
    match llm {
        "google" | "gemini" => {
            GeminiCompletion::call_model(model, system, user, temperature, is_json, is_chat).await
        },
        "openai" | "gpt" => {
            GptCompletion::call_model(model, system, user, temperature, is_json, is_chat).await
        },
        "mistral" => {
            MistralCompletion::call_model(model, system, user, temperature, is_json, is_chat).await
        },
        "anthropic" | "claude" => {
            ClaudeCompletion::call_model(model, system, user, temperature, is_json, is_chat).await
        },
        "deepseek" => {
            DeepseekCompletion::call_model(model, system, user, temperature, is_json, is_chat).await
        },
        _ => {
            GroqCompletion::call_model(model, system, user, temperature, is_json, is_chat).await
        },
    }
}

fn get_model(llm: &str) -> String {
    let model =
        match llm {
            "google" | "gemini" => {
                    std::env::var("GEMINI_MODEL")
            },
            "openai" | "gpt" => {
                    std::env::var("GPT_MODEL")
            },
            "mistral" => {
                    std::env::var("MISTRAL_MODEL")
            },
            "anthropic" | "claude" => {
                    std::env::var("CLAUDE_MODEL")
            },
            _ => {
                    std::env::var("GROQ_MODEL")
            },
        };

    model.expect("{llm} MODEL not found in enviroment variables")
}

/// Call default named LLM with common parameters supplied
pub async fn call_llm(llm: &str, system: &str, user: &[String], temperature: f32, is_json: bool, is_chat: bool) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {
    let model = get_model(llm);

    call_llm_model(llm, &model, system, user, temperature, is_json, is_chat).await
}

/// Call default (see LLM_TO_USE env var) LLM with common parameters supplied
pub async fn call(system: &str, user: &[String], temperature: f32, is_json: bool, is_chat: bool) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {
    let llm: &str = &std::env::var("LLM_TO_USE").map_err(|_| "groq".to_string()).unwrap();

    call_llm(llm, system, user, temperature, is_json, is_chat).await
}

/// Call single shot default LLM with default values for parameters supplied
pub async fn single_call(system: &str, user: &[String]) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {

    call(system, user, 0.2, false, false).await
}

/// Call single shot default LLM with default values for parameters supplied
/// Should return JSON
pub async fn single_call_json(system: &str, user: &[String]) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {
    let system = &format!("Return valid JSON only. {system}");

    call(system, user, 0.2, true, false).await
}

/// Call chat default LLM with default values for parameters supplied
pub async fn chat_call(system: &str, user: &[String]) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {

    call(system, user, 0.2, false, true).await
}

/// Call chat default LLM with default values for parameters supplied
/// Should return JSON
pub async fn chat_call_json(system: &str, user: &[String]) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {
    let system = &format!("Return valid JSON only. {system}");

    call(system, user, 0.2, true, true).await
}

/// Call single shot default LLM with temperature supplied
pub async fn single_call_temperature(system: &str, user: &[String], temperature: f32) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {

    call(system, user, temperature, false, false).await
}

/// Call single shot default LLM with temperature supplied
/// Should return JSON
pub async fn single_call_json_temperature(system: &str, user: &[String], temperature: f32) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {
    let system = &format!("Return valid JSON only. {system}");

    call(system, user, temperature, true, false).await
}

/// Call chat default LLM with temperature supplied
pub async fn chat_call_temperature(system: &str, user: &[String], temperature: f32) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {

    call(system, user, temperature, false, true).await
}

/// Call chat default LLM with temperature supplied
/// Should return JSON
pub async fn chat_call_json_temperature(system: &str, user: &[String], temperature: f32) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {
    let system = &format!("Return valid JSON only. {system}");

    call(system, user, temperature, true, true).await
}

/// Call single shot named LLM with default values for parameters supplied
pub async fn single_call_llm(llm: &str, system: &str, user: &[String]) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {

    call_llm(llm, system, user, 0.2, false, false).await
}

/// Call single shot named LLM with default values for parameters supplied
/// Should return JSON
pub async fn single_call_json_llm(llm: &str, system: &str, user: &[String]) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {
    let system = &format!("Return valid JSON only. {system}");

    call_llm(llm, system, user, 0.2, true, false).await
}

/// Call chat named LLM with default values for parameters supplied
pub async fn chat_call_llm(llm: &str, system: &str, user: &[String]) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {

    call_llm(llm, system, user, 0.2, false, true).await
}

/// Call chat named LLM with default values for parameters supplied
/// Should return JSON
pub async fn chat_call_json_llm(llm: &str, system: &str, user: &[String]) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {
    let system = &format!("Return valid JSON only. {system}");

    call_llm(llm, system, user, 0.2, true, true).await
}

/// Call single shot named LLM with temperature supplied
pub async fn single_call_temperature_llm(llm: &str, system: &str, user: &[String], temperature: f32) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {

    call_llm(llm, system, user, temperature, false, false).await
}

/// Call single shot named LLM with temperature supplied
/// Should return JSON
pub async fn single_call_json_temperature_llm(llm: &str, system: &str, user: &[String], temperature: f32) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {
    let system = &format!("Return valid JSON only. {system}");

    call_llm(llm, system, user, temperature, true, false).await
}

/// Call chat named LLM with temperature supplied
pub async fn chat_call_temperature_llm(llm: &str, system: &str, user: &[String], temperature: f32) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {

    call_llm(llm, system, user, temperature, false, true).await
}

/// Call chat named LLM with temperature supplied
/// Should return JSON
pub async fn chat_call_json_temperature_llm(llm: &str, system: &str, user: &[String], temperature: f32) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {
    let system = &format!("Return valid JSON only. {system}");

    call_llm(llm, system, user, temperature, true, true).await
}

/// Call single shot named LLM/Model with default values for parameters supplied
pub async fn single_call_llm_model(llm: &str, model: &str, system: &str, user: &[String]) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {

    call_llm_model(llm, model, system, user, 0.2, false, false).await
}

/// Call single shot named LLM/Model with default values for parameters supplied
/// Should return JSON
pub async fn single_call_json_llm_model(llm: &str, model: &str, system: &str, user: &[String]) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {
    let system = &format!("Return valid JSON only. {system}");

    call_llm_model(llm, model, system, user, 0.2, true, false).await
}

/// Call chat named LLM/Model with default values for parameters supplied
pub async fn chat_call_llm_model(llm: &str, model: &str, system: &str, user: &[String]) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {

    call_llm_model(llm, model, system, user, 0.2, false, true).await
}

/// Call chat named LLM/Model with default values for parameters supplied
/// Should return JSON
pub async fn chat_call_json_llm_model(llm: &str, model: &str, system: &str, user: &[String]) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {
    let system = &format!("Return valid JSON only. {system}");

    call_llm_model(llm, model, system, user, 0.2, true, true).await
}

/// Call single shot named LLM/Model with temperature supplied
pub async fn single_call_temperature_llm_model(llm: &str, model: &str, system: &str, user: &[String], temperature: f32) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {

    call_llm_model(llm, model, system, user, temperature, false, false).await
}

/// Call single shot named LLM/Model with temperature supplied
/// Should return JSON
pub async fn single_call_json_temperature_llm_model(llm: &str, model: &str, system: &str, user: &[String], temperature: f32) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {
    let system = &format!("Return valid JSON only. {system}");

    call_llm_model(llm, model, system, user, temperature, true, false).await
}

/// Call chat named LLM/Model with temperature supplied
pub async fn chat_call_temperature_llm_model(llm: &str, model: &str, system: &str, user: &[String], temperature: f32) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {

    call_llm_model(llm, model, system, user, temperature, false, true).await
}

/// Call chat named LLM/Model with temperature supplied
/// Should return JSON
pub async fn chat_call_json_temperature_llm_model(llm: &str, model: &str, system: &str, user: &[String], temperature: f32) -> Result<LlmReturn, Box<dyn std::error::Error + Send>> {
    let system = &format!("Return valid JSON only. {system}");

    call_llm_model(llm, model, system, user, temperature, true, true).await
}

/// Common HTTP client with header setup
pub async fn get_client(mut headers: HeaderMap) -> Result<Client, Box<dyn std::error::Error + Send>> {
    // We would like json
    headers.insert(
        "Content-Type",
        HeaderValue::from_str("appication/json; charset=utf-8")
            .map_err(|e| -> Box<dyn std::error::Error + Send> { Box::new(e) })?,
    );
    headers.insert(
        "Accept",
        HeaderValue::from_str("appication/json")
            .map_err(|e| -> Box<dyn std::error::Error + Send> { Box::new(e) })?,
    );

    // Create client
    let client: Client = Client::builder()
        .user_agent("TargetR")
        .timeout(std::time::Duration::new(120, 0))
        //.gzip(true)
        .default_headers(headers)
        .build()
        .map_err(|e| -> Box<dyn std::error::Error + Send> { Box::new(e) })?;

    Ok(client)
}