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
//! Google PSE (Programmable Search Engine) Provider
//!
//! Google Programmable Search Engine integration for search-augmented generation.

use futures::Stream;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::pin::Pin;
use tracing::debug;

use crate::core::providers::base::{
    BaseConfig, BaseHttpClient, HttpErrorMapper, OpenAIRequestTransformer, UrlBuilder,
    apply_headers, get_pricing_db, header_static,
};
use crate::core::providers::unified_provider::ProviderError;
use crate::core::traits::{
    error_mapper::trait_def::ErrorMapper, provider::ProviderConfig,
    provider::llm_provider::trait_definition::LLMProvider,
};
use crate::core::types::{
    chat::ChatMessage,
    chat::ChatRequest,
    context::RequestContext,
    embedding::EmbeddingRequest,
    health::HealthStatus,
    message::MessageRole,
    model::ModelInfo,
    model::ProviderCapability,
    responses::{ChatChoice, ChatChunk, ChatResponse, EmbeddingResponse, FinishReason, Usage},
};

// Static capabilities
const GOOGLE_PSE_CAPABILITIES: &[ProviderCapability] = &[ProviderCapability::ChatCompletion];

/// Google PSE provider configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GooglePSEConfig {
    /// API key for authentication
    pub api_key: String,
    /// Search Engine ID
    pub search_engine_id: String,
    /// API base URL (defaults to <https://www.googleapis.com/customsearch/v1>)
    pub api_base: String,
    /// Request timeout in seconds
    pub timeout_seconds: u64,
    /// Maximum retries for failed requests
    pub max_retries: u32,
}

impl Default for GooglePSEConfig {
    fn default() -> Self {
        Self {
            api_key: String::new(),
            search_engine_id: String::new(),
            api_base: "https://www.googleapis.com/customsearch/v1".to_string(),
            timeout_seconds: 30,
            max_retries: 3,
        }
    }
}

impl ProviderConfig for GooglePSEConfig {
    fn validate(&self) -> Result<(), String> {
        self.validate_standard("Google PSE")?;
        if self.search_engine_id.is_empty() {
            return Err("Google PSE Search Engine ID is required".to_string());
        }
        Ok(())
    }

    fn api_key(&self) -> Option<&str> {
        Some(&self.api_key)
    }

    fn api_base(&self) -> Option<&str> {
        Some(&self.api_base)
    }

    fn timeout(&self) -> std::time::Duration {
        std::time::Duration::from_secs(self.timeout_seconds)
    }

    fn max_retries(&self) -> u32 {
        self.max_retries
    }
}

/// Google PSE error type (using unified ProviderError)
pub type GooglePSEError = ProviderError;

/// Google PSE error mapper
pub struct GooglePSEErrorMapper;

impl ErrorMapper<GooglePSEError> for GooglePSEErrorMapper {
    fn map_http_error(&self, status_code: u16, response_body: &str) -> GooglePSEError {
        HttpErrorMapper::map_status_code("google_pse", status_code, response_body)
    }

    fn map_json_error(&self, error_response: &Value) -> GooglePSEError {
        HttpErrorMapper::parse_json_error("google_pse", error_response)
    }

    fn map_network_error(&self, error: &dyn std::error::Error) -> GooglePSEError {
        ProviderError::network("google_pse", error.to_string())
    }

    fn map_parsing_error(&self, error: &dyn std::error::Error) -> GooglePSEError {
        ProviderError::response_parsing("google_pse", error.to_string())
    }

    fn map_timeout_error(&self, timeout_duration: std::time::Duration) -> GooglePSEError {
        ProviderError::timeout(
            "google_pse",
            format!("Request timed out after {:?}", timeout_duration),
        )
    }
}

/// Google PSE provider implementation
#[derive(Debug, Clone)]
pub struct GooglePSEProvider {
    config: GooglePSEConfig,
    base_client: BaseHttpClient,
    models: Vec<ModelInfo>,
}

impl GooglePSEProvider {
    /// Create a new Google PSE provider instance
    pub async fn new(config: GooglePSEConfig) -> Result<Self, GooglePSEError> {
        config
            .validate()
            .map_err(|e| ProviderError::configuration("google_pse", e))?;

        let base_config = BaseConfig {
            api_key: Some(config.api_key.clone()),
            api_base: Some(config.api_base.clone()),
            timeout: config.timeout_seconds,
            max_retries: config.max_retries,
            headers: HashMap::new(),
            organization: None,
            api_version: None,
        };

        let base_client = BaseHttpClient::new(base_config)?;

        let models = vec![ModelInfo {
            id: "google-pse-search".to_string(),
            name: "Google PSE Search".to_string(),
            provider: "google_pse".to_string(),
            max_context_length: 1024,
            max_output_length: None,
            supports_streaming: false,
            supports_tools: false,
            supports_multimodal: false,
            input_cost_per_1k_tokens: Some(0.005),
            output_cost_per_1k_tokens: Some(0.0),
            currency: "USD".to_string(),
            capabilities: vec![],
            created_at: None,
            updated_at: None,
            metadata: HashMap::new(),
        }];

        Ok(Self {
            config,
            base_client,
            models,
        })
    }
}

impl LLMProvider for GooglePSEProvider {
    fn name(&self) -> &'static str {
        "google_pse"
    }

    fn capabilities(&self) -> &'static [ProviderCapability] {
        GOOGLE_PSE_CAPABILITIES
    }

    fn models(&self) -> &[ModelInfo] {
        &self.models
    }

    fn get_supported_openai_params(&self, _model: &str) -> &'static [&'static str] {
        &["num_results", "search_type"]
    }

    async fn map_openai_params(
        &self,
        params: HashMap<String, Value>,
        _model: &str,
    ) -> Result<HashMap<String, Value>, ProviderError> {
        let mut mapped = HashMap::new();
        for (key, value) in params {
            match key.as_str() {
                "num_results" | "search_type" => {
                    mapped.insert(key, value);
                }
                _ => {}
            }
        }
        Ok(mapped)
    }

    async fn transform_request(
        &self,
        request: ChatRequest,
        _context: RequestContext,
    ) -> Result<Value, ProviderError> {
        Ok(OpenAIRequestTransformer::transform_chat_request(&request))
    }

    async fn transform_response(
        &self,
        raw_response: &[u8],
        _model: &str,
        _request_id: &str,
    ) -> Result<ChatResponse, ProviderError> {
        serde_json::from_slice(raw_response)
            .map_err(|e| ProviderError::response_parsing("google_pse", e.to_string()))
    }

    fn get_error_mapper(&self) -> Box<dyn ErrorMapper<ProviderError>> {
        Box::new(GooglePSEErrorMapper)
    }

    async fn chat_completion(
        &self,
        request: ChatRequest,
        _context: RequestContext,
    ) -> Result<ChatResponse, ProviderError> {
        debug!("Google PSE search request: model={}", request.model);

        let query = if let Some(last_message) = request.messages.last() {
            if let Some(crate::core::types::message::MessageContent::Text(text)) =
                &last_message.content
            {
                text.clone()
            } else {
                return Err(ProviderError::invalid_request(
                    "google_pse",
                    "Last message must contain text content".to_string(),
                ));
            }
        } else {
            return Err(ProviderError::invalid_request(
                "google_pse",
                "Request must contain at least one message".to_string(),
            ));
        };

        let url = UrlBuilder::new(&self.config.api_base)
            .with_query("key", &self.config.api_key)
            .with_query("cx", &self.config.search_engine_id)
            .with_query("q", &query)
            .build();

        let headers = vec![header_static("Content-Type", "application/json")];

        let response = apply_headers(self.base_client.inner().get(&url), headers)
            .send()
            .await
            .map_err(|e| ProviderError::network("google_pse", e.to_string()))?;

        if !response.status().is_success() {
            let status = response.status().as_u16();
            let body = response.text().await.unwrap_or_default();
            return Err(HttpErrorMapper::map_status_code(
                "google_pse",
                status,
                &body,
            ));
        }

        let search_response: Value = response
            .json()
            .await
            .map_err(|e| ProviderError::response_parsing("google_pse", e.to_string()))?;

        let content = format!(
            "Search results: {}",
            serde_json::to_string_pretty(&search_response).unwrap_or_default()
        );

        Ok(ChatResponse {
            id: format!("pse-{}", uuid::Uuid::new_v4()),
            object: "chat.completion".to_string(),
            created: chrono::Utc::now().timestamp(),
            model: request.model.clone(),
            choices: vec![ChatChoice {
                index: 0,
                message: ChatMessage {
                    role: MessageRole::Assistant,
                    content: Some(crate::core::types::message::MessageContent::Text(content)),
                    thinking: None,
                    name: None,
                    tool_calls: None,
                    tool_call_id: None,
                    function_call: None,
                },
                finish_reason: Some(FinishReason::Stop),
                logprobs: None,
            }],
            usage: Some(Usage {
                prompt_tokens: 0,
                completion_tokens: 0,
                total_tokens: 0,
                prompt_tokens_details: None,
                completion_tokens_details: None,
                thinking_usage: None,
            }),
            system_fingerprint: None,
        })
    }

    async fn chat_completion_stream(
        &self,
        _request: ChatRequest,
        _context: RequestContext,
    ) -> Result<Pin<Box<dyn Stream<Item = Result<ChatChunk, ProviderError>> + Send>>, ProviderError>
    {
        Err(ProviderError::not_implemented(
            "google_pse",
            "Google PSE does not support streaming".to_string(),
        ))
    }

    async fn embeddings(
        &self,
        _request: EmbeddingRequest,
        _context: RequestContext,
    ) -> Result<EmbeddingResponse, ProviderError> {
        Err(ProviderError::not_implemented(
            "google_pse",
            "Google PSE does not support embeddings".to_string(),
        ))
    }

    async fn health_check(&self) -> HealthStatus {
        let url = UrlBuilder::new(&self.config.api_base)
            .with_query("key", &self.config.api_key)
            .with_query("cx", &self.config.search_engine_id)
            .with_query("q", "test")
            .build();

        match self.base_client.inner().get(&url).send().await {
            Ok(response) if response.status().is_success() => HealthStatus::Healthy,
            Ok(response) => {
                debug!(
                    "Google PSE health check failed: status={}",
                    response.status()
                );
                HealthStatus::Unhealthy
            }
            Err(e) => {
                debug!("Google PSE health check error: {}", e);
                HealthStatus::Unhealthy
            }
        }
    }

    async fn calculate_cost(
        &self,
        model: &str,
        input_tokens: u32,
        output_tokens: u32,
    ) -> Result<f64, ProviderError> {
        let usage = crate::core::providers::base::pricing::Usage {
            prompt_tokens: input_tokens,
            completion_tokens: output_tokens,
            total_tokens: input_tokens + output_tokens,
            reasoning_tokens: None,
        };
        Ok(get_pricing_db().calculate(model, &usage))
    }
}

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

    fn create_test_config() -> GooglePSEConfig {
        GooglePSEConfig {
            api_key: "test_api_key".to_string(),
            search_engine_id: "test_engine_id".to_string(),
            ..Default::default()
        }
    }

    #[tokio::test]
    async fn test_provider_creation() {
        let config = create_test_config();
        let provider = GooglePSEProvider::new(config).await;
        assert!(provider.is_ok());

        let provider = provider.unwrap();
        assert_eq!(provider.name(), "google_pse");
    }

    #[test]
    fn test_config_validation() {
        let mut config = GooglePSEConfig::default();
        assert!(config.validate().is_err());

        config.api_key = "test_key".to_string();
        assert!(config.validate().is_err());

        config.search_engine_id = "test_engine_id".to_string();
        assert!(config.validate().is_ok());
    }
}