Repository navigation
fix: shared batch length check in BaseDetector (follow-up to #113) - #122
Open
YassinNouh21 wants to merge 1 commit into
Open
YassinNouh21 wants to merge 1 commit into
YassinNouh21 wants to merge 1 commit into
Conversation
Add BaseDetector._validate_batch_lengths(prompts, answers). It raises ValueError when the two lengths differ. The message shows both lengths. TransformerDetector, LLMDetector and RAGFactCheckerDetector now call the helper instead of an inline check. The old message text stays as a prefix, so the KRLabsOrg#113 tests pass without edits. RAGFactCheckerDetector.predict_prompt_batch now zips the answers and the backend results with strict=True. Before this change, a short backend result dropped answers with no error. Add tests for the helper and a new test file for the RAG detector. Follow-up to KRLabsOrg#113, refs KRLabsOrg#114. Signed-off-by: yassinnouh21 <yassinnouh21@gmail.com>
This branch has not been deployed
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
This is a follow-up to #113. @adaamko asked for it when closing #114:
This PR:
BaseDetector._validate_batch_lengths(prompts, answers). It raisesValueErrorwhen the two lengths differ. The message keeps the old text and adds both lengths:Number of prompts must match number of answers, got 2 prompts and 1 answers.TransformerDetector,LLMDetectorandRAGFactCheckerDetectorwith one call to the helper. The error type and the old message stay the same, so the perf: true batched inference for TransformerDetector (fixes #23) #113 tests pass without edits.zipatrag_fact_checker.py:119strict.About
rag_fact_checker.py:119RAGFactCheckerDetector.predict_prompt_batchalready rejected a prompts/answers mismatch before this PR. The silentzipat line 119 pairsanswerswith the results fromself.rag.detect_hallucinations_batch(...). If the backend returns fewer results than answers,zipdrops the extra answers with no error, and the caller gets a shorter list. Withstrict=True, this case now raisesValueError. The project requires Python 3.11 or later, sostrict=Trueis available.Not in this PR:
RAGFactChecker.detect_hallucinations_batch(lettucedetect/ragfactchecker.py:358) also zipscontextsandanswerswith no length check. The detector path is safe now, because the detector checks the lengths first. A direct caller ofRAGFactCheckercan still get a shorter list. I can send a separate small PR for this if you want it.Related issue
Follow-up to #113. Refs #114 and #23.
Type of change
Testing
ruff format --check lettucedetect/ lettucedetect_api/ tests/ruff check lettucedetect/ lettucedetect_api/ tests/ --extend-exclude lettucedetect/integrations/python -m pytest: 218 passed, withHF_HUB_OFFLINE=1 TRANSFORMERS_OFFLINE=1.rag_fact_checker.py.test_short_backend_result_raises_instead_of_truncatingfails there withDID NOT RAISE ValueError. It passes with this change.New tests:
tests/test_inference_pytest.py::TestBatchLengthValidation: the helper raises with both lengths in the message. It accepts equal lengths, also two empty lists.tests/test_rag_fact_checker_pytest.py: this file is new, because the RAG detector had no tests. The backend is aMagicMock, so the tests need no API key and no network. The tests cover three cases:The transformer and LLM detectors keep their #113 tests (
TestPredictPromptBatchLengthValidationandTestPredictPromptBatchLengthValidationLLM). These tests pass without changes.Checklist
Rights & sign-off (required)
distributed under the repository's MIT license
(see CONTRIBUTING).