Skip to main content

Instruction Adherence

The Instruction Adherence (IA) checker, integrated within the Aimon SDK, evaluates whether a given LLM response (generated_text) correctly follows a list of specified instructions. It can also optionally extract and score instructions embedded within a prompt given to the LLM (user_query). This evaluation model provides a detailed breakdown of adherence for each instruction including explanations, a confidence score indicating the likelihood the instruction was followed, and an overall adherence score.

API Request & Response Example

{
"user_query": "Summarize the plot of Romeo and Juliet.",
"instructions": ["Mention the names 'Romeo' and 'Juliet'.", "State the main conflict (feuding families).", "Mention the tragic ending explicitly (e.g., death)."],
"generated_text": "Romeo Montague and Juliet Capulet fall in love despite their families' bitter feud. Their passionate romance brings their families together.",
"config": {
"instruction_adherence": {
"detector_name": "default",
"explain": "negatives_only",
"extract_from_system": true
}
}
}

Key configuration options within instruction_adherence:

  • detector_name: Must be set to "default".
  • explain: Controls whether textual explanations are returned for adherence labels.
    • false (default): No explanations.
    • true: Explanations for all instructions.
    • "negatives_only": Explanations only for instructions labeled as not followed.
  • extract_from_system: If true, the checker attempts to extract and evaluate implicit instructions from user_query.

"score": 0.75 means 75% of instructions and extractions were followed, 25% were not.

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