agent.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from abc import ABC, abstractmethod
  2. from typing import List, Dict, Type, TYPE_CHECKING
  3. if TYPE_CHECKING:
  4. from opendevin.action import Action
  5. from opendevin.state import State
  6. class Agent(ABC):
  7. """
  8. This abstract base class is an general interface for an agent dedicated to
  9. executing a specific instruction and allowing human interaction with the
  10. agent during execution.
  11. It tracks the execution status and maintains a history of interactions.
  12. :param instruction: The instruction for the agent to execute.
  13. :param model_name: The litellm name of the model to use for the agent.
  14. """
  15. _registry: Dict[str, Type["Agent"]] = {}
  16. def __init__(self, model_name: str):
  17. self.model_name = model_name
  18. self.instruction: str = "" # need to be set before step
  19. self._complete = False
  20. @property
  21. def complete(self) -> bool:
  22. """
  23. Indicates whether the current instruction execution is complete.
  24. Returns:
  25. - complete (bool): True if execution is complete; False otherwise.
  26. """
  27. return self._complete
  28. @abstractmethod
  29. def step(self, state: "State") -> "Action":
  30. """
  31. Starts the execution of the assigned instruction. This method should
  32. be implemented by subclasses to define the specific execution logic.
  33. """
  34. pass
  35. @abstractmethod
  36. def search_memory(self, query: str) -> List[str]:
  37. """
  38. Searches the agent's memory for information relevant to the given query.
  39. Parameters:
  40. - query (str): The query to search for in the agent's memory.
  41. Returns:
  42. - response (str): The response to the query.
  43. """
  44. pass
  45. def reset(self) -> None:
  46. """
  47. Resets the agent's execution status and clears the history. This method can be used
  48. to prepare the agent for restarting the instruction or cleaning up before destruction.
  49. """
  50. self.instruction = ""
  51. self._complete = False
  52. @classmethod
  53. def register(cls, name: str, agent_cls: Type["Agent"]):
  54. """
  55. Registers an agent class in the registry.
  56. Parameters:
  57. - name (str): The name to register the class under.
  58. - agent_cls (Type['Agent']): The class to register.
  59. """
  60. if name in cls._registry:
  61. raise ValueError(f"Agent class already registered under '{name}'.")
  62. cls._registry[name] = agent_cls
  63. @classmethod
  64. def get_cls(cls, name: str) -> Type["Agent"]:
  65. """
  66. Retrieves an agent class from the registry.
  67. Parameters:
  68. - name (str): The name of the class to retrieve
  69. Returns:
  70. - agent_cls (Type['Agent']): The class registered under the specified name.
  71. """
  72. if name not in cls._registry:
  73. raise ValueError(f"No agent class registered under '{name}'.")
  74. return cls._registry[name]