Skip to main content

LLM Steer

A Python module to steer LLM responses towards a certain topic/subject and to enhance capabilities (e.g., making it provide correct responses to tricky logical puzzles more often). A practical tool for using activation engineering by adding steer vectors to different layers of a Large Language Model (LLM). It should be used along with the transformers library.

Demo

Google Colab demo: https://colab.research.google.com/github/Mihaiii/llm_steer/blob/main/demo/llm_steer_demo.ipynb

Basic usage

Install it: pip install llm_steer Then use:

from llm_steer import Steer
steered_model = Steer(model, tokenizer)

Add a steering vector on a particular layer of the model with a given coefficient and text. The coefficient can also be negative.

steered_model.add(layer_idx=20, coeff=0.4, text="logical")

Get all the applied steering vectors:

steered_model.get_all()

Remove all steering vectors to revert to initial model:

steered_model.reset_all()

Q / A

Q: What's the difference between llm_steer and mentioning what you want in the system prompt?

A: I see llm_steer as an enhancer. It can be used together with the system prompt.


Q: How to determine the best parameters to be used?

A: I don't have a method; it's all trial and error. I recommend starting middle layers and with a small coefficient and then slowly increase it.


Q: What models are supported?

A: I tested it on multiple architectures, including LLaMa, Mistral, Phi, StableLM. Keep in mind that llm_steer is meant to be used together with HuggingFace's transformers library, so it won't work on GGUF, for example.


Q: I applied steering vectors, but the LLM outputs gibberish. What should I do?

A: Try a lower coeff value or another layer.


Q: Can I add multiple steering vectors on the same layer? Can I add the same steering vector on multiple layers? Can I add steering vectors with negative coefficients?

A: Yes, and please do. llm_steer is built for experimenting. See the Colab for examples: https://colab.research.google.com/github/Mihaiii/llm_steer/blob/main/demo/llm_steer_demo.ipynb


Q: Can I use steer vectors to enhance role-play characteristics (e.g., personas that are more funny or cocky)?

A: Yes.


Q: Can I use negative steering vectors to force it not to say "As an AI language model"?

A: Yes.

Credits / Thanks

  • DL Explorers for his video on activation engineer which goes over an article and a colab he made. The resources mentioned in that video were the starting point of llm_steer.
  • Gary Bernhardt for his excellent Python for programmers course. I needed a course that could help me go through the basics of Python without treating me like a dev noob (like most basic level tutorials treat their audience).
  • Andrej Karpathy for his State of GPT video. I always wanted to make an open-source project, but there already was a repo for every idea I had. Not when it comes to tools for LLMs, though!

Metadata

Release files for llm-steer 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llm-steer 2.0.1
File Size Uploaded
llm_steer-2.0.1.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-steer 2.0.1
File Interpreter ABI Platform
llm_steer-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.4 kB

Release files / llm_steer-2.0.1.tar.gz

Download URL llm_steer-2.0.1.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5cc07257608cec21e858f568de3fd4fae71f208921e5175e178caa2fa7ba6151
BLAKE2b-256 checksum
How to use checksums
07a479a79f429fa3ae7bc26e48d62cf71b4aadfdd4ad530365449d6b6fb27130
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.12

Release files / llm_steer-2.0.1-py3-none-any.whl

Download URL llm_steer-2.0.1-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a8c4b7724b976cca012e551100130b76e2ffb46ffe053a56bf785013455c8fa
BLAKE2b-256 checksum
How to use checksums
f561cce606aa5c645866c26ebf7386aa257b80187d69036f9f443c287a4b4aec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 release files

2.0.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.3

2 release files

1.0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page