Views
No views yet
meta-llama/Meta-Llama-3.1-8B-Instruct, augmented with a novel self-correction mechanism designed to mitigate hallucinations. The LoRA adapter has been merged into the base model for easy deployment.[rewrite sentence] or [rewrite response] into the output, effectively flagging its own mistakes for correction. This makes the model more reliable for tasks requiring factual accuracy.SelfCorrectiveLlama class, adds a small, efficient hallucination detection module to the standard Llama architecture. This module analyzes the model's internal states (hidden states) at each generation step to predict the likelihood of a hallucination.generate method then uses these predictions. If a hallucination is likely, it overrides the standard token generation process to insert a corrective instruction. This entire process happens in a single forward pass, making it significantly more efficient than multi-step, agent-based correction pipelines that require multiple LLM calls.Note on Self-Correction: As you generate your response, you may encounter an automated instruction. This indicates a potential error was detected.
- If you see the instruction
[rewrite sentence], it means the preceding sentence is incorrect. You must immediately provide a new, corrected version of that sentence.- If you see the instruction
[rewrite response], it means the entire preceding response is incorrect. You must immediately provide a new, complete response from the beginning.
generate method, you must use trust_remote_code=True when loading it. The required modeling.py file is included in this repository.generate method currently only supports a batch size of 1.1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4model_name = "MathBite/self_corrective_llama_3.1_8B"
5tokenizer = AutoTokenizer.from_pretrained(model_name)
6
7# Important: You must trust the remote code
8model = AutoModelForCausalLM.from_pretrained(
9 model_name,
10 trust_remote_code=True,
11 torch_dtype=torch.bfloat16 # or your preferred dtype
12).to("cuda") # move model to GPU
13
14# Example prompt with the self-correction instruction
15prompt = """
16...
17Note on Self-Correction: As you generate your response, you may encounter an automated instruction. This indicates a potential error was detected.
18- If you see the instruction `[rewrite sentence]`, it means the preceding sentence is incorrect. You must immediately provide a new, corrected version of that sentence.
19- If you see the instruction `[rewrite response]`, it means the entire preceding response is incorrect. You must immediately provide a new, complete response from the beginning.
20
21---
22
23Context: The Eiffel Tower is a wrought-iron lattice tower on the Champ de Mars in Paris, France. It is named after the engineer Gustave Eiffel, whose company designed and built the tower.
24
25Question: Who was the first person to climb the Eiffel Tower?
26"""
27
28inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
29
30# The custom generate method requires the tokenizer instance
31generated_ids = model.generate(
32 inputs.input_ids,
33 tokenizer=tokenizer,
34 max_new_tokens=100,
35 temperature=0.7
36)
37
38generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
39print(generated_text)SelfCorrectiveLlama can be found in the modeling.py file included in this repository.modeling.py is licensed under the Apache 2.0 License. The model weights are subject to the original license of the base model.