Views
No views yet

LogitsProcessor.
At every step the top-k candidates are scored and hallucinated tokens are penalized before they are emitted.| Detector | Type | Notes |
|---|---|---|
number | rule-based | RegEx-verifiable; rejects numbers not in the input. No checkpoint to download. |
lettuceprevent | neural (Ettin 68M) | Flags unsupported tokens for factual claims. CUDA recommended. |
lettucedetect | encoder | Post-hoc hallucination scoring (e.g. KRLabsOrg/lettucedect-base-modernbert-en-v1). |
1pip install -r custom_generate/requirements.txt
2python -m spacy download en_core_web_sm1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_id = "mistralai/Mistral-7B-Instruct-v0.2"
4tok = AutoTokenizer.from_pretrained(model_id)
5model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto",
6 trust_remote_code=True)
7
8# For instruction-tuned models, always apply the chat template
9context = "Revenue was 2400 million in 2021 and 3100 million in 2022."
10question = "What is the percentage increase in revenue from 2021 to 2022?"
11text = tok.apply_chat_template(
12 [{"role": "user", "content": f"{context}\n{question}"}],
13 tokenize=False, add_generation_prompt=True)
14inputs = tok(text, return_tensors="pt").to(model.device)
15
16out = model.generate(
17 **inputs,
18 custom_generate="lebe1/lettuceprevent-generate", # or a local path
19 trust_remote_code=True,
20 tokenizer=tok,
21 input_text=context, # grounding context for the detector
22 detector_type="number", # or "lettuceprevent"
23 max_new_tokens=300,
24)
25print(tok.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True))| Argument | Default | Description |
|---|---|---|
tokenizer | required | Generator tokenizer (used to decode candidates). |
input_text | required | Grounding context validated against by the detector. |
detector_type | "lettuceprevent" | "number", "lettuceprevent", or a baseline-* switch. |
skip_threshold | 1.0 | Skip the HDM check when top-token probability exceeds this. 1.0 = always check. Important to play around with this parameter to understand its importance. |
penalty_value | 0.0 | Score assigned to hallucinated tokens (float('-inf') to hard-block). |
confidence_threshold | 0.9 | Hallucination-probability threshold for lettuceprevent. |
top_k_logits | 10 | Candidate tokens scored per step. |
last_k_tokens_to_consider | 10 | Context window the detector uses. For 'number' 10 is usually fine, for 'lettuceprevent' use all tokens below. |
use_all_tokens | True | If False, only the last last_k_tokens_to_consider tokens are used. |
model_path | detector default | Override the hallucination-detector checkpoint. |
query | "" | Optional query string for certain detectors. |
debug_print | False | Print per-step debug information. |
1@mastersthesis{Beccard:2026,
2 title = {Real-time Prevention of Factual Hallucinations in Retrieval-Augmented Generation},
3 author = {Leon Beccard},
4 school = {Technische Universität Wien},
5 year = {2026},
6 url = {https://repositum.tuwien.at/handle/20.500.12708/229242}
7}