observation.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import copy
  2. from typing import List
  3. from dataclasses import dataclass
  4. @dataclass
  5. class Observation:
  6. """
  7. This data class represents an observation of the environment.
  8. """
  9. content: str
  10. def __str__(self) -> str:
  11. return self.content
  12. def to_dict(self) -> dict:
  13. """Converts the observation to a dictionary."""
  14. extras = copy.deepcopy(self.__dict__)
  15. extras.pop("content", None)
  16. return {
  17. "observation": self.__class__.__name__,
  18. "content": self.content,
  19. "extras": extras,
  20. "message": self.message,
  21. }
  22. @property
  23. def message(self) -> str:
  24. """Returns a message describing the observation."""
  25. return ""
  26. @dataclass
  27. class CmdOutputObservation(Observation):
  28. """
  29. This data class represents the output of a command.
  30. """
  31. command_id: int
  32. command: str
  33. exit_code: int = 0
  34. @property
  35. def error(self) -> bool:
  36. return self.exit_code != 0
  37. @property
  38. def message(self) -> str:
  39. return f'Command `{self.command}` executed with exit code {self.exit_code}.'
  40. @dataclass
  41. class FileReadObservation(Observation):
  42. """
  43. This data class represents the content of a file.
  44. """
  45. path: str
  46. @property
  47. def message(self) -> str:
  48. return f"I read the file {self.path}."
  49. @dataclass
  50. class FileWriteObservation(Observation):
  51. """
  52. This data class represents a file write operation
  53. """
  54. path: str
  55. @property
  56. def message(self) -> str:
  57. return f"I wrote to the file {self.path}."
  58. @dataclass
  59. class BrowserOutputObservation(Observation):
  60. """
  61. This data class represents the output of a browser.
  62. """
  63. url: str
  64. status_code: int = 200
  65. error: bool = False
  66. @property
  67. def message(self) -> str:
  68. return "Visited " + self.url
  69. @dataclass
  70. class UserMessageObservation(Observation):
  71. """
  72. This data class represents a message sent by the user.
  73. """
  74. role: str = "user"
  75. @property
  76. def message(self) -> str:
  77. return ""
  78. @dataclass
  79. class AgentMessageObservation(Observation):
  80. """
  81. This data class represents a message sent by the agent.
  82. """
  83. role: str = "assistant"
  84. @property
  85. def message(self) -> str:
  86. return ""
  87. @dataclass
  88. class AgentRecallObservation(Observation):
  89. """
  90. This data class represents a list of memories recalled by the agent.
  91. """
  92. memories: List[str]
  93. role: str = "assistant"
  94. @property
  95. def message(self) -> str:
  96. return "The agent recalled memories."
  97. @dataclass
  98. class AgentErrorObservation(Observation):
  99. """
  100. This data class represents an error encountered by the agent.
  101. """
  102. @property
  103. def message(self) -> str:
  104. return "Oops. Something went wrong: " + self.content
  105. @dataclass
  106. class NullObservation(Observation):
  107. """
  108. This data class represents a null observation.
  109. This is used when the produced action is NOT executable.
  110. """
  111. @property
  112. def message(self) -> str:
  113. return ""