Skip to content

Commit

Permalink
feat: Updated dspy/primitives/assertions.py
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] committed Dec 20, 2023
1 parent faeb6dc commit c54a118
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions dspy/primitives/assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@


def setup_logger():
"""
Set up a logger for the module.
The logger logs messages with the level DEBUG to a file named "assertion.log".
Returns:
Logger: The set up logger.
"""
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

Expand Down Expand Up @@ -40,6 +48,14 @@ class DSPyAssertionError(AssertionError):
"""Custom exception raised when a DSPy `Assert` fails."""

def __init__(self, id: str, msg: str, state: Any = None) -> None:
"""
Initialize a new instance of the DSPyAssertionError class.
Args:
id (str): The ID of the assertion.
msg (str): The error message.
state (Any): The state of the assertion.
"""
super().__init__(msg)
self.id = id
self.msg = msg
Expand All @@ -52,6 +68,15 @@ class DSPySuggestionError(AssertionError):
def __init__(
self, id: str, msg: str, target_module: Any = None, state: Any = None
) -> None:
"""
Initialize a new instance of the DSPySuggestionError class.
Args:
id (str): The ID of the suggestion.
msg (str): The error message.
target_module (Any): The target module of the suggestion.
state (Any): The state of the suggestion.
"""
super().__init__(msg)
self.id = id
self.msg = msg
Expand All @@ -76,6 +101,19 @@ class Assert(Constraint):
"""DSPy Assertion"""

def __call__(self) -> bool:
"""
Call the Assert instance.
If the result is True, return True. If the result is False and bypass_assert is set in the settings, log an error and return True.
If the result is False and bypass_assert is not set in the settings, log an error and raise a DSPyAssertionError.
Raises:
ValueError: If the result is not a boolean.
DSPyAssertionError: If the result is False and bypass_assert is not set in the settings.
Returns:
bool: The result of the assertion.
"""
if isinstance(self.result, bool):
if self.result:
return True
Expand Down

0 comments on commit c54a118

Please sign in to comment.