AI-lib: Unified AI SDK for Rust
A unified Rust SDK that provides a single interface to multiple AI providers using a hybrid architecture
Overview
ai-lib is a unified AI SDK for Rust that offers a single, consistent interface for interacting with multiple large language model providers. It uses a hybrid architecture that balances developer ergonomics with provider-specific features, providing progressive configuration options from simple usage to advanced customization, along with powerful tools for building custom model managers and load-balanced arrays.
Note: Upgrade guides and PR notes have been moved to the docs/
directory. See docs/UPGRADE_0.2.0.md
and docs/PR_0.2.0.md
for migration and PR details.
Supported AI Providers
- ✅ Groq (config-driven) — llama3, mixtral models
- ✅ xAI Grok (config-driven) — grok models
- ✅ DeepSeek (config-driven) — deepseek-chat, deepseek-reasoner
- ✅ Anthropic Claude (config-driven) — claude-3.5-sonnet
- ✅ Google Gemini (independent adapter) — gemini-1.5-pro, gemini-1.5-flash
- ✅ OpenAI (independent adapter) — gpt-3.5-turbo, gpt-4
- ✅ Qwen / Tongyi Qianwen (config-driven) — Qwen family (OpenAI-compatible)
- ✅ Cohere (independent adapter) — command/generate models
- ✅ Baidu Wenxin (ERNIE) (config-driven) — ernie-3.5, ernie-4.0
- ✅ Tencent Hunyuan (config-driven) — hunyuan family
- ✅ iFlytek Spark (config-driven) — spark models (voice+text friendly)
- ✅ Moonshot / Kimi (config-driven) — kimi series (long-text scenarios)
- ✅ Mistral (independent adapter) — mistral models
- ✅ Hugging Face Inference (config-driven) — hub-hosted models
- ✅ TogetherAI (config-driven) — together.ai hosted models
- ✅ Azure OpenAI (config-driven) — Azure-hosted OpenAI endpoints
- ✅ Ollama (config-driven/local) — local Ollama instances
Core Features
🚀 Unified Interface & Provider Switching
Switch between AI providers with a single line of code:
let groq_client = new?;
let gemini_client = new?;
let claude_client = new?;
🎯 Progressive Configuration
Build AI clients with progressive customization levels:
// Level 1: Simple usage with auto-detection
let client = new?;
// Level 2: Custom base URL
let client = new
.with_base_url
.build?;
// Level 3: Add proxy support
let client = new
.with_base_url
.with_proxy
.build?;
// Level 4: Advanced configuration
let client = new
.with_base_url
.with_proxy
.with_timeout
.with_pool_config
.build?;
🌊 Universal Streaming Support
Real-time streaming responses for all providers with SSE parsing and fallback emulation:
use StreamExt;
let mut stream = client.chat_completion_stream.await?;
while let Some = stream.next.await
🔄 Enterprise-Grade Reliability
- Automatic retries with exponential backoff
- Smart error classification (retryable vs. permanent)
- Proxy support with authentication
- Timeout management and graceful degradation
🌐 Flexible Proxy Configuration
The library provides flexible proxy configuration options to avoid automatic environment variable reading:
// Default: No proxy, no environment variable reading
let client = new.build?;
// Explicitly disable proxy
let client = new
.without_proxy
.build?;
// Use specific proxy URL
let client = new
.with_proxy
.build?;
// Use AI_PROXY_URL environment variable
let client = new
.with_proxy
.build?;
match client.chat_completion.await
⚡ Hybrid Architecture
- Config-driven adapters: Minimal wiring for OpenAI-compatible APIs
- Independent adapters: Full control for unique APIs
- Four-layer design: Client → Adapter → Transport → Common types
- Benefits: Code reuse, extensibility, automatic feature inheritance
🏗️ Custom Model Management
Build sophisticated model management systems:
// Performance-based model selection
let mut manager = new
.with_strategy;
// Load-balanced model arrays
let mut array = new
.with_strategy;
array.add_endpoint;
📊 Advanced Capabilities
- Multimodal support: Text, JSON, image, and audio content
- Function calling: Unified
Tool
andFunctionCall
types - Metrics & observability: Request counters and duration timers
- Dependency injection: Mock transports for testing
- Performance: <2MB memory, <1ms overhead, <10ms streaming latency
Quickstart
Installation
[]
= "0.2.11"
= { = "1.0", = ["full"] }
= "0.3"
Basic Usage
use ;
Production Best Practices
use ;
// 1. Use environment variables for configuration
let client = new
.with_timeout
.build?;
// 2. Implement model management
let mut manager = new
.with_strategy;
// 3. Add health checks and monitoring
let mut array = new
.with_strategy;
Environment Variables
Examples
Getting Started
- Quickstart:
cargo run --example quickstart
- Simple usage guide - Builder Pattern:
cargo run --example builder_pattern
- Configuration examples
Advanced Features
- Model Management:
cargo run --example model_management
- Custom managers and load balancing
Core Functionality
- Architecture:
cargo run --example test_hybrid_architecture
- Streaming:
cargo run --example test_streaming_improved
- Retry:
cargo run --example test_retry_mechanism
- Providers:
cargo run --example test_groq_generic
Provider Details
Provider | Architecture | Streaming | Models | Notes |
---|---|---|---|---|
Groq | config-driven | ✅ | llama3-8b/70b, mixtral-8x7b | Fast inference |
DeepSeek | config-driven | ✅ | deepseek-chat, deepseek-reasoner | China-focused |
Anthropic | config-driven | ✅ | claude-3.5-sonnet | Custom auth |
Google Gemini | independent | 🔄 | gemini-1.5-pro/flash | URL auth |
OpenAI | independent | ✅ | gpt-3.5-turbo, gpt-4 | Proxy may be needed |
Qwen | config-driven | ✅ | Qwen family | OpenAI-compatible |
Baidu Wenxin | config-driven | ✅ | ernie-3.5, ernie-4.0 | Qianfan platform |
Tencent Hunyuan | config-driven | ✅ | hunyuan family | Cloud endpoints |
iFlytek Spark | config-driven | ✅ | spark family | Voice+text friendly |
Moonshot Kimi | config-driven | ✅ | kimi family | Long-text scenarios |
Model Management Tools
Key Features
- Selection strategies: Round-robin, weighted, performance-based, cost-based
- Load balancing: Health checks, connection tracking, multiple endpoints
- Cost analysis: Calculate costs for different token counts
- Performance metrics: Speed and quality tiers with response time tracking
Example Usage
use ;
let mut manager = new
.with_strategy;
let model = ModelInfo ;
manager.add_model;
Roadmap
✅ Implemented
- Hybrid architecture with universal streaming
- Enterprise-grade error handling and retry
- Multimodal primitives and function calling
- Progressive client configuration
- Custom model management tools
- Load balancing and health checks
🚧 Planned
- Advanced backpressure API
- Connection pool tuning
- Plugin system
- Built-in caching
Contributing
- Clone:
git clone https://github.com/hiddenpath/ai-lib.git
- Branch:
git checkout -b feature/new-feature
- Test:
cargo test
- PR: Open a pull request
Community & Support
- 📖 Documentation: docs.rs/ai-lib
- 🐛 Issues: GitHub Issues
- 💬 Discussions: GitHub Discussions
License
Dual licensed: MIT or Apache 2.0
Citation