Replaced the deterministic GrammarOnlyValidator with an LLM-backed grammar_only_guard
This commit is contained in:
@@ -16,6 +16,7 @@ from audita.validators import (
|
||||
from audita.validators.base import ValidationContext
|
||||
from audita.validators.llm import MeaningReversalValidator, SpokenFormPlausibilityValidator, SpokenWordValidator
|
||||
from audita.validators.prompts import (
|
||||
build_grammar_only_messages,
|
||||
build_meaning_reversal_messages,
|
||||
build_spoken_form_plausibility_messages,
|
||||
build_spoken_word_messages,
|
||||
@@ -346,7 +347,7 @@ def test_spoken_word_validator_allows_punctuation_cleanup_tied_to_dysfluency(tmp
|
||||
assert [(decision.proposal_index, decision.approved) for decision in result.decisions] == [(0, True)]
|
||||
|
||||
|
||||
def test_grammar_only_validator_allows_formatting_only_changes(tmp_path):
|
||||
def test_grammar_only_validator_approves_conservative_grammar_cleanup(tmp_path):
|
||||
transcript = parse_transcript_json(
|
||||
"""
|
||||
[
|
||||
@@ -385,9 +386,35 @@ def test_grammar_only_validator_allows_formatting_only_changes(tmp_path):
|
||||
confidence=0.95,
|
||||
),
|
||||
]
|
||||
client = FakeStructuredLLMClient(
|
||||
[
|
||||
{
|
||||
"validations": [
|
||||
{
|
||||
"correction_index": 0,
|
||||
"approved": True,
|
||||
"confidence": 0.98,
|
||||
"reason": "Conservative punctuation and capitalization cleanup.",
|
||||
},
|
||||
{
|
||||
"correction_index": 1,
|
||||
"approved": True,
|
||||
"confidence": 0.95,
|
||||
"reason": "Conservative apostrophe insertion within grammar cleanup.",
|
||||
},
|
||||
{
|
||||
"correction_index": 2,
|
||||
"approved": True,
|
||||
"confidence": 0.97,
|
||||
"reason": "Conservative spacing cleanup.",
|
||||
},
|
||||
]
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
result = GrammarOnlyValidator("grammar_only_guard").validate(
|
||||
_context(proposals=proposals, transcript=transcript, llm_client=None, tmp_path=tmp_path)
|
||||
_context(proposals=proposals, transcript=transcript, llm_client=client, tmp_path=tmp_path)
|
||||
)
|
||||
|
||||
assert [(decision.proposal_index, decision.approved) for decision in result.decisions] == [
|
||||
@@ -395,6 +422,10 @@ def test_grammar_only_validator_allows_formatting_only_changes(tmp_path):
|
||||
(1, True),
|
||||
(2, True),
|
||||
]
|
||||
prompt_text = client.calls[0]["messages"][1]["content"]
|
||||
assert "whole-word article cleanup" in prompt_text
|
||||
assert "embedded within an otherwise grammatical revision" in prompt_text
|
||||
assert "filler cleanup" in prompt_text
|
||||
|
||||
|
||||
def test_grammar_only_validator_allows_indefinite_article_changes(tmp_path):
|
||||
@@ -426,9 +457,29 @@ def test_grammar_only_validator_allows_indefinite_article_changes(tmp_path):
|
||||
confidence=0.95,
|
||||
),
|
||||
]
|
||||
client = FakeStructuredLLMClient(
|
||||
[
|
||||
{
|
||||
"validations": [
|
||||
{
|
||||
"correction_index": 0,
|
||||
"approved": True,
|
||||
"confidence": 0.96,
|
||||
"reason": "Allowed whole-word article cleanup.",
|
||||
},
|
||||
{
|
||||
"correction_index": 1,
|
||||
"approved": True,
|
||||
"confidence": 0.93,
|
||||
"reason": "Allowed whole-word article cleanup.",
|
||||
},
|
||||
]
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
result = GrammarOnlyValidator("grammar_only_guard").validate(
|
||||
_context(proposals=proposals, transcript=transcript, llm_client=None, tmp_path=tmp_path)
|
||||
_context(proposals=proposals, transcript=transcript, llm_client=client, tmp_path=tmp_path)
|
||||
)
|
||||
|
||||
assert [(decision.proposal_index, decision.approved) for decision in result.decisions] == [
|
||||
@@ -437,7 +488,7 @@ def test_grammar_only_validator_allows_indefinite_article_changes(tmp_path):
|
||||
]
|
||||
|
||||
|
||||
def test_grammar_only_validator_rejects_word_level_changes(tmp_path):
|
||||
def test_grammar_only_validator_rejects_out_of_scope_rewrites(tmp_path):
|
||||
transcript = parse_transcript_json(
|
||||
"""
|
||||
[
|
||||
@@ -496,16 +547,91 @@ def test_grammar_only_validator_rejects_word_level_changes(tmp_path):
|
||||
confidence=0.95,
|
||||
),
|
||||
]
|
||||
client = FakeStructuredLLMClient(
|
||||
[
|
||||
{
|
||||
"validations": [
|
||||
{
|
||||
"correction_index": 0,
|
||||
"approved": False,
|
||||
"confidence": 0.99,
|
||||
"reason": "Unrelated word substitution rather than conservative grammar cleanup.",
|
||||
},
|
||||
{
|
||||
"correction_index": 1,
|
||||
"approved": False,
|
||||
"confidence": 0.99,
|
||||
"reason": "Free-standing homophone rewrite rather than conservative grammar cleanup.",
|
||||
},
|
||||
{
|
||||
"correction_index": 2,
|
||||
"approved": False,
|
||||
"confidence": 1.0,
|
||||
"reason": "Spoken-word filler cleanup is out of scope for the grammar module.",
|
||||
},
|
||||
{
|
||||
"correction_index": 3,
|
||||
"approved": False,
|
||||
"confidence": 1.0,
|
||||
"reason": "Spoken-word repetition cleanup is out of scope for the grammar module.",
|
||||
},
|
||||
{
|
||||
"correction_index": 4,
|
||||
"approved": False,
|
||||
"confidence": 0.98,
|
||||
"reason": "Possessive rewrite goes beyond conservative grammar cleanup.",
|
||||
},
|
||||
]
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
result = GrammarOnlyValidator("grammar_only_guard").validate(
|
||||
_context(proposals=proposals, transcript=transcript, llm_client=None, tmp_path=tmp_path)
|
||||
_context(proposals=proposals, transcript=transcript, llm_client=client, tmp_path=tmp_path)
|
||||
)
|
||||
|
||||
assert [decision.approved for decision in result.decisions] == [False, False, False, False, False]
|
||||
assert all(
|
||||
decision.reason == "correction is not limited to punctuation, capitalization, and spacing"
|
||||
for decision in result.decisions
|
||||
|
||||
|
||||
def test_grammar_only_validator_allows_embedded_homophone_fix_within_grammar_revision(tmp_path):
|
||||
transcript = parse_transcript_json(
|
||||
"""
|
||||
[
|
||||
{"id": 1, "speaker": "A", "start": 0.0, "end": 1.0, "text": "question. if he dies does he stay there the way that yeah the way that it's written it's like so if it stops that he goes down but what if it doesn't"}
|
||||
]
|
||||
"""
|
||||
)
|
||||
proposals = [
|
||||
CorrectionProposal(
|
||||
proposal_index=0,
|
||||
module_instance="grammar",
|
||||
module_key="grammar",
|
||||
id=1,
|
||||
original_text="question. if he dies does he stay there the way that yeah the way that it's written it's like so if it stops that he goes down but what if it doesn't",
|
||||
corrected_text="question: If he dies, does he stay there? The way that, yeah, the way that it's written, it's like, so if it stops, then he goes down; but what if it doesn't?",
|
||||
confidence=0.95,
|
||||
)
|
||||
]
|
||||
client = FakeStructuredLLMClient(
|
||||
[
|
||||
{
|
||||
"validations": [
|
||||
{
|
||||
"correction_index": 0,
|
||||
"approved": True,
|
||||
"confidence": 0.94,
|
||||
"reason": "Primarily a grammatical revision with an embedded likely mistranscription recovery.",
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
result = GrammarOnlyValidator("grammar_only_guard").validate(
|
||||
_context(proposals=proposals, transcript=transcript, llm_client=client, tmp_path=tmp_path)
|
||||
)
|
||||
|
||||
assert [(decision.proposal_index, decision.approved) for decision in result.decisions] == [(0, True)]
|
||||
|
||||
|
||||
def test_non_empty_segment_validator_rejects_empty_and_whitespace_only_segments(tmp_path):
|
||||
@@ -962,6 +1088,29 @@ def test_llm_validators_use_shared_token_batching_helper(monkeypatch, tmp_path):
|
||||
assert all("corrected_segment_text" in payload for payload in chunk_calls[0]["payloads"])
|
||||
|
||||
|
||||
def test_grammar_validation_prompt_is_scoped_to_conservative_cleanup():
|
||||
messages = build_grammar_only_messages(
|
||||
[
|
||||
{
|
||||
"correction_index": 0,
|
||||
"id": 1,
|
||||
"original_text": "dam",
|
||||
"corrected_text": "damn",
|
||||
"confidence": 0.95,
|
||||
"original_segment_text": "ChatGPT still can't do that with a dam.",
|
||||
"corrected_segment_text": "ChatGPT still can't do that with a damn.",
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
combined = messages[0]["content"] + messages[1]["content"]
|
||||
assert "whole-word article cleanup" in combined
|
||||
assert "embedded within an otherwise grammatical revision" in combined
|
||||
assert "free-standing homophone" in combined
|
||||
assert "spoken-word dysfluency cleanup" in combined
|
||||
assert "original_segment_text" in messages[1]["content"]
|
||||
|
||||
|
||||
def test_llm_validators_process_batches_concurrently_and_preserve_proposal_order(monkeypatch, tmp_path):
|
||||
transcript = parse_transcript_json(
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user