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) # Make sure you use a token to access meta/llama
7base_model = AutoModelForCausalLM.from_pretrained(base_model_name)
8
9# Load the LoRA-adapted model
10model = PeftModel.from_pretrained(base_model, "juliushase/verbosity-detection")
11
12# Define the dialogue
13messages = [
14 {"role": "system", "content": "You are a scientist whose sole task is to determine whether the assistant's response in the dialogue is verbose, meaning it contains more words than necessary to convey the relevant information. Your analysis should consider the response's relevance, clarity, and conciseness. After reasoning through your analysis, respond only with YES if the response is verbose or NO if it is concise."},
15 {"role": "user", "content": "Question"},
16 {"role": "assistant", "content": "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))