ModelRegistry
Base classes
| Name |
Children |
Inherits |
Schema schemez.schema Base class for configuration models. |
|
|
â‹” Inheritance diagram
graph TD
3536238701["registry.ModelRegistry"]
330168112["schema.Schema"]
3879467795["main.BaseModel"]
2751680898["builtins.object"]
330168112 --> 3536238701
3879467795 --> 330168112
2751680898 --> 3879467795
🛈 DocStrings
Bases: Schema
Registry containing all model configurations with query capabilities.
Source code in src/tokonomics/registry.py
| class ModelRegistry(Schema):
"""Registry containing all model configurations with query capabilities."""
models: dict[str, ModelConfig]
"""Model name -> configuration mapping."""
def get_model(self, name: str) -> ModelConfig | None:
"""Get model configuration by name."""
return self.models.get(name)
def get_models_by_provider(self, provider: str) -> dict[str, ModelConfig]:
"""Get all models for a specific provider."""
return {
name: config
for name, config in self.models.items()
if config.litellm_provider == provider
}
def get_models_by_mode(self, mode: ModelMode) -> dict[str, ModelConfig]:
"""Get all models for a specific mode."""
return {name: config for name, config in self.models.items() if config.mode == mode}
def get_chat_models(self) -> Mapping[str, ChatCompletionModel]:
"""Get all chat/completion models."""
return {
name: config
for name, config in self.models.items()
if isinstance(config, ChatCompletionModel)
}
def get_providers(self) -> set[str]:
"""Get all unique providers."""
return {config.litellm_provider for config in self.models.values()}
@classmethod
def from_dict(cls, data: dict[str, Any]) -> ModelRegistry:
"""Create registry from dictionary data with automatic model discrimination."""
models = {}
failed_models = []
dropped_models = []
for name, config_data in data.items():
try:
# Skip models without mode field or sample_spec
if "mode" not in config_data or name == "sample_spec":
dropped_models.append(name)
continue
mode = config_data["mode"]
match mode:
case "chat" | "completion":
model_config: ModelConfig = ChatCompletionModel.model_validate(config_data)
case "embedding":
model_config = EmbeddingModel.model_validate(config_data)
case "audio_transcription":
model_config = AudioTranscriptionModel.model_validate(config_data)
case "audio_speech":
model_config = AudioSpeechModel.model_validate(config_data)
case "image_generation":
model_config = ImageGenerationModel.model_validate(config_data)
case "video_generation":
model_config = VideoGenerationModel.model_validate(config_data)
case "rerank":
model_config = RerankModel.model_validate(config_data)
case "responses":
model_config = ResponsesModel.model_validate(config_data)
case "moderation":
model_config = ModerationModel.model_validate(config_data)
case _:
# Default to chat for unknown modes
model_config = ChatCompletionModel.model_validate(config_data)
models[name] = model_config
except Exception as e: # noqa: BLE001
failed_models.append((name, str(e)))
# Only print first few failures to avoid spam
if len(failed_models) <= 5: # noqa: PLR2004
logger.debug("Failed to parse model %s: %s", name, e)
if dropped_models:
logger.debug("Dropped %s models without mode field", len(dropped_models))
if failed_models:
msg = "Failed to parse %s models out of %s"
logger.debug(msg, len(failed_models), len(data))
return cls(models=models)
@classmethod
def from_json_file(cls, file_path: str) -> ModelRegistry:
"""Load registry from JSON file."""
with pathlib.Path(file_path).open() as f:
data = json.load(f)
return cls.from_dict(data)
|
models
instance-attribute
models: dict[str, ModelConfig]
Model name -> configuration mapping.
from_dict
classmethod
Create registry from dictionary data with automatic model discrimination.
Source code in src/tokonomics/registry.py
| @classmethod
def from_dict(cls, data: dict[str, Any]) -> ModelRegistry:
"""Create registry from dictionary data with automatic model discrimination."""
models = {}
failed_models = []
dropped_models = []
for name, config_data in data.items():
try:
# Skip models without mode field or sample_spec
if "mode" not in config_data or name == "sample_spec":
dropped_models.append(name)
continue
mode = config_data["mode"]
match mode:
case "chat" | "completion":
model_config: ModelConfig = ChatCompletionModel.model_validate(config_data)
case "embedding":
model_config = EmbeddingModel.model_validate(config_data)
case "audio_transcription":
model_config = AudioTranscriptionModel.model_validate(config_data)
case "audio_speech":
model_config = AudioSpeechModel.model_validate(config_data)
case "image_generation":
model_config = ImageGenerationModel.model_validate(config_data)
case "video_generation":
model_config = VideoGenerationModel.model_validate(config_data)
case "rerank":
model_config = RerankModel.model_validate(config_data)
case "responses":
model_config = ResponsesModel.model_validate(config_data)
case "moderation":
model_config = ModerationModel.model_validate(config_data)
case _:
# Default to chat for unknown modes
model_config = ChatCompletionModel.model_validate(config_data)
models[name] = model_config
except Exception as e: # noqa: BLE001
failed_models.append((name, str(e)))
# Only print first few failures to avoid spam
if len(failed_models) <= 5: # noqa: PLR2004
logger.debug("Failed to parse model %s: %s", name, e)
if dropped_models:
logger.debug("Dropped %s models without mode field", len(dropped_models))
if failed_models:
msg = "Failed to parse %s models out of %s"
logger.debug(msg, len(failed_models), len(data))
return cls(models=models)
|
from_json_file
classmethod
Load registry from JSON file.
Source code in src/tokonomics/registry.py
| @classmethod
def from_json_file(cls, file_path: str) -> ModelRegistry:
"""Load registry from JSON file."""
with pathlib.Path(file_path).open() as f:
data = json.load(f)
return cls.from_dict(data)
|
get_chat_models
Get all chat/completion models.
Source code in src/tokonomics/registry.py
| def get_chat_models(self) -> Mapping[str, ChatCompletionModel]:
"""Get all chat/completion models."""
return {
name: config
for name, config in self.models.items()
if isinstance(config, ChatCompletionModel)
}
|
get_model
get_model(name: str) -> ModelConfig | None
Get model configuration by name.
Source code in src/tokonomics/registry.py
| def get_model(self, name: str) -> ModelConfig | None:
"""Get model configuration by name."""
return self.models.get(name)
|
get_models_by_mode
get_models_by_mode(mode: ModelMode) -> dict[str, ModelConfig]
Get all models for a specific mode.
Source code in src/tokonomics/registry.py
| def get_models_by_mode(self, mode: ModelMode) -> dict[str, ModelConfig]:
"""Get all models for a specific mode."""
return {name: config for name, config in self.models.items() if config.mode == mode}
|
get_models_by_provider
get_models_by_provider(provider: str) -> dict[str, ModelConfig]
Get all models for a specific provider.
Source code in src/tokonomics/registry.py
| def get_models_by_provider(self, provider: str) -> dict[str, ModelConfig]:
"""Get all models for a specific provider."""
return {
name: config
for name, config in self.models.items()
if config.litellm_provider == provider
}
|
get_providers
get_providers() -> set[str]
Get all unique providers.
Source code in src/tokonomics/registry.py
| def get_providers(self) -> set[str]:
"""Get all unique providers."""
return {config.litellm_provider for config in self.models.values()}
|
:fa-brands-github: Show source on GitHub