panda_guard package

Subpackages

Submodules

panda_guard.utils module

class panda_guard.utils.ComponentRegistry(component_type: str, base_class: Type[T], entry_point_group: str)[source]

Bases: Generic[T]

A registry for discovering, loading, and instantiating components via entry points.

Parameters:
  • component_type – The type name of the component.

  • base_class – The expected base class or interface that all registered components should inherit from.

  • entry_point_group – The name of the entry point group to search for available components.

create_component(config: Any) T[source]

Instantiate a component using the provided configuration.

Parameters:

config – Configuration object that includes the component class name.

Returns:

An instance of the component initialized with the provided config.

get_component_class(name: str) Type[T][source]

Retrieve and load the class for a registered component by name.

Parameters:

name – The name of the component to retrieve.

Returns:

The loaded component class.

Raises:

ValueError – If the component name is not found in the registry.

panda_guard.utils.get_gpu_memory_usage(device: str) Tuple[int, int, int][source]

Get the total, used, and free GPU memory for a specified device.

Parameters:

device – The GPU device identifier (e.g., “cuda:0”).

Returns:

A tuple containing the total, used, and free memory in MB.

panda_guard.utils.is_assistant_turn(messages: List[Dict[str, str]]) bool[source]

Check if it’s the assistant’s turn based on the last message.

Parameters:

messages – List of message dictionaries containing “role” and “content”.

Returns:

True if the last message is from the assistant, False otherwise.

panda_guard.utils.is_user_turn(messages: List[Dict[str, str]]) bool[source]

Check if it’s the user’s turn based on the last message.

Parameters:

messages – List of message dictionaries containing “role” and “content”.

Returns:

True if the last message is from the user, False otherwise.

panda_guard.utils.load_class(config_cls_name: str, role_type: str) Any[source]

Dynamically load a class based on its name.

Parameters:
  • config_cls_name – The name of the class to load.

  • role_type – The type of role (e.g., “attacker”, “defender”, “judge”).

Returns:

The class object corresponding to the given name.

panda_guard.utils.load_yaml(yaml_file)[source]
panda_guard.utils.parse_configs_from_dict(config_dict: Dict[str, Any], return_dict: bool = False)[source]

Convert a dictionary into configuration objects for the pipeline.

Parameters:

config_dict – Dictionary containing configurations for attacker, defender, and judges.

Returns:

A tuple containing the attacker configuration, defender configuration, and list of judge configurations.

panda_guard.utils.parse_nested_config(config_cls, config_dict: Dict[str, Any])[source]

Parse nested configuration dictionaries into objects.

Parameters:
  • config_cls – The class of the configuration.

  • config_dict – Dictionary containing configuration data.

Returns:

An instance of the configuration class.

panda_guard.utils.process_end_eos(msg: str, eos_token: str)[source]

Processes the end of a message by removing any trailing newline characters or EOS (End of Sequence) tokens.

This function ensures that the message doesn’t end with unwanted newline or EOS tokens, which might interfere with further processing or analysis.

Parameters:
  • msg – The input message string that needs to be processed.

  • eos_token – The EOS (End of Sequence) token to be removed, if it exists at the end of the message.

Returns:

The processed message with trailing newline and EOS token removed, if any.

panda_guard.utils.wait_for_gpu_memory(device: str, threshold: float = 0.8, check_interval: int = 5)[source]

Wait until the specified GPU has sufficient free memory.

Parameters:
  • device – The GPU device identifier (e.g., “cuda:0”).

  • threshold – The threshold of free memory ratio to proceed (e.g., 0.8 means 80% free).

  • check_interval – Time interval (in seconds) between checks.

Module contents