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.0.tar.gz (27.5 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.0-py3-none-any.whl (32.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: persona_vectors-0.7.0.tar.gz
  • Upload date:
  • Size: 27.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.12 {"installer":{"name":"uv","version":"0.11.12","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.0.tar.gz
Algorithm Hash digest
SHA256 b3bb10d61fb58e7a151a2f715a3bf3afecc6a2e30da85b73ccb5e5485109909b
MD5 597f251ea0423a308f3b4f70de2ffc5d
BLAKE2b-256 2103c419bd2809447ae3de0dfbe06de8c7462a141feebaea7157f2e64a119416

See more details on using hashes here.

File details

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

File metadata

  • Download URL: persona_vectors-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 32.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.12 {"installer":{"name":"uv","version":"0.11.12","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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 89c3ddd1f1a0ffc0dfce59319ce9417e0764011f8680fd2e7844fbea2ee75afe
MD5 eb0419ff5148f71267a76d8d16aefe19
BLAKE2b-256 3fcf5603631767d29cff21a7b02fb21f4ca53b85bc4369bb0a5060bdee01ee4e

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