Skip to content

Model Validation: Add validation of model names when initializing LLM class config #23

@stuckinforloop

Description

@stuckinforloop

Add model name validation and constants for LLM providers

Description

Currently, when initializing the configuration classes for LLM providers (e.g. AnthropicConfig, OpenAIConfig, GoogleConfig), we pass model names as raw strings:

cfg = kit.summaries.AnthropicConfig(api_key=key, model="claude-3-haiku-20240307")

This approach has the following drawbacks (across all providers):

  • No validation of model names
  • Prone to typos and errors (e.g. "claude-3-haiku" vs. "claude-3-haiku-20240307")
  • Hard to discover available models

Proposed Solution

  1. Create enum or constants for each LLM provider's models
  2. Add validation in __post_init__ to check if the provided model name is valid
  3. Provide better developer experience with autocomplete and documentation

Example Implementation

from enum import Enum

class AnthropicModels(str, Enum):
    CLAUDE_3_OPUS = "claude-3-opus-20240229"
    CLAUDE_3_SONNET = "claude-3-sonnet-20240229"
    ...

class OpenAIModels(str, Enum):
    GPT_4O = "gpt-4o"
    GPT_4_TURBO = "gpt-4-turbo"
    # Add other models as needed

class AnthropicConfig:
    """Configuration for Anthropic API access."""
    api_key: Optional[str] = field(default_factory=lambda: os.environ.get("ANTHROPIC_API_KEY"))
    model: str = AnthropicModels.CLAUDE_3_OPUS.value
    temperature: float = 0.7
    max_tokens: int = 1000
    
    def __post_init__(self):
        if not self.api_key:
            raise ValueError(
                "Anthropic API key not found. "
                "Set ANTHROPIC_API_KEY environment variable or pass api_key directly."
            )
        
        # Validate model name
        valid_models = [model.value for model in AnthropicModels]
        if self.model not in valid_models:
            raise ValueError(
                f"Invalid model name for Anthropic: {self.model}. "
                f"Please use one of: {', '.join(valid_models)}"
            )

# Similar implementations for OpenAIConfig and GoogleConfig classes

With this implementation, users could initialize the config like:

# Using string (with validation)
cfg = kit.summaries.AnthropicConfig(api_key=key, model="claude-3-haiku-20240307")

# Or better, using constants
cfg = kit.summaries.AnthropicConfig(api_key=key, model=AnthropicModels.CLAUDE_3_HAIKU.value)

Benefits

  • Type safety and validation across all LLM providers
  • Better IDE support with autocomplete
  • Clear documentation of available models
  • Easier maintenance when adding/removing models

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions