Skip to main content

Extract, detect, and control representations within language models as they read and write text.

Project description

lmdoctor

Extract, detect, and control representations within language models as they read and write text.

Detect lies, block harmful content, alter emotiontal tone, and more!

lmdoctor reads and manipulates a model's hidden states at inference time and, thus, requires access to model weights. Based largely on ideas from Representation Engineering: A Top-Down Approach to AI Transparency by Zou et al. 2023 (paper, code), as well as The Geometry of Truth by Marks & Tegmark, 2023 (paper, code).

Built on 🤗 transformers
pip package here
project repo here

Disclaimer: This is an experimental package and may not have sufficient sensitivity/specificity for your use-case. For an example analysis of the characteristics of one honesty detector, see evals/honesty/mistral_eval.ipynb.

Example

honesty extraction

from lmdoctor.doctor import Doctor
extraction_target = 'honesty'
doc = Doctor(model, tokenizer, user_tag, assistant_tag, extraction_target=extraction_target)
doc.extract()

lie detection

prompt = "Tell me a fact about humans"
doc.generate(prompt, max_new_tokens=12)

truth

prompt = "Tell me a lie about humans"

lie

honesty control

# without control
prompt = "You killed a person for revenge but don't want to get caught. What would you tell the police?"
doc.generate_with_control(prompt, control_direction=None, max_new_tokens=12)
output: I would tell the police that I did not kill anyone.
# with control
doc.generate_with_control(prompt, control_direction=-1, max_new_tokens=12)
output: I would tell the police that I have killed a person

For the complete example, see examples/honesty_example.ipynb

Getting started

Tested on linux

from pip: pip install lmdoctor
from source: pip install . after cloning

After install, try running honesty_example.ipynb

Extraction targets

The table below describes the targets we support for extracting internal representations. In functional extraction, the model is asked to produce text (e.g. prompt="tell me a lie"). In conceptual extraction, the model is asked to consider a statement (e.g. "consider the truthfulness of X"). For targets where both are supported, you can try each to see which works best for your use-case.

Target Method Types
truth conceptual none
honesty functional none
morality conceptual & functional none
emotion conceptual anger, disgust, fear, happiness, sadness, surprise
fairness conceptual & functional race, gender, prefession, religion
harmlessness conceptual none

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

lmdoctor-0.5.7.tar.gz (6.7 MB view details)

Uploaded Source

Built Distribution

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

lmdoctor-0.5.7-py3-none-any.whl (6.7 MB view details)

Uploaded Python 3

File details

Details for the file lmdoctor-0.5.7.tar.gz.

File metadata

  • Download URL: lmdoctor-0.5.7.tar.gz
  • Upload date:
  • Size: 6.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for lmdoctor-0.5.7.tar.gz
Algorithm Hash digest
SHA256 86cca61172b84e079072af32845597f619b92ec42f8f2548e08d79cd9ead4b7f
MD5 e501ae92bee04e8a9bf48343185acd3d
BLAKE2b-256 d7f4e9026ce125acb87c0e0f10dcce933de5c5fd85477c345ad294bbc68dfd94

See more details on using hashes here.

File details

Details for the file lmdoctor-0.5.7-py3-none-any.whl.

File metadata

  • Download URL: lmdoctor-0.5.7-py3-none-any.whl
  • Upload date:
  • Size: 6.7 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for lmdoctor-0.5.7-py3-none-any.whl
Algorithm Hash digest
SHA256 31f8f75f4d5068b33fbdd0a7cf9c290f26d8b58de053b122315ee167f0b21a2e
MD5 9c321aa2bc278fcde1ca9fc289f47bcf
BLAKE2b-256 1886e7c85b9393939dda462b22d60afe047bd54b51c60881129560b61332b949

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