|
|
@@ -1,9 +1,14 @@
|
|
|
-from typing import List, Dict, Type
|
|
|
+from typing import List
|
|
|
|
|
|
-import agenthub.langchains_agent.utils.llm as llm
|
|
|
+from opendevin.llm.llm import LLM
|
|
|
from opendevin.agent import Agent
|
|
|
+from opendevin.state import State
|
|
|
+from opendevin.action import Action
|
|
|
+import agenthub.langchains_agent.utils.prompts as prompts
|
|
|
+from agenthub.langchains_agent.utils.monologue import Monologue
|
|
|
+from agenthub.langchains_agent.utils.memory import LongTermMemory
|
|
|
+
|
|
|
from opendevin.action import (
|
|
|
- Action,
|
|
|
CmdRunAction,
|
|
|
CmdKillAction,
|
|
|
BrowseURLAction,
|
|
|
@@ -14,15 +19,12 @@ from opendevin.action import (
|
|
|
AgentFinishAction,
|
|
|
)
|
|
|
from opendevin.observation import (
|
|
|
- Observation,
|
|
|
CmdOutputObservation,
|
|
|
- BrowserOutputObservation,
|
|
|
)
|
|
|
-from opendevin.state import State
|
|
|
|
|
|
-from agenthub.langchains_agent.utils.monologue import Monologue
|
|
|
-from agenthub.langchains_agent.utils.memory import LongTermMemory
|
|
|
|
|
|
+MAX_MONOLOGUE_LENGTH = 20000
|
|
|
+MAX_OUTPUT_LENGTH = 5000
|
|
|
|
|
|
INITIAL_THOUGHTS = [
|
|
|
"I exist!",
|
|
|
@@ -66,26 +68,12 @@ INITIAL_THOUGHTS = [
|
|
|
MAX_OUTPUT_LENGTH = 5000
|
|
|
MAX_MONOLOGUE_LENGTH = 20000
|
|
|
|
|
|
-
|
|
|
-ACTION_TYPE_TO_CLASS: Dict[str, Type[Action]] = {
|
|
|
- "run": CmdRunAction,
|
|
|
- "kill": CmdKillAction,
|
|
|
- "browse": BrowseURLAction,
|
|
|
- "read": FileReadAction,
|
|
|
- "write": FileWriteAction,
|
|
|
- "recall": AgentRecallAction,
|
|
|
- "think": AgentThinkAction,
|
|
|
- "finish": AgentFinishAction,
|
|
|
-}
|
|
|
-
|
|
|
-CLASS_TO_ACTION_TYPE: Dict[Type[Action], str] = {v: k for k, v in ACTION_TYPE_TO_CLASS.items()}
|
|
|
-
|
|
|
class LangchainsAgent(Agent):
|
|
|
_initialized = False
|
|
|
|
|
|
- def __init__(self, model_name: str):
|
|
|
- super().__init__(model_name)
|
|
|
- self.monologue = Monologue(self.model_name)
|
|
|
+ def __init__(self, llm: LLM):
|
|
|
+ super().__init__(llm)
|
|
|
+ self.monologue = Monologue()
|
|
|
self.memory = LongTermMemory()
|
|
|
|
|
|
def _add_event(self, event: dict):
|
|
|
@@ -95,7 +83,7 @@ class LangchainsAgent(Agent):
|
|
|
self.monologue.add_event(event)
|
|
|
self.memory.add_event(event)
|
|
|
if self.monologue.get_total_length() > MAX_MONOLOGUE_LENGTH:
|
|
|
- self.monologue.condense()
|
|
|
+ self.monologue.condense(self.llm)
|
|
|
|
|
|
def _initialize(self):
|
|
|
if self._initialized:
|
|
|
@@ -103,6 +91,8 @@ class LangchainsAgent(Agent):
|
|
|
|
|
|
if self.instruction is None or self.instruction == "":
|
|
|
raise ValueError("Instruction must be provided")
|
|
|
+ self.monologue = Monologue()
|
|
|
+ self.memory = LongTermMemory()
|
|
|
|
|
|
next_is_output = False
|
|
|
for thought in INITIAL_THOUGHTS:
|
|
|
@@ -128,7 +118,7 @@ class LangchainsAgent(Agent):
|
|
|
else:
|
|
|
d = {"action": "think", "args": {"thought": thought}}
|
|
|
|
|
|
- self._add_event(d)
|
|
|
+ self._add_event(d)
|
|
|
self._initialized = True
|
|
|
|
|
|
def step(self, state: State) -> Action:
|
|
|
@@ -143,14 +133,8 @@ class LangchainsAgent(Agent):
|
|
|
d = {"action": "error", "args": {"output": obs.content}}
|
|
|
else:
|
|
|
d = {"action": "output", "args": {"output": obs.content}}
|
|
|
- # elif isinstance(obs, UserMessageObservation):
|
|
|
- # d = {"action": "output", "args": {"output": obs.message}}
|
|
|
- # elif isinstance(obs, AgentMessageObservation):
|
|
|
- # d = {"action": "output", "args": {"output": obs.message}}
|
|
|
- elif isinstance(obs, (BrowserOutputObservation, Observation)):
|
|
|
- d = {"action": "output", "args": {"output": obs.content}}
|
|
|
else:
|
|
|
- raise NotImplementedError(f"Unknown observation type: {obs}")
|
|
|
+ d = {"action": "output", "args": {"output": obs.content}}
|
|
|
self._add_event(d)
|
|
|
|
|
|
|
|
|
@@ -175,18 +159,16 @@ class LangchainsAgent(Agent):
|
|
|
self._add_event(d)
|
|
|
|
|
|
state.updated_info = []
|
|
|
-
|
|
|
- action_dict = llm.request_action(
|
|
|
+
|
|
|
+ prompt = prompts.get_request_action_prompt(
|
|
|
self.instruction,
|
|
|
self.monologue.get_thoughts(),
|
|
|
- self.model_name,
|
|
|
state.background_commands_obs,
|
|
|
)
|
|
|
- if action_dict is None:
|
|
|
- action_dict = {"action": "think", "args": {"thought": "..."}}
|
|
|
-
|
|
|
- # Translate action_dict to Action
|
|
|
- action = ACTION_TYPE_TO_CLASS[action_dict["action"]](**action_dict["args"])
|
|
|
+ messages = [{"content": prompt,"role": "user"}]
|
|
|
+ resp = self.llm.completion(messages=messages)
|
|
|
+ action_resp = resp['choices'][0]['message']['content']
|
|
|
+ action = prompts.parse_action_response(action_resp)
|
|
|
self.latest_action = action
|
|
|
return action
|
|
|
|