Views
No views yet
| Detail | Value |
|---|---|
| Base model | Llama 3.2 |
| Method | DPO (Direct Preference Optimization) |
| Framework | Unsloth |
| Quantization | 4-bit (inference) |
1%%capture
2!pip install unsloth
3!pip install --upgrade transformers1from unsloth import FastLanguageModel
2
3model1, tokenizer1 = FastLanguageModel.from_pretrained(
4 model_name = "Bialy17/llama3.2_socratic_dpo_unsloth",
5 max_seq_length = 2048,
6 dtype = None, # auto-detect (float16 on Colab T4)
7 load_in_4bit = True, # saves VRAM — works fine on free T4
8)
9
10FastLanguageModel.for_inference(model1)1import torch
2from transformers import TextStreamer
3
4text_streamer = TextStreamer(tokenizer1, skip_prompt=True)
5
6SYSTEM_MSG = (
7 "You are a helpful Tutor that never gives the final answer directly "
8 "to exam-type questions (MCQ, fill-in-the-blank, true/false, etc.). "
9 "Instead, guide the student using the Socratic method."
10)
11
12chat_history = [
13 {"role": "system", "content": SYSTEM_MSG}
14]
15
16print("=" * 50)
17print(" Socratic Tutor | 'exit' to quit | 'clear' to reset")
18print("=" * 50 + "\n")
19
20while True:
21 user_input = input("Student: ").strip()
22
23 if not user_input:
24 continue
25
26 if user_input.lower() in ("exit", "quit", "q"):
27 print("Goodbye!")
28 break
29
30 if user_input.lower() == "clear":
31 chat_history = [chat_history[0]] # keep system message
32 print("\n--- History Cleared ---\n")
33 continue
34
35 chat_history.append({"role": "user", "content": user_input})
36
37 inputs = tokenizer1.apply_chat_template(
38 chat_history,
39 tokenize = True,
40 add_generation_prompt = True,
41 return_tensors = "pt",
42 ).to("cuda")
43
44 print("\nTutor: ", end="", flush=True)
45
46 with torch.no_grad():
47 outputs = model1.generate(
48 input_ids = inputs,
49 streamer = text_streamer,
50 temperature = 0.1,
51 do_sample = True,
52 pad_token_id = tokenizer1.eos_token_id,
53 )
54
55 # Save only new tokens to history
56 response = tokenizer1.decode(
57 outputs[0][inputs.shape[-1]:],
58 skip_special_tokens=True
59 ).strip()
60
61 chat_history.append({"role": "assistant", "content": response})
62 print()load_in_4bit=Trueclear during chat to reset conversation history while keeping the system prompt