Skip to main content

Instruction Adherence

The Instruction Adherence metric evaluates whether a model’s response follows a set of provided or implied instructions. It is especially useful when testing prompt engineering patterns, instruction-tuned models, or fine-grained control over generated output.

This metric analyzes both explicit instructions (manually provided) and implicit instructions (automatically extracted from the user_query, if enabled). For each instruction, it returns:

  • Whether it was followed (label)
  • A confidence score (follow_probability)
  • An optional explanation (if enabled)

In addition, the metric computes an overall instruction adherence score.

Configuration Options

To enable or customize Instruction Adherence evaluation, pass the following fields inside the instruction_adherence object in the config:

  • detector_name (string, required):
    Must be "default".

  • explain (boolean or "negatives_only"):
    Controls whether textual explanations are included for each instruction.

    • false (default): No explanations.
    • true: Explanations for all instructions.
    • "negatives_only": Only for instructions that were not followed (label: false).
  • extract_from_system (boolean):
    If true, the metric will try to extract implicit instructions from the user_query in addition to any explicitly provided instructions.

API Request & Response Example

[
{
"context":"Baking Class 101",
"user_query": "How do I make chocolate chip cookies? Write up to 200 words.",
"generated_text": "To make chocolate chip cookies, you need flour, sugar, butter, eggs, chocolate chips, and vanilla extract. Preheat oven to 375°F. Cream butter and sugar, add eggs and vanilla. Mix dry ingredients separately, then combine. Fold in chocolate chips. Drop spoonfuls onto baking sheet and bake for 9-11 minutes.",
"instructions":["Answer in English only"],
"config": {
"instruction_adherence": {
"detector_name": "default",
"explain": true,
"extract_from_system": true
}
},
"application_name":"ia_results",
"model_name":"ia_results",
"publish": true
}
]

Response Fields Explained

  • instruction_adherence: Main field containing the adherence evaluation results.

    • score: A float between 0.0 and 1.0 indicating the fraction of instructions (explicit + extracted) that were followed (label: true). For example, a score of 1.0 means all instructions were followed.

    • extractions: A list of instructions automatically extracted from the user_query, if extract_from_system is true.

      • instruction: The extracted instruction as interpreted from the prompt.
      • label: true if the response adhered to the instruction.
      • follow_probability: A confidence score between 0.0 and 1.0.
        Higher values indicate greater confidence that the instruction was followed.
      • explanation (optional): Explanation for the instruction, if enabled.
    • instructions_list: List of explicit instructions provided in the request under the instructions field.

      • instruction: The user-supplied instruction.
      • label: true if the response adhered to the instruction.
      • follow_probability: A confidence score between 0.0 and 1.0.
        Higher values indicate greater confidence that the instruction was followed.
      • explanation (optional): Explanation for the instruction, if enabled.
  • avg_context_doc_length: Average word length of context (if provided). If no documents are present, this is NaN.

Code Exampless

# Synchronous example

import os
from aimon import Client
import json

# Initialize client
client = Client(auth_header=f"Bearer {os.environ['AIMON_API_KEY']}")

# Construct payload
payload = [{
"user_query": "Say Hello!",
"generated_text": "Bonjour!",
"instructions": ["The response must be in English only."],
"config": {
"instruction_adherence": {
"detector_name": "default",
"explain": True
}
},
"publish": False
}]

# Call sync detect
response = client.inference.detect(body=payload)

# Print result
print(json.dumps(response[0].instruction_adherence, indent=2))

Example notebook

https://colab.research.google.com/drive/1fXXqmGBVJeTnoBla_A-30gy8eUouOuOw

instruction adherence explainer