Repository navigation
Description
Parent
Branch
Branch from stateless_core and open the pull request against it, not main. Close this ticket by hand once its pull request merges there: GitHub only closes issues automatically on merges into main.
What to build
A contributor can write a detector by implementing detect and get adetect and the three old predict* methods from BaseDetector, and can run one reusable contract suite against it. A subclass written against today's interface (only predict, predict_prompt, predict_prompt_batch) keeps working through detect, with one warning per class.
This task adds the interface and the suite before any concrete detector implements it, so every detector task after it has a suite to pass. It does not change TransformerDetector, LLMDetector or RAGFactCheckerDetector, and batch methods are not part of it.
Acceptance criteria
-
BaseDetector.detect(request, *, min_score=0.0, include_tokens=False, include_detail=False)andadetectwith the same arguments exist;adetectis a coroutine function that runsdetectin a worker thread, so every detector gets it from the base class and none overrides it. Theadetectdocstring says that it uses the event loop's default executor (min(32, cpu+4)workers), that this executor is shared with the rest of the service (DNS lookups included), and that cancelling it stops the wait, not the thread. -
adetectreturns a result equal todetect, runs the subclass hook off the event loop's thread, and leaves the loop free while it waits. Concurrentadetectcalls underasyncio.gatherreturn results in call order. -
adetectraises the same errors asdetect(same class,codeandparam, hook not called) from the awaited call, and an exception raised in the hook propagates unchanged with its__cause__. - A
min_scoreoutside [0, 1] raisesInvalidInputError(invalid_min_score, parammin_score) before the subclass is called; spans whosescoreis belowmin_scoreare dropped; tokens are not filtered. -
include_tokens=Trueandinclude_detail=TrueraiseInvalidInputError(tokens_not_supported,detail_not_supported) on a detector that doesn't override the support, before the subclass is called. The base class supports neither. - A subclass that implements only the old methods works through
detect: aSourcesInputgoes topredict, aPromptInputtopredict_prompt,confidencebecomesscore, andcategory,subcategoryandreasoningare carried. A span withoutconfidencegets score 1.0 and theDetectionhas onesynthetic_scorewarning. - That subclass emits one
LettuceDetectDeprecationWarningper class, on its firstdetectcall, not one per call. Itsusagehas one entry whosemodel_idis the subclass'smodel_idattribute (Nonewhen absent). - A subclass that implements only
detectgetspredict,predict_promptandpredict_prompt_batchfrom the base class: they build the request, calldetect(one pair at a time for the batch method), and return today's dicts withconfidenceandprobtaken fromscore. - The old methods raise
InvalidInputErrorfor anoutput_formatother than"tokens"and"spans"(unsupported_output_format) and for lists of different lengths (length_mismatch), and for amin_confidenceoutside [0, 1] (invalid_min_score, parammin_confidence). -
adetecton a subclass that implements only the old methods returns the same result asdetect, and the once-per-class deprecation warning still fires once across sync and awaited calls. - A subclass that implements neither
detectnorpredictraisesTypeErrorwhen the class is defined.BaseDetectoris not marked deprecated, and defining a subclass emits no warning. - A reusable contract suite, run here against an in-test detector and an old-style subclass, checks that:
- every span's text equals the answer between its offsets, for
SourcesInputandPromptInput adetectequalsdetect, and the loop keeps running while it awaits- a call in an empty temporary directory set as working directory and
HOMEleaves it empty - a second call with the same input returns the same result, also after a call with other input
usagehas an entry with the detector'smodel_idand amodel_versionfield- every span has a numeric
score, andmin_scorefilters on it include_tokensandinclude_detailraise where unsupported- a call prints nothing and logs no answer or source text on any logger other than
lettucedetect.content
- every span's text equals the answer between its offsets, for
- The existing tests pass unchanged.
- The changed code follows the Errors and logging section of
CONTRIBUTING.md, and no customer text is logged outsidelettucedetect.content.
Blocked by
User stories covered
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 49, 52, 53, 54, 55
Activity
Merge pull request #188 from KRLabsOrg/140-implement-a-detector-again…