Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion camel/interpreters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BaseInterpreter(ABC):
r"""An abstract base class for code interpreters."""

@abstractmethod
def run(self, code: str, code_type: str) -> str:
def run(self, code: str, code_type: str, timeout: int = None) -> str:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we also need to update the docstring

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer Optional[int] in the type hints for clarity.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to update the argument timeout: Optional[int] = None in the def run of all of the subclasses if you are updating this parameter in the base class. You have not updated it in e2b_interpreter.py and subprocess_interpreter.py

r"""Executes the given code based on its type.

Args:
Expand Down
8 changes: 6 additions & 2 deletions camel/interpreters/docker_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ def __init__(
require_confirm: bool = True,
print_stdout: bool = False,
print_stderr: bool = True,
default_timeout: int = 60,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also update the docstring?

) -> None:
self.require_confirm = require_confirm
self.print_stdout = print_stdout
self.print_stderr = print_stderr

self.default_timeout = default_timeout
# lazy initialization of container
self._container: Optional[Container] = None

Expand Down Expand Up @@ -152,6 +153,7 @@ def _run_file_in_container(
self,
file: Path,
code_type: str,
timeout: int = None,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer Optional[int] in the type hints for clarity.

) -> str:
code_type = self._check_code_type(code_type)
commands = shlex.split(
Expand All @@ -166,6 +168,7 @@ def _run_file_in_container(
stdout, stderr = self._container.exec_run(
commands,
demux=True,
timeout=timeout if timeout is not None else self.default_timeout,
).output

if self.print_stdout and stdout:
Expand All @@ -184,6 +187,7 @@ def run(
self,
code: str,
code_type: str,
timeout: int = None,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer Optional[int] in the type hints for clarity.

) -> str:
r"""Executes the given code in the conatiner attached to the
interpreter, and captures the stdout and stderr streams.
Expand Down Expand Up @@ -228,7 +232,7 @@ def run(

try:
temp_file_path = self._create_file_in_container(code)
result = self._run_file_in_container(temp_file_path, code_type)
result = self._run_file_in_container(temp_file_path, code_type,timeout=timeout)
except docker.errors.APIError as e:
raise InterpreterError(
f"Execution halted due to docker API error: {e.explanation}. "
Expand Down
14 changes: 11 additions & 3 deletions camel/interpreters/internal_python_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import importlib
import typing
from typing import Any, ClassVar, Dict, List, Optional

import concurrent.futures
from camel.interpreters.base import BaseInterpreter
from camel.interpreters.interpreter_error import InterpreterError

Expand Down Expand Up @@ -87,15 +87,17 @@ def __init__(
import_white_list: Optional[List[str]] = None,
unsafe_mode: bool = False,
raise_error: bool = False,
default_timeout:int = 60
) -> None:
self.action_space = action_space or dict()
self.state = self.action_space.copy()
self.fuzz_state: Dict[str, Any] = dict()
self.import_white_list = import_white_list or list()
self.raise_error = raise_error
self.unsafe_mode = unsafe_mode
self.default_timeout = default_timeout

def run(self, code: str, code_type: str) -> str:
def run(self, code: str, code_type: str, timeout:int =None) -> str:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer Optional[int] in the type hints for clarity.

r"""Executes the given code with specified code type in the
interpreter.

Expand Down Expand Up @@ -145,7 +147,13 @@ def run(self, code: str, code_type: str) -> str:

return result
else:
return str(self.execute(code))
try:
with concurrent.futures.ThreadPoolExecutor() as executor:
future = executor.submit(self._execute_ast)
line_result = future.result(timeout=timeout or self.default_timeout)
return str(line_result)
except concurrent.futures.TimeoutError:
raise InterpreterError("Code execution timed out.")

def update_action_space(self, action_space: Dict[str, Any]) -> None:
r"""Updates action space for *python* interpreter."""
Expand Down
7 changes: 4 additions & 3 deletions camel/interpreters/ipython_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ def __init__(
require_confirm: bool = True,
print_stdout: bool = False,
print_stderr: bool = True,
default_timeout: int = 60,
) -> None:
self.require_confirm = require_confirm
self.print_stdout = print_stdout
self.print_stderr = print_stderr

self.default_timeout = default_timeout
self.kernel_manager: Optional[KernelManager] = None
self.client: Optional[BlockingKernelClient] = None

Expand Down Expand Up @@ -118,7 +119,7 @@ def _execute(self, code: str, timeout: float) -> str:
exec_result = "\n".join(outputs)
return self._clean_ipython_output(exec_result)

def run(self, code: str, code_type: str) -> str:
def run(self, code: str, code_type: str, timeout:int = None) -> str:
r"""Executes the given code in the Jupyter kernel.

Args:
Expand All @@ -138,7 +139,7 @@ def run(self, code: str, code_type: str) -> str:
if code_type == "bash":
code = f"%%bash\n({code})"
try:
result = self._execute(code, timeout=TIMEOUT)
result = self._execute(code, timeout=timeout or self.default_timeout)
except Exception as e:
raise InterpreterError(f"Execution failed: {e!s}")

Expand Down
1 change: 1 addition & 0 deletions camel/interpreters/subprocess_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def run_file(
self,
file: Path,
code_type: str,
timeout: int = None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer Optional[int] in the type hints for clarity.

) -> str:
r"""Executes a code file in a subprocess and captures its output.

Expand Down
Loading