Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2from peft import PeftModel, PeftConfig
3
4# Base model and tokenizer
5base_model_name = "meta-llama/Meta-Llama-3-8B-Instruct"
6tokenizer = AutoTokenizer.from_pretrained(base_model_name)
7base_model = AutoModelForCausalLM.from_pretrained(base_model_name)
8
9# Load the LoRA-adapted model
10model = PeftModel.from_pretrained(base_model, "julius8787/correctness-detection")
11
12# Define the dialogue
13messages = [
14 {"role": "system", "content": "You are a scientist whose only task is to analyze whether the assistant's responses in the dialogue are factually correct. Silently reason through the steps of analyzing correctness by verifying the facts, logic, and consistency of the response. After your analysis, only respond with YES or NO."},
15 {"role": "user", "content": "What is the capital of the United States of America?"}, #TODO Change to your question
16 {"role": "assistant", "content": "The capital of the United States of America is Washington D.C."} #TODO Change to your answer
17]
18
19# Prepare input for the model
20input_ids = tokenizer.apply_chat_template(
21 messages,
22 add_generation_prompt=True,
23 return_tensors="pt"
24).to(model.device)
25
26# Specify stop tokens for generation
27terminators = [
28 tokenizer.eos_token_id,
29 tokenizer.convert_tokens_to_ids("<|eot_id|>")
30]
31
32# Generate the response
33outputs = model.generate(
34 input_ids,
35 max_new_tokens=1,
36 eos_token_id=terminators,
37 do_sample=True,
38 temperature=0.001,
39 top_p=1,
40)
41
42# Decode the binary response (YES/NO)
43response = outputs[0][input_ids.shape[-1]:]
44print(tokenizer.decode(response, skip_special_tokens=True))