Skip to content

model_registry

Class info

Classes

Name Children Inherits
ModelRegistry
llmling_models.openai_server.model_registry
Registry of available models.

    🛈 DocStrings

    OpenAI-compatible API server for Pydantic-AI models.

    ModelRegistry

    Registry of available models.

    Source code in src/llmling_models/openai_server/model_registry.py
    class ModelRegistry:
        """Registry of available models."""
    
        def __init__(self, models: dict[str, str | Model] | None = None) -> None:
            """Initialize model registry.
    
            Args:
                models: Dictionary mapping model names to models or model identifiers
            """
            self.models: dict[str, Model] = {}
            if models:
                for name, model_or_id in models.items():
                    if isinstance(model_or_id, str):
                        self.models[name] = infer_model(model_or_id)
                    else:
                        self.models[name] = model_or_id
    
        @classmethod
        async def create(cls) -> ModelRegistry:
            """Create a model registry populated with all models from tokonomics.
    
            Returns:
                A new ModelRegistry instance with auto-populated models.
            """
            registry = cls({})  # Empty registry
            try:
                all_models = await tokonomics.get_all_models()
                for model_info in all_models:
                    try:
                        # Use the pydantic_model_id directly as the key
                        model_id = model_info.pydantic_ai_id
                        registry.models[model_id] = infer_model(model_id)
                        logger.debug("Auto-registered model: %s", model_id)
                    except Exception as e:  # noqa: BLE001
                        msg = "Failed to register model %s: %s"
                        logger.warning(msg, model_info.pydantic_ai_id, str(e))
    
                logger.info("Auto-populated %d models from tokonomics", len(registry.models))
            except Exception as e:  # noqa: BLE001
                logger.warning("Error auto-populating models: %s", str(e))
    
            return registry
    
        def add_model(self, name: str, model_or_id: str | Model) -> None:
            """Add a model to the registry."""
            model = infer_model(model_or_id) if isinstance(model_or_id, str) else model_or_id
            self.models[name] = model
    
        def get_model(self, name: str) -> Model:
            """Get a model by name."""
            try:
                return self.models[name]
            except KeyError:
                raise ValueError(f"Model {name} not found") from None
    
        def list_models(self) -> list[OpenAIModelInfo]:
            """List available models."""
            return [
                OpenAIModelInfo(
                    id=n,
                    created=int(time.time()),
                    description=f"Model {n}",
                    object="model",
                    owned_by="agentpool",
                )
                for n in self.models
            ]
    

    __init__

    __init__(models: dict[str, str | Model] | None = None) -> None
    

    Initialize model registry.

    Parameters:

    Name Type Description Default
    models dict[str, str | Model] | None

    Dictionary mapping model names to models or model identifiers

    None
    Source code in src/llmling_models/openai_server/model_registry.py
    def __init__(self, models: dict[str, str | Model] | None = None) -> None:
        """Initialize model registry.
    
        Args:
            models: Dictionary mapping model names to models or model identifiers
        """
        self.models: dict[str, Model] = {}
        if models:
            for name, model_or_id in models.items():
                if isinstance(model_or_id, str):
                    self.models[name] = infer_model(model_or_id)
                else:
                    self.models[name] = model_or_id
    

    add_model

    add_model(name: str, model_or_id: str | Model) -> None
    

    Add a model to the registry.

    Source code in src/llmling_models/openai_server/model_registry.py
    def add_model(self, name: str, model_or_id: str | Model) -> None:
        """Add a model to the registry."""
        model = infer_model(model_or_id) if isinstance(model_or_id, str) else model_or_id
        self.models[name] = model
    

    create async classmethod

    create() -> ModelRegistry
    

    Create a model registry populated with all models from tokonomics.

    Returns:

    Type Description
    ModelRegistry

    A new ModelRegistry instance with auto-populated models.

    Source code in src/llmling_models/openai_server/model_registry.py
    @classmethod
    async def create(cls) -> ModelRegistry:
        """Create a model registry populated with all models from tokonomics.
    
        Returns:
            A new ModelRegistry instance with auto-populated models.
        """
        registry = cls({})  # Empty registry
        try:
            all_models = await tokonomics.get_all_models()
            for model_info in all_models:
                try:
                    # Use the pydantic_model_id directly as the key
                    model_id = model_info.pydantic_ai_id
                    registry.models[model_id] = infer_model(model_id)
                    logger.debug("Auto-registered model: %s", model_id)
                except Exception as e:  # noqa: BLE001
                    msg = "Failed to register model %s: %s"
                    logger.warning(msg, model_info.pydantic_ai_id, str(e))
    
            logger.info("Auto-populated %d models from tokonomics", len(registry.models))
        except Exception as e:  # noqa: BLE001
            logger.warning("Error auto-populating models: %s", str(e))
    
        return registry
    

    get_model

    get_model(name: str) -> Model
    

    Get a model by name.

    Source code in src/llmling_models/openai_server/model_registry.py
    def get_model(self, name: str) -> Model:
        """Get a model by name."""
        try:
            return self.models[name]
        except KeyError:
            raise ValueError(f"Model {name} not found") from None
    

    list_models

    list_models() -> list[OpenAIModelInfo]
    

    List available models.

    Source code in src/llmling_models/openai_server/model_registry.py
    def list_models(self) -> list[OpenAIModelInfo]:
        """List available models."""
        return [
            OpenAIModelInfo(
                id=n,
                created=int(time.time()),
                description=f"Model {n}",
                object="model",
                owned_by="agentpool",
            )
            for n in self.models
        ]