agent.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. :param instruction: The instruction for the agent to execute.
  14. :param model_name: The litellm name of the model to use for the agent.
  15. """
  16. _registry: Dict[str, Type["Agent"]] = {}
  17. def __init__(
  18. self,
  19. llm: LLM,
  20. ):
  21. self.instruction = ""
  22. self.llm = llm
  23. self._complete = False
  24. @property
  25. def complete(self) -> bool:
  26. """
  27. Indicates whether the current instruction execution is complete.
  28. Returns:
  29. - complete (bool): True if execution is complete; False otherwise.
  30. """
  31. return self._complete
  32. @abstractmethod
  33. def step(self, state: "State") -> "Action":
  34. """
  35. Starts the execution of the assigned instruction. This method should
  36. be implemented by subclasses to define the specific execution logic.
  37. """
  38. pass
  39. @abstractmethod
  40. def search_memory(self, query: str) -> List[str]:
  41. """
  42. Searches the agent's memory for information relevant to the given query.
  43. Parameters:
  44. - query (str): The query to search for in the agent's memory.
  45. Returns:
  46. - response (str): The response to the query.
  47. """
  48. pass
  49. def reset(self) -> None:
  50. """
  51. Resets the agent's execution status and clears the history. This method can be used
  52. to prepare the agent for restarting the instruction or cleaning up before destruction.
  53. """
  54. self.instruction = ""
  55. self._complete = False
  56. @classmethod
  57. def register(cls, name: str, agent_cls: Type["Agent"]):
  58. """
  59. Registers an agent class in the registry.
  60. Parameters:
  61. - name (str): The name to register the class under.
  62. - agent_cls (Type['Agent']): The class to register.
  63. """
  64. if name in cls._registry:
  65. raise ValueError(f"Agent class already registered under '{name}'.")
  66. cls._registry[name] = agent_cls
  67. @classmethod
  68. def get_cls(cls, name: str) -> Type["Agent"]:
  69. """
  70. Retrieves an agent class from the registry.
  71. Parameters:
  72. - name (str): The name of the class to retrieve
  73. Returns:
  74. - agent_cls (Type['Agent']): The class registered under the specified name.
  75. """
  76. if name not in cls._registry:
  77. raise ValueError(f"No agent class registered under '{name}'.")
  78. return cls._registry[name]