Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Arize ai Phoenix Legacy BaseModel

From Leeroopedia

Overview

BaseModel is the abstract base class (ABC) for all legacy LLM model wrappers in the phoenix-evals package. It defines the core contract that every model implementation must follow, including synchronous and asynchronous text generation, rate limiting integration, and token usage tracking. The module also provides two NamedTuple data classes (Usage and ExtraInfo) for structured output metadata, and a set_verbosity() context manager for controlling logging behavior during evaluation runs.

LLM_Evaluation Model_Integration

Description

The BaseModel class is implemented as a Python dataclass decorated with @dataclass and inheriting from ABC (Abstract Base Class). It serves as the foundation for all legacy model wrappers in Phoenix Evals, providing:

  • Abstract interface: Subclasses must implement _generate_with_extra() and _async_generate_with_extra() to perform actual LLM calls.
  • Callable protocol: Instances can be invoked directly via __call__(), which delegates to _generate().
  • Rate limiting infrastructure: Integrates with the RateLimiter class to dynamically throttle API calls.
  • Verbosity control: The set_verbosity() context manager temporarily adjusts the verbosity of both the model and its rate limiter.
  • Keyword-only instantiation: The custom __new__ method enforces that all subclass instances must be created using keyword arguments only.

The Usage NamedTuple captures token consumption (prompt, completion, and total tokens), while ExtraInfo wraps an optional Usage instance for returning metadata alongside generated text.

Usage

from phoenix.evals.legacy.models.base import BaseModel, Usage, ExtraInfo, set_verbosity

# BaseModel is abstract and cannot be instantiated directly.
# It is extended by concrete model wrappers such as OpenAIModel, AnthropicModel, etc.

# Example: Using set_verbosity context manager with a concrete model instance
from phoenix.evals.models import OpenAIModel

model = OpenAIModel(model="gpt-4o")
with set_verbosity(model, verbose=True) as verbose_model:
    result = verbose_model("What is 2 + 2?")

# Example: Usage and ExtraInfo data structures
usage = Usage(prompt_tokens=50, completion_tokens=20, total_tokens=70)
extra = ExtraInfo(usage=usage)
print(extra.usage.total_tokens)  # 70

Code Reference

Source Location

Property Value
Repository Arize-ai/phoenix
File packages/phoenix-evals/src/phoenix/evals/legacy/models/base.py
Lines 134
Module phoenix.evals.legacy.models.base

Classes

Class Type Description
Usage NamedTuple Token usage data with fields: prompt_tokens, completion_tokens, total_tokens (all int).
ExtraInfo NamedTuple Response metadata wrapper with optional usage: Optional[Usage] field (defaults to None).
BaseModel ABC, dataclass Abstract base class for all LLM model implementations.

BaseModel Signature

@dataclass
class BaseModel(ABC):
    default_concurrency: int = 20
    _verbose: bool = False
    _rate_limiter: RateLimiter = field(default_factory=RateLimiter)

Key Methods

Method Signature Description
__call__ (self, prompt: Union[str, MultimodalPrompt], instruction: Optional[str] = None, **kwargs) -> str Callable interface; delegates to _generate() after type validation.
_generate (self, prompt: Union[str, MultimodalPrompt], **kwargs) -> str Synchronous generation; returns only the text portion from _generate_with_extra().
_async_generate async (self, prompt: Union[str, MultimodalPrompt], **kwargs) -> str Asynchronous generation; returns only the text portion from _async_generate_with_extra().
_generate_with_extra (self, prompt: Union[str, MultimodalPrompt], **kwargs) -> Tuple[str, ExtraInfo] Abstract. Synchronous generation returning text and metadata.
_async_generate_with_extra async (self, prompt: Union[str, MultimodalPrompt], **kwargs) -> Tuple[str, ExtraInfo] Abstract. Asynchronous generation returning text and metadata.
_model_name @property (abstract) -> str Abstract property returning the model identifier string.
reload_client (self) -> None No-op by default; subclasses override to reinitialize API clients.
verbose_generation_info (self) -> str Returns model-specific verbose info; empty string by default.
_raise_import_error @staticmethod (package_name, package_display_name, package_min_version) -> None Helper to raise formatted ImportError messages for missing dependencies.

Function: set_verbosity

@contextmanager
def set_verbosity(model: BaseModel, verbose: bool = False) -> Generator[BaseModel, None, None]:

A context manager that temporarily sets the _verbose flag on both the model and its _rate_limiter, restoring original values upon exit.

Import

from phoenix.evals.legacy.models.base import BaseModel
from phoenix.evals.legacy.models.base import Usage, ExtraInfo, set_verbosity

I/O Contract

Direction Type Description
Input Union[str, MultimodalPrompt] A text string or multimodal prompt containing text, image, or audio parts.
Input (optional) Optional[str] An optional system instruction string passed via the instruction parameter.
Output (text) str The generated text response from the LLM.
Output (with extra) Tuple[str, ExtraInfo] A tuple of the generated text and an ExtraInfo instance containing optional Usage metadata.

Usage Examples

Subclassing BaseModel

from dataclasses import dataclass
from typing import Any, Tuple, Union
from phoenix.evals.legacy.models.base import BaseModel, ExtraInfo
from phoenix.evals.legacy.templates import MultimodalPrompt


@dataclass
class CustomModel(BaseModel):
    model: str = "custom-model-v1"

    @property
    def _model_name(self) -> str:
        return self.model

    def _generate_with_extra(
        self, prompt: Union[str, MultimodalPrompt], **kwargs: Any
    ) -> Tuple[str, ExtraInfo]:
        # Custom synchronous generation logic
        text = f"Response to: {prompt}"
        return text, ExtraInfo()

    async def _async_generate_with_extra(
        self, prompt: Union[str, MultimodalPrompt], **kwargs: Any
    ) -> Tuple[str, ExtraInfo]:
        # Custom asynchronous generation logic
        text = f"Async response to: {prompt}"
        return text, ExtraInfo()

Using the Callable Interface

model = CustomModel(model="custom-model-v1")
result = model("Tell me a joke.")
print(result)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment