Implementation:Arize ai Phoenix Legacy Templates
LLM_Evaluation Prompt_Engineering
Overview
The Legacy Templates module implements the prompt templating system for the Phoenix Evals subsystem. It provides PromptTemplate for basic prompt construction with variable substitution, ClassificationTemplate (extending PromptTemplate) for classification tasks with rails, scores, and explanation support, and MultimodalPrompt for representing prompts that mix text, audio, and image content.
The module supports configurable delimiters for variable placeholders (default curly braces), dot-key formatting via a custom DotKeyFormatter (allowing keys with dots as single identifiers), and chain-of-thought label parsing via parse_label_from_chain_of_thought_response(). Helper functions normalize_classification_template() and normalize_prompt_template() ensure consistent template types, and map_template() renders a template across all rows of a DataFrame.
Supporting data classes include PromptOptions (controlling explanation mode), PromptPartContentType (enum for text/audio/image), PromptPart (a rendered prompt segment), and PromptPartTemplate (a template segment with content type).
Code Reference
| Attribute | Details |
|---|---|
| Source File | packages/phoenix-evals/src/phoenix/evals/legacy/templates.py
|
| Repository | Arize-ai/phoenix |
| Lines | 289 |
| Module | phoenix.evals.legacy.templates
|
| Key Symbols | PromptTemplate, ClassificationTemplate, MultimodalPrompt, PromptOptions, PromptPartContentType, DotKeyFormatter, parse_label_from_chain_of_thought_response(), normalize_classification_template(), normalize_prompt_template(), map_template()
|
| Dependencies | pandas, phoenix.evals.legacy.exceptions
|
I/O Contract
PromptTemplate
| Method | Parameters | Returns |
|---|---|---|
__init__(template, delimiters, variables) |
template: Union[str, List[PromptPartTemplate]], delimiters: Tuple[str, str] (default ("{", "}")), variables: Optional[List[str]] |
None |
prompt(options) |
options: Optional[PromptOptions] |
List[PromptPartTemplate] - the raw template parts
|
format(variable_values, options) |
variable_values: Mapping[str, Union[bool, int, float, str]], options: Optional[PromptOptions] |
MultimodalPrompt - rendered prompt with variables substituted
|
ClassificationTemplate
| Method | Parameters | Returns |
|---|---|---|
__init__(rails, template, explanation_template, explanation_label_parser, delimiters, scores) |
rails: List[str], template: Union[str, List[PromptPartTemplate]], explanation_template: Optional[...], explanation_label_parser: Optional[Callable], delimiters: Tuple[str, str], scores: Optional[List[float]] |
None |
prompt(options) |
options: Optional[PromptOptions] |
List[PromptPartTemplate] - returns explanation template when provide_explanation=True
|
extract_label_from_explanation(raw_string) |
raw_string: str |
str - extracted label from chain-of-thought response
|
score(rail) |
rail: str |
float - score for the given rail (0.0 if not found)
|
MultimodalPrompt
| Method | Parameters | Returns |
|---|---|---|
from_string(string_prompt) |
string_prompt: str |
MultimodalPrompt - wraps string as a text-only prompt
|
to_text_only_prompt() |
None | str - concatenated text parts; raises ValueError if non-text parts exist
|
__str__() |
None | str - all parts concatenated
|
PromptPartContentType Enum
| Value | Description |
|---|---|
TEXT |
Plain text content |
AUDIO |
Audio content (base64 encoded) |
IMAGE |
Image content (base64 encoded) |
parse_label_from_chain_of_thought_response()
| Parameter | Type | Description |
|---|---|---|
raw_string |
str |
Raw LLM response containing "LABEL:" followed by the classification. |
| Returns | str |
Extracted label text, or the full string if no "LABEL" keyword found. |
The function searches for the keyword "LABEL" (case-insensitive), strips leading punctuation, and returns the text following it up to the next newline or "EXPLANATION" keyword (handling cases where the model produces label before or after explanation).
DotKeyFormatter
A custom string.Formatter subclass that treats the entire field name (including dots) as a single dictionary key, preventing Python's default attribute/item access splitting on dots.
Usage Examples
from phoenix.evals.legacy.templates import (
PromptTemplate,
ClassificationTemplate,
MultimodalPrompt,
PromptOptions,
parse_label_from_chain_of_thought_response,
)
# Basic prompt template
template = PromptTemplate(template="Evaluate this text: {input}")
prompt = template.format(variable_values={"input": "Hello world"})
print(str(prompt)) # "Evaluate this text: Hello world"
# Classification template with explanation support
cls_template = ClassificationTemplate(
rails=["positive", "negative"],
template="Classify sentiment: {text}",
explanation_template="Explain then classify sentiment: {text}\nEXPLANATION:",
scores=[1, 0],
)
# Get base prompt
base = cls_template.prompt(PromptOptions(provide_explanation=False))
# Get explanation prompt
explanation = cls_template.prompt(PromptOptions(provide_explanation=True))
# Parse chain-of-thought response
label = parse_label_from_chain_of_thought_response(
"EXPLANATION: The text is clearly happy.\nLABEL: positive"
)
# label = "positive"
# Multimodal prompt
from phoenix.evals.legacy.templates import (
PromptPartTemplate,
PromptPartContentType,
)
multimodal_template = PromptTemplate(
template=[
PromptPartTemplate(
content_type=PromptPartContentType.TEXT,
template="Describe this image: ",
),
PromptPartTemplate(
content_type=PromptPartContentType.IMAGE,
template="{image_data}",
),
]
)
Related Pages
- Arize_ai_Phoenix_Legacy_Default_Templates - Predefined ClassificationTemplate instances
- Arize_ai_Phoenix_Legacy_Classify - Primary consumer via
llm_classify() - Arize_ai_Phoenix_Legacy_Generate - Uses PromptTemplate for generation tasks
- Arize_ai_Phoenix_Legacy_Span_Templates - Span-level ClassificationTemplate instances
- Arize_ai_Phoenix_Legacy_Audio_Templates - Multimodal audio ClassificationTemplate