-
Notifications
You must be signed in to change notification settings - Fork 752
MAINT unify how default adversarial and scorer targets are set in scenarios #1695
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
Open
behnam-o
wants to merge
12
commits into
microsoft:main
Choose a base branch
from
behnam-o:scenario-targets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8477420
refactor default scenario targets
5545543
rename
b26f553
log
3e22987
Merge branch 'main' of https://github.com/microsoft/PyRIT into scenar…
6921832
more removals
behnam-o 11cb369
update test
07c8b56
Merge branch 'main' into scenario-targets
behnam-o 19a495e
update foundry tests
52dc2a2
override method instead of classvar
ea8acd8
warn
1fddeb9
register main and fallback scorers in scorer initializer
00c839b
use the registry default scorer's target if available
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,98 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| import logging | ||
|
|
||
| from pyrit.prompt_target import OpenAIChatTarget, PromptChatTarget | ||
| from pyrit.prompt_target.common.target_capabilities import CapabilityName | ||
| from pyrit.registry import TargetRegistry | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def get_default_scorer_target() -> PromptChatTarget: | ||
|
behnam-o marked this conversation as resolved.
|
||
| """ | ||
| Resolve the default objective scorer chat target. | ||
|
|
||
| First checks the ``TargetRegistry`` for an ``"objective_scorer_chat"`` entry | ||
| (populated by ``TargetInitializer`` from ``OBJECTIVE_SCORER_CHAT_*`` env vars). | ||
| Falls back to a plain ``OpenAIChatTarget`` | ||
|
|
||
| Returns: | ||
| PromptChatTarget: The resolved objective scorer chat target. | ||
|
|
||
| Raises: | ||
| ValueError: If the registered target does not support multi-turn. | ||
| """ | ||
| return _get_default_chat_target(preferred_target_key="objective_scorer_chat") | ||
|
|
||
|
behnam-o marked this conversation as resolved.
|
||
|
|
||
| def get_default_adversarial_target() -> PromptChatTarget: | ||
| """ | ||
| Resolve the default adversarial chat target. | ||
|
|
||
| First checks the ``TargetRegistry`` for an ``"adversarial_chat"`` entry | ||
| (populated by ``TargetInitializer`` from ``ADVERSARIAL_CHAT_*`` env vars). | ||
| Falls back to a default fallback target with temperature=1.2 | ||
|
|
||
| Returns: | ||
| PromptChatTarget: The resolved adversarial chat target. | ||
|
|
||
| Raises: | ||
| ValueError: If the registered target does not support multi-turn. | ||
| """ | ||
| return _get_default_chat_target( | ||
| preferred_target_key="adversarial_chat", | ||
| required_capabilities={CapabilityName.MULTI_TURN}, | ||
| fallback_temperature=1.2, | ||
| ) | ||
|
|
||
|
|
||
| def _get_default_chat_target( | ||
| *, | ||
| preferred_target_key: str, | ||
| required_capabilities: set[CapabilityName] | None = None, | ||
| fallback_temperature: float | None = None, | ||
| ) -> PromptChatTarget: | ||
| """ | ||
| Resolve a chat target from TargetRegistry with configurable fallback behavior. | ||
|
|
||
| Resolution order: | ||
| 1. ``preferred_target_key`` entry from ``TargetRegistry`` | ||
| 2. ``OpenAIChatTarget(...)`` with optional temperature | ||
|
|
||
| Args: | ||
| preferred_target_key (str): TargetRegistry key to resolve first. | ||
| required_capabilities (set[CapabilityName] | None): Optional capabilities | ||
| that a resolved target must support. | ||
| fallback_temperature (float | None): Optional temperature for fallback | ||
| ``OpenAIChatTarget`` construction. | ||
|
|
||
| Returns: | ||
| PromptChatTarget: The resolved chat target. | ||
|
|
||
| Raises: | ||
| ValueError: If the resolved target does not satisfy required capabilities. | ||
| ValueError: If the registry entry exists but is not a PromptChatTarget. | ||
| """ | ||
| registry = TargetRegistry.get_registry_singleton() | ||
| target = registry.get(preferred_target_key) | ||
| if target is not None: | ||
| # Check required capabilities first (fail fast) | ||
| if required_capabilities: | ||
| for capability in required_capabilities: | ||
| if not target.capabilities.includes(capability=capability): | ||
| raise ValueError(f"Registry entry '{preferred_target_key}' must support {capability.value}.") | ||
|
|
||
| # Then check type | ||
| if not isinstance(target, PromptChatTarget): | ||
| raise ValueError( | ||
| f"Registry entry '{preferred_target_key}' must be a PromptChatTarget, but got {type(target).__name__}" | ||
| ) | ||
|
|
||
| return target | ||
|
|
||
| logger.warning( | ||
| f"TargetRegistry entry '{preferred_target_key}' not found. Falling back to default OpenAIChatTarget." | ||
| ) | ||
| return OpenAIChatTarget(temperature=fallback_temperature) | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.