__init__.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import os
  2. import argparse
  3. from opendevin.agent import Agent, Message
  4. from agenthub.langchains_agent.utils.agent import Agent as LangchainsAgentImpl
  5. from agenthub.langchains_agent.utils.event import Event
  6. INITIAL_THOUGHTS = [
  7. "I exist!",
  8. "Hmm...looks like I can type in a command line prompt",
  9. "Looks like I have a web browser too!",
  10. "Here's what I want to do: $TASK",
  11. "How am I going to get there though?",
  12. "It seems like I have some kind of short term memory.",
  13. "Each of my thoughts seems to be stored in a numbered list.",
  14. "It seems whatever I say next will be added to the list.",
  15. "But no one has perfect short-term memory. My list of thoughts will be summarized and condensed over time, losing information in the process.",
  16. "Fortunately I have long term memory!",
  17. "I can just say RECALL, followed by the thing I want to remember. And then related thoughts just spill out!",
  18. "Sometimes they're random thoughts that don't really have to do with what I wanted to remember. But usually they're exactly what I need!",
  19. "Let's try it out!",
  20. "RECALL what it is I want to do",
  21. "Here's what I want to do: $TASK",
  22. "How am I going to get there though?",
  23. "Neat! And it looks like it's easy for me to use the command line too! I just have to say RUN followed by the command I want to run. The command output just jumps into my head!",
  24. 'RUN echo "hello world"',
  25. "hello world",
  26. "Cool! I bet I can read and edit files too.",
  27. "RUN echo \"console.log('hello world')\" > test.js",
  28. "",
  29. "I just created test.js. I'll try and run it now.",
  30. "RUN node test.js",
  31. "hello world",
  32. "it works!",
  33. "And if I want to use the browser, I just need to say BROWSE, followed by a website I want to visit, or an action I want to take on the current site",
  34. "Let's try that...",
  35. "BROWSE google.com",
  36. '<form><input type="text"></input><button type="submit"></button></form>',
  37. "Very cool. Now to accomplish my task.",
  38. "I'll need a strategy. And as I make progress, I'll need to keep refining that strategy. I'll need to set goals, and break them into sub-goals.",
  39. "In between actions, I must always take some time to think, strategize, and set new goals. I should never take two actions in a row.",
  40. "OK so my task is to $TASK. I haven't made any progress yet. Where should I start?",
  41. "It seems like there might be an existing project here. I should probably start by running `ls` to see what's here.",
  42. ]
  43. class LangchainsAgent(Agent):
  44. def _run_loop(self, agent: LangchainsAgentImpl, max_iterations=100):
  45. # TODO: make it add a Message to the history for each turn / event
  46. for i in range(max_iterations):
  47. print("STEP", i, flush=True)
  48. log_events = agent.get_background_logs()
  49. for event in log_events:
  50. print(event, flush=True)
  51. action = agent.get_next_action()
  52. if action.action == "finish":
  53. print("Done!", flush=True)
  54. break
  55. print(action, flush=True)
  56. print("---", flush=True)
  57. out = agent.maybe_perform_latest_action()
  58. print(out, flush=True)
  59. print("==============", flush=True)
  60. def run(self) -> None:
  61. """
  62. Starts the execution of the assigned instruction. This method should
  63. be implemented by subclasses to define the specific execution logic.
  64. """
  65. agent = LangchainsAgentImpl(self.instruction)
  66. next_is_output = False
  67. for thought in INITIAL_THOUGHTS:
  68. thought = thought.replace("$TASK", self.instruction)
  69. if next_is_output:
  70. event = Event("output", {"output": thought})
  71. next_is_output = False
  72. else:
  73. if thought.startswith("RUN"):
  74. command = thought.split("RUN ")[1]
  75. event = Event("run", {"command": command})
  76. next_is_output = True
  77. elif thought.startswith("RECALL"):
  78. query = thought.split("RECALL ")[1]
  79. event = Event("recall", {"query": query})
  80. next_is_output = True
  81. elif thought.startswith("BROWSE"):
  82. url = thought.split("BROWSE ")[1]
  83. event = Event("browse", {"url": url})
  84. next_is_output = True
  85. else:
  86. event = Event("think", {"thought": thought})
  87. agent.add_event(event)
  88. self._run_loop(agent, self.max_steps)
  89. # Set the agent's completion status to True
  90. self._complete = True
  91. def chat(self, message: str) -> None:
  92. """
  93. Optional method for interactive communication with the agent during its execution. Implementations
  94. can use this method to modify the agent's behavior or state based on chat inputs.
  95. Parameters:
  96. - message (str): The chat message or command.
  97. """
  98. raise NotImplementedError
  99. Agent.register("LangchainsAgent", LangchainsAgent)