Expand description
AI-lib: A Unified AI SDK for Rust
This library provides a single, consistent interface for interacting with multiple AI model providers.
§Quick Start
use ai_lib::{AiClient, Provider, ChatCompletionRequest, Message, Role};
use ai_lib::types::common::Content;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Switch provider by changing Provider:: value
let client = AiClient::new(Provider::Groq)?;
let request = ChatCompletionRequest::new(
"llama3-8b-8192".to_string(),
vec![Message {
role: Role::User,
content: Content::Text("Hello, how are you?".to_string()),
function_call: None,
}],
);
println!("Client created successfully with provider: {:?}", client.current_provider());
println!("Request prepared for model: {}", request.model);
Ok(())
}
§Proxy Support
AI-lib supports proxy configuration via environment variables:
# Set proxy server
export AI_PROXY_URL=http://proxy.example.com:8080
# Proxy with authentication
export AI_PROXY_URL=http://username:password@proxy.example.com:8080
# HTTPS proxy
export AI_PROXY_URL=https://proxy.example.com:8080
All AI provider requests will automatically use the specified proxy server.
Re-exports§
pub use api::ChatApi;
pub use client::AiClient;
pub use client::AiClientBuilder;
pub use client::Provider;
pub use types::AiLibError;
pub use types::ChatCompletionRequest;
pub use types::ChatCompletionResponse;
pub use types::Choice;
pub use types::Message;
pub use types::Role;
pub use types::Usage;
pub use api::ChatCompletionChunk;
pub use client::CancelHandle;
pub use metrics::Metrics;
pub use metrics::MetricsExt;
pub use metrics::NoopMetrics;
pub use metrics::NoopTimer;
pub use metrics::Timer;
pub use transport::DynHttpTransport;
pub use transport::DynHttpTransportRef;
pub use transport::HttpClient;
pub use transport::HttpTransport;
pub use transport::TransportError;
pub use types::common::Content;
pub use provider::config::FieldMapping;
pub use provider::config::ProviderConfig;
pub use provider::configs::ProviderConfigs;
pub use provider::models::CustomModelManager;
pub use provider::models::LoadBalancingStrategy;
pub use provider::models::ModelArray;
pub use provider::models::ModelCapabilities;
pub use provider::models::ModelEndpoint;
pub use provider::models::ModelInfo;
pub use provider::models::ModelSelectionStrategy;
pub use provider::models::PerformanceMetrics;
pub use provider::models::PricingInfo;
pub use provider::models::QualityTier;
pub use provider::models::SpeedTier;
pub use api::chat::batch_utils;
pub use api::chat::BatchResult;
pub use utils::file::create_temp_dir;
pub use utils::file::get_file_extension;
pub use utils::file::get_file_size;
pub use utils::file::guess_mime_from_path;
pub use utils::file::is_audio_file;
pub use utils::file::is_file_size_acceptable;
pub use utils::file::is_image_file;
pub use utils::file::is_text_file;
pub use utils::file::is_video_file;
pub use utils::file::read_file;
pub use utils::file::remove_file;
pub use utils::file::save_temp_file;
pub use utils::file::validate_file;