litellm-rs 0.4.16

A high-performance AI Gateway written in Rust, providing OpenAI-compatible APIs with intelligent routing, load balancing, and enterprise features
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
492
493
494
495
496
497
498
499
500
501
502
503
504
//! Cohere Chat Completions Handler
//!
//! Handles chat completion requests for Cohere Command models.
//! Supports both v1 (legacy) and v2 (OpenAI-compatible) APIs.

use serde_json::{Value, json};
use std::collections::HashMap;

use super::config::{CohereApiVersion, CohereConfig};
use super::error::CohereError;
use crate::core::types::chat::ChatMessage as ResponseMessage;
use crate::core::types::responses::{ChatChoice, ChatResponse, FinishReason, Usage};
use crate::core::types::tools::ToolCall;
use crate::core::types::{chat::ChatRequest, message::MessageContent, message::MessageRole};

/// Chat handler utilities
pub struct CohereChatHandler;

impl CohereChatHandler {
    /// Transform ChatRequest to Cohere format
    pub fn transform_request(
        request: &ChatRequest,
        config: &CohereConfig,
    ) -> Result<Value, CohereError> {
        let mut body = json!({
            "model": request.model,
            "messages": request.messages,
        });

        // Add optional parameters
        if let Some(temperature) = request.temperature {
            body["temperature"] = json!(temperature);
        }

        if let Some(max_tokens) = request.max_tokens.or(request.max_completion_tokens) {
            body["max_tokens"] = json!(max_tokens);
        }

        if let Some(top_p) = request.top_p {
            body["p"] = json!(top_p);
        }

        if let Some(frequency_penalty) = request.frequency_penalty {
            body["frequency_penalty"] = json!(frequency_penalty);
        }

        if let Some(presence_penalty) = request.presence_penalty {
            body["presence_penalty"] = json!(presence_penalty);
        }

        if let Some(stop) = &request.stop {
            body["stop_sequences"] = json!(stop);
        }

        if request.stream {
            body["stream"] = json!(true);
        }

        if let Some(tools) = &request.tools {
            // Transform OpenAI tools to Cohere format if using v1
            if config.api_version == CohereApiVersion::V1 {
                // Convert tools to JSON values first
                let tools_json: Vec<Value> = tools
                    .iter()
                    .filter_map(|t| serde_json::to_value(t).ok())
                    .collect();
                let cohere_tools = Self::transform_tools_to_v1(&tools_json)?;
                body["tools"] = cohere_tools;
            } else {
                body["tools"] = json!(tools);
            }
        }

        if let Some(seed) = request.seed {
            body["seed"] = json!(seed);
        }

        Ok(body)
    }

    /// Transform OpenAI tools to Cohere v1 format
    fn transform_tools_to_v1(tools: &[Value]) -> Result<Value, CohereError> {
        let mut cohere_tools = Vec::new();

        for tool in tools {
            if let Some(function) = tool.get("function") {
                let name = function.get("name").and_then(|n| n.as_str()).unwrap_or("");
                let description = function
                    .get("description")
                    .and_then(|d| d.as_str())
                    .unwrap_or("");

                let mut parameter_definitions = HashMap::new();

                if let Some(params) = function.get("parameters")
                    && let Some(properties) = params.get("properties").and_then(|p| p.as_object())
                {
                    let required = params
                        .get("required")
                        .and_then(|r| r.as_array())
                        .map(|arr| arr.iter().filter_map(|v| v.as_str()).collect::<Vec<_>>())
                        .unwrap_or_default();

                    for (param_name, param_def) in properties {
                        let param_type = param_def
                            .get("type")
                            .and_then(|t| t.as_str())
                            .unwrap_or("string");
                        let param_desc = param_def
                            .get("description")
                            .and_then(|d| d.as_str())
                            .unwrap_or("");

                        parameter_definitions.insert(
                            param_name.clone(),
                            json!({
                                "type": param_type,
                                "description": param_desc,
                                "required": required.contains(&param_name.as_str())
                            }),
                        );
                    }
                }

                cohere_tools.push(json!({
                    "name": name,
                    "description": description,
                    "parameter_definitions": parameter_definitions
                }));
            }
        }

        Ok(json!(cohere_tools))
    }

    /// Transform Cohere v2 response to standard ChatResponse
    pub fn transform_response(
        response_json: Value,
        model: &str,
    ) -> Result<ChatResponse, CohereError> {
        let id = response_json
            .get("id")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();

        // Extract content from v2 format
        let content = Self::extract_content(&response_json)?;

        // Extract tool calls if present
        // Note: tool_calls parsing is handled by parse_tool_calls method

        // Extract usage
        let usage = Self::extract_usage(&response_json)?;

        let message = ResponseMessage {
            role: MessageRole::Assistant,
            content: Some(MessageContent::Text(content)),
            thinking: None,
            tool_calls: Self::parse_tool_calls(&response_json),
            name: None,
            function_call: None,
            tool_call_id: None,
        };

        let finish_reason = response_json
            .get("finish_reason")
            .and_then(|v| v.as_str())
            .map(|s| match s.to_lowercase().as_str() {
                "stop" | "complete" | "end_turn" => FinishReason::Stop,
                "length" | "max_tokens" => FinishReason::Length,
                "tool_calls" | "tool_use" => FinishReason::ToolCalls,
                "content_filter" => FinishReason::ContentFilter,
                _ => FinishReason::Stop,
            });

        let choice = ChatChoice {
            index: 0,
            message,
            finish_reason,
            logprobs: None,
        };

        Ok(ChatResponse {
            id,
            object: "chat.completion".to_string(),
            created: chrono::Utc::now().timestamp(),
            model: model.to_string(),
            choices: vec![choice],
            usage: Some(usage),
            system_fingerprint: None,
        })
    }

    /// Extract content from Cohere response
    fn extract_content(response_json: &Value) -> Result<String, CohereError> {
        // v2 format: message.content is an array of content blocks
        if let Some(message) = response_json.get("message")
            && let Some(content) = message.get("content")
            && let Some(content_array) = content.as_array()
        {
            let text: String = content_array
                .iter()
                .filter_map(|c| c.get("text").and_then(|t| t.as_str()))
                .collect::<Vec<_>>()
                .join("");
            return Ok(text);
        }

        // v1 format: text is at top level
        if let Some(text) = response_json.get("text").and_then(|t| t.as_str()) {
            return Ok(text.to_string());
        }

        Ok(String::new())
    }

    /// Extract usage from Cohere response
    fn extract_usage(response_json: &Value) -> Result<Usage, CohereError> {
        // v2 format: usage.tokens
        if let Some(usage) = response_json.get("usage")
            && let Some(tokens) = usage.get("tokens")
        {
            let prompt_tokens = tokens
                .get("input_tokens")
                .and_then(|v| v.as_u64())
                .unwrap_or(0) as u32;
            let completion_tokens = tokens
                .get("output_tokens")
                .and_then(|v| v.as_u64())
                .unwrap_or(0) as u32;

            return Ok(Usage {
                prompt_tokens,
                completion_tokens,
                total_tokens: prompt_tokens + completion_tokens,
                prompt_tokens_details: None,
                completion_tokens_details: None,
                thinking_usage: None,
            });
        }

        // v1 format: meta.billed_units
        if let Some(meta) = response_json.get("meta")
            && let Some(billed_units) = meta.get("billed_units")
        {
            let prompt_tokens = billed_units
                .get("input_tokens")
                .and_then(|v| v.as_u64())
                .unwrap_or(0) as u32;
            let completion_tokens = billed_units
                .get("output_tokens")
                .and_then(|v| v.as_u64())
                .unwrap_or(0) as u32;

            return Ok(Usage {
                prompt_tokens,
                completion_tokens,
                total_tokens: prompt_tokens + completion_tokens,
                prompt_tokens_details: None,
                completion_tokens_details: None,
                thinking_usage: None,
            });
        }

        Ok(Usage {
            prompt_tokens: 0,
            completion_tokens: 0,
            total_tokens: 0,
            prompt_tokens_details: None,
            completion_tokens_details: None,
            thinking_usage: None,
        })
    }

    /// Parse tool calls from Cohere response
    fn parse_tool_calls(response_json: &Value) -> Option<Vec<ToolCall>> {
        let tool_calls_arr = response_json
            .get("message")
            .and_then(|m| m.get("tool_calls"))
            .and_then(|tc| tc.as_array())?;

        let tool_calls: Vec<ToolCall> = tool_calls_arr
            .iter()
            .map(|tc| {
                let id = tc
                    .get("id")
                    .and_then(|v| v.as_str())
                    .unwrap_or("")
                    .to_string();
                let name = tc
                    .get("function")
                    .and_then(|f| f.get("name"))
                    .and_then(|n| n.as_str())
                    .unwrap_or("")
                    .to_string();
                let arguments = tc
                    .get("function")
                    .and_then(|f| f.get("arguments"))
                    .map(|a| {
                        if a.is_string() {
                            a.as_str().unwrap_or("{}").to_string()
                        } else {
                            serde_json::to_string(a).unwrap_or_else(|_| "{}".to_string())
                        }
                    })
                    .unwrap_or_else(|| "{}".to_string());

                ToolCall {
                    id,
                    tool_type: "function".to_string(),
                    function: crate::core::types::tools::FunctionCall { name, arguments },
                }
            })
            .collect();

        if tool_calls.is_empty() {
            None
        } else {
            Some(tool_calls)
        }
    }

    /// Get supported OpenAI parameters for Cohere
    pub fn get_supported_params() -> &'static [&'static str] {
        &[
            "stream",
            "temperature",
            "max_tokens",
            "max_completion_tokens",
            "top_p",
            "frequency_penalty",
            "presence_penalty",
            "stop",
            "n",
            "tools",
            "tool_choice",
            "seed",
        ]
    }

    /// Map OpenAI parameters to Cohere format
    pub fn map_openai_params(params: HashMap<String, Value>) -> HashMap<String, Value> {
        let mut mapped = HashMap::new();

        for (key, value) in params {
            match key.as_str() {
                "stream" => {
                    mapped.insert("stream".to_string(), value);
                }
                "temperature" => {
                    mapped.insert("temperature".to_string(), value);
                }
                "max_tokens" | "max_completion_tokens" => {
                    mapped.insert("max_tokens".to_string(), value);
                }
                "top_p" => {
                    mapped.insert("p".to_string(), value);
                }
                "frequency_penalty" => {
                    mapped.insert("frequency_penalty".to_string(), value);
                }
                "presence_penalty" => {
                    mapped.insert("presence_penalty".to_string(), value);
                }
                "stop" => {
                    mapped.insert("stop_sequences".to_string(), value);
                }
                "n" => {
                    mapped.insert("num_generations".to_string(), value);
                }
                "tools" => {
                    mapped.insert("tools".to_string(), value);
                }
                "seed" => {
                    mapped.insert("seed".to_string(), value);
                }
                _ => {}
            }
        }

        mapped
    }
}

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

    #[test]
    fn test_supported_params() {
        let params = CohereChatHandler::get_supported_params();
        assert!(params.contains(&"temperature"));
        assert!(params.contains(&"max_tokens"));
        assert!(params.contains(&"tools"));
    }

    #[test]
    fn test_map_openai_params() {
        let mut params = HashMap::new();
        params.insert("temperature".to_string(), json!(0.7));
        params.insert("max_tokens".to_string(), json!(100));
        params.insert("top_p".to_string(), json!(0.9));
        params.insert("stop".to_string(), json!(["END"]));

        let mapped = CohereChatHandler::map_openai_params(params);

        assert_eq!(mapped.get("temperature").unwrap(), &json!(0.7));
        assert_eq!(mapped.get("max_tokens").unwrap(), &json!(100));
        assert_eq!(mapped.get("p").unwrap(), &json!(0.9));
        assert_eq!(mapped.get("stop_sequences").unwrap(), &json!(["END"]));
    }

    #[test]
    fn test_extract_usage_v2() {
        let response = json!({
            "usage": {
                "tokens": {
                    "input_tokens": 100,
                    "output_tokens": 50
                }
            }
        });

        let usage = CohereChatHandler::extract_usage(&response).unwrap();
        assert_eq!(usage.prompt_tokens, 100);
        assert_eq!(usage.completion_tokens, 50);
        assert_eq!(usage.total_tokens, 150);
    }

    #[test]
    fn test_extract_usage_v1() {
        let response = json!({
            "meta": {
                "billed_units": {
                    "input_tokens": 80,
                    "output_tokens": 40
                }
            }
        });

        let usage = CohereChatHandler::extract_usage(&response).unwrap();
        assert_eq!(usage.prompt_tokens, 80);
        assert_eq!(usage.completion_tokens, 40);
        assert_eq!(usage.total_tokens, 120);
    }

    #[test]
    fn test_extract_content_v2() {
        let response = json!({
            "message": {
                "content": [
                    {"type": "text", "text": "Hello, "},
                    {"type": "text", "text": "world!"}
                ]
            }
        });

        let content = CohereChatHandler::extract_content(&response).unwrap();
        assert_eq!(content, "Hello, world!");
    }

    #[test]
    fn test_extract_content_v1() {
        let response = json!({
            "text": "Hello from v1!"
        });

        let content = CohereChatHandler::extract_content(&response).unwrap();
        assert_eq!(content, "Hello from v1!");
    }

    #[test]
    fn test_transform_tools_to_v1() {
        let tools = vec![json!({
            "type": "function",
            "function": {
                "name": "get_weather",
                "description": "Get weather info",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "City name"
                        }
                    },
                    "required": ["location"]
                }
            }
        })];

        let cohere_tools = CohereChatHandler::transform_tools_to_v1(&tools).unwrap();
        let tools_array = cohere_tools.as_array().unwrap();

        assert_eq!(tools_array.len(), 1);
        assert_eq!(tools_array[0]["name"], "get_weather");
        assert!(
            tools_array[0]["parameter_definitions"]["location"]["required"]
                .as_bool()
                .unwrap()
        );
    }
}