Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal Docker Sandbox with GPT-3.5 Execution Example #48

Merged
merged 22 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
dd0ff16
minimal docker sandbox
xingyaoww Mar 18, 2024
b41efe1
make container_image as an argument (fall back to ubuntu);
xingyaoww Mar 18, 2024
79d1c1d
add a minimal working (imperfect) example
xingyaoww Mar 18, 2024
4f2eb53
fix typo
xingyaoww Mar 18, 2024
1dcb0b4
change default container name
xingyaoww Mar 18, 2024
415efea
attempt to fix "Bad file descriptor" error
xingyaoww Mar 18, 2024
652af30
handle ctrl+D
xingyaoww Mar 18, 2024
10f02a6
add Python gitignore
xingyaoww Mar 20, 2024
1cf3540
Merge commit '10f02a660e3c5c17bba685522d17a353974c049f' into sandbox
xingyaoww Mar 20, 2024
7754e37
push sandbox to shared dockerhub for ease of use
xingyaoww Mar 20, 2024
1747269
move codeact example into research folder
xingyaoww Mar 20, 2024
386abe3
add README for opendevin
xingyaoww Mar 20, 2024
43d4927
change container image name to opendevin dockerhub
xingyaoww Mar 20, 2024
b1551a0
Merge commit '0380070e98e8d59efa411fc74dd95aa49f0ea752' into sandbox
xingyaoww Mar 21, 2024
cea4b4b
move folder; change example to a more general agent
xingyaoww Mar 21, 2024
135f3ee
update Message and Role
xingyaoww Mar 21, 2024
f65c0b7
update docker sandbox to support mounting folder and switch to user w…
xingyaoww Mar 21, 2024
e2b4b90
make network as host
xingyaoww Mar 21, 2024
c10a2d9
handle erorrs when attrs are not set yet
xingyaoww Mar 21, 2024
45e4c6d
convert codeact agent into a compatible agent
xingyaoww Mar 21, 2024
6ebffca
add workspace to gitignore
xingyaoww Mar 21, 2024
8815aa9
make sure the agent interface adjustment works for langchain_agent
xingyaoww Mar 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move folder; change example to a more general agent
  • Loading branch information
xingyaoww committed Mar 21, 2024
commit cea4b4bfd42bb2f8f7eaad2686904588f5036296
1 change: 1 addition & 0 deletions agenthub/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import langchains_agent
from . import codeact_agent
File renamed without changes.
119 changes: 119 additions & 0 deletions agenthub/codeact_agent/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import os
import re
import argparse
from litellm import completion
from termcolor import colored
from typing import List, Dict

from opendevin.agent import Agent, Message
from opendevin.sandbox.docker import DockerInteractive

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, instruction: str, max_steps: int = 100) -> 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__(instruction, max_steps)
self._history = [Message(Agent.SYSTEM, SYSTEM_MESSAGE)]
self._history.append(Message(Agent.USER, instruction))
self.env = DockerInteractive("opendevin/sandbox:latest")

self.model_name = "gpt-3.5-turbo-0125" # TODO: hard-coded here, will need to accept this as an argument in future PR

def _history_to_messages(self) -> List[Dict]:
return [message.to_dict() for message in self._history]

def run(self) -> None:
"""
Starts the execution of the assigned instruction. This method should
be implemented by subclasses to define the specific execution logic.
"""
for _ in range(self.max_turns):
response = completion(
messages=self._history_to_messages(),
model=self.model_name,
stop=["</execute>"],
temperature=0.0,
seed=42,
)
action = parse_response(response)
self._history.append(Message(Agent.ASSISTANT, action))
print(colored("===ASSISTANT:===\n" + action, "yellow"))

command = re.search(r"<execute>(.*)</execute>", action, re.DOTALL)
if command is not None:
# a command was found
command = command.group(1)
if command.strip() == "exit":
print(colored("Exit received. Exiting...", "red"))
break
# execute the code
observation = self.env.execute(command)
self._history.append(Message(Agent.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(Agent.ASSISTANT, observation))
print(colored("===ENV OBSERVATION:===\n" + observation, "blue"))

self.env.close()

def chat(self, message: str) -> None:
"""
Optional method for interactive communication with the agent during its execution. Implementations
can use this method to modify the agent's behavior or state based on chat inputs.

Parameters:
- message (str): The chat message or command.
"""
raise NotImplementedError


Agent.register("CodeActAgent", CodeActAgent)
94 changes: 0 additions & 94 deletions research/codeact/examples/run_flask_server_with_bash.py

This file was deleted.