agent.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. class Agent(ABC):
  8. """
  9. This abstract base class is an general interface for an agent dedicated to
  10. executing a specific instruction and allowing human interaction with the
  11. agent during execution.
  12. It tracks the execution status and maintains a history of interactions.
  13. """
  14. _registry: Dict[str, Type["Agent"]] = {}
  15. def __init__(
  16. self,
  17. llm: LLM,
  18. ):
  19. self.llm = llm
  20. self._complete = False
  21. @property
  22. def complete(self) -> bool:
  23. """
  24. Indicates whether the current instruction execution is complete.
  25. Returns:
  26. - complete (bool): True if execution is complete; False otherwise.
  27. """
  28. return self._complete
  29. @abstractmethod
  30. def step(self, state: "State") -> "Action":
  31. """
  32. Starts the execution of the assigned instruction. This method should
  33. be implemented by subclasses to define the specific execution logic.
  34. """
  35. pass
  36. @abstractmethod
  37. def search_memory(self, query: str) -> List[str]:
  38. """
  39. Searches the agent's memory for information relevant to the given query.
  40. Parameters:
  41. - query (str): The query to search for in the agent's memory.
  42. Returns:
  43. - response (str): The response to the query.
  44. """
  45. pass
  46. def reset(self) -> None:
  47. """
  48. Resets the agent's execution status and clears the history. This method can be used
  49. to prepare the agent for restarting the instruction or cleaning up before destruction.
  50. """
  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]