File size: 32,563 Bytes
c5828bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 | """
ModelRouter - Multi-Model LLM Orchestration Framework
This module implements the model routing architecture that allows agents to
interact with various LLM providers including:
- OpenAI (GPT models)
- Anthropic (Claude models)
- Groq (Llama and Mixtral)
- Ollama (local models)
- DeepSeek (DeepSeek models)
Key capabilities:
- Provider-agnostic interface for agent interactions
- Dynamic provider selection based on capabilities
- Fallback chains for reliability
- Prompt template management with provider-specific optimizations
- Token usage tracking and optimization
- Output parsing and normalization
Internal Note: The model router implements a symbolic abstraction layer over
different LLM providers while maintaining a unified attribution interface.
"""
import os
import json
import logging
import time
import asyncio
from typing import Dict, List, Any, Optional, Union, Callable
import traceback
from enum import Enum
from abc import ABC, abstractmethod
# Optional imports for different providers
try:
import openai
except ImportError:
openai = None
try:
import anthropic
except ImportError:
anthropic = None
try:
import groq
except ImportError:
groq = None
try:
import ollama
except ImportError:
ollama = None
class ModelCapability(Enum):
"""Capabilities that models may support."""
REASONING = "reasoning" # Complex multi-step reasoning
CODE_GENERATION = "code_generation" # Code writing and analysis
FINANCE = "finance" # Financial analysis and modeling
RAG = "rag" # Retrieval augmented generation
TOOL_USE = "tool_use" # Using external tools
FUNCTION_CALLING = "function_calling" # Structured function calling
JSON_MODE = "json_mode" # Reliable JSON output
class ModelProvider(ABC):
"""Abstract base class for model providers."""
@abstractmethod
def generate(self, prompt: str, **kwargs) -> str:
"""Generate text from prompt."""
pass
@abstractmethod
def get_available_models(self) -> List[str]:
"""Get list of available models."""
pass
@abstractmethod
def get_model_capabilities(self, model_name: str) -> List[ModelCapability]:
"""Get capabilities of a specific model."""
pass
class OpenAIProvider(ModelProvider):
"""OpenAI model provider."""
def __init__(self, api_key: Optional[str] = None):
"""
Initialize OpenAI provider.
Args:
api_key: OpenAI API key (defaults to OPENAI_API_KEY env var)
"""
self.api_key = api_key or os.environ.get("OPENAI_API_KEY")
if not self.api_key:
logging.warning("OpenAI API key not provided. OpenAI provider will not work.")
if openai is None:
logging.warning("OpenAI Python package not installed. OpenAI provider will not work.")
# Initialize client if possible
self.client = None
if openai is not None and self.api_key:
self.client = openai.OpenAI(api_key=self.api_key)
# Define models and capabilities
self.models = {
"gpt-4-0125-preview": [
ModelCapability.REASONING,
ModelCapability.CODE_GENERATION,
ModelCapability.FINANCE,
ModelCapability.RAG,
ModelCapability.TOOL_USE,
ModelCapability.FUNCTION_CALLING,
ModelCapability.JSON_MODE,
],
"gpt-4-turbo-preview": [
ModelCapability.REASONING,
ModelCapability.CODE_GENERATION,
ModelCapability.FINANCE,
ModelCapability.RAG,
ModelCapability.TOOL_USE,
ModelCapability.FUNCTION_CALLING,
ModelCapability.JSON_MODE,
],
"gpt-4": [
ModelCapability.REASONING,
ModelCapability.CODE_GENERATION,
ModelCapability.FINANCE,
ModelCapability.RAG,
ModelCapability.TOOL_USE,
ModelCapability.FUNCTION_CALLING,
],
"gpt-3.5-turbo": [
ModelCapability.CODE_GENERATION,
ModelCapability.RAG,
ModelCapability.TOOL_USE,
ModelCapability.FUNCTION_CALLING,
ModelCapability.FUNCTION_CALLING,
ModelCapability.JSON_MODE,
],
"gpt-3.5-turbo-instruct": [
ModelCapability.CODE_GENERATION,
ModelCapability.RAG,
],
}
def generate(self, prompt: str, **kwargs) -> str:
"""
Generate text from prompt using OpenAI.
Args:
prompt: Input prompt
**kwargs: Additional parameters
- model: Model name (default: gpt-4-turbo-preview)
- temperature: Temperature (default: 0.7)
- max_tokens: Maximum tokens (default: 2000)
- json_mode: Whether to enforce JSON output (default: False)
Returns:
Generated text
"""
if self.client is None:
raise ValueError("OpenAI client not initialized. Provide a valid API key.")
# Extract parameters with defaults
model = kwargs.get("model", "gpt-4-turbo-preview")
temperature = kwargs.get("temperature", 0.7)
max_tokens = kwargs.get("max_tokens", 2000)
json_mode = kwargs.get("json_mode", False)
try:
# Create messages
messages = [{"role": "user", "content": prompt}]
# Create parameters
params = {
"model": model,
"messages": messages,
"temperature": temperature,
"max_tokens": max_tokens,
}
# Add response format if JSON mode
if json_mode:
params["response_format"] = {"type": "json_object"}
# Make request
response = self.client.chat.completions.create(**params)
# Extract text
return response.choices[0].message.content
except Exception as e:
logging.error(f"Error generating text with OpenAI: {e}")
logging.error(traceback.format_exc())
raise
def get_available_models(self) -> List[str]:
"""Get list of available models."""
return list(self.models.keys())
def get_model_capabilities(self, model_name: str) -> List[ModelCapability]:
"""Get capabilities of a specific model."""
return self.models.get(model_name, [])
class AnthropicProvider(ModelProvider):
"""Anthropic model provider."""
def __init__(self, api_key: Optional[str] = None):
"""
Initialize Anthropic provider.
Args:
api_key: Anthropic API key (defaults to ANTHROPIC_API_KEY env var)
"""
self.api_key = api_key or os.environ.get("ANTHROPIC_API_KEY")
if not self.api_key:
logging.warning("Anthropic API key not provided. Anthropic provider will not work.")
if anthropic is None:
logging.warning("Anthropic Python package not installed. Anthropic provider will not work.")
# Initialize client if possible
self.client = None
if anthropic is not None and self.api_key:
self.client = anthropic.Anthropic(api_key=self.api_key)
# Define models and capabilities
self.models = {
"claude-3-opus-20240229": [
ModelCapability.REASONING,
ModelCapability.CODE_GENERATION,
ModelCapability.FINANCE,
ModelCapability.RAG,
ModelCapability.TOOL_USE,
ModelCapability.JSON_MODE,
],
"claude-3-sonnet-20240229": [
ModelCapability.REASONING,
ModelCapability.CODE_GENERATION,
ModelCapability.FINANCE,
ModelCapability.RAG,
ModelCapability.TOOL_USE,
ModelCapability.JSON_MODE,
],
"claude-3-haiku-20240307": [
ModelCapability.CODE_GENERATION,
ModelCapability.RAG,
ModelCapability.JSON_MODE,
],
"claude-2.1": [
ModelCapability.REASONING,
ModelCapability.CODE_GENERATION,
ModelCapability.FINANCE,
ModelCapability.RAG,
],
"claude-2.0": [
ModelCapability.REASONING,
ModelCapability.CODE_GENERATION,
ModelCapability.FINANCE,
ModelCapability.RAG,
],
"claude-instant-1.2": [
ModelCapability.CODE_GENERATION,
ModelCapability.RAG,
],
}
def generate(self, prompt: str, **kwargs) -> str:
"""
Generate text from prompt using Anthropic.
Args:
prompt: Input prompt
**kwargs: Additional parameters
- model: Model name (default: claude-3-sonnet-20240229)
- temperature: Temperature (default: 0.7)
- max_tokens: Maximum tokens (default: 2000)
- system_prompt: Optional system prompt
Returns:
Generated text
"""
if self.client is None:
raise ValueError("Anthropic client not initialized. Provide a valid API key.")
# Extract parameters with defaults
model = kwargs.get("model", "claude-3-sonnet-20240229")
temperature = kwargs.get("temperature", 0.7)
max_tokens = kwargs.get("max_tokens", 2000)
system_prompt = kwargs.get("system_prompt", "")
try:
# Create parameters
params = {
"model": model,
"messages": [{"role": "user", "content": prompt}],
"temperature": temperature,
"max_tokens": max_tokens,
}
# Add system prompt if provided
if system_prompt:
params["system"] = system_prompt
# Make request
response = self.client.messages.create(**params)
# Extract text
return response.content[0].text
except Exception as e:
logging.error(f"Error generating text with Anthropic: {e}")
logging.error(traceback.format_exc())
raise
def get_available_models(self) -> List[str]:
"""Get list of available models."""
return list(self.models.keys())
def get_model_capabilities(self, model_name: str) -> List[ModelCapability]:
"""Get capabilities of a specific model."""
return self.models.get(model_name, [])
class GroqProvider(ModelProvider):
"""Groq model provider."""
def __init__(self, api_key: Optional[str] = None):
"""
Initialize Groq provider.
Args:
api_key: Groq API key (defaults to GROQ_API_KEY env var)
"""
self.api_key = api_key or os.environ.get("GROQ_API_KEY")
if not self.api_key:
logging.warning("Groq API key not provided. Groq provider will not work.")
if groq is None:
logging.warning("Groq Python package not installed. Groq provider will not work.")
# Initialize client if possible
self.client = None
if groq is not None and self.api_key:
self.client = groq.Groq(api_key=self.api_key)
# Define models and capabilities
self.models = {
"llama2-70b-4096": [
ModelCapability.REASONING,
ModelCapability.CODE_GENERATION,
ModelCapability.FINANCE,
ModelCapability.RAG,
],
"mixtral-8x7b-32768": [
ModelCapability.REASONING,
ModelCapability.CODE_GENERATION,
ModelCapability.FINANCE,
ModelCapability.RAG,
],
"gemma-7b-it": [
ModelCapability.CODE_GENERATION,
ModelCapability.RAG,
],
}
def generate(self, prompt: str, **kwargs) -> str:
"""
Generate text from prompt using Groq.
Args:
prompt: Input prompt
**kwargs: Additional parameters
- model: Model name (default: mixtral-8x7b-32768)
- temperature: Temperature (default: 0.7)
- max_tokens: Maximum tokens (default: 2000)
Returns:
Generated text
"""
if self.client is None:
raise ValueError("Groq client not initialized. Provide a valid API key.")
# Extract parameters with defaults
model = kwargs.get("model", "mixtral-8x7b-32768")
temperature = kwargs.get("temperature", 0.7)
max_tokens = kwargs.get("max_tokens", 2000)
try:
# Create parameters
params = {
"model": model,
"messages": [{"role": "user", "content": prompt}],
"temperature": temperature,
"max_tokens": max_tokens,
}
# Make request
response = self.client.chat.completions.create(**params)
# Extract text
return response.choices[0].message.content
except Exception as e:
logging.error(f"Error generating text with Groq: {e}")
logging.error(traceback.format_exc())
raise
def get_available_models(self) -> List[str]:
"""Get list of available models."""
return list(self.models.keys())
def get_model_capabilities(self, model_name: str) -> List[ModelCapability]:
"""Get capabilities of a specific model."""
return self.models.get(model_name, [])
class OllamaProvider(ModelProvider):
"""Ollama model provider for local models."""
def __init__(self, host: str = "http://localhost:11434"):
"""
Initialize Ollama provider.
Args:
host: Ollama host address (default: http://localhost:11434)
"""
self.host = host
if ollama is None:
logging.warning("Ollama Python package not installed. Ollama provider will not work.")
# Check if Ollama is available
self.available = False
if ollama is not None:
try:
# Try to list models
self._list_models()
self.available = True
except Exception as e:
logging.warning(f"Ollama not available: {e}")
# Define models and capabilities
self.models = {
"llama3:latest": [
ModelCapability.REASONING,
ModelCapability.CODE_GENERATION,
ModelCapability.RAG,
],
"mistral:latest": [
ModelCapability.REASONING,
ModelCapability.CODE_GENERATION,
ModelCapability.RAG,
],
"codellama:latest": [
ModelCapability.CODE_GENERATION,
ModelCapability.RAG,
],
"deepseek-coder:latest": [
ModelCapability.CODE_GENERATION,
ModelCapability.RAG,
],
"wizardcoder:latest": [
ModelCapability.CODE_GENERATION,
ModelCapability.RAG,
],
"gemma2:latest": [
ModelCapability.REASONING,
ModelCapability.CODE_GENERATION,
ModelCapability.RAG,
],
}
def _list_models(self) -> List[str]:
"""List available Ollama models."""
if ollama is None:
return []
try:
response = ollama.list(api_base=self.host)
return [model['name'] for model in response['models']]
except Exception as e:
logging.error(f"Error listing Ollama models: {e}")
return []
def generate(self, prompt: str, **kwargs) -> str:
"""
Generate text from prompt using Ollama.
Args:
prompt: Input prompt
**kwargs: Additional parameters
- model: Model name (default: mistral:latest)
- temperature: Temperature (default: 0.7)
- max_tokens: Maximum tokens (default: 2000)
Returns:
Generated text
"""
if ollama is None:
raise ValueError("Ollama package not installed.")
if not self.available:
raise ValueError("Ollama not available.")
# Extract parameters with defaults
model = kwargs.get("model", "mistral:latest")
temperature = kwargs.get("temperature", 0.7)
max_tokens = kwargs.get("max_tokens", 2000)
try:
# Make request
response = ollama.chat(
model=model,
messages=[{"role": "user", "content": prompt}],
temperature=temperature,
num_predict=max_tokens,
api_base=self.host,
)
# Extract text
return response['message']['content']
except Exception as e:
logging.error(f"Error generating text with Ollama: {e}")
logging.error(traceback.format_exc())
raise
def get_available_models(self) -> List[str]:
"""Get list of available models."""
if not self.available:
return []
return self._list_models()
def get_model_capabilities(self, model_name: str) -> List[ModelCapability]:
"""Get capabilities of a specific model."""
# For unknown models, assume basic capabilities
if model_name not in self.models:
return [ModelCapability.RAG]
return self.models.get(model_name, [])
class DeepSeekProvider(ModelProvider):
"""DeepSeek model provider using OpenAI-compatible API."""
def __init__(self, api_key: Optional[str] = None, api_base: str = "https://api.deepseek.com/v1"):
"""
Initialize DeepSeek provider.
Args:
api_key: DeepSeek API key (defaults to DEEPSEEK_API_KEY env var)
api_base: DeepSeek API base URL
"""
self.api_key = api_key or os.environ.get("DEEPSEEK_API_KEY")
self.api_base = api_base
if not self.api_key:
logging.warning("DeepSeek API key not provided. DeepSeek provider will not work.")
if openai is None:
logging.warning("OpenAI Python package not installed. DeepSeek provider will not work.")
# Initialize client if possible
self.client = None
if openai is not None and self.api_key:
self.client = openai.OpenAI(
api_key=self.api_key,
base_url=self.api_base,
)
# Define models and capabilities
self.models = {
"deepseek-chat": [
ModelCapability.REASONING,
ModelCapability.CODE_GENERATION,
ModelCapability.FINANCE,
ModelCapability.RAG,
],
"deepseek-coder": [
ModelCapability.CODE_GENERATION,
ModelCapability.RAG,
],
}
def generate(self, prompt: str, **kwargs) -> str:
"""
Generate text from prompt using DeepSeek.
Args:
prompt: Input prompt
**kwargs: Additional parameters
- model: Model name (default: deepseek-chat)
- temperature: Temperature (default: 0.7)
- max_tokens: Maximum tokens (default: 2000)
Returns:
Generated text
"""
if self.client is None:
raise ValueError("DeepSeek client not initialized. Provide a valid API key.")
# Extract parameters with defaults
model = kwargs.get("model", "deepseek-chat")
temperature = kwargs.get("temperature", 0.7)
max_tokens = kwargs.get("max_tokens", 2000)
try:
# Create messages
messages = [{"role": "user", "content": prompt}]
# Make request
response = self.client.chat.completions.create(
model=model,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
)
# Extract text
return response.choices[0].message.content
except Exception as e:
logging.error(f"Error generating text with DeepSeek: {e}")
logging.error(traceback.format_exc())
raise
def get_available_models(self) -> List[str]:
"""Get list of available models."""
return list(self.models.keys())
def get_model_capabilities(self, model_name: str) -> List[ModelCapability]:
"""Get capabilities of a specific model."""
return self.models.get(model_name, [])
class ModelRouter:
"""
Model router for multi-provider LLM orchestration.
The ModelRouter provides:
- Unified interface for multiple LLM providers
- Dynamic provider selection based on capabilities
- Fallback chains for reliability
- Prompt template management
- Attribution tracing for interpretability
"""
def __init__(
self,
provider: str = "anthropic",
model: Optional[str] = None,
fallback_providers: Optional[List[str]] = None,
openai_api_key: Optional[str] = None,
anthropic_api_key: Optional[str] = None,
groq_api_key: Optional[str] = None,
):
"""
Initialize model router.
Args:
provider: Default provider
model: Default model (provider-specific)
fallback_providers: List of fallback providers
openai_api_key: OpenAI API key
anthropic_api_key: Anthropic API key
groq_api_key: Groq API key
"""
self.default_provider = provider
self.default_model = model
self.fallback_providers = fallback_providers or []
# Track usage
self.usage_stats = {
"total_calls": 0,
"total_tokens": 0,
"provider_calls": {},
"model_calls": {},
"errors": {},
}
# Initialize providers
self.providers = {}
# Initialize OpenAI
try:
self.providers["openai"] = OpenAIProvider(api_key=openai_api_key)
# Set default model if not specified
if provider == "openai" and model is None:
self.default_model = "gpt-4-turbo-preview"
except Exception as e:
logging.warning(f"Failed to initialize OpenAI provider: {e}")
# Initialize Anthropic
try:
self.providers["anthropic"] = AnthropicProvider(api_key=anthropic_api_key)
# Set default model if not specified
if provider == "anthropic" and model is None:
self.default_model = "claude-3-sonnet-20240229"
except Exception as e:
logging.warning(f"Failed to initialize Anthropic provider: {e}")
# Initialize Groq
try:
self.providers["groq"] = GroqProvider(api_key=groq_api_key)
# Set default model if not specified
if provider == "groq" and model is None:
self.default_model = "mixtral-8x7b-32768"
except Exception as e:
logging.warning(f"Failed to initialize Groq provider: {e}")
# Initialize Ollama
try:
self.providers["ollama"] = OllamaProvider()
# Set default model if not specified
if provider == "ollama" and model is None:
self.default_model = "mistral:latest"
except Exception as e:
logging.warning(f"Failed to initialize Ollama provider: {e}")
# Initialize DeepSeek
try:
self.providers["deepseek"] = DeepSeekProvider()
# Set default model if not specified
if provider == "deepseek" and model is None:
self.default_model = "deepseek-chat"
except Exception as e:
logging.warning(f"Failed to initialize DeepSeek provider: {e}")
# Verify default provider is available
if self.default_provider not in self.providers:
available_providers = list(self.providers.keys())
if available_providers:
logging.warning(f"Default provider '{self.default_provider}' not available. "
f"Using '{available_providers[0]}' instead.")
self.default_provider = available_providers[0]
else:
raise ValueError("No LLM providers available. Check API keys and dependencies.")
def generate(self, prompt: str, provider: Optional[str] = None,
model: Optional[str] = None, **kwargs) -> str:
"""
Generate text from prompt.
Args:
prompt: Input prompt
provider: Provider to use (default is instance default)
model: Model to use (default is instance default)
**kwargs: Additional provider-specific parameters
Returns:
Generated text
"""
# Use default provider if not specified
provider = provider or self.default_provider
# Use default model if not specified
model = model or self.default_model
# Update usage stats
self.usage_stats["total_calls"] += 1
# Update provider stats
if provider not in self.usage_stats["provider_calls"]:
self.usage_stats["provider_calls"][provider] = 0
self.usage_stats["provider_calls"][provider] += 1
# Update model stats
model_key = f"{provider}:{model}"
if model_key not in self.usage_stats["model_calls"]:
self.usage_stats["model_calls"][model_key] = 0
self.usage_stats["model_calls"][model_key] += 1
# Check if provider is available
if provider not in self.providers:
# Try fallback providers
for fallback_provider in self.fallback_providers:
if fallback_provider in self.providers:
logging.warning(f"Provider '{provider}' not available. "
f"Using fallback provider '{fallback_provider}'.")
return self.generate(prompt, provider=fallback_provider, model=model, **kwargs)
# No fallback providers available
raise ValueError(f"Provider '{provider}' not available and no fallback providers available.")
try:
# Get provider
provider_instance = self.providers[provider]
# Add model to kwargs
if model:
kwargs["model"] = model
# Generate text
start_time = time.time()
response = provider_instance.generate(prompt, **kwargs)
end_time = time.time()
# Log generation time
logging.debug(f"Generated text with {provider}:{model} in {end_time - start_time:.2f} seconds.")
return response
except Exception as e:
# Update error stats
error_key = str(type(e).__name__)
if error_key not in self.usage_stats["errors"]:
self.usage_stats["errors"][error_key] = 0
self.usage_stats["errors"][error_key] += 1
# Try fallback providers
for fallback_provider in self.fallback_providers:
if fallback_provider in self.providers:
logging.warning(f"Error with provider '{provider}': {e}. "
f"Using fallback provider '{fallback_provider}'.")
return self.generate(prompt, provider=fallback_provider, model=model, **kwargs)
# No fallback providers available
logging.error(f"Error generating text with {provider}:{model}: {e}")
logging.error(traceback.format_exc())
raise
async def generate_async(self, prompt: str, provider: Optional[str] = None,
model: Optional[str] = None, **kwargs) -> str:
"""
Generate text from prompt asynchronously.
Args:
prompt: Input prompt
provider: Provider to use (default is instance default)
model: Model to use (default is instance default)
**kwargs: Additional provider-specific parameters
Returns:
Generated text
"""
# Async implementation using run_in_executor
loop = asyncio.get_event_loop()
return await loop.run_in_executor(
None,
lambda: self.generate(prompt, provider, model, **kwargs)
)
def get_available_providers(self) -> List[str]:
"""Get list of available providers."""
return list(self.providers.keys())
def get_available_models(self, provider: Optional[str] = None) -> Dict[str, List[str]]:
"""
Get available models for all providers or a specific provider.
Args:
provider: Optional provider to get models for
Returns:
Dictionary mapping providers to lists of models
"""
if provider:
if provider not in self.providers:
return {}
return {provider: self.providers[provider].get_available_models()}
# Get models for all providers
models = {}
for provider_name, provider_instance in self.providers.items():
models[provider_name] = provider_instance.get_available_models()
return models
def get_model_capabilities(self, provider: str, model: str) -> List[ModelCapability]:
"""
Get capabilities of a specific model.
Args:
provider: Provider name
model: Model name
Returns:
List of model capabilities
"""
if provider not in self.providers:
return []
return self.providers[provider].get_model_capabilities(model)
def find_models_with_capabilities(self, capabilities: List[ModelCapability]) -> List[Tuple[str, str]]:
"""
Find models that have all specified capabilities.
Args:
capabilities: List of required capabilities
Returns:
List of (provider, model) tuples that have all capabilities
"""
matching_models = []
for provider_name, provider_instance in self.providers.items():
for model in provider_instance.get_available_models():
model_capabilities = provider_instance.get_model_capabilities(model)
# Check if model has all required capabilities
if all(capability in model_capabilities for capability in capabilities):
matching_models.append((provider_name, model))
return matching_models
def get_usage_stats(self) -> Dict[str, Any]:
"""Get usage statistics."""
return self.usage_stats.copy()
|