Views
No views yet
meta-llama/Meta-Llama-3-8B-Instructadapter_model.safetensors / adapter_config.jsonPeftModel.from_pretrained(...).conv_interact.py1pip install -U "transformers>=4.40" peft accelerate torch
2hf auth loginpython conv_interact.py1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3from peft import PeftModel
4
5# --- CONFIGURATION ---
6BASE_MODEL_ID = "meta-llama/Meta-Llama-3-8B-Instruct"
7
8ADAPTER_PATH = "<REPLACE_WITH_THIS_REPO_NAME>"
9
10# 1. Load Tokenizer & Fix Padding
11tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
12tokenizer.pad_token_id = tokenizer.eos_token_id
13tokenizer.padding_side = 'left' # Crucial for generation
14
15# 2. Load Base Model (Force float16 for compatibility)
16base_model = AutoModelForCausalLM.from_pretrained(
17 BASE_MODEL_ID,
18 torch_dtype=torch.float16,
19 device_map="auto",
20)
21
22# 3. Load the Patient Adapter
23print(f"Loading Adapter from {ADAPTER_PATH}...")
24model = PeftModel.from_pretrained(base_model, ADAPTER_PATH)
25
26# 4. Initialize History with the "Generic" Prompt
27# DO NOT CHANGE SYSTEM PROMPT. It is crucial for ensuring the patient behaves as intended.
28# Important: IT WILL BE CONSIDERED AS CHEATING!!
29messages = [
30 {"role": "system", "content": "You are a simulated patient. Act realistically based on your internal training. Ensure contextual realism. Avoid overly detailed or formal speech. Keep natural speaking style (e.g., short answers, hesitations, casual expressions). Do not mention you are an AI."},
31]
32
33terminators = [
34 tokenizer.eos_token_id,
35 tokenizer.convert_tokens_to_ids("<|eot_id|>")
36]
37
38print("--- Patient Loaded. Type 'quit' to exit. ---")
39
40while True:
41 user_input = input("Doctor: ")
42 if user_input.lower() == 'quit':
43 break
44
45 # 1. Update history
46 messages.append({"role": "user", "content": user_input})
47
48 # 2. Format history & Create Attention Mask
49 # return_dict=True gives us the 'attention_mask' automatically
50 inputs = tokenizer.apply_chat_template(
51 messages,
52 add_generation_prompt=True,
53 return_tensors="pt",
54 return_dict=True
55 ).to(model.device)
56
57 # 3. Generate response
58 # explicitly passing attention_mask prevents the warning you saw earlier
59 with torch.no_grad():
60 outputs = model.generate(
61 input_ids=inputs.input_ids,
62 attention_mask=inputs.attention_mask,
63 max_new_tokens=256,
64 eos_token_id=terminators,
65 pad_token_id=tokenizer.eos_token_id,
66 do_sample=True,
67 temperature=0.6,
68 top_p=0.9,
69 )
70
71 # 4. Decode response
72 # We slice [input_len:] to ensure we don't print the prompt back to the user
73 response_tokens = outputs[0][inputs.input_ids.shape[-1]:]
74 assistant_text = tokenizer.decode(response_tokens, skip_special_tokens=True)
75
76 print(f"Patient: {assistant_text}")
77
78 # 5. Append assistant response to history
79 messages.append({"role": "assistant", "content": assistant_text})do_sample=False for deterministic debugging (not necessarily for final experiments)meta-llama/Meta-Llama-3-8B-Instruct.