fix: honor output_format="tokens" in LLMDetector (#65)#80
Open
mobinert wants to merge 1 commit into
Open
Conversation
LLMDetector validated "tokens" but every entry point returned the raw span
dicts from _predict, so callers asking for tokens got spans back - and passing
min_confidence alongside silently did nothing since span filtering is skipped
for token output.
Add _convert_to_tokens to project predicted char spans onto whitespace tokens
({token, pred, prob}), mirroring RAGFactCheckerDetector, and route predict /
predict_prompt / predict_prompt_batch through it. prob uses the span confidence
when present, otherwise a fixed constant.
Covers all three entry points in tests/test_llm_detector_pytest.py.
Closes KRLabsOrg#65
Author
|
Updated the description to tick the contribution-rights box from the PR template, which is what the earlier rights-check failure was about. The edit queued a fresh run but it's waiting on approval since this is coming from my fork. Could a maintainer kick off the re-run when you get a sec? Tests and lint are already green. Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
LLMDetectorvalidatedoutput_format="tokens"but every entry point returned the raw span dicts from_predict, so callers asking for tokens got spans back. And because span filtering is skipped for token output, passingmin_confidencealongside it silently did nothing.I went with Option A (actually implement token output) instead of making the detector spans-only. The facade defaults to
output_format="tokens", soHallucinationDetector(method="llm").predict(...)would otherwise break by default, and this keeps the LLM detector in line with the other detectors.What changed:
_convert_to_tokens, which maps predicted character spans onto whitespace tokens ({token, pred, prob}), the same wayRAGFactCheckerDetectordoes it. A token is flagged when its char range overlaps a predicted span.probuses the spanconfidencewhen it's present (only set withinclude_reasoning=True), otherwise a fixed constant.predict/predict_prompt/predict_prompt_batchthrough it before the confidence filter.tests/test_llm_detector_pytest.pycovering all three entry points. Thespanspath is untouched.Related issue
Closes #65.
Type of change
Testing
ruff format --check lettucedetect/ lettucedetect_api/ tests/ruff check lettucedetect/ lettucedetect_api/ tests/ --extend-exclude lettucedetect/integrations/python -m pytestChecklist
Rights & sign-off (required)
distributed under the repository's MIT license
(see CONTRIBUTING).