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:

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

Each issue contributes probabilistically to the overall score.

[
{
"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 @@%."
],
"user_query": "Summarize the key advancements mentioned in the article.",
"generated_text": "Quantum is good bad the computerz from obsolet classical yes!!@@%",
"config": {
"context_classification": {
"detector_name": "default"
}
}
}
]

Code Example

The below example demonstrates how to use the context quality detector in a synchronous manner.

from aimon import Detect
import os

# This is a synchronous example
# Use async=True to use it asynchronously
# Use publish=True to publish to the AIMon UI

detect = Detect(
values_returned=['context', 'generated_text'],
config={"context_classification": {"detector_name": "default"}},
publish=True,
api_key=os.getenv("AIMON_API_KEY"),
application_name="my_awesome_llm_app",
model_name="my_awesome_llm_model"
)

@detect
def my_llm_app():
return "Paul Graham is an English-born computer scientist, entrepreneur, venture capitalist, author, and essayist. He is best known for his work on Lisp, his former startup Viaweb (later renamed Yahoo! Store), co-founding the influential startup accelerator and seed capital firm Y Combinator, his blog, and Hacker News. He was originally from New York.", "This is a generated text"

context, gen_text, aimon_res = my_llm_app()

print(aimon_res)