agent.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. from opendevin.llm.llm import LLM
  7. from opendevin.exceptions import AgentAlreadyRegisteredError, AgentNotRegisteredError
  8. class Agent(ABC):
  9. """
  10. This abstract base class is an general interface for an agent dedicated to
  11. executing a specific instruction and allowing human interaction with the
  12. agent during execution.
  13. It tracks the execution status and maintains a history of interactions.
  14. """
  15. _registry: Dict[str, Type['Agent']] = {}
  16. def __init__(
  17. self,
  18. llm: LLM,
  19. ):
  20. self.llm = llm
  21. self._complete = False
  22. @property
  23. def complete(self) -> bool:
  24. """
  25. Indicates whether the current instruction execution is complete.
  26. Returns:
  27. - complete (bool): True if execution is complete; False otherwise.
  28. """
  29. return self._complete
  30. @abstractmethod
  31. def step(self, state: 'State') -> 'Action':
  32. """
  33. Starts the execution of the assigned instruction. This method should
  34. be implemented by subclasses to define the specific execution logic.
  35. """
  36. pass
  37. @abstractmethod
  38. def search_memory(self, query: str) -> List[str]:
  39. """
  40. Searches the agent's memory for information relevant to the given query.
  41. Parameters:
  42. - query (str): The query to search for in the agent's memory.
  43. Returns:
  44. - response (str): The response to the query.
  45. """
  46. pass
  47. def reset(self) -> None:
  48. """
  49. Resets the agent's execution status and clears the history. This method can be used
  50. to prepare the agent for restarting the instruction or cleaning up before destruction.
  51. """
  52. self._complete = False
  53. @classmethod
  54. def register(cls, name: str, agent_cls: Type['Agent']):
  55. """
  56. Registers an agent class in the registry.
  57. Parameters:
  58. - name (str): The name to register the class under.
  59. - agent_cls (Type['Agent']): The class to register.
  60. Raises:
  61. - AgentAlreadyRegisteredError: If name already registered
  62. """
  63. if name in cls._registry:
  64. raise AgentAlreadyRegisteredError(name)
  65. cls._registry[name] = agent_cls
  66. @classmethod
  67. def get_cls(cls, name: str) -> Type['Agent']:
  68. """
  69. Retrieves an agent class from the registry.
  70. Parameters:
  71. - name (str): The name of the class to retrieve
  72. Returns:
  73. - agent_cls (Type['Agent']): The class registered under the specified name.
  74. Raises:
  75. - AgentNotRegisteredError: If name not registered
  76. """
  77. if name not in cls._registry:
  78. raise AgentNotRegisteredError(name)
  79. return cls._registry[name]
  80. @classmethod
  81. def list_agents(cls) -> list[str]:
  82. """
  83. Retrieves the list of all agent names from the registry.
  84. Raises:
  85. - AgentNotRegisteredError: If no agent is registered
  86. """
  87. if not bool(cls._registry):
  88. raise AgentNotRegisteredError()
  89. return list(cls._registry.keys())