Skip to content

Commit

Permalink
make codeact agent and langchains compatible with Tuple[Action, Obser…
Browse files Browse the repository at this point in the history
…vation]
  • Loading branch information
xingyaoww committed Mar 25, 2024
1 parent bb36f15 commit d3807dd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 54 deletions.
32 changes: 14 additions & 18 deletions agenthub/codeact_agent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,19 @@ def step(self, state: State) -> Action:
updated_info = state.updated_info

if updated_info:

for item in updated_info:
if isinstance(item, Action):
assert isinstance(item, (CmdRunAction, AgentEchoAction)), "Expecting CmdRunAction or AgentEchoAction for Action"

elif isinstance(item, AgentMessageObservation): # warning message from itself
self.messages.append({"role": "user", "content": item.content})
print(colored("===USER:===\n" + item.content, "green"))

elif isinstance(item, CmdOutputObservation):
content = "OBSERVATION:\n" + item.content
content += f"\n[Command {item.command_id} finished with exit code {item.exit_code}]]"
for prev_action, obs in updated_info:
assert isinstance(prev_action, (CmdRunAction, AgentEchoAction)), "Expecting CmdRunAction or AgentEchoAction for Action"

if isinstance(obs, AgentMessageObservation): # warning message from itself
self.messages.append({"role": "user", "content": obs.content})
print(colored("===USER:===\n" + obs.content, "green"))
elif isinstance(obs, CmdOutputObservation):
content = "OBSERVATION:\n" + obs.content
content += f"\n[Command {obs.command_id} finished with exit code {obs.exit_code}]]"
self.messages.append({"role": "user", "content": content})
print(colored("===ENV OBSERVATION:===\n" + content, "blue"))

else:
raise NotImplementedError(f"Unknown observation type: {item}")
raise NotImplementedError(f"Unknown observation type: {obs.__class__}")

response = completion(
messages=self.messages,
Expand All @@ -112,11 +108,11 @@ def step(self, state: State) -> Action:
temperature=0.0,
seed=42,
)
action = parse_response(response)
self.messages.append({"role": "assistant", "content": action})
print(colored("===ASSISTANT:===\n" + action, "yellow"))
action_str: str = parse_response(response)
self.messages.append({"role": "assistant", "content": action_str})
print(colored("===ASSISTANT:===\n" + action_str, "yellow"))

command = re.search(r"<execute>(.*)</execute>", action, re.DOTALL)
command = re.search(r"<execute>(.*)</execute>", action_str, re.DOTALL)
if command is not None:
# a command was found
command_group = command.group(1)
Expand Down
70 changes: 35 additions & 35 deletions agenthub/langchains_agent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,42 +137,42 @@ def step(self, state: State) -> Action:
# completly from ground up

# Translate state to action_dict
for info in state.updated_info:
if isinstance(info, Observation):
if isinstance(info, CmdOutputObservation):
if info.error:
d = {"action": "error", "args": {"output": info.content}}
else:
d = {"action": "output", "args": {"output": info.content}}
# elif isinstance(info, UserMessageObservation):
# d = {"action": "output", "args": {"output": info.message}}
# elif isinstance(info, AgentMessageObservation):
# d = {"action": "output", "args": {"output": info.message}}
elif isinstance(info, BrowserOutputObservation):
d = {"action": "output", "args": {"output": info.content}}
for prev_action, obs in state.updated_info:
if isinstance(obs, CmdOutputObservation):
if obs.error:
d = {"action": "error", "args": {"output": obs.content}}
else:
raise NotImplementedError(f"Unknown observation type: {info}")
self._add_event(d)
elif isinstance(info, Action):
if isinstance(info, CmdRunAction):
d = {"action": "run", "args": {"command": info.command}}
elif isinstance(info, CmdKillAction):
d = {"action": "kill", "args": {"id": info.id}}
elif isinstance(info, BrowseURLAction):
d = {"action": "browse", "args": {"url": info.url}}
elif isinstance(info, FileReadAction):
d = {"action": "read", "args": {"file": info.path}}
elif isinstance(info, FileWriteAction):
d = {"action": "write", "args": {"file": info.path, "content": info.contents}}
elif isinstance(info, AgentRecallAction):
d = {"action": "recall", "args": {"query": info.query}}
elif isinstance(info, AgentThinkAction):
d = {"action": "think", "args": {"thought": info.thought}}
elif isinstance(info, AgentFinishAction):
d = {"action": "finish"}
else:
raise NotImplementedError(f"Unknown action type: {info}")
self._add_event(d)
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):
d = {"action": "output", "args": {"output": obs.content}}
else:
raise NotImplementedError(f"Unknown observation type: {obs.__class__}")
self._add_event(d)


if isinstance(prev_action, CmdRunAction):
d = {"action": "run", "args": {"command": prev_action.command}}
elif isinstance(prev_action, CmdKillAction):
d = {"action": "kill", "args": {"id": prev_action.id}}
elif isinstance(prev_action, BrowseURLAction):
d = {"action": "browse", "args": {"url": prev_action.url}}
elif isinstance(prev_action, FileReadAction):
d = {"action": "read", "args": {"file": prev_action.path}}
elif isinstance(prev_action, FileWriteAction):
d = {"action": "write", "args": {"file": prev_action.path, "content": prev_action.contents}}
elif isinstance(prev_action, AgentRecallAction):
d = {"action": "recall", "args": {"query": prev_action.query}}
elif isinstance(prev_action, AgentThinkAction):
d = {"action": "think", "args": {"thought": prev_action.thought}}
elif isinstance(prev_action, AgentFinishAction):
d = {"action": "finish"}
else:
raise NotImplementedError(f"Unknown action type: {prev_action}")
self._add_event(d)

state.updated_info = []

Expand Down
2 changes: 1 addition & 1 deletion opendevin/controller/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async def start_loop(self, task_instruction: str):
observation: Observation = action.run(self)
else:
print("ACTION NOT EXECUTABLE", flush=True)
observation: Observation = NullObservation("")
observation = NullObservation("")

self.state_updated_info.append((action, observation))

Expand Down

0 comments on commit d3807dd

Please sign in to comment.