Skip to main content

litellm_rs/
lib.rs

1//! # LiteLLM-RS
2//!
3//! A Rust implementation of Python LiteLLM - call 100+ LLM APIs using OpenAI format.
4//! High-performance AI Gateway with unified interface for multiple providers.
5//!
6//! ## Features
7//!
8//! - **Python LiteLLM Compatible**: Drop-in replacement with same API design
9//! - **OpenAI Compatible**: Full compatibility with OpenAI API format
10//! - **Multi-Provider**: Support for 100+ AI providers (OpenAI, Anthropic, Azure, Google, etc.)
11//! - **Unified Interface**: Call any LLM using the same function signature
12//! - **High Performance**: Built with Rust and Tokio for maximum throughput
13//! - **Intelligent Routing**: Smart load balancing and failover across providers
14//! - **Cost Optimization**: Automatic cost tracking and provider selection
15//! - **Streaming Support**: Real-time response streaming
16//!
17//! ## Quick Start - Python LiteLLM Style
18//!
19//! ```rust,no_run
20//! use litellm_rs::{completion, user_message, system_message};
21//!
22//! #[tokio::main]
23//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
24//!     // Call OpenAI (default provider for gpt-* models)
25//!     let response = completion(
26//!         "gpt-4",
27//!         vec![
28//!             system_message("You are a helpful assistant."),
29//!             user_message("Hello, how are you?"),
30//!         ],
31//!         None,
32//!     ).await?;
33//!     
34//!     if let Some(content) = &response.choices[0].message.content {
35//!         println!("Response: {}", content);
36//!     }
37//!
38//!     // Call Anthropic with explicit provider
39//!     let response = completion(
40//!         "anthropic/claude-3-sonnet-20240229",
41//!         vec![user_message("What is the capital of France?")],
42//!         None,
43//!     ).await?;
44//!     
45//!     if let Some(content) = &response.choices[0].message.content {
46//!         println!("Claude says: {}", content);
47//!     }
48//!     
49//!     Ok(())
50//! }
51//! ```
52//!
53//! ## Gateway Mode
54//!
55//! ```rust,no_run
56//! use litellm_rs::{Gateway, Config};
57//!
58//! #[tokio::main]
59//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
60//!     let config = Config::from_file("config/gateway.yaml").await?;
61//!     let gateway = Gateway::new(config).await?;
62//!     gateway.run().await?;
63//!     Ok(())
64//! }
65//! ```
66
67#![allow(missing_docs)]
68#![warn(clippy::all)]
69#![allow(clippy::module_inception)]
70
71// Public module exports
72mod auth;
73// Core completion API moved to core::completion
74pub mod config;
75pub mod core;
76mod monitoring;
77pub mod sdk; // New SDK module
78pub mod server;
79pub mod services; // Add services module
80pub mod storage;
81pub mod utils;
82pub mod version; // Build and version information
83
84// Re-export main types
85pub use config::Config;
86pub use utils::error::{GatewayError, Result};
87pub use version::{BuildInfo, GIT_HASH, VERSION, build_info, full_version};
88
89// Export core completion functionality (Python LiteLLM compatible)
90pub use core::completion::{
91    Choice, CompletionOptions, CompletionResponse, ContentPart, LiteLLMError, Message, Router,
92    Usage, acompletion, assistant_message, completion, completion_stream, system_message,
93    user_message,
94};
95
96// Export core embedding functionality (Python LiteLLM compatible)
97pub use core::embedding::{
98    EmbeddingInput, EmbeddingOptions, EmbeddingResponse, aembedding, cosine_similarity,
99    dot_product, embed_text, embed_texts, embed_texts_with_options, embedding, euclidean_distance,
100    normalize,
101};
102
103// Export streaming types
104pub use core::streaming::types::{
105    ChatCompletionChunk, ChatCompletionChunkChoice, ChatCompletionDelta,
106};
107
108// Export unified type system
109pub use core::types::{MessageContent, MessageRole};
110
111// Export core functionality
112pub use core::models::{RequestContext, openai::*};
113pub use core::providers::{
114    Provider, ProviderError, ProviderRegistry, ProviderType, UnifiedProviderError,
115};
116
117// Export unified router
118pub use core::router::{
119    CooldownReason, Deployment, DeploymentConfig, FallbackConfig, FallbackType, RouterConfig,
120    RouterError, UnifiedRouter, UnifiedRoutingStrategy as RoutingStrategy,
121};
122
123use tracing::info;
124
125/// A minimal LiteLLM Gateway implementation
126pub struct Gateway {
127    config: Config,
128    server: server::server::HttpServer,
129}
130
131impl Gateway {
132    /// Create a new gateway instance
133    pub async fn new(config: Config) -> Result<Self> {
134        info!("Creating new gateway instance");
135
136        // Create HTTP server
137        let server = server::server::HttpServer::new(&config).await?;
138
139        Ok(Self { config, server })
140    }
141
142    /// Run the gateway server
143    pub async fn run(self) -> Result<()> {
144        info!("Starting LiteLLM Gateway");
145        info!("Configuration: {:#?}", self.config);
146
147        // Start HTTP server
148        self.server.start().await?;
149
150        Ok(())
151    }
152}
153
154// Version information - re-exported from version module
155/// Name of the crate
156pub const NAME: &str = env!("CARGO_PKG_NAME");
157/// Description of the crate
158pub const DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");
159
160#[cfg(test)]
161mod tests {
162    use super::*;
163
164    #[test]
165    fn test_build_info() {
166        let info = build_info();
167        assert!(!info.version.is_empty());
168        assert_eq!(info.version, VERSION);
169    }
170
171    #[test]
172    fn test_constants() {
173        // Test that constants are defined and have expected values
174        assert_eq!(VERSION, env!("CARGO_PKG_VERSION"));
175        assert_eq!(NAME, env!("CARGO_PKG_NAME"));
176        assert_eq!(DESCRIPTION, env!("CARGO_PKG_DESCRIPTION"));
177    }
178}