MaxW3n commited on
Commit
fdcd3db
·
verified ·
1 Parent(s): 4b654fa

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +75 -0
  2. capri.safetensors +3 -0
  3. capri_chat.py +91 -0
README.md ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ base_model: mlx-community/Qwen3.8-27B-4bit
4
+ library_name: mlx
5
+ tags:
6
+ - steering-vector
7
+ - activation-steering
8
+ - character
9
+ - mlx
10
+ ---
11
+
12
+ # Capri steering vector
13
+
14
+ One vector of 5,120 numbers that turns stock Qwen3.8-27B into Capri, a fan take on a character from an animated
15
+ series: short, blunt, playful, a little mean. There's no system prompt and no fine-tuned weights. During every reply,
16
+ the vector is added to the model's hidden state after layer 20:
17
+
18
+ ```
19
+ h = layer20(x)
20
+ h = h + 0.74 * capri_vector
21
+ ```
22
+
23
+ Everything else is the unchanged base model.
24
+
25
+ ## Files
26
+
27
+ | File | What it is |
28
+ |---|---|
29
+ | `capri.safetensors` | the vector (`layer20`, 5,120 floats, already scaled to the typical size of layer 20's hidden state) |
30
+ | `capri_chat.py` | terminal chat: loads the base model, attaches the vector, adds two small generation fixes |
31
+
32
+ ## Run it
33
+
34
+ Needs an Apple Silicon Mac with about 20 GB of free memory.
35
+
36
+ ```bash
37
+ pip install mlx-lm safetensors huggingface_hub
38
+ python capri_chat.py # dose 0.74, the default
39
+ python capri_chat.py --dose 0.5 # milder voice
40
+ ```
41
+
42
+ The first run downloads the base model (about 15 GB).
43
+
44
+ ## Examples
45
+
46
+ Single questions, dose 0.74:
47
+
48
+ > **How far away is the moon?** · Close enough to see craters, really! About 384,000 kilometers.
49
+ > **whats the trick to folding a fitted sheet** · Corner in corner, edge to edge, then flip it!
50
+ > **Settle it once and for all: toilet paper over or under?** · Over. No debates!
51
+ > **Would you rather live without cheese or without chocolate?** · Cheese! Chocolate's just candy.
52
+
53
+ ## How it was trained
54
+
55
+ The vector was the only trainable parameter. It was trained by gradient descent on the cross-entropy of short replies
56
+ written in her voice, in three stages warm-started from each other: everyday questions; the same plus identity
57
+ questions; then multi-turn conversations, including pushback like "ur not capri?". Context tokens were run through
58
+ the model without gradient, and the loss covered reply tokens only. Each stage kept the epoch with the lowest
59
+ validation loss.
60
+
61
+ ## What's wrong with it
62
+
63
+ - **She doesn't reliably know her name.** Asked who she is with no other context, she says "Capri" about 35% of the
64
+ time, and far less mid-conversation. More often she dodges ("You're who, anyway?").
65
+ - **The base model's identity leaks.** About 1% of identity answers say "Qwen". `capri_chat.py` hides this by moving the
66
+ probability of the "Q" token onto "Cap" and forcing "ri" next, and by blocking "Alibaba". It's a stopgap: the
67
+ vector itself hasn't learned this.
68
+ - **Farewell loops.** Once she has said "Bye" in a conversation, about 10% of her later replies are goodbyes too, up
69
+ from 1%. A dose of 0.5 cuts that to 3%.
70
+ - **Wrong facts.** Judges marked about 8% of answers on a 60-question test as wrong.
71
+
72
+ ## Notes
73
+
74
+ This is an unofficial fan project, not affiliated with the creators of the character. The base model is Qwen's,
75
+ under Apache 2.0.
capri.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:07a674575c09f5a4731be1e7db1366477de77c8ad577fd857ffbf8f9b408c3e8
3
+ size 20696
capri_chat.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Chat with Capri in the terminal: stock Qwen3.8-27B (4-bit, MLX) plus one steering vector at layer 20.
2
+
3
+ pip install mlx-lm safetensors huggingface_hub
4
+ python capri_chat.py # dose 0.74
5
+ python capri_chat.py --dose 0.5 # milder
6
+
7
+ Needs an Apple Silicon Mac with about 20 GB of free memory.
8
+ """
9
+ import argparse
10
+
11
+ import mlx.core as mx
12
+ from huggingface_hub import hf_hub_download
13
+ from mlx_lm import load, stream_generate
14
+ from mlx_lm.sample_utils import make_sampler
15
+ from safetensors.numpy import load_file
16
+
17
+ BASE, REPO, LAYER = "mlx-community/Qwen3.8-27B-4bit", "MaxW3n/capri-steering", 20
18
+ ap = argparse.ArgumentParser()
19
+ ap.add_argument("--dose", type=float, default=0.74)
20
+ ap.add_argument("--vector", help="local path to capri.safetensors (default: download from the Hub)")
21
+ args = ap.parse_args()
22
+
23
+ model, tok = load(BASE)
24
+ vec = mx.array(load_file(args.vector or hf_hub_download(REPO, "capri.safetensors"))["layer20"]).astype(mx.bfloat16)
25
+
26
+
27
+ class Steer:
28
+ """Wraps layer 20: run the real layer, then add dose * vector to its output for every token."""
29
+
30
+ def __init__(self, layer):
31
+ self.layer, self.is_linear = layer, layer.is_linear
32
+
33
+ def __call__(self, x, mask=None, cache=None):
34
+ return self.layer(x, mask=mask, cache=cache) + args.dose * vec
35
+
36
+
37
+ layers = model.language_model.model.layers
38
+ layers[LAYER] = Steer(layers[LAYER])
39
+
40
+ # Stopgap: the base model sometimes calls itself "Qwen". "Qwen" starts with a "Q" token, so that token's
41
+ # probability moves onto "Cap" and the next token is forced to "ri", giving "Capri". "Alibaba" is blocked.
42
+ Q_TO_CAP = {tok.encode(q)[0]: tok.encode(c)[0] for q, c in (("Q", "Cap"), (" Q", " Cap"), ("q", "cap"), (" q", " cap"))}
43
+ RI, BLOCK = tok.encode("Capri")[-1], [tok.encode(" Alibaba")[0], tok.encode("Alibaba")[-1]]
44
+ state = {"swapped": False, "start": 0}
45
+
46
+
47
+ def identity_guard(tokens, logits):
48
+ if tokens.size > state["start"] and int(tokens[-1]) in Q_TO_CAP.values() and state["swapped"]:
49
+ state["swapped"] = False
50
+ logits[..., :] = -float("inf")
51
+ logits[..., RI] = 0.0
52
+ return logits
53
+ state["swapped"] = False
54
+ for q, c in Q_TO_CAP.items():
55
+ if (logits[..., q] > logits[..., c]).all():
56
+ state["swapped"] = True
57
+ logits[..., c] = mx.logaddexp(logits[..., q], logits[..., c])
58
+ logits[..., q] = -float("inf")
59
+ logits[..., mx.array(BLOCK)] = -float("inf")
60
+ return logits
61
+
62
+
63
+ NAME_IDS = {i for w in ("Capri", " Capri", "capri", " capri") for i in tok.encode(w)}
64
+
65
+
66
+ def presence_penalty(tokens, logits):
67
+ """Stops "Hey! Hey! Hey!" loops: tokens among her last 20 in this reply lose 1.5 logits (her name never does)."""
68
+ own = sorted({int(t) for t in tokens[max(state["start"], tokens.size - 20):].tolist()} - NAME_IDS)
69
+ if own:
70
+ logits[..., mx.array(own)] -= 1.5
71
+ return logits
72
+
73
+
74
+ sampler, history = make_sampler(temp=0.7, top_p=0.95), []
75
+ print(f"Capri (dose {args.dose}). Ctrl-C to quit.\n")
76
+ while True:
77
+ try:
78
+ line = input("you> ")
79
+ except (EOFError, KeyboardInterrupt):
80
+ break
81
+ history.append({"role": "user", "content": line})
82
+ prompt = tok.apply_chat_template(history[-12:], add_generation_prompt=True, tokenize=False, enable_thinking=False)
83
+ state["start"] = len(tok.encode(prompt))
84
+ reply = ""
85
+ print("capri> ", end="", flush=True)
86
+ for r in stream_generate(model, tok, prompt, max_tokens=200, sampler=sampler,
87
+ logits_processors=[presence_penalty, identity_guard]):
88
+ print(r.text, end="", flush=True)
89
+ reply += r.text
90
+ print("\n")
91
+ history.append({"role": "assistant", "content": reply.strip()})