mirror of
https://github.com/OpenHands/OpenHands.git
synced 2025-12-26 05:48:36 +08:00
* rearrange workspace_dir and max_step as arguments to controller * remove unused output * abstract each action into dataclass * move actions * fix action import * move cmd manager and change method to private * move controller * rename action folder * add state * a draft of Controller & new agent abstraction * add agent actions * remove controller file * add observation to perform a refractor on langchains agent * revert to make this compatible via translation * fix typo and translate error * add error to observation * index thought as dict * refractor controller * fix circular dependency caused by type hint * add runnable attribute to agent * add mixin to denote executable * change baseclass * make file read/write action compatible w/ docker directory * remove event * fix some merge issue * fix sandbox w/ permission issue * cleanup history abstraction since langchains agent is not really using it * tweak to make langchains agent working * make all actions return observation * fix missing import * add echo action for agent * add error code to cmd output obs * make cmd manager returns cmd output obs * fix codeact agent to make it work * fix all ruff issue * fix mypy * add import agenthub back * add message for Action attribute (migrate from previous event) * fix typo * fix instruction setting * fix instruction setting * attempt to fix session * ruff fix * add .to_dict method for base and observation * add message for recall * try to simplify the state_updated_info with tuple of action and obs * update_info to Tuple[Action, Observation] * make codeact agent and langchains compatible with Tuple[Action, Observation] * fix ruff * fix ruff * change to base path to fix minimal langchains agent * add NullAction to potentially handle for chat scenario * Update opendevin/controller/command_manager.py Co-authored-by: Robert Brennan <accounts@rbren.io> * fix event args * set the default workspace to "workspace" * make directory relative (so it does not show up to agent in File*Action) * fix typo * await to yield for sending observation * fix message format --------- Co-authored-by: Robert Brennan <accounts@rbren.io>
142 lines
5.3 KiB
Python
142 lines
5.3 KiB
Python
import os
|
|
import re
|
|
from litellm import completion
|
|
from termcolor import colored
|
|
from typing import List, Mapping
|
|
|
|
from opendevin.agent import Agent
|
|
from opendevin.state import State
|
|
from opendevin.action import (
|
|
Action,
|
|
CmdRunAction,
|
|
AgentEchoAction,
|
|
AgentFinishAction,
|
|
)
|
|
from opendevin.observation import (
|
|
CmdOutputObservation,
|
|
AgentMessageObservation,
|
|
)
|
|
|
|
|
|
assert (
|
|
"OPENAI_API_KEY" in os.environ
|
|
), "Please set the OPENAI_API_KEY environment variable."
|
|
|
|
|
|
|
|
SYSTEM_MESSAGE = """You are a helpful assistant. You will be provided access (as root) to a bash shell to complete user-provided tasks.
|
|
You will be able to execute commands in the bash shell, interact with the file system, install packages, and receive the output of your commands.
|
|
|
|
DO NOT provide code in ```triple backticks```. Instead, you should execute bash command on behalf of the user by wrapping them with <execute> and </execute>.
|
|
For example:
|
|
|
|
You can list the files in the current directory by executing the following command:
|
|
<execute>ls</execute>
|
|
|
|
You can also install packages using pip:
|
|
<execute> pip install numpy </execute>
|
|
|
|
You can also write a block of code to a file:
|
|
<execute>
|
|
echo "import math
|
|
print(math.pi)" > math.py
|
|
</execute>
|
|
|
|
When you are done, execute "exit" to close the shell and end the conversation.
|
|
"""
|
|
|
|
INVALID_INPUT_MESSAGE = (
|
|
"I don't understand your input. \n"
|
|
"If you want to execute command, please use <execute> YOUR_COMMAND_HERE </execute>.\n"
|
|
"If you already completed the task, please exit the shell by generating: <execute> exit </execute>."
|
|
)
|
|
|
|
|
|
def parse_response(response) -> str:
|
|
action = response.choices[0].message.content
|
|
if "<execute>" in action and "</execute>" not in action:
|
|
action += "</execute>"
|
|
return action
|
|
|
|
|
|
class CodeActAgent(Agent):
|
|
def __init__(
|
|
self,
|
|
model_name: str
|
|
) -> None:
|
|
"""
|
|
Initializes a new instance of the CodeActAgent class.
|
|
|
|
Parameters:
|
|
- instruction (str): The instruction for the agent to execute.
|
|
- max_steps (int): The maximum number of steps to run the agent.
|
|
"""
|
|
super().__init__(model_name)
|
|
self.messages: List[Mapping[str, str]] = []
|
|
self.instruction: str = ""
|
|
|
|
def step(self, state: State) -> Action:
|
|
if len(self.messages) == 0:
|
|
assert self.instruction, "Expecting instruction to be set"
|
|
self.messages = [
|
|
{"role": "system", "content": SYSTEM_MESSAGE},
|
|
{"role": "user", "content": self.instruction},
|
|
]
|
|
print(colored("===USER:===\n" + self.instruction, "green"))
|
|
|
|
updated_info = state.updated_info
|
|
|
|
if updated_info:
|
|
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: {obs.__class__}")
|
|
|
|
response = completion(
|
|
messages=self.messages,
|
|
model=self.model_name,
|
|
stop=["</execute>"],
|
|
temperature=0.0,
|
|
seed=42,
|
|
)
|
|
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_str, re.DOTALL)
|
|
if command is not None:
|
|
# a command was found
|
|
command_group = command.group(1)
|
|
if command_group.strip() == "exit":
|
|
print(colored("Exit received. Exiting...", "red"))
|
|
return AgentFinishAction()
|
|
return CmdRunAction(command = command_group)
|
|
# # execute the code
|
|
# # TODO: does exit_code get loaded into Message?
|
|
# exit_code, observation = self.env.execute(command_group)
|
|
# self._history.append(Message(Role.ASSISTANT, observation))
|
|
# print(colored("===ENV OBSERVATION:===\n" + observation, "blue"))
|
|
else:
|
|
# we could provide a error message for the model to continue similar to
|
|
# https://github.com/xingyaoww/mint-bench/blob/main/mint/envs/general_env.py#L18-L23
|
|
# observation = INVALID_INPUT_MESSAGE
|
|
# self._history.append(Message(Role.ASSISTANT, observation))
|
|
# print(colored("===ENV OBSERVATION:===\n" + observation, "blue"))
|
|
return AgentEchoAction(content=INVALID_INPUT_MESSAGE) # warning message to itself
|
|
|
|
|
|
def search_memory(self, query: str) -> List[str]:
|
|
raise NotImplementedError("Implement this abstract method")
|
|
|
|
|
|
Agent.register("CodeActAgent", CodeActAgent)
|