openrouter_rs/
lib.rs

1//! # OpenRouter Rust SDK
2//!
3//! `openrouter-rs` is a type-safe, async Rust SDK for the [OpenRouter API](https://openrouter.ai/),
4//! providing easy access to 200+ AI models from providers like OpenAI, Anthropic, Google, and more.
5//!
6//! ## ✨ Key Features
7//!
8//! - **🔒 Type Safety**: Leverages Rust's type system for compile-time error prevention
9//! - **⚡ Async/Await**: Built on `tokio` for high-performance async operations  
10//! - **🏗️ Builder Pattern**: Ergonomic client and request construction
11//! - **📡 Streaming Support**: Real-time response streaming with `futures`
12//! - **🧠 Reasoning Tokens**: Advanced support for chain-of-thought reasoning
13//! - **⚙️ Model Presets**: Pre-configured model groups for different use cases
14//! - **🎯 Full API Coverage**: Complete OpenRouter API endpoint support
15//!
16//! ## 🚀 Quick Start
17//!
18//! Add to your `Cargo.toml`:
19//! ```toml
20//! [dependencies]
21//! openrouter-rs = "0.4.5"
22//! tokio = { version = "1", features = ["full"] }
23//! ```
24//!
25//! ### Basic Chat Completion
26//!
27//! ```rust
28//! use openrouter_rs::{
29//!     OpenRouterClient,
30//!     api::chat::{ChatCompletionRequest, Message},
31//!     types::Role,
32//! };
33//!
34//! #[tokio::main]
35//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
36//!     // Create client with builder pattern
37//!     let client = OpenRouterClient::builder()
38//!         .api_key("your_api_key")
39//!         .http_referer("https://yourapp.com")
40//!         .x_title("My App")
41//!         .build()?;
42//!
43//!     // Build chat request
44//!     let request = ChatCompletionRequest::builder()
45//!         .model("anthropic/claude-sonnet-4")
46//!         .messages(vec![
47//!             Message::new(Role::System, "You are a helpful assistant"),
48//!             Message::new(Role::User, "Explain Rust ownership in simple terms"),
49//!         ])
50//!         .temperature(0.7)
51//!         .max_tokens(500)
52//!         .build()?;
53//!
54//!     // Send request and get response
55//!     let response = client.send_chat_completion(&request).await?;
56//!     println!("Response: {}", response.choices[0].content().unwrap_or(""));
57//!
58//!     Ok(())
59//! }
60//! ```
61//!
62//! ### Streaming Responses
63//!
64//! ```rust
65//! use futures_util::StreamExt;
66//! use openrouter_rs::{OpenRouterClient, api::chat::*};
67//!
68//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
69//! let client = OpenRouterClient::builder()
70//!     .api_key("your_api_key")
71//!     .build()?;
72//!
73//! let request = ChatCompletionRequest::builder()
74//!     .model("google/gemini-2.5-flash")
75//!     .messages(vec![Message::new(Role::User, "Write a haiku about Rust")])
76//!     .build()?;
77//!
78//! let mut stream = client.stream_chat_completion(&request).await?;
79//!
80//! while let Some(result) = stream.next().await {
81//!     if let Ok(response) = result {
82//!         if let Some(content) = response.choices[0].content() {
83//!             print!("{}", content);
84//!         }
85//!     }
86//! }
87//! # Ok(())
88//! # }
89//! ```
90//!
91//! ### Reasoning Tokens (Chain-of-Thought)
92//!
93//! ```rust
94//! use openrouter_rs::{OpenRouterClient, api::chat::*, types::{Role, Effort}};
95//!
96//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
97//! let client = OpenRouterClient::builder()
98//!     .api_key("your_api_key")
99//!     .build()?;
100//!
101//! let request = ChatCompletionRequest::builder()
102//!     .model("deepseek/deepseek-r1")
103//!     .messages(vec![Message::new(Role::User, "What's bigger: 9.9 or 9.11?")])
104//!     .reasoning_effort(Effort::High)  // Enable high-effort reasoning
105//!     .reasoning_max_tokens(1000)      // Limit reasoning tokens
106//!     .build()?;
107//!
108//! let response = client.send_chat_completion(&request).await?;
109//!
110//! println!("Reasoning: {}", response.choices[0].reasoning().unwrap_or(""));
111//! println!("Answer: {}", response.choices[0].content().unwrap_or(""));
112//! # Ok(())
113//! # }
114//! ```
115//!
116//! ## 📚 Core Modules
117//!
118//! - [`client`] - Client configuration and HTTP operations
119//! - [`api`] - OpenRouter API endpoints (chat, models, credits, etc.)
120//! - [`types`] - Request/response types and enums
121//! - [`config`] - Configuration management and model presets
122//! - [`error`] - Error types and handling
123//!
124//! ## 🎯 Model Presets
125//!
126//! The SDK includes curated model presets for different use cases:
127//!
128//! - **`programming`**: Code generation and software development
129//! - **`reasoning`**: Advanced reasoning and problem-solving  
130//! - **`free`**: Free-tier models for experimentation
131//!
132//! ```rust
133//! use openrouter_rs::config::OpenRouterConfig;
134//!
135//! let config = OpenRouterConfig::default();
136//! println!("Available models: {:?}", config.get_resolved_models());
137//! ```
138//!
139//! ## 🔗 API Coverage
140//!
141//! | Feature | Status | Module |
142//! |---------|--------|---------|
143//! | Chat Completions | ✅ | [`api::chat`] |
144//! | Text Completions | ✅ | [`api::completion`] |
145//! | Model Information | ✅ | [`api::models`] |
146//! | Streaming | ✅ | [`api::chat`] |
147//! | Reasoning Tokens | ✅ | [`api::chat`] |
148//! | API Key Management | ✅ | [`api::api_keys`] |
149//! | Credit Management | ✅ | [`api::credits`] |
150//! | Generation Data | ✅ | [`api::generation`] |
151//! | Authentication | ✅ | [`api::auth`] |
152//!
153//! ## 📖 Examples
154//!
155//! Check out the [`examples/`](https://github.com/realmorrisliu/openrouter-rs/tree/main/examples)
156//! directory for comprehensive usage examples:
157//!
158//! - Basic chat completion
159//! - Streaming responses  
160//! - Reasoning tokens
161//! - Model management
162//! - Error handling
163//! - Advanced configurations
164//!
165//! ## 🤝 Contributing
166//!
167//! Contributions are welcome! Please see our
168//! [GitHub repository](https://github.com/realmorrisliu/openrouter-rs) for issues and pull requests.
169
170pub mod api;
171pub mod client;
172pub mod config;
173pub mod error;
174pub mod types;
175pub mod utils;
176
177pub use api::chat::Message;
178pub use api::models::Model;
179pub use client::OpenRouterClient;