ai_lib/lib.rs
1//! AI-lib: A Unified AI SDK for Rust
2//!
3//! This library provides a single, consistent interface for interacting with multiple AI model providers.
4//!
5//! # Quick Start
6//!
7//! ```rust
8//! use ai_lib::{AiClient, Provider, ChatCompletionRequest, Message, Role};
9//! use ai_lib::types::common::Content;
10//!
11//! #[tokio::main]
12//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
13//! // Switch provider by changing Provider:: value
14//! let client = AiClient::new(Provider::Groq)?;
15//!
16//! let request = ChatCompletionRequest::new(
17//! "llama3-8b-8192".to_string(),
18//! vec![Message {
19//! role: Role::User,
20//! content: Content::Text("Hello, how are you?".to_string()),
21//! function_call: None,
22//! }],
23//! );
24//!
25//! println!("Client created successfully with provider: {:?}", client.current_provider());
26//! println!("Request prepared for model: {}", request.model);
27//!
28//! Ok(())
29//! }
30//! ```
31//!
32//! # Proxy Support
33//!
34//! AI-lib supports proxy configuration via environment variables:
35//!
36//! ```bash
37//! # Set proxy server
38//! export AI_PROXY_URL=http://proxy.example.com:8080
39//!
40//! # Proxy with authentication
41//! export AI_PROXY_URL=http://username:password@proxy.example.com:8080
42//!
43//! # HTTPS proxy
44//! export AI_PROXY_URL=https://proxy.example.com:8080
45//! ```
46//!
47//! All AI provider requests will automatically use the specified proxy server.
48//!
49//! Optional environment variables for feature-gated capabilities:
50//! - cost_metrics feature:
51//! - `COST_INPUT_PER_1K`: USD per 1000 input tokens
52//! - `COST_OUTPUT_PER_1K`: USD per 1000 output tokens
53//!
54//! These cost metrics are read from environment variables for simplicity.
55//!
56//! Note: In ai-lib-pro, these can be centrally configured and hot-reloaded
57//! via external configuration providers for enterprise deployments.
58
59pub mod api;
60pub mod client;
61pub mod config;
62pub mod metrics;
63pub mod provider;
64pub mod transport;
65pub mod types;
66pub mod utils; // minimal explicit configuration entrypoint
67
68// Feature-gated modules (OSS progressive complexity)
69#[cfg(feature = "interceptors")]
70pub mod interceptors;
71
72#[cfg(feature = "unified_sse")]
73pub mod sse;
74
75#[cfg(feature = "unified_transport")]
76pub mod net { pub use crate::transport::client_factory; }
77
78#[cfg(feature = "observability")]
79pub mod observability;
80
81#[cfg(feature = "config_hot_reload")]
82pub mod config_hot_reload;
83
84// Resilience modules
85pub mod circuit_breaker;
86pub mod rate_limiter;
87pub mod error_handling;
88
89// Re-export main types for user convenience
90pub use api::ChatApi;
91pub use client::{AiClient, AiClientBuilder, ModelOptions, Provider};
92pub use types::{
93 AiLibError, ChatCompletionRequest, ChatCompletionResponse, Choice, Message, Role, Usage,
94};
95// Convenience re-exports: make the most-used types available from the crate root so
96// users don't need deep imports for common flows.
97pub use api::ChatCompletionChunk;
98pub use client::CancelHandle;
99pub use metrics::{Metrics, MetricsExt, NoopMetrics, NoopTimer, Timer};
100pub use transport::{
101 DynHttpTransport, DynHttpTransportRef, HttpClient, HttpTransport, TransportError,
102};
103// Re-export minimal configuration type
104pub use config::ConnectionOptions;
105pub use types::common::Content;
106
107// Re-export configuration types
108pub use provider::config::{FieldMapping, ProviderConfig};
109pub use provider::configs::ProviderConfigs;
110
111// Re-export model management tools (feature-gated)
112#[cfg(feature = "routing_mvp")]
113pub use provider::models::{
114 CustomModelManager, LoadBalancingStrategy, ModelArray, ModelCapabilities, ModelEndpoint,
115 ModelInfo, ModelSelectionStrategy, PerformanceMetrics, PricingInfo, QualityTier, SpeedTier,
116};
117
118// Re-export batch processing functionality
119pub use api::chat::{batch_utils, BatchResult};
120
121// Re-export enhanced file utilities
122pub use utils::file::{
123 create_temp_dir, get_file_extension, get_file_size, guess_mime_from_path, is_audio_file,
124 is_file_size_acceptable, is_image_file, is_text_file, is_video_file, read_file, remove_file,
125 save_temp_file, validate_file,
126};