llmkit 0.1.3

Production-grade LLM client - 100+ providers, 11,000+ models. Pure Rust.
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
505
506
507
508
509
510
511
512
513
514
515
//! Fal AI API provider implementation.
//!
//! This module provides access to Fal AI's inference platform, including
//! image generation, LLM inference, and other AI models.
//!
//! # Example
//!
//! ```ignore
//! use llmkit::providers::FalProvider;
//!
//! // From environment variable
//! let provider = FalProvider::from_env()?;
//!
//! // Or with explicit API key
//! let provider = FalProvider::with_api_key("your-api-key")?;
//! ```
//!
//! # Supported Models
//!
//! ## LLM Models
//! - `fal-ai/llavav15-13b` - LLaVA vision-language model
//! - `fal-ai/any-llm` - Router to various LLMs
//!
//! ## Image Generation
//! - `fal-ai/flux/schnell` - Fast FLUX image generation
//! - `fal-ai/flux/dev` - Development FLUX model
//! - `fal-ai/stable-diffusion-v3` - Stable Diffusion 3
//!
//! # Environment Variables
//!
//! - `FAL_KEY` - Your Fal AI API key

use std::pin::Pin;

use async_trait::async_trait;
use futures::Stream;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::error::{Error, Result};
use crate::provider::{Provider, ProviderConfig};
use crate::types::{
    CompletionRequest, CompletionResponse, ContentBlock, ContentDelta, Role, StopReason,
    StreamChunk, StreamEventType, Usage,
};

const FAL_API_URL: &str = "https://fal.run";

/// Fal AI API provider.
///
/// Provides access to Fal AI's inference platform for LLMs and image generation.
pub struct FalProvider {
    config: ProviderConfig,
    client: Client,
}

impl FalProvider {
    /// Create a new Fal AI provider with the given configuration.
    pub fn new(config: ProviderConfig) -> Result<Self> {
        let mut headers = reqwest::header::HeaderMap::new();

        if let Some(ref key) = config.api_key {
            headers.insert(
                reqwest::header::AUTHORIZATION,
                format!("Key {}", key)
                    .parse()
                    .map_err(|_| Error::config("Invalid API key format"))?,
            );
        }

        headers.insert(
            reqwest::header::CONTENT_TYPE,
            "application/json".parse().unwrap(),
        );

        let client = Client::builder()
            .timeout(config.timeout)
            .default_headers(headers)
            .build()?;

        Ok(Self { config, client })
    }

    /// Create a new Fal AI provider from environment variable.
    ///
    /// Reads the API key from `FAL_KEY`.
    pub fn from_env() -> Result<Self> {
        let config = ProviderConfig::from_env("FAL_KEY");
        Self::new(config)
    }

    /// Create a new Fal AI provider with an API key.
    pub fn with_api_key(api_key: impl Into<String>) -> Result<Self> {
        let config = ProviderConfig::new(api_key);
        Self::new(config)
    }

    fn model_url(&self, model: &str) -> String {
        format!(
            "{}/{}",
            self.config.base_url.as_deref().unwrap_or(FAL_API_URL),
            model
        )
    }

    /// Check if a model is an image generation model.
    fn is_image_model(model: &str) -> bool {
        model.contains("flux")
            || model.contains("stable-diffusion")
            || model.contains("sdxl")
            || model.contains("kandinsky")
            || model.contains("midjourney")
    }

    /// Generate an image using Fal AI.
    pub async fn generate_image(&self, model: &str, prompt: &str) -> Result<FalImageResponse> {
        let request = FalImageRequest {
            prompt: prompt.to_string(),
            image_size: Some("landscape_4_3".to_string()),
            num_inference_steps: Some(4),
            num_images: Some(1),
            enable_safety_checker: Some(true),
        };

        let response = self
            .client
            .post(self.model_url(model))
            .json(&request)
            .send()
            .await?;

        if !response.status().is_success() {
            let status = response.status();
            let error_text = response.text().await.unwrap_or_default();
            return Err(Error::server(
                status.as_u16(),
                format!("Fal AI API error {}: {}", status, error_text),
            ));
        }

        let api_response: FalImageResponse = response.json().await?;
        Ok(api_response)
    }

    /// Run LLM inference using Fal AI.
    async fn llm_inference(&self, model: &str, request: &CompletionRequest) -> Result<String> {
        // Extract messages
        let messages: Vec<FalMessage> = request
            .messages
            .iter()
            .map(|m| {
                let role = match m.role {
                    Role::User => "user",
                    Role::Assistant => "assistant",
                    Role::System => "system",
                };
                let content = m
                    .content
                    .iter()
                    .filter_map(|b| {
                        if let ContentBlock::Text { text } = b {
                            Some(text.clone())
                        } else {
                            None
                        }
                    })
                    .collect::<Vec<_>>()
                    .join("\n");
                FalMessage {
                    role: role.to_string(),
                    content,
                }
            })
            .collect();

        let fal_request = FalLlmRequest {
            messages,
            max_tokens: request.max_tokens,
            temperature: request.temperature,
        };

        let response = self
            .client
            .post(self.model_url(model))
            .json(&fal_request)
            .send()
            .await?;

        if !response.status().is_success() {
            let status = response.status();
            let error_text = response.text().await.unwrap_or_default();
            return Err(Error::server(
                status.as_u16(),
                format!("Fal AI API error {}: {}", status, error_text),
            ));
        }

        let api_response: FalLlmResponse = response.json().await?;
        Ok(api_response.output.unwrap_or_default())
    }
}

#[async_trait]
impl Provider for FalProvider {
    fn name(&self) -> &str {
        "fal"
    }

    async fn complete(&self, request: CompletionRequest) -> Result<CompletionResponse> {
        // Check if this is an image generation request
        if Self::is_image_model(&request.model) {
            // Extract prompt from last user message
            let prompt = request
                .messages
                .iter()
                .rfind(|m| matches!(m.role, Role::User))
                .and_then(|m| {
                    m.content.iter().find_map(|block| {
                        if let ContentBlock::Text { text } = block {
                            Some(text.clone())
                        } else {
                            None
                        }
                    })
                })
                .ok_or_else(|| Error::invalid_request("No prompt found for image generation"))?;

            let result = self.generate_image(&request.model, &prompt).await?;

            // Format result with image URLs
            let content = if let Some(images) = result.images {
                let urls: Vec<String> = images.into_iter().map(|img| img.url).collect();
                format!("Generated {} image(s):\n{}", urls.len(), urls.join("\n"))
            } else {
                "Image generation completed but no images returned".to_string()
            };

            return Ok(CompletionResponse {
                id: uuid::Uuid::new_v4().to_string(),
                model: request.model,
                content: vec![ContentBlock::Text { text: content }],
                stop_reason: StopReason::EndTurn,
                usage: Usage {
                    input_tokens: 0,
                    output_tokens: 0,
                    cache_creation_input_tokens: 0,
                    cache_read_input_tokens: 0,
                },
            });
        }

        // LLM inference
        let output = self.llm_inference(&request.model, &request).await?;

        Ok(CompletionResponse {
            id: uuid::Uuid::new_v4().to_string(),
            model: request.model,
            content: vec![ContentBlock::Text { text: output }],
            stop_reason: StopReason::EndTurn,
            usage: Usage {
                input_tokens: 0,
                output_tokens: 0,
                cache_creation_input_tokens: 0,
                cache_read_input_tokens: 0,
            },
        })
    }

    async fn complete_stream(
        &self,
        request: CompletionRequest,
    ) -> Result<Pin<Box<dyn Stream<Item = Result<StreamChunk>> + Send>>> {
        // Fal AI streaming is model-specific, fall back to regular completion
        let response = self.complete(request).await?;

        let stream = async_stream::try_stream! {
            yield StreamChunk {
                event_type: StreamEventType::ContentBlockStart,
                index: Some(0),
                delta: None,
                stop_reason: None,
                usage: None,
            };

            for block in response.content {
                if let ContentBlock::Text { text } = block {
                    yield StreamChunk {
                        event_type: StreamEventType::ContentBlockDelta,
                        index: Some(0),
                        delta: Some(ContentDelta::Text { text }),
                        stop_reason: None,
                        usage: None,
                    };
                }
            }

            yield StreamChunk {
                event_type: StreamEventType::MessageStop,
                index: None,
                delta: None,
                stop_reason: Some(StopReason::EndTurn),
                usage: None,
            };
        };

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

// Fal AI API types

#[derive(Debug, Serialize)]
struct FalImageRequest {
    prompt: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    image_size: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    num_inference_steps: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    num_images: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    enable_safety_checker: Option<bool>,
}

/// Response from Fal AI image generation.
#[derive(Debug, Deserialize)]
pub struct FalImageResponse {
    /// Generated images.
    pub images: Option<Vec<FalImage>>,
    /// Timing information.
    pub timings: Option<Value>,
    /// Seed used for generation.
    pub seed: Option<u64>,
}

/// A generated image.
#[derive(Debug, Deserialize)]
pub struct FalImage {
    /// URL to the generated image.
    pub url: String,
    /// Width of the image.
    pub width: Option<u32>,
    /// Height of the image.
    pub height: Option<u32>,
    /// Content type.
    pub content_type: Option<String>,
}

#[derive(Debug, Serialize)]
struct FalLlmRequest {
    messages: Vec<FalMessage>,
    #[serde(skip_serializing_if = "Option::is_none")]
    max_tokens: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    temperature: Option<f32>,
}

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

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

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

    #[test]
    fn test_provider_creation() {
        let provider = FalProvider::new(ProviderConfig::new("test-key")).unwrap();
        assert_eq!(provider.name(), "fal");
    }

    #[test]
    fn test_provider_with_api_key() {
        let provider = FalProvider::with_api_key("test-key").unwrap();
        assert_eq!(provider.name(), "fal");
    }

    #[test]
    fn test_model_url() {
        let provider = FalProvider::new(ProviderConfig::new("test-key")).unwrap();
        let url = provider.model_url("fal-ai/flux/schnell");
        assert_eq!(url, "https://fal.run/fal-ai/flux/schnell");
    }

    #[test]
    fn test_model_url_custom_base() {
        let mut config = ProviderConfig::new("test-key");
        config.base_url = Some("https://custom.fal.ai".to_string());
        let provider = FalProvider::new(config).unwrap();
        let url = provider.model_url("fal-ai/flux/schnell");
        assert_eq!(url, "https://custom.fal.ai/fal-ai/flux/schnell");
    }

    #[test]
    fn test_is_image_model() {
        // FLUX models
        assert!(FalProvider::is_image_model("fal-ai/flux/schnell"));
        assert!(FalProvider::is_image_model("fal-ai/flux/dev"));
        assert!(FalProvider::is_image_model("fal-ai/flux-pro"));

        // Stable Diffusion models
        assert!(FalProvider::is_image_model("fal-ai/stable-diffusion-v3"));
        assert!(FalProvider::is_image_model("fal-ai/sdxl"));

        // Other image models
        assert!(FalProvider::is_image_model("fal-ai/kandinsky"));
        assert!(FalProvider::is_image_model("fal-ai/midjourney"));

        // Non-image models
        assert!(!FalProvider::is_image_model("fal-ai/any-llm"));
        assert!(!FalProvider::is_image_model("fal-ai/llavav15-13b"));
    }

    #[test]
    fn test_image_request_serialization() {
        let request = FalImageRequest {
            prompt: "A beautiful sunset".to_string(),
            image_size: Some("landscape_4_3".to_string()),
            num_inference_steps: Some(4),
            num_images: Some(1),
            enable_safety_checker: Some(true),
        };

        let json = serde_json::to_string(&request).unwrap();
        assert!(json.contains("A beautiful sunset"));
        assert!(json.contains("landscape_4_3"));
        assert!(json.contains("4"));
        assert!(json.contains("enable_safety_checker"));
    }

    #[test]
    fn test_image_response_deserialization() {
        let json = r#"{
            "images": [{
                "url": "https://fal.ai/image1.png",
                "width": 1024,
                "height": 768,
                "content_type": "image/png"
            }],
            "seed": 12345
        }"#;

        let response: FalImageResponse = serde_json::from_str(json).unwrap();
        assert!(response.images.is_some());
        let images = response.images.unwrap();
        assert_eq!(images.len(), 1);
        assert_eq!(images[0].url, "https://fal.ai/image1.png");
        assert_eq!(images[0].width, Some(1024));
        assert_eq!(images[0].height, Some(768));
        assert_eq!(response.seed, Some(12345));
    }

    #[test]
    fn test_llm_request_serialization() {
        let request = FalLlmRequest {
            messages: vec![FalMessage {
                role: "user".to_string(),
                content: "Hello".to_string(),
            }],
            max_tokens: Some(1000),
            temperature: Some(0.7),
        };

        let json = serde_json::to_string(&request).unwrap();
        assert!(json.contains("Hello"));
        assert!(json.contains("1000"));
        assert!(json.contains("0.7"));
    }

    #[test]
    fn test_llm_response_deserialization() {
        let json = r#"{"output": "Hello! How can I help you?"}"#;
        let response: FalLlmResponse = serde_json::from_str(json).unwrap();
        assert_eq!(
            response.output,
            Some("Hello! How can I help you?".to_string())
        );
    }

    #[test]
    fn test_fal_image_deserialization() {
        let json = r#"{
            "url": "https://example.com/img.png",
            "width": 512,
            "height": 512,
            "content_type": "image/png"
        }"#;

        let image: FalImage = serde_json::from_str(json).unwrap();
        assert_eq!(image.url, "https://example.com/img.png");
        assert_eq!(image.width, Some(512));
        assert_eq!(image.height, Some(512));
        assert_eq!(image.content_type, Some("image/png".to_string()));
    }

    #[test]
    fn test_fal_message_serialization() {
        let message = FalMessage {
            role: "assistant".to_string(),
            content: "I can help you with that!".to_string(),
        };

        let json = serde_json::to_string(&message).unwrap();
        assert!(json.contains("assistant"));
        assert!(json.contains("I can help you with that!"));
    }
}