simplify_baml 0.2.0

Simplified BAML runtime for structured LLM outputs using native Rust types with macros
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
/// HTTP client for calling LLM APIs
///
/// Simplified wrapper around reqwest that handles common LLM API patterns.
/// Supports OpenAI-compatible APIs.

use anyhow::{Context, Result};
use async_trait::async_trait;
use futures::stream::{Stream, StreamExt};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;

#[async_trait]
pub trait LLMClientTrait: Send + Sync {
    async fn call(&self, prompt: &str) -> Result<String>;
}

pub type DynLLMClient = Arc<dyn LLMClientTrait>;

const DEFAULT_TIMEOUT_SECS: u64 = 60;

/// OpenRouter provider preferences for routing requests
#[derive(Debug, Clone, Default, Serialize)]
pub struct ProviderPreferences {
    /// List of provider slugs to try in order (e.g. ["anthropic", "openai"])
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order: Option<Vec<String>>,
    /// Whether to allow backup providers when the primary is unavailable
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allow_fallbacks: Option<bool>,
    /// Only use providers that support all parameters in your request
    #[serde(skip_serializing_if = "Option::is_none")]
    pub require_parameters: Option<bool>,
    /// Control whether to use providers that may store data ("allow" or "deny")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data_collection: Option<String>,
    /// List of provider slugs to allow for this request
    #[serde(skip_serializing_if = "Option::is_none")]
    pub only: Option<Vec<String>>,
    /// List of provider slugs to skip for this request
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ignore: Option<Vec<String>>,
    /// List of quantization levels to filter by (e.g. ["int4", "int8"])
    #[serde(skip_serializing_if = "Option::is_none")]
    pub quantizations: Option<Vec<String>>,
    /// Sort providers by price or throughput (e.g. "price" or "throughput")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sort: Option<String>,
}

impl ProviderPreferences {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_order(mut self, order: Vec<String>) -> Self {
        self.order = Some(order);
        self
    }

    pub fn with_allow_fallbacks(mut self, allow: bool) -> Self {
        self.allow_fallbacks = Some(allow);
        self
    }

    pub fn with_require_parameters(mut self, require: bool) -> Self {
        self.require_parameters = Some(require);
        self
    }

    pub fn with_data_collection(mut self, policy: impl Into<String>) -> Self {
        self.data_collection = Some(policy.into());
        self
    }

    pub fn with_only(mut self, providers: Vec<String>) -> Self {
        self.only = Some(providers);
        self
    }

    pub fn with_ignore(mut self, providers: Vec<String>) -> Self {
        self.ignore = Some(providers);
        self
    }

    pub fn with_quantizations(mut self, quantizations: Vec<String>) -> Self {
        self.quantizations = Some(quantizations);
        self
    }

    pub fn with_sort(mut self, sort: impl Into<String>) -> Self {
        self.sort = Some(sort.into());
        self
    }
}

/// LLM Client configuration
#[derive(Clone)]
pub struct LLMClient {
    pub api_key: String,
    pub base_url: String,
    pub model: String,
    pub max_tokens: Option<u32>,
    pub temperature: Option<f32>,
    pub timeout: Duration,
    pub provider: Option<ProviderPreferences>,
}

impl std::fmt::Debug for LLMClient {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LLMClient")
            .field("api_key", &"[REDACTED]")
            .field("base_url", &self.base_url)
            .field("model", &self.model)
            .field("max_tokens", &self.max_tokens)
            .field("temperature", &self.temperature)
            .field("timeout", &self.timeout)
            .field("provider", &self.provider)
            .finish()
    }
}

impl LLMClient {
    /// Create a new OpenAI client
    pub fn openai(api_key: String, model: String) -> Self {
        Self {
            api_key,
            base_url: "https://api.openai.com/v1".to_string(),
            model,
            max_tokens: None,
            temperature: None,
            timeout: Duration::from_secs(DEFAULT_TIMEOUT_SECS),
            provider: None,
        }
    }

    /// Create a new Anthropic client
    pub fn anthropic(api_key: String, model: String) -> Self {
        Self {
            api_key,
            base_url: "https://api.anthropic.com/v1".to_string(),
            model,
            max_tokens: None,
            temperature: None,
            timeout: Duration::from_secs(DEFAULT_TIMEOUT_SECS),
            provider: None,
        }
    }

    /// Create a custom client
    pub fn custom(api_key: String, base_url: String, model: String) -> Self {
        Self {
            api_key,
            base_url,
            model,
            max_tokens: None,
            temperature: None,
            timeout: Duration::from_secs(DEFAULT_TIMEOUT_SECS),
            provider: None,
        }
    }

    /// Set a custom timeout
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Set provider preferences (for OpenRouter routing)
    pub fn with_provider(mut self, provider: ProviderPreferences) -> Self {
        self.provider = Some(provider);
        self
    }

    /// Call the LLM with a prompt (non-trait method for backward compatibility)
    pub async fn call_direct(&self, prompt: &str) -> Result<String> {
        // Debug: log the prompt being sent
        if std::env::var("BAML_DEBUG").is_ok() {
            eprintln!("[BAML DEBUG] Prompt:\n{}", prompt);
        }

        // Build the request body (OpenAI format)
        let request_body = ChatCompletionRequest {
            model: self.model.clone(),
            messages: vec![
                Message {
                    role: "user".to_string(),
                    content: prompt.to_string(),
                }
            ],
            max_tokens: self.max_tokens,
            temperature: self.temperature,
            stream: false,
            provider: self.provider.clone(),
        };

        // Make the HTTP request with timeout
        let client = reqwest::Client::builder()
            .timeout(self.timeout)
            .build()
            .context("Failed to build HTTP client")?;
        let response = client
            .post(format!("{}/chat/completions", self.base_url))
            .header("Authorization", format!("Bearer {}", self.api_key))
            .header("Content-Type", "application/json")
            .json(&request_body)
            .send()
            .await
            .context("Failed to send request to LLM API")?;

        // Check for errors
        if !response.status().is_success() {
            let status = response.status();
            let error_text = response.text().await.unwrap_or_default();
            anyhow::bail!("LLM API error ({}): {}", status, error_text);
        }

        // Parse the response
        let response_body: ChatCompletionResponse = response
            .json()
            .await
            .context("Failed to parse LLM API response")?;

        // Debug: log the raw response content
        if std::env::var("BAML_DEBUG").is_ok() {
            if let Some(choice) = response_body.choices.first() {
                eprintln!("[BAML DEBUG] LLM Response:\n{}", choice.message.content);
            }
        }

        // Extract the content
        response_body
            .choices
            .first()
            .and_then(|choice| Some(choice.message.content.clone()))
            .ok_or_else(|| anyhow::anyhow!("No response from LLM"))
    }

    /// Stream LLM response, yielding content deltas as they arrive
    pub async fn call_stream(
        &self,
        prompt: &str,
    ) -> Result<Pin<Box<dyn Stream<Item = Result<String>> + Send>>> {
        let request_body = ChatCompletionRequest {
            model: self.model.clone(),
            messages: vec![Message {
                role: "user".to_string(),
                content: prompt.to_string(),
            }],
            max_tokens: self.max_tokens,
            temperature: self.temperature,
            stream: true,
            provider: self.provider.clone(),
        };

        let client = reqwest::Client::builder()
            .timeout(self.timeout)
            .build()
            .context("Failed to build HTTP client")?;

        let response = client
            .post(format!("{}/chat/completions", self.base_url))
            .header("Authorization", format!("Bearer {}", self.api_key))
            .header("Content-Type", "application/json")
            .json(&request_body)
            .send()
            .await
            .context("Failed to send streaming request to LLM API")?;

        if !response.status().is_success() {
            let status = response.status();
            let error_text = response.text().await.unwrap_or_default();
            anyhow::bail!("LLM API error ({}): {}", status, error_text);
        }

        let stream = response.bytes_stream().map(move |chunk_result| {
            let chunk = chunk_result.context("Failed to read stream chunk")?;
            let text = String::from_utf8_lossy(&chunk);
            
            let mut content = String::new();
            for line in text.lines() {
                if let Some(data) = line.strip_prefix("data: ") {
                    if data.trim() == "[DONE]" {
                        continue;
                    }
                    if let Ok(sse_event) = serde_json::from_str::<StreamChatCompletionChunk>(data) {
                        if let Some(choice) = sse_event.choices.first() {
                            if let Some(delta_content) = &choice.delta.content {
                                content.push_str(delta_content);
                            }
                        }
                    }
                }
            }
            Ok(content)
        });

        Ok(Box::pin(stream))
    }
}

#[async_trait]
impl LLMClientTrait for LLMClient {
    async fn call(&self, prompt: &str) -> Result<String> {
        self.call_direct(prompt).await
    }
}

/// OpenAI Chat Completion Request
#[derive(Debug, Serialize)]
struct ChatCompletionRequest {
    model: String,
    messages: Vec<Message>,
    #[serde(skip_serializing_if = "Option::is_none")]
    max_tokens: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    temperature: Option<f32>,
    stream: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    provider: Option<ProviderPreferences>,
}

#[derive(Debug, Serialize, Deserialize)]
struct Message {
    role: String,
    content: String,
}

/// OpenAI Chat Completion Response
#[derive(Debug, Deserialize)]
struct ChatCompletionResponse {
    choices: Vec<Choice>,
}

#[derive(Debug, Deserialize)]
struct Choice {
    message: Message,
}

/// SSE streaming chunk from OpenAI-compatible APIs
#[derive(Debug, Deserialize)]
struct StreamChatCompletionChunk {
    choices: Vec<StreamChoice>,
}

#[derive(Debug, Deserialize)]
struct StreamChoice {
    delta: Delta,
}

#[derive(Debug, Deserialize)]
struct Delta {
    content: Option<String>,
}

/// Mock client for testing (doesn't make real API calls)
pub struct MockLLMClient {
    responses: HashMap<String, String>,
}

impl MockLLMClient {
    pub fn new() -> Self {
        Self {
            responses: HashMap::new(),
        }
    }

    /// Add a mock response for a specific prompt pattern
    pub fn add_response(&mut self, pattern: &str, response: &str) {
        self.responses.insert(pattern.to_string(), response.to_string());
    }

    /// Call the mock client (non-trait method)
    pub async fn call_direct(&self, prompt: &str) -> Result<String> {
        // Find the first matching pattern
        for (pattern, response) in &self.responses {
            if prompt.contains(pattern) {
                return Ok(response.clone());
            }
        }

        // Default response
        Ok(r#"{"name": "Mock Response", "age": 25}"#.to_string())
    }
}

#[async_trait]
impl LLMClientTrait for MockLLMClient {
    async fn call(&self, prompt: &str) -> Result<String> {
        self.call_direct(prompt).await
    }
}

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

    #[tokio::test]
    async fn test_mock_client() {
        let mut client = MockLLMClient::new();
        client.add_response("Extract person", r#"{"name": "John", "age": 30}"#);

        let response = client.call("Extract person info from text").await.unwrap();
        assert_eq!(response, r#"{"name": "John", "age": 30}"#);
    }

    #[test]
    fn test_client_configuration() {
        let client = LLMClient::openai("test-key".to_string(), "gpt-4".to_string());
        assert_eq!(client.model, "gpt-4");
        assert_eq!(client.base_url, "https://api.openai.com/v1");
    }

    #[test]
    fn test_api_key_redacted_in_debug() {
        let secret_key = "sk-super-secret-api-key-12345";
        let client = LLMClient::openai(secret_key.to_string(), "gpt-4".to_string());
        let debug_output = format!("{:?}", client);
        
        assert!(!debug_output.contains(secret_key), "API key should not appear in debug output");
        assert!(debug_output.contains("[REDACTED]"), "Debug output should show [REDACTED]");
        assert!(debug_output.contains("gpt-4"), "Model should appear in debug output");
    }

    #[test]
    fn test_default_timeout() {
        let client = LLMClient::openai("test-key".to_string(), "gpt-4".to_string());
        assert_eq!(client.timeout, Duration::from_secs(60));
    }

    #[test]
    fn test_custom_timeout() {
        let client = LLMClient::openai("test-key".to_string(), "gpt-4".to_string())
            .with_timeout(Duration::from_secs(120));
        assert_eq!(client.timeout, Duration::from_secs(120));
    }

    #[test]
    fn test_timeout_in_debug_output() {
        let client = LLMClient::openai("test-key".to_string(), "gpt-4".to_string());
        let debug_output = format!("{:?}", client);
        assert!(debug_output.contains("timeout"), "Debug output should include timeout");
    }
}