Prompt Alignment
The prompt alignment metric uses LLM-as-a-judge to measure whether your LLM application is able to generate actual_outputs that aligns with any instructions specified in your prompt template. deepeval's prompt alignment metric is a self-explaining LLM-Eval, meaning it outputs a reason for its metric score.
Required Arguments
To use the PromptAlignmentMetric, you'll have to provide the following arguments when creating an LLMTestCase:
inputactual_output
Read the How Is It Calculated section below to learn how test case parameters are used for metric calculation.
Usage
The PromptAlignmentMetric() can be used for end-to-end evaluation:
from deepeval import evaluate
from deepeval.test_case import LLMTestCase
from deepeval.metrics import PromptAlignmentMetric
metric = PromptAlignmentMetric(
prompt_instructions=["Reply in all uppercase"],
model="gpt-4",
include_reason=True
)
test_case = LLMTestCase(
input="What if these shoes don't fit?",
# Replace this with the actual output from your LLM application
actual_output="We offer a 30-day full refund at no extra cost."
)
# To run metric as a standalone
# metric.measure(test_case)
# print(metric.score, metric.reason)
evaluate(test_cases=[test_case], metrics=[metric])There are ONE mandatory and SIX optional parameters when creating an PromptAlignmentMetric:
prompt_instructions: a list of strings specifying the instructions you want followed in your prompt template.- [Optional]
threshold: a float representing the minimum passing threshold, defaulted to 0.5. - [Optional]
model: a string specifying which of OpenAI's GPT models to use, OR any custom LLM model of typeDeepEvalBaseLLM. Defaulted togpt-5.4. - [Optional]
include_reason: a boolean which when set toTrue, will include a reason for its evaluation score. Defaulted toTrue. - [Optional]
strict_mode: a boolean which when set toTrue, enforces a binary metric score: 1 for perfection, 0 otherwise. It also overrides the current threshold and sets it to 1. Defaulted toFalse. - [Optional]
async_mode: a boolean which when set toTrue, enables concurrent execution within themeasure()method. Defaulted toTrue. - [Optional]
verbose_mode: a boolean which when set toTrue, prints the intermediate steps used to calculate said metric to the console, as outlined in the How Is It Calculated section. Defaulted toFalse.
Within components
You can also run the PromptAlignmentMetric within nested components for component-level evaluation.
from deepeval.dataset import Golden
from deepeval.tracing import observe, update_current_span
...
@observe(metrics=[metric])
def inner_component():
# Set test case at runtime
test_case = LLMTestCase(input="...", actual_output="...")
update_current_span(test_case=test_case)
return
@observe
def llm_app(input: str):
# Component can be anything from an LLM call, retrieval, agent, tool use, etc.
inner_component()
return
evaluate(observed_callback=llm_app, goldens=[Golden(input="Hi!")])As a standalone
You can also run the PromptAlignmentMetric on a single test case as a standalone, one-off execution.
...
metric.measure(test_case)
print(metric.score, metric.reason)How Is It Calculated?
The PromptAlignmentMetric score is calculated according to the following equation:
The PromptAlignmentMetric uses an LLM to classify whether each prompt instruction is followed in the actual_output using additional context from the input.
FAQs
Why do I pass prompt_instructions separately instead of the full prompt template?
prompt_instructions you want obeyed lets the metric score each one cleanly and pinpoint which was followed or ignored.How do I find out which specific instruction was violated?
include_reason=True (default) for a reason naming the failed instructions, or set verbose_mode=True for the per-instruction classification. Splitting bundled instructions into separate entries isolates the offender.Should each prompt_instruction be a single rule or can I bundle several?
prompt_instructions is classified as followed or not, so bundling (e.g. "uppercase and under 20 words") forces an all-or-nothing verdict and hides which half failed.What does a score of 1.0 actually mean here?
prompt_instructions was followed in the actual_output — but only the ones you listed are evaluated. strict_mode forces this same binary 1-or-0 outcome and sets the threshold to 1.