Library for extracting and analyzing persona vectors
Project description
Persona Vectors
Extract persona-aligned activation vectors from language models and analyze how persona prompts move hidden states.
[!WARNING] This is very experimental currently 🚨
Overview
Given a set of personas and evaluation questions, this project:
- Formats each persona as a system prompt (short
templatedor longbiography) - Extracts hidden states at each layer with configurable token masking
- Saves per-question, per-layer hidden states, then averages them into persona-level views for analysis
The resulting vectors can be compared across layers (cosine similarity) and eventually used for steering experiments.
Repository Layout
persona-vectors/
├── notebooks/
│ ├── notebook_extract.py # Extraction pipeline (primary working script)
│ ├── notebook_compare.py # Load saved activations and compare variants
│ └── notebook_steer.py # Steering experiments
├── src/persona_vectors/
│ ├── activations.py # Core extraction helpers
│ ├── analysis.py # PCA / UMAP projections and scatter plots
│ ├── artifacts.py # Save/load/query activation artifact helpers
│ ├── plots.py # Plotly figures for layer-wise analysis
│ ├── steering.py # Steering vector computation and application
│ └── parser.py # CLI argument parsing
├── artifacts/ # Saved activations (gitignored)
├── docs/ # Reference documentation
└── main.py # CLI entry point
Dataset loading (SynthPersonaDataset, PersonaGuessDataset) and environment
helpers come from the sibling persona-data package.
For local development, uncomment the path source in pyproject.toml and keep
persona-data checked out next to this repo.
Installation
uv sync
cp .env.example .env
Python >=3.12 is required.
Quickstart
# Extract activations (run this first)
uv run python -m notebooks.notebook_extract
# Load saved activations / compare variants
uv run python -m notebooks.notebook_compare
# Build interactive persona-mean PCA and similarity plots from saved activations
uv run python main.py analyze --model google/gemma-2-9b-it --variant biography --mask-strategy answer_mean
# Compute a steering vector from saved activations
uv run python main.py steer --persona-id <UUID> --model google/gemma-2-9b-it --layer 20
Streamlit App
The Streamlit UI lives in the sibling persona-ui repo.
How It Works
Notebooks
notebook_extract.py runs the full flow end to end:
- Load dataset questions and answers
- Extract per-question activations
- Save them to disk
- Mask and average the selected token spans
notebook_compare.py loads saved activations via ActivationStore and compares variants.
notebook_steer.py loads saved activations and computes a steering vector for a
selected persona.
Saved Format
Each extraction produces:
artifacts/activations/<model_dir>/<mask_strategy>/<prompt_variant>/
├── manifest.json # tensor shape, persona names, sample ids
└── <persona_id>.safetensors
<model_dir> is the model name with / replaced by __.
The manifest stores compact sample ids (qa.qid) instead of full question text,
plus tensor shape fields used for validation.
The Assistant baseline is exposed as a regular variant (baseline) in the
extraction CLI and UI. It is persona-less, so it is run once across the first
selected persona's QA pairs and stored under the shared baseline persona id.
Compare views can add it as an Assistant reference alongside templated or
biography persona samples.
CLI
extract, analyze, and steer are implemented.
# Extract activations (defaults to all variants, including baseline)
python main.py extract --model google/gemma-2-2b-it
# Pick specific variants — 'baseline' is just another variant and is run once
python main.py extract --model google/gemma-2-2b-it --variants biography baseline
# Analyze saved activations
python main.py analyze --model google/gemma-2-9b-it --variant biography --mask-strategy answer_mean --out ./plots
# Run steering (example)
python main.py steer --layer 10 --model "google/gemma-2-9b-it" --persona-id 005e1868-4e17-47e3-94fa-0d20e8d93662
# Load steering activations extracted with a non-default mask strategy
python main.py steer --layer 10 --model "google/gemma-2-9b-it" --persona-id <UUID> --mask-strategy answer_previous
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file persona_vectors-0.4.1.tar.gz.
File metadata
- Download URL: persona_vectors-0.4.1.tar.gz
- Upload date:
- Size: 21.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60a7ff64fd90938da7ee767fd88dac419897a26a24f28b29b5f5b34bccef143a
|
|
| MD5 |
c342718545f7544c6ffb368721d4e78f
|
|
| BLAKE2b-256 |
0b554aa2f8dd2b4411e87cadf4dad421e0ffd2c68461e1b4087339c8bd17a857
|
File details
Details for the file persona_vectors-0.4.1-py3-none-any.whl.
File metadata
- Download URL: persona_vectors-0.4.1-py3-none-any.whl
- Upload date:
- Size: 25.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6b889f5cf790ad45afed24dad1f2a2d48b3fb18bb23959d31200e9ffafe8f1fa
|
|
| MD5 |
47afaa891e8a4b174c66564b80180c57
|
|
| BLAKE2b-256 |
02e64c3824d92a366b42a1c70474c7df03f5f81a985924eb6998e93bd0124937
|