Skip to content

Commit

Permalink
fix repr: switch pprint to pformat
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-ashkinaze committed Sep 5, 2024
1 parent 4e0559b commit e18901c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
4 changes: 2 additions & 2 deletions plurals/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from plurals.helpers import *

from pprint import pprint
from pprint import pformat

DEFAULTS = load_yaml("instructions.yaml")
DEFAULTS = strip_nested_dict(DEFAULTS)
Expand Down Expand Up @@ -573,7 +573,7 @@ def prompts(self):
return [history[i]['prompts'] for i in range(len(history))]

def __repr__(self):
return pprint(self.info)
return pformat(self.info, indent=2)

def set_task(self, task: str):
"""
Expand Down
4 changes: 2 additions & 2 deletions plurals/deliberation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from plurals.helpers import SmartString, strip_nested_dict
import re
import collections
from pprint import pprint
from pprint import pformat

DEFAULTS = load_yaml("instructions.yaml")
DEFAULTS = strip_nested_dict(DEFAULTS)
Expand Down Expand Up @@ -447,7 +447,7 @@ def process(self) -> None:
"This method must be implemented in a subclass")

def __repr__(self):
return pprint(self.info)
return pformat(self.info, indent=2)


# Concrete Structures
Expand Down
1 change: 0 additions & 1 deletion plurals/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def print_anes_mapping():
print(f"{val}")
print()


def strip_nested_dict(d: Dict[str, Any]) -> Dict[str, Any]:
"""
Strip whitespace from all strings in a nested dictionary.
Expand Down
52 changes: 52 additions & 0 deletions plurals/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,5 +1152,57 @@ def test_only_remove_double_period(self):
self.assertEqual("Think about this task, respond slowly...", formatted_string, )


class TestAgentRepr(unittest.TestCase):
def setUp(self):
self.agent = Agent(task="Test task", model="gpt-3.5-turbo")

def test_agent_repr(self):
"""Test that the Agent's __repr__ method produces a string"""
repr_string = repr(self.agent)
print(repr_string)
self.assertIsInstance(repr_string, str)
self.assertIn("Test task", repr_string)
self.assertIn("gpt-3.5-turbo", repr_string)

def test_agent_repr_with_none_values(self):
"""Test that the Agent's __repr__ handles None values correctly"""
repr_string = repr(self.agent)
print(repr_string)
self.assertIn("'persona': None", repr_string)
self.assertIn("'system_instructions': None", repr_string)

def test_agent_repr_with_empty_collections(self):
"""Test that the Agent's __repr__ handles empty collections correctly"""
repr_string = repr(self.agent)
self.assertIn("'history': None", repr_string)
self.assertIn("'kwargs': {}", repr_string)

class TestStructureRepr(unittest.TestCase):
def setUp(self):
self.agent1 = Agent(task="Task 1", model="gpt-3.5-turbo")
self.agent2 = Agent(task="Task 2", model="gpt-3.5-turbo")
self.chain = Chain([self.agent1, self.agent2], task="Chain task")

def test_structure_repr(self):
"""Test that the Structure's __repr__ method produces a string"""
repr_string = repr(self.chain)
self.assertIsInstance(repr_string, str)
self.assertIn("Chain task", repr_string)
self.assertIn("gpt-3.5-turbo", repr_string)

def test_structure_repr_with_none_values(self):
"""Test that the Structure's __repr__ handles None values correctly"""
self.chain.final_response = None
repr_string = repr(self.chain)
self.assertIn("'final_response': None", repr_string)

def test_structure_repr_with_empty_collections(self):
"""Test that the Structure's __repr__ handles empty collections correctly"""
self.chain.responses = []
repr_string = repr(self.chain)
print(repr_string)
self.assertIn("'responses': []", repr_string)


if __name__ == '__main__':
unittest.main()

0 comments on commit e18901c

Please sign in to comment.