Providers
The Providers
class in YosrAI manages integrations with various Language Model providers. It provides a unified interface for accessing different LLM providers and their models, with built-in configuration management and dynamic loading.
Class Overview
from yosrai.core import Providers
from yosrai.utils.config import Config
# Initialize providers
providers = Providers(config=Config())
# Access a provider
openai = providers("openai")
Supported Providers
YosrAI supports multiple LLM providers out of the box:
- OpenAI
- Anthropic
- Groq
- Mistral
- Ollama
- Fireworks
- Google AI
- NVIDIA
- Together
- XAI
- Cohere
- Nebius
- DeepSeek
Usage Examples
Basic Provider Access
from yosrai.core import Providers
providers = Providers()
# Access OpenAI provider
openai = providers("openai")
# Access Anthropic provider
anthropic = providers("anthropic")
Getting Available Models
# List models for a provider
openai_models = providers.get_models("openai")
# Get default model for a provider
default_model = providers.get_default_model("openai")
Listing All Providers
Visualizing Provider Structure
Configuration
The Providers class can be initialized with custom configuration:
from yosrai.utils.config import Config
config = Config(
api_keys={
"openai": "your-api-key",
"anthropic": "your-api-key"
}
)
providers = Providers(config=config)
Provider Management
Dynamic Loading
Providers are loaded dynamically when first accessed:
# Provider is loaded only when accessed
openai = providers("openai") # First access loads the provider
Error Handling
The class includes robust error handling:
# Safe provider access with fallback
try:
provider = providers("unknown_provider")
except Exception as e:
print(f"Provider not available: {e}")
Integration with LLM Class
The Providers class works seamlessly with the LLM class:
from yosrai.core import LLM
# Get provider and create LLM instance
provider = providers("openai")
llm = LLM(
provider=provider.provider_name,
model=providers.get_default_model("openai")
)
Provider Features
Each provider instance offers:
- List of available models
- Default model selection
- Provider-specific configurations
- API key management
- Model compatibility checks
Methods
call
Access and load a specific provider.
list
Get list of all available providers.
get_models
Get list of models available for a provider.
get_default_model
Get the default model for a provider.
print_tree
Display a tree view of providers and their models.
Best Practices
-
Configuration Management:
-
Error Handling:
-
Model Selection:
-
Provider Inspection:
Advanced Usage
Custom Provider Integration
# Access provider-specific features
provider = providers("openai")
if hasattr(provider, "custom_feature"):
result = provider.custom_feature()
Provider-Specific Configuration
config = Config(
provider_configs={
"openai": {
"organization": "org-id",
"api_version": "2024-01"
}
}
)
providers = Providers(config=config)