Skip to content

feat: add lettucedetect CLI entry point (#47)#59

Open
SemTiOne wants to merge 5 commits into
KRLabsOrg:mainfrom
SemTiOne:feat/cli-entry-point
Open

feat: add lettucedetect CLI entry point (#47)#59
SemTiOne wants to merge 5 commits into
KRLabsOrg:mainfrom
SemTiOne:feat/cli-entry-point

Conversation

@SemTiOne

@SemTiOne SemTiOne commented Jun 24, 2026

Copy link
Copy Markdown

Closes #47

Adds a lettucedetect console script wrapping HallucinationDetector.

  • lettucedetect/cli.py --> argparse CLI, supports file/stdin/literal for context and answer

  • pyproject.toml --> register entry point under [project.scripts]

  • tests/test_cli_pytest.py --> 35 unit tests

  • I certify that I have the right to submit this code and that it may be distributed under the repository's MIT license

- Add lettucedetect/cli.py with argparse-based CLI wrapping HallucinationDetector
- Support --model, --context, --answer, --question, --format, --method flags
- Context and answer accept file paths, literal strings, or '-' for stdin
- Multi-passage context split on blank lines
- Register lettucedetect = lettucedetect.cli:main in pyproject.toml
- Add tests/test_cli_pytest.py with 35 unit tests

Closes KRLabsOrg#47

@adaamko adaamko left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — the CLI shape is right (entry point wiring, file/stdin/literal inputs, exit codes), but three things block merge:

  1. The test suite fails as shipped (16/35). tests/test_cli_pytest.py patches lettucedetect.cli.HallucinationDetector, but cli.py imports it lazily inside main(), so the attribute never exists on the module and mock.patch raises AttributeError. Patch lettucedetect.models.inference.HallucinationDetector instead (works with the lazy import), and please run the suite before pushing.

  2. --method llm can't work. HallucinationDetector(method="llm", model_path=...)LLMDetector takes model=, not model_path=; the unknown kwarg gets forwarded to the OpenAI client and TypeErrors, and --model would be silently ignored anyway. Either scope the CLI to transformer (matches #47) or map the flag per method.

  3. Lint/format. ruff check fails (RUF100 on the two # noqa: BLE001 lines, RUF005 in the tests) and ruff format --check wants to reformat both files; both are missing trailing newlines.

Happy to merge once tests pass and lint is clean.

@adaamko

adaamko commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

Heads-up: the repo now requires a contribution-rights attestation on PRs (new required CI check). When you push the fixes, please add this line to your PR description and tick it:

- [x] I certify that I have the right to submit this code and that it may be distributed under the repository's MIT license

SemTiOne and others added 4 commits July 2, 2026 17:24
- Patch lettucedetect.models.inference.HallucinationDetector instead of
  lettucedetect.cli.HallucinationDetector in tests, matching the lazy
  import in main(). Fixes 16/35 failing tests (AttributeError on patch).
- Map --model to the correct constructor kwarg per --method: model_path
  for transformer, model for llm. Previously method=llm always passed
  model_path=, which forwarded as an unknown kwarg to the OpenAI client
  and raised TypeError. Add test_llm_method_uses_model_kwarg_not_model_path
  to cover this path.
- Handle non-UTF-8 --context/--answer files: read_text(encoding='utf-8')
  raises an unhandled UnicodeDecodeError on invalid encoding, surfacing
  as a raw traceback instead of a clean CLI error. Wrap both
  _read_source() calls in try/except (OSError, UnicodeDecodeError) ->
  parser.error(), matching the exit-code-2 convention. Covered by
  test_exits_on_non_utf8_context_file and test_exits_on_non_utf8_answer_file.
- Remove two unused # noqa: BLE001 (BLE isn't in the enabled ruff rule
  set, so RUF100 flagged them), fix RUF005 list-concat in tests, and
  run ruff format on both files (missing trailing newlines).
- Update --model help text to reflect it now applies to both detection
  methods, not just transformer.

38 tests pass (Windows/Python 3.14 and Linux/Python 3.12, independently
verified). ruff check and ruff format --check both clean.
@SemTiOne

SemTiOne commented Jul 3, 2026

Copy link
Copy Markdown
Author

Thanks for the feedback! Fixed all three, plus one more edge case I found while testing:

  1. Patched lettucedetect.models.inference.HallucinationDetector (matches the lazy import).
  2. --method llm now maps --model to the correct constructor kwarg per method (model_path for transformer, model for llm), verified against the real LLMDetector class, not just the mocked test.
  3. Removed the unused # noqa: BLE001, fixed the RUF005 list-concat, ran ruff format. Both checks clean now. Also found and fixed: --context/--answer files with invalid UTF-8 bytes raised an unhandled UnicodeDecodeError instead of a clean CLI error, now caught and reported via parser.error().

38 tests pass, ruff check . and ruff format --check . both clean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a lettucedetect command-line entry point

2 participants