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 MistralAIModel

From Leeroopedia

Overview

MistralAIModel is a legacy model wrapper in the phoenix-evals package that provides an interface for using Mistral AI models within Phoenix LLM evaluations. It extends BaseModel and integrates with the Mistral AI SDK, providing both synchronous and asynchronous generation, dynamic rate limiting based on HTTP 429 status detection, tool call extraction, and structured token usage tracking.

LLM_Evaluation Model_Integration

Description

The MistralAIModel class is implemented as a Python dataclass that extends the abstract BaseModel. Key characteristics include:

  • Mistral SDK integration: Initializes a Mistral client from the mistralai package with a minimum required version of 1.0.0.
  • Full async support: Both sync (_client.chat.complete()) and async (_client.chat.complete_async()) generation methods are natively implemented.
  • Custom rate limit detection: Since the Mistral SDK raises a generic SDKError for all HTTP errors, the wrapper inspects the http_status attribute to detect 429 rate limit responses and re-raises them as MistralRateLimitError for the rate limiter to handle.
  • Dynamic rate limiting: Uses the RateLimiter infrastructure with a custom MistralRateLimitError exception, 5 requests per second initial rate, and 1-minute enforcement window.
  • Tool call support: The _extract_text() method handles ToolCall objects with both string and dictionary argument formats.
  • Multi-format content extraction: Supports both string content and list-of-chunks content (using TextChunk) in message responses.
  • Strict None filtering: The invocation_parameters() method filters out None values since the Mistral API rejects them.

Usage

# Set the MISTRAL_API_KEY environment variable

from phoenix.evals.models import MistralAIModel

# Basic usage with defaults (mistral-large-latest)
model = MistralAIModel()

# Specify model and parameters
model = MistralAIModel(
    model="mistral-large-latest",
    temperature=0.3,
    top_p=0.9,
)

response = model("What are the key differences between supervised and unsupervised learning?")
print(response)

Code Reference

Source Location

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

Class Signature

@dataclass
class MistralAIModel(BaseModel):
    model: str = "mistral-large-latest"
    temperature: float = 0
    top_p: Optional[float] = None
    random_seed: Optional[int] = None
    response_format: Optional[Dict[str, str]] = None
    api_key: Optional[str] = os.getenv("MISTRAL_API_KEY")
    safe_prompt: bool = False
    initial_rate_limit: int = 5
    timeout: int = 120

Constructor Parameters

Parameter Type Default Description
model str "mistral-large-latest" The Mistral model name to use.
temperature float 0 Sampling temperature for generation.
top_p Optional[float] None Nucleus sampling probability mass.
random_seed Optional[int] None Random seed for reproducible generation.
response_format Optional[Dict[str, str]] None Response format specification (e.g., JSON mode).
api_key Optional[str] os.getenv("MISTRAL_API_KEY") The Mistral API key.
safe_prompt bool False Whether to use Mistral's safe prompt mode.
initial_rate_limit int 5 Initial requests-per-second rate limit.
timeout int 120 Timeout for API requests in seconds.

Key Methods

Method Signature Description
__post_init__ (self) -> None Initializes the Mistral client and rate limiter.
_init_client (self) -> None Creates the Mistral client and captures SDKError.
_init_rate_limiter (self) -> None Configures rate limiter with custom MistralRateLimitError.
invocation_parameters (self) -> Dict[str, Any] Returns API call parameters with None values filtered out.
_generate_with_extra (self, prompt, **kwargs) -> Tuple[str, ExtraInfo] Synchronous generation with rate limiting.
_async_generate_with_extra async (self, prompt, **kwargs) -> Tuple[str, ExtraInfo] Native async generation with rate limiting.
_rate_limited_completion (self, **kwargs) -> Tuple[str, ExtraInfo] Rate-limited synchronous completion with 429 detection.
_async_rate_limited_completion async (self, **kwargs) -> Tuple[str, ExtraInfo] Rate-limited async completion with 429 detection.
_extract_text (self, response: ChatCompletionResponse) -> str Extracts text from tool calls, string content, or TextChunk lists.
_extract_usage (self, usage_info: Optional[UsageInfo]) -> Optional[Usage] Extracts token usage from the response.
_format_prompt (self, prompt: MultimodalPrompt) -> List[Dict[str, str]] Converts prompt to Mistral chat messages format (text only).

Helper Class

Class Description
MistralRateLimitError Custom exception raised when the Mistral SDK returns an HTTP 429 status, used as the rate limiter's trigger exception.

Import

from phoenix.evals.models import MistralAIModel

I/O Contract

Direction Type Description
Input Union[str, MultimodalPrompt] A text string or multimodal prompt (only text parts supported for Mistral).
Input (optional) Optional[str] Instruction parameter (ignored; stripped before API call).
Output str Generated text response, or tool call arguments as string/JSON.
Output (with extra) Tuple[str, ExtraInfo] Generated text paired with ExtraInfo containing optional Usage token counts.
Error ImportError Raised if mistralai package is not installed or version is too old.
Error ValueError Raised if a non-text content type is provided in the prompt.

Usage Examples

Basic Generation

from phoenix.evals.models import MistralAIModel

model = MistralAIModel(model="mistral-large-latest", temperature=0.0)
response = model("Explain the concept of gradient descent.")
print(response)

Async Generation

import asyncio
from phoenix.evals.models import MistralAIModel

model = MistralAIModel(model="mistral-large-latest")

async def generate():
    result = await model._async_generate("What is transfer learning?")
    return result

response = asyncio.run(generate())
print(response)

With Safe Prompt and Seed

from phoenix.evals.models import MistralAIModel

model = MistralAIModel(
    model="mistral-large-latest",
    safe_prompt=True,
    random_seed=42,
    temperature=0.5,
)

response = model("Generate a creative story introduction.")
print(response)

Related Pages

Page Connections

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