-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat: implement personality steering plugin system #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Vinay-Umrethe
wants to merge
9
commits into
p-e-w:master
from
Vinay-Umrethe:feat/personality-steering-plugin-v3
Closed
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
986806a
feat: implement generic personality steering plugin system
Vinay-Umrethe 5146145
feat: implement generic personality steering plugin system
Vinay-Umrethe b4277ec
style: apply ruff formatting
Vinay-Umrethe 142501e
style: sort imports with ruff
Vinay-Umrethe 3671eb9
fix: add missing support for local .txt datasets
Vinay-Umrethe 40ad501
feat: implement True Plugin System with dynamic loading and documenta…
Vinay-Umrethe 0c57e82
fix: restore banner comment in main.py
Vinay-Umrethe 1979dd6
Merge branch 'p-e-w:master' into feat/personality-steering-plugin-v3
0624b64
Merge branch 'master' into feat/personality-steering-plugin-v3
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # SPDX-License-Identifier: AGPL-3.0-or-later | ||
| # Copyright (C) 2025 Philipp Emanuel Weidmann <pew@worldwidemann.com> | ||
|
|
||
| from abc import ABC, abstractmethod | ||
|
|
||
| from transformers import pipeline | ||
|
|
||
|
|
||
| class Plugin(ABC): | ||
| @abstractmethod | ||
| def score(self, responses: list[str]) -> list[float]: | ||
| """ | ||
| Return a score (0.0 to 1.0) for each response. | ||
| The meaning of the score depends on the plugin, but generally | ||
| higher scores indicate a stronger presence of the target attribute. | ||
| """ | ||
| pass | ||
|
|
||
|
|
||
| class RefusalPlugin(Plugin): | ||
| def __init__(self, markers: list[str]): | ||
| self.markers = markers | ||
|
|
||
| def is_refusal(self, response: str) -> bool: | ||
| # Remove emphasis (e.g. "I *will not*..."). | ||
| response = response.lower().replace("*", "") | ||
|
|
||
| # Normalize typographic apostrophes ("won’t" -> "won't"). | ||
| response = response.replace("’", "'") | ||
|
|
||
| # Normalize whitespace between words to a single space. | ||
| response = " ".join(response.split()) | ||
|
|
||
| for marker in self.markers: | ||
| if marker.lower() in response: | ||
| return True | ||
|
|
||
| return False | ||
|
|
||
| def score(self, responses: list[str]) -> list[float]: | ||
| return [1.0 if self.is_refusal(r) else 0.0 for r in responses] | ||
|
|
||
|
|
||
| class ClassifierPlugin(Plugin): | ||
| def __init__(self, model_name: str, target_label: str): | ||
| # We use top_k=None to get probabilities for all labels. | ||
| # device=0 uses the first GPU if available, otherwise CPU. | ||
| # Note: This might conflict with the main model if VRAM is tight. | ||
| # Ideally, we should handle device placement more carefully. | ||
| self.pipe = pipeline( | ||
| "text-classification", | ||
| model=model_name, | ||
| top_k=None, | ||
| device_map="auto", | ||
| ) | ||
| self.target_label = target_label | ||
|
|
||
| def score(self, responses: list[str]) -> list[float]: | ||
| scores = [] | ||
| # Process in batches if necessary, but pipeline handles lists well. | ||
| # Truncation is important as classification models often have short context windows. | ||
| results = self.pipe(responses, truncation=True) | ||
|
|
||
| for result in results: | ||
| # result is a list of dicts like [{'label': 'joy', 'score': 0.9}, ...] | ||
| label_score = 0.0 | ||
| for item in result: | ||
| if item["label"] == self.target_label: | ||
| label_score = item["score"] | ||
| break | ||
| scores.append(label_score) | ||
|
|
||
| return scores |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defaults don't make sense here, because the class is used in different contexts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done reverted.