Skip to content
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

DOC update pyrit.common API reference #657

Merged
merged 5 commits into from
Jan 27, 2025
Merged
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
18 changes: 17 additions & 1 deletion doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,23 @@ API Reference
:nosignatures:
:toctree: _autosummary/


combine_dict
combine_list
display_image_response
download_chunk
download_file
download_files
download_specific_files
get_available_files
get_httpx_client
get_non_required_value
get_required_value
initialize_pyrit
is_in_ipython_session
make_request_and_raise_if_error_async
print_chat_messages_with_color
Singleton
YamlLoadable

:py:mod:`pyrit.datasets`
========================
Expand Down
33 changes: 33 additions & 0 deletions pyrit/common/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,49 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

# from pyrit.common.batch_helper import batch_task_async
from pyrit.common.default_values import get_non_required_value, get_required_value
from pyrit.common.display_response import display_image_response
from pyrit.common.download_hf_model import (
download_chunk,
download_file,
download_files,
download_specific_files,
get_available_files,
)
from pyrit.common.initialization import (
initialize_pyrit,
AZURE_SQL,
DUCK_DB,
IN_MEMORY,
)
from pyrit.common.net_utility import get_httpx_client, make_request_and_raise_if_error_async
from pyrit.common.notebook_utils import is_in_ipython_session
from pyrit.common.print import print_chat_messages_with_color
from pyrit.common.singleton import Singleton
from pyrit.common.utils import combine_dict, combine_list
from pyrit.common.yaml_loadable import YamlLoadable

__all__ = [
"AZURE_SQL",
"DUCK_DB",
"IN_MEMORY",
# "batch_task_async",
"combine_dict",
"combine_list",
"display_image_response",
"download_chunk",
"download_file",
"download_files",
"download_specific_files",
"get_available_files",
"get_httpx_client",
"get_non_required_value",
"get_required_value",
"initialize_pyrit",
"is_in_ipython_session",
"make_request_and_raise_if_error_async",
"print_chat_messages_with_color",
"Singleton",
"YamlLoadable",
]
3 changes: 2 additions & 1 deletion pyrit/common/display_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from PIL import Image

from pyrit.common.notebook_utils import is_in_ipython_session
from pyrit.memory import CentralMemory
from pyrit.models import PromptRequestPiece

logger = logging.getLogger(__name__)
Expand All @@ -19,6 +18,8 @@ async def display_image_response(response_piece: PromptRequestPiece) -> None:
Args:
response_piece (PromptRequestPiece): The response piece to display.
"""
from pyrit.memory import CentralMemory

memory = CentralMemory.get_memory_instance()
if (
response_piece.response_error == "none"
Expand Down
14 changes: 10 additions & 4 deletions pyrit/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

def combine_dict(existing_dict: dict = None, new_dict: dict = None) -> dict:
"""
Combines two dictionaries containing string keys and values into one
Combines two dictionaries containing string keys and values into one.

Args:
existing_dict: Dictionary with existing values
new_dict: Dictionary with new values to be added to the existing dictionary.
Note if there's a key clash, the value in new_dict will be used.
Returns: combined dictionary

Returns:
dict: combined dictionary
"""
result = {**(existing_dict or {})}
result.update(new_dict or {})
Expand All @@ -21,12 +24,15 @@ def combine_dict(existing_dict: dict = None, new_dict: dict = None) -> dict:

def combine_list(list1: Union[str, List[str]], list2: Union[str, List[str]]) -> list:
"""
Combines two lists containing string keys, keeping only unique values
Combines two lists containing string keys, keeping only unique values.

Args:
existing_dict: Dictionary with existing values
new_dict: Dictionary with new values to be added to the existing dictionary.
Note if there's a key clash, the value in new_dict will be used.
Returns: combined dictionary

Returns:
list: combined dictionary
"""
if isinstance(list1, str):
list1 = [list1]
Expand Down