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//! # 代理服务器支持
33//!
34//! AI-lib 支持通过环境变量配置代理服务器:
35//!
36//! ```bash
37//! # 设置代理服务器
38//! export AI_PROXY_URL=http://proxy.example.com:8080
39//!
40//! # 带认证的代理
41//! export AI_PROXY_URL=http://username:password@proxy.example.com:8080
42//!
43//! # HTTPS代理
44//! export AI_PROXY_URL=https://proxy.example.com:8080
45//! ```
46//!
47//! 设置后,所有AI提供商的请求都会自动通过指定的代理服务器。
48
49pub mod api;
50pub mod client;
51pub mod metrics;
52pub mod provider;
53pub mod transport;
54pub mod types;
55pub mod utils;
56
57// 重新导出主要类型,方便用户使用
58pub use api::ChatApi;
59pub use client::{AiClient, Provider};
60pub use types::{
61    AiLibError, ChatCompletionRequest, ChatCompletionResponse, Choice, Message, Role, Usage,
62};
63// Convenience re-exports: make the most-used types available from the crate root so
64// users don't need deep imports for common flows.
65pub use api::ChatCompletionChunk;
66pub use types::common::Content;
67pub use client::CancelHandle;
68pub use transport::{DynHttpTransport, DynHttpTransportRef, HttpTransport, HttpClient, TransportError};
69pub use metrics::{Metrics, NoopMetrics, Timer, NoopTimer, MetricsExt};
70
71// 重新导出配置相关类型
72pub use provider::config::{ProviderConfig, FieldMapping};
73pub use provider::configs::ProviderConfigs;
74
75    // 重新导出批处理功能
76    pub use api::chat::{BatchResult, batch_utils};
77
78// 重新导出增强的文件工具
79pub use utils::file::{
80    save_temp_file, read_file, remove_file, guess_mime_from_path,
81    validate_file, get_file_size, create_temp_dir,
82    is_image_file, is_audio_file, is_video_file, is_text_file,
83    get_file_extension, is_file_size_acceptable
84};