Evals Introduction
Arize Phoenix Evals 2.0
We are excited to introduce arize-phoenix-evals 2.0, an open-source library providing tools to evaluate AI systems so you can build faster and with more confidence. We have rebuilt the library from the ground up to make evaluation faster, easier, and more powerful.
In this notebook, you will learn more about:
- Our guiding principles
- The core library abstractions
- Usage examples
- What's changed between 2.0 and the previous version
Our Guiding Principles
Fast: We are optimizing for maximum speed, minimal headache.
Ergonomic: It should be user-friendly and easy to pick up.
Flexible: We make minimal assumptions about the shape of your data or evals.
Powerful: Built with extensibility in mind, the library enables complex evaluation tasks.
⚠️ PHOENIX_COLLECTOR_ENDPOINT is set to https://app.phoenix.arize.com/s/ehutton. ⚠️ This means that traces will be sent to the collector endpoint and not this app. ⚠️ If you would like to use this app to view traces, please unset this environmentvariable via e.g. `del os.environ['PHOENIX_COLLECTOR_ENDPOINT']` ⚠️ You will need to restart your notebook to apply this change. /Users/elizabethhutton/.local/share/uv/python/cpython-3.9.23-macos-aarch64-none/lib/python3.9/contextlib.py:126: SAWarning: Skipped unsupported reflection of expression-based index ix_cumulative_llm_token_count_total next(self.gen) /Users/elizabethhutton/.local/share/uv/python/cpython-3.9.23-macos-aarch64-none/lib/python3.9/contextlib.py:126: SAWarning: Skipped unsupported reflection of expression-based index ix_latency next(self.gen)
🌍 To view the Phoenix app in your browser, visit http://localhost:6006/ 📖 For more information on how to use Phoenix, check out https://arize.com/docs/phoenix
/Users/elizabethhutton/Projects/phoenix/src/phoenix/otel/otel.py:434: UserWarning: Could not infer collector endpoint protocol, defaulting to HTTP.
warnings.warn("Could not infer collector endpoint protocol, defaulting to HTTP.")
🔭 OpenTelemetry Tracing Details 🔭
| Phoenix Project: default
| Span Processor: SimpleSpanProcessor
| Collector Endpoint: https://app.phoenix.arize.com/s/ehutton/v1/traces
| Transport: HTTP + protobuf
| Transport Headers: {'authorization': '****'}
|
| Using a default SpanProcessor. `add_span_processor` will overwrite this default.
|
| ⚠️ WARNING: It is strongly advised to use a BatchSpanProcessor in production environments.
|
| `register` has set this TracerProvider as the global OpenTelemetry default.
| To disable this behavior, call `register` with `set_global_tracer_provider=False`.
LLM Configuration
Core Design Principle: The library should work with any LLM model and provider.
The LLM wrapper unifies generation tasks across model providers by delegating to the most commonly installed client SDKs (OpenAI, LangChain, LiteLLM) via adapters.
Evaluators and Scores
An evaluation is defined as any process that returns a Score.
Hallucination result:
{
"name": "hallucination",
"score": 1.0,
"label": "factual",
"explanation": "The response states that Paris is the capital of France, which is accurate according to the provided context that clearly states Paris is the capital and largest city of France.",
"metadata": {
"model": "gpt-4o-mini"
},
"kind": "llm",
"direction": "maximize"
}
Core Design Principle: The output of evaluators should be rich with information.
Evaluators always return a list of Score objects. Often, this will be a list of length 1, but some evaluators may return multiple scores for a single eval_input (e.g. precision/recall or multi-criteria evals).
Built-In Metrics
Precision, Recall, F1 (multi-score)
A single evaluator can return multiple scores!
Results:
Score(name='precision', score=0.5, label=None, explanation=None, metadata={'beta': 1.0, 'average': 'macro', 'labels': ['yes', 'no'], 'positive_label': 'yes'}, kind='code', direction='maximize')
Score(name='recall', score=0.5, label=None, explanation=None, metadata={'beta': 1.0, 'average': 'macro', 'labels': ['yes', 'no'], 'positive_label': 'yes'}, kind='code', direction='maximize')
Score(name='f1', score=0.5, label=None, explanation=None, metadata={'beta': 1.0, 'average': 'macro', 'labels': ['yes', 'no'], 'positive_label': 'yes'}, kind='code', direction='maximize')
Custom LLM Classification Evaluators
This is similar to llm_classify, for LLM-as-a-judge evaluations that output a label and explanation.
{
"name": "sentiment",
"score": 1.0,
"label": "positive",
"explanation": "The text expresses a strong positive emotion towards something, indicating enjoyment or satisfaction.",
"metadata": {
"model": "gpt-4o-mini"
},
"kind": "llm",
"direction": "maximize"
}
About the ClassificationEvaluator
New features:
- Specify scores for each label
- Runs on single records (not just a dataframe)
- Leverages model tool calling / structured output for more reliable output parsing
- There is also a factory function
create_classifierto createClassificationEvaluatorobjects.
This abstraction can be easily extended to support multi-criteria evaluations where a judge is asked to evaluate an input across multiple dimensions in one request.
Input Mapping and Transformation
Core Design Principle: The inputs to an evaluator should be well-defined and discoverable.
Every evaluator has an input_schema which describes what inputs it expects.
Use .describe() to inspect an Evaluator's input schema
Because pydantic BaseModel is used for the input_schema, input fields can be annotated with types, descriptions, and even aliases.
{'name': 'hallucination',
, 'kind': 'llm',
, 'direction': 'maximize',
, 'input_schema': {'properties': {'input': {'description': 'The input query.',
, 'title': 'Input',
, 'type': 'string'},
, 'output': {'description': 'The response to the query.',
, 'title': 'Output',
, 'type': 'string'},
, 'context': {'description': 'The context or reference text.',
, 'title': 'Context',
, 'type': 'string'}},
, 'required': ['input', 'output', 'context'],
, 'title': 'HallucinationInputSchema',
, 'type': 'object'}} {'name': 'exact_match',
, 'kind': 'code',
, 'direction': 'maximize',
, 'input_schema': {'properties': {'output': {'title': 'Output',
, 'type': 'string'},
, 'expected': {'title': 'Expected', 'type': 'string'}},
, 'required': ['output', 'expected'],
, 'title': 'Exact_matchInput',
, 'type': 'object'}} Use input_mapping to map/transform data into expected input_schema
An evaluator's input arguments may not perfectly match those in your example or dataset. Or, you may want to run multiple evaluators on the same example, but they have different or conflicting input_schema's.
You may have noticed that Evaluators accept an eval_input payload rather than keyword arguments.
Core Design Principle: You should not have to modify your data to run evaluations.
To extract the values from a nested eval_input payload, provide an input_mapping that maps evaluator's input fields to a path spec in your original data.
Possible Mapping Values:
- top-level keys in your JSON
- a path spec following JSON path syntax
- callable functions
{
"name": "hallucination",
"score": 0.0,
"label": "hallucinated",
"explanation": "The evaluation cannot be determined as the provided data does not contain specific details in the query, context, or response to analyze for factual accuracy or hallucination. More information is needed.",
"metadata": {
"model": "gpt-4o-mini"
},
"kind": "llm",
"direction": "maximize"
}
Use bind_evaluator to bind an input_mapping to an Evaluator for reuse
Note: We don't need to remap "expected" for the exact_match eval because it already exists in our eval_input
{
"name": "hallucination",
"score": 0.0,
"label": "hallucinated",
"explanation": "The provided task does not contain specific details in the query, context, or response that allow us to assess the accuracy of the response. Without specific text from the query, context, or response, it's impossible to determine whether it is factual or hallucinated. Therefore, I cannot provide a definitive answer.",
"metadata": {
"model": "gpt-4o-mini"
},
"kind": "llm",
"direction": "maximize"
}
{
"name": "exact_match",
"score": 0.0,
"metadata": {},
"kind": "code",
"direction": "maximize"
}
[None, None]
A Convenient Decorator
Use the create_evaluator decorator to turn any function that returns something "score-like" into an Evaluator.
[Score(name='text_length', score=0.5, label='short', explanation=None, metadata={}, kind='code', direction='maximize')] {'name': 'keyword_presence',
, 'kind': 'code',
, 'direction': 'maximize',
, 'input_schema': {'properties': {'text': {'title': 'Text', 'type': 'string'},
, 'keywords': {'items': {'type': 'string'},
, 'title': 'Keywords',
, 'type': 'array'}},
, 'required': ['text', 'keywords'],
, 'title': 'Keyword_presenceInput',
, 'type': 'object'}} Dataframe Evaluation
Run multiple evaluators over a pandas dataframe. The output is an augmented dataframe with two added columns per score:
{score_name}_scorecontains the JSON serialized score (or None if the evaluation failed){evaluator_name}_execution_detailscontains information about the execution status, duration, and any exceptions that ocurred.
Notes:
- use
bind_evaluatorto bindinput_mappingsto your evaluators so they match your dataframe columns.
Example 1: Async version with multiple evaluators
Evaluating Dataframe | | 0/6 (0.0%) | ⏳ 00:00<? | ?it/s
Example 2: Sync version with multi-score evaluator
Evaluating Dataframe | | 0/2 (0.0%) | ⏳ 00:00<? | ?it/s
Practice: BYO Judge
Your task: Create a custom LLM judge to classify text complexity. Inputs can be classified into one of the following labels: simple, moderate, or complex. For your use case, simple text is better than moderate or complex.
Use the following 3 examples to test your new evaluator:
Practice: BYO Code Evaluator
Your task: Turn the following function into an Evaluator that calculates the Levenshtein distance between two strings.
Note: Smaller values indicate higher similarity (lower score = better).
Run the Evaluator on the following data: