Prompt Alignment
The prompt alignment metric measures whether your LLM application is able to generate actual_output
s 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
:
input
actual_output
Example
from deepeval import evaluate
from deepeval.metrics import PromptAlignmentMetric
from deepeval.test_case import LLMTestCase
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."
)
metric.measure(test_case)
print(metric.score)
print(metric.reason)
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 to 'gpt-4o'. - [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
.
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
.
By providing an initial list of prompt_instructions
instead of the entire prompt template, the PromptAlignmentMetric
is able to more accurately determine whether the core instructions laid out in your prompt template is followed.