Skip to main content

A research platform for LLM steering vector optimization and analysis

Project description

steerex

Attribution: This library is a modular reimplementation of llm-steering-opt by Jacob Dunefsky. Full credit goes to the original author for the steering vector optimization algorithms.

A research platform for LLM activation engineering and steering vector extraction.

Installation

uv pip install -e ".[dev]"

Quick Start

CAA Extraction

Extract steering vectors using Contrastive Activation Addition (difference of means):

from transformers import AutoModelForCausalLM, AutoTokenizer
from steerex import extract, ContrastPair, HuggingFaceBackend

# Load model
model = AutoModelForCausalLM.from_pretrained("google/gemma-2-2b")
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-2b")
backend = HuggingFaceBackend(model, tokenizer)

# Define contrast pairs
pairs = [
    ContrastPair.from_messages(
        positive=[
            {"role": "user", "content": "Hi"},
            {"role": "assistant", "content": "Hello! How can I help?"},
        ],
        negative=[
            {"role": "user", "content": "Hi"},
            {"role": "assistant", "content": "Go away."},
        ],
    ),
]

# Extract and use
result = extract(backend, tokenizer, pairs, layer=16)
steering = result.to_steering()

output = backend.generate_with_steering(
    "Hello!",
    steering_mode=steering,
    layers=16,
    max_new_tokens=50,
)

Gradient Optimization

Learn steering vectors through optimization:

from steerex import (
    SteeringOptimizer,
    VectorSteering,
    HuggingFaceBackend,
    TrainingDatapoint,
    OptimizationConfig,
)

backend = HuggingFaceBackend(model, tokenizer)
steering = VectorSteering()
config = OptimizationConfig(lr=0.1, max_iters=50)

datapoint = TrainingDatapoint(
    prompt="My favorite animal is",
    dst_completions=[" definitely cats!"],  # Promote
    src_completions=[" definitely dogs!"],  # Suppress
)

optimizer = SteeringOptimizer(backend, steering, config)
result = optimizer.optimize([datapoint], layer=10)

output = backend.generate_with_steering(
    "My favorite animal is",
    steering_mode=steering,
    layers=10,
    max_new_tokens=30,
)

Extraction Methods

Method How it works
CAA mean(positive) - mean(negative) activations
Gradient Optimizes vector to promote/suppress completions

Steering Modes

from steerex import VectorSteering, ClampSteering, AffineSteering

VectorSteering()   # Additive: activation += strength * vector
ClampSteering()    # Projects activations toward vector direction
AffineSteering()   # Learned affine transformation

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

steerex-0.1.0.tar.gz (395.7 kB view details)

Uploaded Source

Built Distribution

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

steerex-0.1.0-py3-none-any.whl (61.6 kB view details)

Uploaded Python 3

File details

Details for the file steerex-0.1.0.tar.gz.

File metadata

  • Download URL: steerex-0.1.0.tar.gz
  • Upload date:
  • Size: 395.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for steerex-0.1.0.tar.gz
Algorithm Hash digest
SHA256 82c20b7d11fb247b2bbd93a04da93270d252b659395d27c1fbbdfb6b684f3f99
MD5 3259288a70b9ac887e318187684d34fa
BLAKE2b-256 212b9a5e465ae09cde5cc7c13a2b7039ebd18ab13c1fdd15689d111f03ddf7cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for steerex-0.1.0.tar.gz:

Publisher: publish.yml on AyseAsude/steerex

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file steerex-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: steerex-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 61.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for steerex-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ec27a4300b0af43328f6122cd86ceb007dc7fe2ef27a228dd26b05f613b162e0
MD5 b56d3cafac7258fa3d461163832c30bb
BLAKE2b-256 108e8565ef18a33df907bba88665aa65ff935ec75ed80c70277415aa0d81ba4e

See more details on using hashes here.

Provenance

The following attestation bundles were made for steerex-0.1.0-py3-none-any.whl:

Publisher: publish.yml on AyseAsude/steerex

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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