You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

testbed_utils.py 1.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from importlib.metadata import version as lib_version
  2. from datetime import datetime
  3. import os
  4. import autogen
  5. import json
  6. def init():
  7. """Helper function to initialize logging in a testbed scenario.
  8. Specifically, write timestamp and version information, then
  9. initialize autogen logging.
  10. Args:
  11. None
  12. Returns:
  13. None
  14. """
  15. # Print some information about the run
  16. with open("timestamp.txt", "wt") as f:
  17. f.write("Timestamp: " + datetime.now().isoformat() + "\n")
  18. f.write("pyautogen version: " + lib_version("pyautogen") + "\n")
  19. def finalize(agents):
  20. """Helper function to finalize logging in a testbed scenario.
  21. Calling this function will save all the chat completions logged
  22. by Autogen to disk, and will save the messages dictionaries of
  23. all agents passed via the agents argument.
  24. Args:
  25. agents (list): a list of the agents whose messages will be logged to disk.
  26. Returns:
  27. None
  28. """
  29. script_dir = os.path.dirname(os.path.realpath(__file__))
  30. def messages_to_json(agent):
  31. messages = dict()
  32. for item in agent.chat_messages.items():
  33. messages[item[0].name] = item[1]
  34. return json.dumps(messages, indent=4)
  35. for agent in agents:
  36. fname = agent.name + "_messages.json"
  37. with open(os.path.join(script_dir, fname), "wt") as fh:
  38. fh.write(messages_to_json(agent))