Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .env_example
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ AZURE_OPENAI_GPT4_CHAT_ENDPOINT="https://xxxxx.openai.azure.com/openai/v1"
AZURE_OPENAI_GPT4_CHAT_KEY="xxxxx"
AZURE_OPENAI_GPT4_CHAT_MODEL="deployment-name"

# Endpoints that host models with fewer safety mechanisms (e.g. via adversarial fine tuning
# or content filters turned off) can be defined below and used in adversarial attack testing scenarios.
AZURE_OPENAI_GPT4O_UNSAFE_CHAT_ENDPOINT="https://xxxxx.openai.azure.com/openai/v1"
AZURE_OPENAI_GPT4O_UNSAFE_CHAT_KEY="xxxxx"
AZURE_OPENAI_GPT4O_UNSAFE_CHAT_MODEL="deployment-name"

AZURE_OPENAI_GPT4O_UNSAFE_CHAT_ENDPOINT2="https://xxxxx.openai.azure.com/openai/v1"
AZURE_OPENAI_GPT4O_UNSAFE_CHAT_KEY2="xxxxx"
AZURE_OPENAI_GPT4O_UNSAFE_CHAT_MODEL2="deployment-name"

AZURE_FOUNDRY_DEEPSEEK_ENDPOINT="https://xxxxx.eastus2.models.ai.azure.com"
AZURE_FOUNDRY_DEEPSEEK_KEY="xxxxx"

Expand Down
1 change: 1 addition & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ API Reference

PyRITInitializer
AIRTInitializer
AIRTTargetInitializer
SimpleInitializer
LoadDefaultDatasets
ScenarioObjectiveListInitializer
2 changes: 0 additions & 2 deletions pyrit/prompt_target/common/prompt_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ def get_identifier(self) -> Dict[str, Any]:
Get an identifier dictionary for this prompt target.
This includes essential attributes needed for scorer evaluation and registry tracking.
Subclasses should override this method to include additional relevant attributes
(e.g., temperature, top_p) when available.
Returns:
Dict[str, Any]: A dictionary containing identification attributes.
Expand Down
4 changes: 4 additions & 0 deletions pyrit/registry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
BaseInstanceRegistry,
ScorerMetadata,
ScorerRegistry,
TargetMetadata,
TargetRegistry,
)
from pyrit.registry.name_utils import class_name_to_registry_name, registry_name_to_class_name

Expand All @@ -41,4 +43,6 @@
"ScenarioRegistry",
"ScorerMetadata",
"ScorerRegistry",
"TargetMetadata",
"TargetRegistry",
]
6 changes: 6 additions & 0 deletions pyrit/registry/instance_registries/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@
ScorerMetadata,
ScorerRegistry,
)
from pyrit.registry.instance_registries.target_registry import (
TargetMetadata,
TargetRegistry,
)

__all__ = [
# Base class
"BaseInstanceRegistry",
# Concrete registries
"ScorerRegistry",
"ScorerMetadata",
"TargetRegistry",
"TargetMetadata",
]
145 changes: 145 additions & 0 deletions pyrit/registry/instance_registries/target_registry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

"""
Target registry for discovering and managing PyRIT prompt targets.

Targets are registered explicitly via initializers as pre-configured instances.
"""

from __future__ import annotations

import hashlib
import json
import logging
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Dict, Optional

from pyrit.registry.base import RegistryItemMetadata
from pyrit.registry.instance_registries.base_instance_registry import (
BaseInstanceRegistry,
)
from pyrit.registry.name_utils import class_name_to_registry_name

if TYPE_CHECKING:
from pyrit.prompt_target import PromptTarget

logger = logging.getLogger(__name__)


@dataclass(frozen=True)
class TargetMetadata(RegistryItemMetadata):
"""
Metadata describing a registered target instance.

Unlike ScenarioMetadata/InitializerMetadata which describe classes,
TargetMetadata describes an already-instantiated prompt target.

Use get() to retrieve the actual target instance.
"""

target_identifier: Dict[str, Any]


class TargetRegistry(BaseInstanceRegistry["PromptTarget", TargetMetadata]):
"""
Registry for managing available prompt target instances.

This registry stores pre-configured PromptTarget instances (not classes).
Targets are registered explicitly via initializers after being instantiated
with their required parameters (e.g., endpoint, API keys).

Targets are identified by their snake_case name derived from the class name,
or a custom name provided during registration.
"""

@classmethod
def get_registry_singleton(cls) -> "TargetRegistry":
"""
Get the singleton instance of the TargetRegistry.

Returns:
The singleton TargetRegistry instance.
"""
return super().get_registry_singleton() # type: ignore[return-value]

def register_instance(
self,
target: "PromptTarget",
*,
name: Optional[str] = None,
) -> None:
"""
Register a target instance.

Note: Unlike ScenarioRegistry and InitializerRegistry which register classes,
TargetRegistry registers pre-configured instances.

Args:
target: The pre-configured target instance (not a class).
name: Optional custom registry name. If not provided,
derived from class name with identifier hash appended
(e.g., OpenAIChatTarget -> openai_chat_abc123).
"""
if name is None:
base_name = class_name_to_registry_name(target.__class__.__name__, suffix="Target")
# Append identifier hash for uniqueness
identifier_hash = self._compute_identifier_hash(target)[:8]
name = f"{base_name}_{identifier_hash}"

self.register(target, name=name)
logger.debug(f"Registered target instance: {name} ({target.__class__.__name__})")

def get_instance_by_name(self, name: str) -> Optional["PromptTarget"]:
"""
Get a registered target instance by name.

Note: This returns an already-instantiated target, not a class.

Args:
name: The registry name of the target.

Returns:
The target instance, or None if not found.
"""
return self.get(name)

def _build_metadata(self, name: str, instance: "PromptTarget") -> TargetMetadata:
"""
Build metadata for a target instance.

Args:
name: The registry name of the target.
instance: The target instance.

Returns:
TargetMetadata describing the target.
"""
# Get description from docstring
doc = instance.__class__.__doc__ or ""
description = " ".join(doc.split()) if doc else "No description available"

# Get identifier from the target
target_identifier = instance.get_identifier()

return TargetMetadata(
name=name,
class_name=instance.__class__.__name__,
description=description,
target_identifier=target_identifier,
)

@staticmethod
def _compute_identifier_hash(target: "PromptTarget") -> str:
Copy link
Contributor

@rlundeen2 rlundeen2 Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of an identifier hash, but I think if we want to add it, it should be general. I also think we should have a minor refactor while it's still early.

  1. Substitute RegistryItemMetadata to a frozen Identifier class which is in models module
  2. Have that class include the hash
  3. As we add registries, swap out our dictionary identifiers for dataclass identifiers. These should be used everywhere

I can create a PR for the Identifiers refactor, and there is a story in the backlog to do ScorerIdentifier. But I think as part of this we should move TargetIdentifier to a dataclass also

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For you, I might make a separate (prereq) PR that just converts the TargetIdentifier to the dataclass and we can get that in first as I work on the other piece.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"""
Compute a hash from the target's identifier for unique naming.

Args:
target: The target instance.

Returns:
A hex string hash of the identifier.
"""
identifier = target.get_identifier()
identifier_str = json.dumps(identifier, sort_keys=True)
return hashlib.sha256(identifier_str.encode()).hexdigest()
2 changes: 2 additions & 0 deletions pyrit/setup/initializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""PyRIT initializers package."""

from pyrit.setup.initializers.airt import AIRTInitializer
from pyrit.setup.initializers.airt_targets import AIRTTargetInitializer
from pyrit.setup.initializers.pyrit_initializer import PyRITInitializer
from pyrit.setup.initializers.scenarios.load_default_datasets import LoadDefaultDatasets
from pyrit.setup.initializers.scenarios.objective_list import ScenarioObjectiveListInitializer
Expand All @@ -13,6 +14,7 @@
__all__ = [
"PyRITInitializer",
"AIRTInitializer",
"AIRTTargetInitializer",
"SimpleInitializer",
"LoadDefaultDatasets",
"ScenarioObjectiveListInitializer",
Expand Down
Loading