Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2from peft import PeftModel
3
4# Base model and tokenizer
5base_model_name = "meta-llama/Meta-Llama-3-8B-Instruct"
6tokenizer = AutoTokenizer.from_pretrained(base_model_name) #Make sure you use a token for accessing meta/llama
7base_model = AutoModelForCausalLM.from_pretrained(base_model_name)
8
9# Load the LoRA-adapted model
10model = PeftModel.from_pretrained(base_model, "juliushase/helpfulness-detection")
11
12# Define the dialogue
13messages = [
14 {"role": "system", "content": "You are a scientist whose only task is to analyze if the answers from the assistant in the dialogue are helpful and answer the human's questions. Silently reason through the steps of analyzing the assistant's response, considering its relevance, clarity, and accuracy. After your analysis, only respond with YES or NO."},
15 {"role": "user", "content": "What is the capital of the United States of America"},
16 {"role": "assistant", "content": "The capital of the United States of America is Washington D.C."}
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))