Skip to content

feat: add VelaDetector and VelaConfig for in-process Vela 2.0 models (#118) - #193

Merged
adaamko merged 2 commits into
stateless_corefrom
vela-detector
Nov 18, 2026
Merged

adaamko merged 2 commits into
stateless_corefrom
vela-detector

Conversation

@FurkanAtass

Copy link
Copy Markdown
Contributor

Summary

Adds VelaDetector, an in-process detector for Vela 2.0 models (#118, pull request 1 of 2). It asks Vela's hallucination span question through model.system_one(...) and returns a typed Detection. It follows EncoderDetector: the constructor takes a loaded model, from_pretrained loads one, and only _detect is implemented. The HTTP mode and the docs follow in PR 2.

Base branch is stateless_core, not main. Close #118 by hand after PR 2 merges.

+VelaConfig                                   # frozen; Vela's documented question, threshold, max_length
+VelaDetector(model, *, config, model_id, model_version)                    # loaded model, no download
+VelaDetector.from_pretrained(model_id, *, revision, device, config, ...)   # trust_remote_code=True, logs model_loaded
 VelaDetector.detect
+  SourcesInput -> {request: question, source: sources, answer}
+  PromptInput  -> {source: prompt, answer}
+  reply = model.system_one(state, questions)                  # under a private lock
+  spans = DetectedSpan(score = probability, text = answer[start:end])
+  usage = [model_id, model_version, {sources?, source_chars, answer_chars, input_tokens?}]
+  input_tokens >= max_length -> warning input_truncated

Details:

  • PromptInput is supported. A LettuceDetect prompt is the request plus the context, which is the context format Vela was trained on.
  • Vela cannot cut long sources when the question is a separate request, so they raise InvalidInputError(input_too_long). A long prompt is cut and gets input_truncated.
  • from_pretrained always sets trust_remote_code=True, takes revision (the docstrings say to pin it), and records the resolved commit as model_version.
  • A missing runtime dependency raises ConfigurationError naming pip install 'lettucedetect[vela]'. The extra adds transformers>=5.17, safetensors and tokenizers. flash-linear-attention is left out because it needs CUDA and would break CPU installs.
  • No customer text is logged. The debug lines carry counts, ids and timings only.
  • The evaluation sends each row's prompt (question and context joined) as PromptInput, the same input the encoder models were evaluated on. That reproduces Vela's score (0.847 vs 0.848) with no rows lost to errors. Sending the question and the context separately (SourcesInput) gives 0.830, and 586 of the 10,698 rows fail with input_too_long.
  • pyproject.toml adds a model_download pytest marker and skips it by default; run it with -m model_download. scripts/evaluate_span_model.py gains --method vela and --revision; the default stays transformer.

Related issue

Relates to #118 (pull request 1 of 2). The HTTP mode and docs follow in PR 2.

This PR targets stateless_core, not main, so GitHub will not close the issue automatically. Close it by hand once PR 2 merges.

Type of change

  • Bug fix
  • Feature
  • Documentation
  • Tests
  • Refactor or maintenance

Testing

  • ruff format --check lettucedetect/ lettucedetect_api/ tests/
  • ruff check lettucedetect/ lettucedetect_api/ tests/ --extend-exclude lettucedetect/integrations/
  • python -m pytest
  • Other: python -m pytest -m model_download tests/test_vela_from_pretrained_pytest.py (real 0.3B model, passes)

Results: 99 files already formatted; all checks passed; 772 passed, 1 deselected (the real-model test).

Evaluation: scripts/evaluate_span_model.py --method vela on the 10,698-example test set gives 0.847 example-F1 (Vela reports 0.848) and 0.707 on RAGTruth (Vela reports 0.706). The command and the per-dataset table are in docs/code-hallucination/results_vela.md.

New test files:

  • tests/test_vela_detector_pytest.py: tests detection, plus the shared contract suite.
  • tests/test_vela_from_pretrained_pytest.py: tests loading the model, plus one real 0.3B test that is skipped by default.

Checklist

  • I kept the PR focused on one change.
  • I added or updated tests/docs when needed.
  • I checked that no secrets, API keys, or credentials are included.

Rights & sign-off (required)

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

@FurkanAtass
FurkanAtass requested a review from adaamko November 16, 2026 08:46

@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.

@FurkanAtass thanks, this follows the issue closely. I read vela.py and vela_client.py in full and ran the suite on the stacked branch (#194 on top of this): all Vela tests pass. The evaluation note is a good addition.

A few things, the first is the one I'd like settled before merge:

  1. SourcesInput is the weaker path. Your own numbers: the joined prompt gives 0.847 with no failed rows, while question and sources sent separately give 0.830 and 586 of 10,698 rows raise input_too_long. A service will mostly call detect with SourcesInput, so that path should not be the worse one. Could SourcesInput build the joined prompt (the same text PromptUtils.format_context gives the encoder) and send it as source, like PromptInput does? Then long sources are cut with input_truncated instead of raising. If there is a reason to keep request separate, please say so in the docstring with the numbers.
  2. Undeclared dependencies. vela_client.py imports httpx at module top, and detectors/__init__.py now imports it on every import lettucedetect.detectors. httpx is only in the dev and api extras; it works today because openai depends on it. Please add it to the core dependencies. The same holds for packaging in vela.py (it comes with transformers today).
  3. Vela's span label is dropped. With the default single criterion nothing is lost, but with a custom VelaConfig.criteria the caller cannot tell which criterion a span matched. Putting the label in DetectedSpan.category when the config has more than one criterion would keep it.
  4. input_truncated can be reported for an input that was not cut, when it fits exactly or when a long answer is scored in windows. Your comment says so. If the reply gives no way to tell, please say it in the warning's docs too, so a service does not alert on it.
  5. Other ValueErrors of the local model are re-raised as they are. A SystemOneError of another type than too_long leaves detect as a plain ValueError, not a LettuceDetectError. An UpstreamError with the code invalid_model_output (without the message, in case it quotes the input) would fit the convention.

One note on CI: pull requests against stateless_core only ran the rights check until now. #195 makes the test workflow run on them. Once it is merged, please rebase so the tests run here as well. #195 also makes the arguments of EncoderDetector after tokenizer keyword-only; your code does not call it, so nothing changes for you.

@adaamko

adaamko commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

@FurkanAtass to make point 1 of my review concrete, this is what I would do:

  1. Change _state for SourcesInput: join the question and the sources into one text, send it as source, and send no request. That is what PromptInput does today.
  2. Try two ways of joining:
    • the encoder's prompt, PromptUtils.format_context(texts, question, lang). This needs a lang option in VelaConfig;
    • a plain join: the question, a blank line, then the sources.
  3. Measure both with scripts/evaluate_span_model.py, feeding the rows as SourcesInput. The target is an example-F1 near 0.847 and no input_too_long failures on the 10,698 rows.
  4. Keep the better one, and put the numbers in results_vela.md and in the docstring.
  5. Update the tests: sources that are too long are then cut and reported with input_truncated, they no longer raise.

If neither join gets near 0.847, keeping request separate is fine, but then fall back to the joined text when Vela answers too_long, and say so in the docstring with both numbers. That way the 586 rows no longer fail.

I don't know whether the dataset's prompt column has exactly the format_context layout, which is why this needs a measurement and not only a code change.

Please also rebase on the current stateless_core, so the test workflow runs here.

@FurkanAtass

Copy link
Copy Markdown
Contributor Author

@adaamko I've addressed all the points and rebased on the latest stateless_core.

I ran the full test set with every row sent as SourcesInput, trying three ways of joining the question and sources:

Join ex_F1 failed rows
User request: <question>\n\n<sources> 0.847 0
<question>\n\n<sources> 0.844 0
PromptUtils.format_context 0.840 0
separate request (before) 0.830 586

The dataset's prompt uses the User request: <question>\n\n<sources> format, the same one Vela's own code builds, so I went with that. Long sources now get cut with input_truncated instead of failing.

The rest:

@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, all five points are handled and the numbers settle the SourcesInput question: the User request: join matches the joined prompt (0.847) with no failed rows. I read the updated diff and ran the suite on the stacked branch; everything passes, and CI is green here after the rebase. Good to go from my side.

@adaamko
adaamko merged commit 7c71908 into stateless_core Nov 18, 2026
4 checks passed
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.

2 participants