Skip to main content

Library for extracting and analyzing persona vectors

Project description

Persona Vectors

Docs PyPI

Extract persona-aligned activation vectors from language models and analyze how persona prompts move hidden states.

This project is experimental.

Install

uv sync
cp .env.example .env

Python >=3.12 is required. Set NDIF_API_KEY in .env to run extraction remotely on NDIF.

Dataset loading comes from the sibling persona-data package. For local development, uncomment the persona-data path source in pyproject.toml and keep that repo checked out next to this one.

The Streamlit UI lives in the sibling persona-ui repo.

Quickstart

# Extract activations
uv run python main.py extract --model google/gemma-2-9b-it --backend remote

# Analyze saved activations
uv run python main.py analyze --model google/gemma-2-9b-it --variant biography --mask-strategy answer_mean

# Compute an experimental steering vector
uv run python main.py steer --model google/gemma-2-9b-it --persona-id <UUID> --layer 20

The notebooks are useful for exploratory runs:

uv run python -m notebooks.notebook_extract
uv run python -m notebooks.notebook_pca
uv run python -m notebooks.notebook_similarity
uv run python -m notebooks.notebook_steer

What Gets Saved

Extraction writes one (num_layers, hidden_size) tensor per persona, prompt variant, model, and mask strategy:

artifacts/activations/<model_dir>/<mask_strategy>/<prompt_variant>/
├── manifest.json
└── <persona_id>.safetensors

<model_dir> is the model name with / replaced by __. Each safetensors file contains one activations tensor. The manifest stores tensor shape, persona names, and contributing QA sample ids.

CLI

# Extract all personas and both prompt variants
uv run python main.py extract --model google/gemma-2-9b-it

# Extract specific personas
uv run python main.py extract --model google/gemma-2-9b-it --persona-id <UUID> baseline_assistant

# Extract the first N personas from the dataset
uv run python main.py extract --model google/gemma-2-9b-it --sample-size 100

# Re-run personas already present locally
uv run python main.py extract --model google/gemma-2-9b-it --persona-id <UUID> --force

# Push local activations to the Hub
uv run python main.py push --model google/gemma-2-9b-it --repo implicit-personalization/synth-persona-vectors

See the docs for API details.

Layout

src/persona_vectors/
├── activations.py   # low-level hidden-state extraction
├── extraction.py    # prompt formatting, masks, persona extraction flow
├── artifacts.py     # local and Hub activation stores
├── analysis.py      # loading, PCA, cosine similarity, clustering
├── plots.py         # Plotly figures
├── steering.py      # experimental steering vectors
└── parser.py        # CLI parser

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

persona_vectors-0.7.3.tar.gz (28.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

persona_vectors-0.7.3-py3-none-any.whl (33.2 kB view details)

Uploaded Python 3

File details

Details for the file persona_vectors-0.7.3.tar.gz.

File metadata

  • Download URL: persona_vectors-0.7.3.tar.gz
  • Upload date:
  • Size: 28.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","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

Hashes for persona_vectors-0.7.3.tar.gz
Algorithm Hash digest
SHA256 75a90e68142097419a2f1cf6d21878dc5202234c12ed342d63349796255baad6
MD5 ec877362364eea53fe4313cdda911db3
BLAKE2b-256 6d3625d766934dc43f60faeba8a51c698da78bdd9af2e5d191b7ce8721612dc4

See more details on using hashes here.

File details

Details for the file persona_vectors-0.7.3-py3-none-any.whl.

File metadata

  • Download URL: persona_vectors-0.7.3-py3-none-any.whl
  • Upload date:
  • Size: 33.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","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

Hashes for persona_vectors-0.7.3-py3-none-any.whl
Algorithm Hash digest
SHA256 abf07b6715321a16b218aede69f7efac7bf6a309e090db62ee376e3f09240fde
MD5 e97bb7650468cb4400fd3534b1913125
BLAKE2b-256 6be7db961133fda6755e215e6cd9d4058a1cb93719d05ab6e24030c5da885d15

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page