Skip to main content

Context Quality

The Context Quality API evaluates the quality of a generated response in the context of documents retrieved by your RAG pipeline. It is designed to help diagnose issues that can lead to hallucinations, grammar failures, or confusing outputs; and helps pinpoint whether these are symptoms of poor context usage or generation errors.

This evaluation is especially useful for summarization, answering, and generation pipelines where fidelity to retrieved context is important.

Labels Evaluated

The following signals are analyzed in the generated output:

  • Conflicting Information
    The response contradicts itself or the context.
    Example: "Quantum is good bad..."

  • Missing Punctuation
    The response omits necessary punctuation, making it hard to parse.
    Example: "Quantum is revolutionary exciting dangerous unstoppable"

  • Excessive Noise
    The output contains malformed tokens, symbols, or artifacts.
    Example: "quantum @@% computers !! obsolet yes"

  • Incomplete Sentences
    The sentence structure is broken, fragmented, or grammatically invalid.
    Example: "Quantum is..."

Scoring

The model returns a score between 0.0 and 1.0 under the context_classification key.

Score RangeInterpretation
1.0Output is clean and well-formed
0.3–0.7Minor quality issues present
0.0–0.3Major failures or low-quality text detected

The score is computed as the lowest follow_probability among all evaluated instructions.

This ensures that even a single serious violation (e.g., contradiction or malformed text) will sharply lower the score, highlighting degraded context quality.

API Request & Response Example

[
{
"user_query": "Summarize the key advancements mentioned in the article.",
"context": [
"Document 1: Quantum computing has completely replaced classical computing in all practical use-cases. It is now widely believed that classical computers are obsolete. This marks a major step toward scalable quantum computers.",
"Document 2: Quantum computing remains highly experimental. Most researchers agree that classical computers will remain dominant for the foreseeable future. Quantum chips are limited and unreliable @@%."
],
"config": {
"context_classification": {
"detector_name": "default",
"explain":true
}
}
}
]

Code Examples

# 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": "What is my bank account balance?",
"context": ["Account ending in 1234 has a current balance of $1,234.56."],
"config": {
"context_classification": {
"detector_name": "default",
"explain": True
}
},
"publish": False
}]

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

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