LMSteer is a command-line tool designed to provide a guided framework for "steering" language models. This approach offers an alternative to traditional fine-tuning by allowing users to precisely control and modify model behavior through targeted interventions at the activation level.
The core idea is to identify specific modules within a Hugging Face transformer model and decide how their activations should be handled. This involves:
- Observation Stage (Future): Capturing activations from selected modules as the model processes input.
- Steering Configuration: Defining rules for which activations to capture and, eventually, how to modify them.
- Inference Stage (Future): Injecting "steering vectors" (modified activations) back into the model during inference to guide its output.
This tool is currently focused on building the Steering Configuration mechanism.
- Model Loading: Specify any Hugging Face model via command-line argument (
--model_nameor--model-name). - Modular Core Logic: The core functionalities have been refactored into separate modules:
lmsteer/app/model_utils.py: Handles loading Hugging Face models and tokenizers. It also builds an internal tree representation (ModuleNode) of the model's structure.lmsteer/app/rules.py: Defines theRuledata structure and contains the logic for compiling a list of defined rules into a final steering configuration. It supports instance-specific, module type-specific, and path pattern (glob-style) rules with defined precedence (Instance > Path Pattern > Module Type).lmsteer/app/config_io.py: Manages saving the generated steering configuration to a JSON file.
- Textual TUI Development: The main script (
main.py) now launches an interactive Terminal User Interface (TUI) built with theTextuallibrary (seelmsteer/tui/app.pyandlmsteer/tui/tui.css). This replaces the previous placeholder TUI.- The TUI loads the specified Hugging Face model.
- It builds and displays an interactive tree representation of the model's module structure.
- Users can navigate this tree (expand/collapse nodes) and view details (path, type, etc.) of the selected module.
- Rule Definition & Configuration (Future TUI Work): The functionality for defining steering rules, compiling them into a steering configuration, and saving that configuration is planned for future TUI development and is not yet implemented in the current Textual TUI.
Ensure you have the lmsteer project directory. This project uses uv as its package manager and pyproject.toml for dependency management. Key Python modules are organized into lmsteer/app/ (for core logic like model_utils.py, rules.py, config_io.py) and lmsteer/tui/ (for UI components like app.py).
Install the necessary dependencies from the project's root directory:
uv pip install -e '.[dev]'Execute the main script from your terminal, providing the Hugging Face model name:
python /workspace/lmsteer/main.py --model_name <your_model_name>
# or
python /workspace/lmsteer/main.py --model-name <your_model_name>Replace <your_model_name> with a model identifier from Hugging Face Hub (e.g., distilbert-base-uncased, gpt2, facebook/opt-125m).
When you run the script:
- The specified Hugging Face model will be loaded.
- The Textual TUI will launch.
- You will see an interactive tree view of the model's module structure.
- You can navigate the tree using arrow keys (or 'j'/'k' for up/down).
- Highlighting a module will display its details (path, type, etc.) in the right-hand pane.
- Rule definition and configuration saving are not yet implemented in the TUI.
-
Resolve TUI Focus Issue in
test_focus_behavior_and_indicators:- Currently, the test
test_focus_behavior_and_indicatorsis failing. Pressing "Enter" on theModuleTreeis intended to transfer focus to theRadioSetin theDetailPane. - The application's internal mechanism for setting focus (
self.set_focus(radio_set)in theCustomTree.NodeExplicitlySelectedevent handler) is not reliably updatingapp.focusedto theRadioSetand/or ensuring theon_focusevent handler correctly updates thepane-focusedCSS class on the panes before test assertions are evaluated. - The
on_focushandler itself, which manages thepane-focusedclass, appears to function correctly when focus is set programmatically from the test. - The immediate goal is to ensure that the application's internal focus logic correctly and reliably transfers focus and updates visual indicators, allowing all tests in
test_app_interaction.pyto pass.
- Currently, the test
-
Complete Textual TUI for Rule Management: The initial Textual TUI (
lmsteer/tui/app.py) allows for model loading and module tree navigation. The next critical step is to implement full rule management capabilities:- Interactively define steering rules for selected modules (e.g., capture, skip, modify activations).
- Display a list of currently defined rules.
- Allow users to edit or delete existing rules.
- Integrate logic to compile the defined rules into a steering configuration (using
rules.py). - Implement functionality to save the generated steering configuration to a JSON file (using
config_io.py).
- Forward Hooks for Observation: Based on the generated steering configuration, register forward hooks to actually capture activations from the targeted modules during an "observation stage" (e.g., when processing a sample dataset).
- Steering Vector Storage & Management: Define how captured activations (steering vectors) are stored and managed.
- Steering Vector Injection: Implement mechanisms to modify and/or inject these steering vectors back into the model during a separate inference run to guide its behavior.
- Structured CLI with Subcommands: Refactor the command-line interface to support subcommands for a more organized workflow (e.g.,
lmsteer new <config_name> <model>to create a config,lmsteer edit <config_name>to modify it,lmsteer observe <config_name> <dataset>to capture activations,lmsteer steer <config_name> <input_prompt>). - Advanced Rule Management in TUI: Enhance the Textual TUI to allow easy viewing, deleting, and modifying of existing rules before compilation.
- Configuration Editing: Allow loading an existing
_steer_config.jsonfile into the TUI for modification.
- Advanced Module Filtering/Selection: Offer more sophisticated ways to select or filter modules within the TUI (e.g., by depth, by regex on name).
- Comprehensive Testing: Develop a suite of tests for the core logic and TUI components.
- Expand Documentation: Continuously update in-code comments, user guides, and examples.
- Initial CLI argument parsing for model name (supporting
--model_nameand--model-name). - Hugging Face model and tokenizer loading.
- Core logic for rule definition (instance, type, path pattern) and compilation with precedence.
- Initial Refactoring: Separated core logic into standalone files (
model_utils.py,rules.py,config_io.py). - Project Structure Refactor: Reorganized the project into
lmsteer/app(for core logic) andlmsteer/tui(for UI components) modules.main.pyis now the only Python script at the root.tui.cssmoved tolmsteer/tui/tui.css. (Commitb2d1e8c) - TUI Enhancements: Implemented details pane for selected module, Enter/Esc key navigation in the tree, and fixed
RadioSetfocus issue. (Commit517ac7b) - Began implementation of the Textual TUI (
lmsteer/tui/app.py,lmsteer/tui/tui.css), replacing the placeholder TUI inmain.py. The TUI now handles model loading, module tree display/navigation, and shows module details. - Set up Git repository and pushed initial refactored code to GitHub.
transformerstorchrich(Currently used for console output by utility functions;Textualbuilds uponrich)textual(Planned for the new TUI)uuid(standard library, used for rule IDs)fnmatch(standard library, for path pattern matching)
- Package Manager: This project uses
uvfor dependency management. Useuv pip syncto install dependencies based onuv.lockandpyproject.toml. - Project Structure:
- Core application logic resides in
lmsteer/app/(e.g.,model_utils.py,rules.py,config_io.py). - Textual TUI components are in
lmsteer/tui/(e.g.,app.py,tui.css). main.pyat the project root is the main entry point.
- Core application logic resides in
- TUI Development Status:
- The "Define Steering Rule..." button in the TUI is currently a placeholder and does not yet open a dialog or implement rule definition logic.
- The
RadioSetfor selecting module status (Observe, Skip, Steer) is present in the UI, but its state is not yet connected to the underlying module configuration orrules.py. - Visual feedback for module status (e.g.,
[C],[S],[I]prefixes in theCustomTree) is not yet implemented.
- Next Steps for TUI:
- Implement the modal/dialog for defining steering rules.
- Connect the
RadioSetand rule definition dialog tolmsteer/app/rules.pyto create and manageRuleobjects. - Implement logic to save these rules using
lmsteer/app/config_io.py. - Calculate and display the "Effective Status" of modules based on the defined rules and their precedence.
- Add visual prefixes to the module tree.
This README will be updated as the project progresses.