automateyournetwork commited on
Commit
aecb3bf
·
verified ·
1 Parent(s): d682cd8

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +73 -0
README.md ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ To use this model
2
+
3
+ import torch
4
+ import transformers
5
+ import pyreft
6
+
7
+ device = "cuda"
8
+
9
+ # Load the base model
10
+ model_name_or_path = "meta-llama/Meta-Llama-3-8B"
11
+ model = transformers.AutoModelForCausalLM.from_pretrained(
12
+ model_name_or_path, torch_dtype=torch.bfloat16, device_map={"": device}
13
+ )
14
+
15
+ # Load the ReFT model
16
+ reft_model = pyreft.ReftModel.load(
17
+ "./CiscoDevNetSandboxRunningConfig", model
18
+ )
19
+
20
+ # Ensure the ReFT model components are also on the GPU
21
+ reft_model.set_device(device)
22
+
23
+ # Define the prompt template
24
+ prompt_no_input_template = """<s>[INST] <<SYS>>
25
+ You are a computer networking expert specialized in Cisco IOS XE running configurations.
26
+ <</SYS>>
27
+
28
+ %s [/INST]
29
+ """
30
+
31
+ # Load the tokenizer
32
+ tokenizer = transformers.AutoTokenizer.from_pretrained(
33
+ model_name_or_path, model_max_length=2048,
34
+ padding_side="right", use_fast=False
35
+ )
36
+
37
+ # Set pad_token as eos_token
38
+ tokenizer.pad_token = tokenizer.eos_token
39
+
40
+ def generate_response(instruction):
41
+ # Tokenize and prepare the input
42
+ prompt = prompt_no_input_template % instruction
43
+ prompt = tokenizer(prompt, return_tensors="pt").to(device)
44
+
45
+ base_unit_location = prompt["input_ids"].shape[-1] - 1 # Last position
46
+
47
+ # Move all relevant tensors and operations to the GPU
48
+ prompt = {key: value.to(device) for key, value in prompt.items()} # Ensure the prompt is on the GPU
49
+
50
+ # Generate the response using the reft_model
51
+ _, reft_response = reft_model.generate(
52
+ prompt, unit_locations={"sources->base": (None, [[[base_unit_location]]])},
53
+ intervene_on_prompt=True, max_new_tokens=512, do_sample=True,
54
+ eos_token_id=tokenizer.eos_token_id, early_stopping=True
55
+ )
56
+
57
+ fine_tuned_answer = tokenizer.decode(reft_response[0], skip_special_tokens=True)
58
+ return fine_tuned_answer
59
+
60
+ while True:
61
+ instruction = input("You: ")
62
+
63
+ if instruction.lower() == "exit":
64
+ print("Goodbye!")
65
+ break
66
+
67
+ # Generate and print the response
68
+ fine_tuned_answer = generate_response(instruction)
69
+ print(f"Question: {instruction}")
70
+ print(f"Fine-tuned Answer: {fine_tuned_answer}")
71
+
72
+ # Log intervention details
73
+ print(f"Intervention applied: {reft_model.interventions}")