langextract-rust 0.5.0

A Rust library for extracting structured and grounded information from text using LLMs
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
//! Universal provider implementation.

use super::config::{ProviderConfig, ProviderType};
use crate::{
    data::FormatType,
    exceptions::{LangExtractError, LangExtractResult},
    inference::{BaseLanguageModel, ScoredOutput},
    logging::{report_progress, ProgressEvent},
    schema::BaseSchema,
};
use async_trait::async_trait;
use std::collections::HashMap;
use tokio::time::Duration;

/// Universal language model provider
pub struct UniversalProvider {
    config: ProviderConfig,
    format_type: FormatType,
    client: reqwest::Client,
    #[cfg(feature = "openai")]
    openai_client: Option<async_openai::Client<async_openai::config::OpenAIConfig>>,
    schema: Option<Box<dyn BaseSchema>>,
    fence_output_override: Option<bool>,
}

impl UniversalProvider {
    /// Retry helper function with exponential backoff
    /// Retries at least 3 times with 30-second delays between attempts
    pub async fn retry_with_backoff<T, F, Fut>(
        &self,
        mut operation: F,
        operation_name: &str,
    ) -> LangExtractResult<T>
    where
        F: FnMut() -> Fut,
        Fut: std::future::Future<Output = LangExtractResult<T>>,
    {
        let max_retries = 3;
        let base_delay = Duration::from_secs(30);

        for attempt in 0..=max_retries {
            match operation().await {
                Ok(result) => return Ok(result),
                Err(e) => {
                    if attempt == max_retries {
                        // Last attempt failed, return the error
                        return Err(LangExtractError::inference_simple(
                            format!("{} failed after {} attempts. Last error: {}", operation_name, max_retries + 1, e)
                        ));
                    }

                    // Calculate delay with exponential backoff (30s, 60s, 90s)
                    let delay = base_delay * (attempt + 1) as u32;
                    report_progress(ProgressEvent::RetryAttempt {
                        operation: operation_name.to_string(),
                        attempt: attempt + 1,
                        max_attempts: max_retries + 1,
                        delay_seconds: delay.as_secs(),
                    });

                    // Add a small delay before the main sleep to ensure logs are printed
                    tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;

                    // Use tokio::time::sleep explicitly
                    tokio::time::sleep(delay).await;
                }
            }
        }

        unreachable!("Should have returned from the loop")
    }

    /// Create a new universal provider
    pub fn new(config: ProviderConfig) -> LangExtractResult<Self> {
        let client = reqwest::Client::new();

        #[cfg(feature = "openai")]
        let openai_client = if config.provider_type == ProviderType::OpenAI {
            if let Some(api_key) = &config.api_key {
                let openai_config = async_openai::config::OpenAIConfig::new()
                    .with_api_key(api_key)
                    .with_api_base(&config.base_url);
                Some(async_openai::Client::with_config(openai_config))
            } else {
                return Err(LangExtractError::configuration(
                    "API key is required for OpenAI provider",
                ));
            }
        } else {
            None
        };

        #[cfg(not(feature = "openai"))]
        let openai_client = None;

        Ok(Self {
            config,
            format_type: FormatType::Json,
            client,
            openai_client,
            schema: None,
            fence_output_override: None,
        })
    }

    /// Process a single OpenAI prompt
    #[cfg(feature = "openai")]
    #[tracing::instrument(skip_all, fields(provider = "openai", model = %self.config.model, prompt_len = prompt.len()))]
    async fn infer_openai_single(
        &self,
        prompt: &str,
        kwargs: &HashMap<String, serde_json::Value>,
    ) -> LangExtractResult<Vec<ScoredOutput>> {
        use async_openai::types::{
            ChatCompletionRequestMessage, ChatCompletionRequestSystemMessage,
            ChatCompletionRequestSystemMessageContent, CreateChatCompletionRequest,
        };

        let client = self.openai_client.as_ref().ok_or_else(|| {
            LangExtractError::configuration("OpenAI client not initialized")
        })?;

        // Create system message for format instructions
        let system_message = match self.format_type {
            FormatType::Json => "You are a helpful assistant that responds in JSON format. Always return valid JSON that matches the expected structure from the examples.",
            FormatType::Yaml => "You are a helpful assistant that responds in YAML format. Always return valid YAML that matches the expected structure from the examples.",
        };

        // Create messages for the chat completion
        let messages = vec![
            ChatCompletionRequestMessage::System(ChatCompletionRequestSystemMessage {
                content: ChatCompletionRequestSystemMessageContent::Text(system_message.to_string()),
                name: None,
            }),
            ChatCompletionRequestMessage::User(async_openai::types::ChatCompletionRequestUserMessage {
                content: async_openai::types::ChatCompletionRequestUserMessageContent::Text(prompt.to_string()),
                name: None,
            }),
        ];

        // Build the request
        let mut request = CreateChatCompletionRequest {
            model: self.config.model.clone(),
            messages,
            temperature: None,
            max_tokens: None,
            ..Default::default()
        };

        // Apply parameters from kwargs
        if let Some(temp) = kwargs.get("temperature") {
            if let Some(temp_f64) = temp.as_f64() {
                request.temperature = Some(temp_f64 as f32);
            }
        }
        
        if let Some(max_tokens) = kwargs.get("max_tokens") {
            if let Some(max_tokens_u64) = max_tokens.as_u64() {
                request.max_tokens = Some(max_tokens_u64 as u32);
            }
        }

        // Make the API call with retry logic
        report_progress(ProgressEvent::ModelCall {
            provider: "OpenAI".to_string(),
            model: self.config.model.clone(),
            input_length: prompt.len(),
        });
        
        let response = self.retry_with_backoff(
            || async {
                let result = client.chat().create(request.clone()).await.map_err(|e| {
                    report_progress(ProgressEvent::Error {
                        operation: "OpenAI API request".to_string(),
                        error: format!("OpenAI API error: {}", e),
                    });
                    LangExtractError::inference_simple(format!("OpenAI API error: {}", e))
                });
                result
            },
            &format!("OpenAI API call for prompt len {}", prompt.len())
        ).await?;

        // Extract the response content
        let content = response
            .choices
            .get(0)
            .and_then(|choice| choice.message.content.as_ref())
            .ok_or_else(|| {
                LangExtractError::parsing("No content in OpenAI response")
            })?;

        Ok(vec![ScoredOutput::from_text(content.clone())])
    }

    /// Inference implementation for OpenAI-compatible APIs — processes prompts concurrently
    #[cfg(feature = "openai")]
    async fn infer_openai(
        &self,
        batch_prompts: &[String],
        kwargs: &HashMap<String, serde_json::Value>,
    ) -> LangExtractResult<Vec<Vec<ScoredOutput>>> {
        use futures::future::join_all;

        // Process all prompts concurrently instead of sequentially
        let futures: Vec<_> = batch_prompts.iter()
            .map(|prompt| self.infer_openai_single(prompt, kwargs))
            .collect();

        let results = join_all(futures).await;
        
        // Collect results, propagating errors
        results.into_iter().collect()
    }

    /// Process a single Ollama prompt
    #[tracing::instrument(skip_all, fields(provider = "ollama", model = %self.config.model, prompt_len = prompt.len()))]
    async fn infer_ollama_single(
        &self,
        prompt: &str,
        kwargs: &HashMap<String, serde_json::Value>,
    ) -> LangExtractResult<Vec<ScoredOutput>> {
        let mut request_body = serde_json::json!({
            "model": self.config.model,
            "prompt": prompt,
            "stream": false,
        });

        // Set format for JSON output if needed
        if self.format_type == FormatType::Json {
            request_body["format"] = serde_json::json!("json");
        }

        // Apply parameters from kwargs
        let mut options = serde_json::Map::new();
        if let Some(temp) = kwargs.get("temperature") {
            options.insert("temperature".to_string(), temp.clone());
        }
        if let Some(max_tokens) = kwargs.get("max_tokens") {
            options.insert("num_predict".to_string(), max_tokens.clone());
        }
        if !options.is_empty() {
            request_body["options"] = serde_json::Value::Object(options);
        }

        let url = format!("{}/api/generate", self.config.base_url);

        // Make the API call with retry logic
        report_progress(ProgressEvent::ModelCall {
            provider: "Ollama".to_string(),
            model: self.config.model.clone(),
            input_length: prompt.len(),
        });
        
        let response_body = self.retry_with_backoff(
            || async {
                let mut request = self.client.post(&url).json(&request_body);

                // Add headers
                for (key, value) in &self.config.headers {
                    request = request.header(key, value);
                }

                let response = request.send().await.map_err(|e| {
                    report_progress(ProgressEvent::Error {
                        operation: "Ollama HTTP request".to_string(),
                        error: format!("HTTP request failed: {}", e),
                    });
                    LangExtractError::NetworkError(e)
                })?;

                if !response.status().is_success() {
                    let status = response.status();
                    report_progress(ProgressEvent::Error {
                        operation: "Ollama HTTP status".to_string(),
                        error: format!("HTTP error status: {}", status),
                    });
                    return Err(LangExtractError::inference_simple(format!(
                        "Ollama API error: HTTP {}",
                        status
                    )));
                }

                let response_body: serde_json::Value = response.json().await.map_err(|e| {
                    report_progress(ProgressEvent::Error {
                        operation: "Ollama JSON parsing".to_string(),
                        error: format!("JSON parsing failed: {}", e),
                    });
                    LangExtractError::parsing(format!("Failed to parse Ollama response: {}", e))
                })?;

                Ok(response_body)
            },
            &format!("Ollama API call for prompt len {}", prompt.len())
        ).await?;

        let content = response_body
            .get("response")
            .and_then(|r| r.as_str())
            .ok_or_else(|| {
                LangExtractError::parsing("Missing 'response' field in Ollama response")
            })?;

        Ok(vec![ScoredOutput::from_text(content.to_string())])
    }

    /// Inference implementation for Ollama — processes prompts concurrently
    async fn infer_ollama(
        &self,
        batch_prompts: &[String],
        kwargs: &HashMap<String, serde_json::Value>,
    ) -> LangExtractResult<Vec<Vec<ScoredOutput>>> {
        use futures::future::join_all;

        // Process all prompts concurrently instead of sequentially
        let futures: Vec<_> = batch_prompts.iter()
            .map(|prompt| self.infer_ollama_single(prompt, kwargs))
            .collect();

        let results = join_all(futures).await;
        
        // Collect results, propagating errors
        results.into_iter().collect()
    }
}

#[async_trait]
impl BaseLanguageModel for UniversalProvider {
    fn get_schema_class(&self) -> Option<Box<dyn BaseSchema>> {
        // Return a format mode schema for now
        crate::schema::FormatModeSchema::from_examples(&[], "_attributes").ok()
    }

    fn apply_schema(&mut self, schema: Option<Box<dyn BaseSchema>>) {
        self.schema = schema;
    }

    fn set_fence_output(&mut self, fence_output: Option<bool>) {
        self.fence_output_override = fence_output;
    }

    fn requires_fence_output(&self) -> bool {
        if let Some(override_val) = self.fence_output_override {
            return override_val;
        }

        // OpenAI with JSON mode doesn't need fences, Ollama might
        match self.config.provider_type {
            ProviderType::OpenAI if self.schema.is_some() => false,
            _ => true,
        }
    }

    #[tracing::instrument(skip_all, fields(batch_size = batch_prompts.len()))]
    async fn infer(
        &self,
        batch_prompts: &[String],
        kwargs: &HashMap<String, serde_json::Value>,
    ) -> LangExtractResult<Vec<Vec<ScoredOutput>>> {
        match self.config.provider_type {
            #[cfg(feature = "openai")]
            ProviderType::OpenAI => self.infer_openai(batch_prompts, kwargs).await,
            ProviderType::Ollama => self.infer_ollama(batch_prompts, kwargs).await,
            ProviderType::Custom => {
                Err(LangExtractError::configuration(
                    "Custom provider inference not yet implemented"
                ))
            }
            #[cfg(not(feature = "openai"))]
            ProviderType::OpenAI => {
                Err(LangExtractError::configuration(
                    "OpenAI feature not enabled. Enable with --features openai"
                ))
            }
        }
    }

    fn format_type(&self) -> FormatType {
        self.format_type
    }

    fn model_id(&self) -> &str {
        &self.config.model
    }

    fn provider_name(&self) -> &str {
        match self.config.provider_type {
            ProviderType::OpenAI => "openai",
            ProviderType::Ollama => "ollama",
            ProviderType::Custom => "custom",
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::providers::config::ProviderConfig;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;

    #[tokio::test]
    async fn test_retry_with_backoff_success_after_failures() {
        let config = ProviderConfig::ollama("test-model", None);
        let provider = UniversalProvider::new(config).unwrap();

        let attempt_count = Arc::new(AtomicUsize::new(0));
        let attempt_count_clone = attempt_count.clone();

        let result = provider.retry_with_backoff(
            move || {
                let attempt_count = attempt_count_clone.clone();
                async move {
                    let current = attempt_count.fetch_add(1, Ordering::SeqCst);
                    if current < 2 {
                        Err::<String, _>(LangExtractError::inference_simple(format!("Attempt {} failed", current + 1)))
                    } else {
                        Ok("Success!".to_string())
                    }
                }
            },
            "Test operation"
        ).await;

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "Success!");
        assert_eq!(attempt_count.load(Ordering::SeqCst), 3);
    }

    #[tokio::test]
    async fn test_retry_with_backoff_immediate_success() {
        let config = ProviderConfig::ollama("test-model", None);
        let provider = UniversalProvider::new(config).unwrap();

        let attempt_count = Arc::new(AtomicUsize::new(0));
        let attempt_count_clone = attempt_count.clone();

        let result = provider.retry_with_backoff(
            move || {
                let attempt_count = attempt_count_clone.clone();
                async move {
                    attempt_count.fetch_add(1, Ordering::SeqCst);
                    Ok("Immediate success!".to_string())
                }
            },
            "Test operation"
        ).await;

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "Immediate success!");
        assert_eq!(attempt_count.load(Ordering::SeqCst), 1);
    }
}