pub struct MultiProvider { /* private fields */ }Expand description
Intelligent routing provider with fallback support.
MultiProvider implements the Provider trait and provides automatic
model selection based on task complexity, with circuit breaker protection
and ordered fallback for resilience.
§Type Parameters
R: The complexity router type (default:DefaultRouter)F: The fallback chain type (default:FallbackChain)
Implementations§
Source§impl MultiProvider
impl MultiProvider
Sourcepub fn new(config: MultiProviderConfig) -> Self
pub fn new(config: MultiProviderConfig) -> Self
Sourcepub fn with_router(router: impl ComplexityRouter + 'static) -> Self
pub fn with_router(router: impl ComplexityRouter + 'static) -> Self
Sourcepub fn with_config_and_router(
config: MultiProviderConfig,
router: impl ComplexityRouter + 'static,
) -> Self
pub fn with_config_and_router( config: MultiProviderConfig, router: impl ComplexityRouter + 'static, ) -> Self
Sourcepub fn set_router(self, router: impl ComplexityRouter + 'static) -> Self
pub fn set_router(self, router: impl ComplexityRouter + 'static) -> Self
Sourcepub fn with_fallback(self, fallback: FallbackChain) -> Self
pub fn with_fallback(self, fallback: FallbackChain) -> Self
Sourcepub fn set_fallback(&mut self, fallback: FallbackChain)
pub fn set_fallback(&mut self, fallback: FallbackChain)
Set the fallback chain by reference.
Sourcepub fn register_provider(&mut self, name: &str, provider: Arc<dyn Provider>)
pub fn register_provider(&mut self, name: &str, provider: Arc<dyn Provider>)
Register a provider with this MultiProvider.
The provider can be referenced by name when calling stream().
Each provider gets its own circuit breaker instance.
§Arguments
name- Provider identifier (e.g., “openai”, “anthropic”)provider- The provider implementation
§Example
let openai_provider = Arc::new(OpenAiProvider::new());
multi_provider.register_provider("openai", openai_provider);Sourcepub fn unregister_provider(&mut self, name: &str) -> bool
pub fn unregister_provider(&mut self, name: &str) -> bool
Sourcepub fn get_breaker(
&self,
provider_name: &str,
) -> Option<Arc<ProviderCircuitBreaker>>
pub fn get_breaker( &self, provider_name: &str, ) -> Option<Arc<ProviderCircuitBreaker>>
Sourcepub fn provider_names(&self) -> Vec<&str>
pub fn provider_names(&self) -> Vec<&str>
Get all registered provider names.
Sourcepub fn circuit_breaker_diagnostics(&self) -> Vec<CircuitBreakerDiagnostics>
pub fn circuit_breaker_diagnostics(&self) -> Vec<CircuitBreakerDiagnostics>
Get diagnostic information for all circuit breakers.
§Returns
Vector of diagnostics for each registered provider.
Sourcepub fn router(&self) -> &Arc<dyn ComplexityRouter> ⓘ
pub fn router(&self) -> &Arc<dyn ComplexityRouter> ⓘ
Get the router used for complexity-based routing.
Sourcepub fn fallback(&self) -> &FallbackChain
pub fn fallback(&self) -> &FallbackChain
Get a reference to the fallback chain.
Sourcepub fn config(&self) -> &MultiProviderConfig
pub fn config(&self) -> &MultiProviderConfig
Get a reference to the configuration.
Sourcepub fn diagnostics(&self) -> MultiProviderDiagnostics
pub fn diagnostics(&self) -> MultiProviderDiagnostics
Get diagnostic summary of the multi-provider state.
Trait Implementations§
Source§impl Provider for MultiProvider
impl Provider for MultiProvider
Source§fn stream<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
model: &'life1 Model,
context: &'life2 Context,
options: Option<StreamOptions>,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = ProviderEvent> + Send>>, ProviderError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn stream<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
model: &'life1 Model,
context: &'life2 Context,
options: Option<StreamOptions>,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = ProviderEvent> + Send>>, ProviderError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Stream assistant message events with intelligent routing.
This method implements the priority order logic:
- If
auto_routing=true: classify complexity and select router’s best model - Try the incoming model (if registered and circuit breaker allows)
- Try fallback chain models in order
For each candidate model:
- Check circuit breaker (skip if open)
- Call provider.stream()
- On retryable error: record failure, retry or move to next
- On non-retryable error: return immediately
- On success: record success to breaker, return stream