Overview
OpenAIModel is a legacy model wrapper in the phoenix-evals package that provides a comprehensive interface for using OpenAI and Azure OpenAI models within Phoenix LLM evaluations. It extends BaseModel and is the most feature-rich wrapper in the legacy model family, supporting multimodal inputs (text, images, audio), function calling, Azure OpenAI integration via the AzureOptions configuration dataclass, legacy completion API models, dynamic rate limiting, and model-specific parameter handling (e.g., reasoning models that do not support temperature).
LLM_Evaluation
Model_Integration
Description
The OpenAIModel class is implemented as a Python dataclass that extends the abstract BaseModel. Key characteristics include:
- Dual backend support: Initializes either standard OpenAI clients (
OpenAI/AsyncOpenAI) or Azure OpenAI clients (AzureOpenAI/AsyncAzureOpenAI) based on the presence of azure_endpoint.
- Azure configuration: The AzureOptions dataclass validates required Azure parameters (
api_version, azure_endpoint) and optional ones (azure_deployment, azure_ad_token, azure_ad_token_provider).
- Full async support: Both sync and async generation are natively implemented with separate client instances.
- Legacy completion API: Automatically detects models like
gpt-3.5-turbo-instruct that require the legacy completions.create() API instead of chat.completions.create().
- Model-aware parameters: Skips
temperature for reasoning models (o1, o3 prefixes) and uses max_completion_tokens vs max_tokens depending on model and backend.
- Dynamic rate limiting: Configures the
RateLimiter with openai.RateLimitError at an initial rate of 10 requests per second.
- Context limit handling: Catches
BadRequestError with "maximum context length" messages and re-raises as PhoenixContextLimitExceeded.
- Multimodal support: Handles text, base64-encoded images, image URLs, and audio content in messages.
- Function calling: Supports both legacy
function_call/functions parameters and modern tool_calls in responses, with Azure API version checks.
- Model-specific system roles: Maps model names to appropriate system roles (
"system" for GPT, "developer" for o1/o3, "user" for o1-mini/o1-preview).
- Deprecated field migration: The
model_name field is deprecated in favor of model with automatic migration and deprecation warnings.
Usage
# Set the OPENAI_API_KEY environment variable
from phoenix.evals.models import OpenAIModel
# Basic usage with defaults (gpt-4)
model = OpenAIModel()
# Specify model and parameters
model = OpenAIModel(
model="gpt-4o",
temperature=0.0,
max_tokens=512,
)
response = model("What are the SOLID principles in software engineering?")
print(response)
Code Reference
Source Location
| Property |
Value
|
| Repository |
Arize-ai/phoenix
|
| File |
packages/phoenix-evals/src/phoenix/evals/legacy/models/openai.py
|
| Lines |
566
|
| Module |
phoenix.evals.legacy.models.openai
|
AzureOptions Class
@dataclass
class AzureOptions:
api_version: str
azure_endpoint: str
azure_deployment: Optional[str]
azure_ad_token: Optional[str]
azure_ad_token_provider: Optional[Callable[[], str]]
OpenAIModel Class Signature
@dataclass
class OpenAIModel(BaseModel):
api_key: Optional[str] = field(repr=False, default=None)
organization: Optional[str] = field(repr=False, default=None)
base_url: Optional[str] = field(repr=False, default=None)
model: str = "gpt-4"
temperature: float = 0.0
max_tokens: Optional[int] = None
top_p: float = 1
frequency_penalty: float = 0
presence_penalty: float = 0
n: int = 1
model_kwargs: Dict[str, Any] = field(default_factory=dict)
request_timeout: Optional[Union[float, Tuple[float, float]]] = 30
initial_rate_limit: int = 10
timeout: int = 120
# Azure options
api_version: Optional[str] = field(default=None)
azure_endpoint: Optional[str] = field(default=None)
azure_deployment: Optional[str] = field(default=None)
azure_ad_token: Optional[str] = field(default=None)
azure_ad_token_provider: Optional[Callable[[], str]] = field(default=None)
default_headers: Optional[Mapping[str, str]] = field(default=None)
# Deprecated
model_name: Optional[str] = field(default=None)
Constructor Parameters
| Parameter |
Type |
Default |
Description
|
| api_key |
Optional[str] |
None |
OpenAI API key; falls back to environment variable.
|
| organization |
Optional[str] |
None |
OpenAI organization; falls back to SDK default.
|
| base_url |
Optional[str] |
None |
Custom base URL for the OpenAI API.
|
| model |
str |
"gpt-4" |
The model name to use (or Azure deployment name).
|
| temperature |
float |
0.0 |
Sampling temperature (skipped for reasoning models).
|
| max_tokens |
Optional[int] |
None |
Maximum tokens to generate; None for unlimited.
|
| top_p |
float |
1 |
Nucleus sampling probability mass.
|
| frequency_penalty |
float |
0 |
Penalizes repeated tokens by frequency.
|
| presence_penalty |
float |
0 |
Penalizes repeated tokens by presence.
|
| n |
int |
1 |
Number of completions to generate.
|
| model_kwargs |
Dict[str, Any] |
{} |
Extra parameters passed to the API call.
|
| request_timeout |
Optional[Union[float, Tuple[float, float]]] |
30 |
Timeout for OpenAI API requests in seconds.
|
| initial_rate_limit |
int |
10 |
Initial requests-per-second rate limit.
|
| timeout |
int |
120 |
Phoenix-level timeout for API requests.
|
| api_version |
Optional[str] |
None |
Azure API version string.
|
| azure_endpoint |
Optional[str] |
None |
Azure OpenAI endpoint URL.
|
| azure_deployment |
Optional[str] |
None |
Azure OpenAI deployment name.
|
| azure_ad_token |
Optional[str] |
None |
Azure AD authentication token.
|
| azure_ad_token_provider |
Optional[Callable[[], str]] |
None |
Callable returning Azure AD token.
|
| default_headers |
Optional[Mapping[str, str]] |
None |
Default HTTP headers for requests.
|
| model_name |
Optional[str] |
None |
Deprecated. Use model instead.
|
Key Methods
| Method |
Signature |
Description
|
| __post_init__ |
(self) -> None |
Migrates deprecated fields, initializes environment, client, and rate limiter.
|
| reload_client |
(self) -> None |
Reinitializes the OpenAI/Azure client.
|
| _init_open_ai |
(self) -> None |
Creates sync and async clients for OpenAI or Azure.
|
| _get_azure_options |
(self) -> AzureOptions |
Validates and returns Azure-specific configuration.
|
| _build_messages |
(self, prompt, system_instruction) -> List[Dict[str, Any]] |
Builds message list with text, image, and audio support.
|
| _generate_with_extra |
(self, prompt, **kwargs) -> Tuple[str, ExtraInfo] |
Synchronous generation with function calling support.
|
| _async_generate_with_extra |
async (self, prompt, **kwargs) -> Tuple[str, ExtraInfo] |
Async generation with function calling support.
|
| _rate_limited_completion |
(self, **kwargs) -> Tuple[str, ExtraInfo] |
Rate-limited sync completion supporting both Chat and legacy APIs.
|
| _async_rate_limited_completion |
async (self, **kwargs) -> Tuple[str, ExtraInfo] |
Rate-limited async completion supporting both Chat and legacy APIs.
|
| _system_role |
(self) -> str |
Returns the appropriate system role for the model.
|
| supports_function_calling |
@property -> bool |
Whether the model/configuration supports function calling.
|
| _extract_text |
(self, response) -> str |
Extracts text from ChatCompletion or legacy Completion responses.
|
Helper Functions
| Function |
Signature |
Description
|
| _model_supports_temperature |
(model: str) -> bool |
Returns False for o1/o3 reasoning models.
|
| _is_url |
(url: str) -> bool |
Checks if a string is a valid URL.
|
| _is_base64 |
(s: str) -> bool |
Checks if a string is valid base64.
|
| _get_token_param_str |
(is_azure: bool, model: str) -> str |
Returns "max_tokens" or "max_completion_tokens" based on backend and model.
|
Model Token Limit Mapping
The module includes a MODEL_TOKEN_LIMIT_MAPPING dictionary mapping model names to their token limits (e.g., "gpt-4-1106-preview": 128000).
Import
from phoenix.evals.models import OpenAIModel
I/O Contract
| Direction |
Type |
Description
|
| Input |
Union[str, MultimodalPrompt] |
A text string or multimodal prompt with text, image, and/or audio parts.
|
| Input (optional) |
Optional[str] |
System instruction string (injected as system/developer/user message based on model).
|
| Input (optional) |
functions, function_call |
Function calling parameters passed via kwargs.
|
| Output |
str |
Generated text, function call arguments, or tool call arguments.
|
| Output (with extra) |
Tuple[str, ExtraInfo] |
Generated text paired with ExtraInfo containing Usage token counts.
|
| Error |
PhoenixContextLimitExceeded |
Raised when prompt exceeds the model's context window.
|
| Error |
ImportError |
Raised if openai package is not installed.
|
Usage Examples
Standard OpenAI Usage
from phoenix.evals.models import OpenAIModel
model = OpenAIModel(model="gpt-4o", temperature=0.0, max_tokens=1024)
response = model("What is the difference between REST and GraphQL?")
print(response)
Azure OpenAI Usage
from phoenix.evals.models import OpenAIModel
model = OpenAIModel(
model="gpt-35-turbo-16k",
azure_endpoint="https://your-endpoint.openai.azure.com/",
api_version="2023-09-15-preview",
)
response = model("Summarize the benefits of cloud computing.")
print(response)
With Function Calling
from phoenix.evals.models import OpenAIModel
model = OpenAIModel(model="gpt-4o")
functions = [{
"name": "get_weather",
"description": "Get the current weather",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"},
},
"required": ["location"],
},
}]
response = model._generate(
"What is the weather in Paris?",
functions=functions,
function_call={"name": "get_weather"},
)
print(response) # JSON string with function arguments
With Custom Base URL (OpenAI-Compatible APIs)
from phoenix.evals.models import OpenAIModel
model = OpenAIModel(
model="my-local-model",
base_url="http://localhost:8000/v1",
api_key="not-needed",
)
Related Pages