Evaluate a python expression using the content of the variables stored in a state and only evaluating a given set of functions. This function will recurse through the nodes of the tree provided. Args: code (`str`): The code to evaluate. static_tools (`D
(
code: str,
static_tools: dict[str, Callable] | None = None,
custom_tools: dict[str, Callable] | None = None,
state: dict[str, Any] | None = None,
authorized_imports: list[str] = BASE_BUILTIN_MODULES,
max_print_outputs_length: int = DEFAULT_MAX_LEN_OUTPUT,
timeout_seconds: int | None = MAX_EXECUTION_TIME_SECONDS,
)
| 1581 | |
| 1582 | |
| 1583 | def evaluate_python_code( |
| 1584 | code: str, |
| 1585 | static_tools: dict[str, Callable] | None = None, |
| 1586 | custom_tools: dict[str, Callable] | None = None, |
| 1587 | state: dict[str, Any] | None = None, |
| 1588 | authorized_imports: list[str] = BASE_BUILTIN_MODULES, |
| 1589 | max_print_outputs_length: int = DEFAULT_MAX_LEN_OUTPUT, |
| 1590 | timeout_seconds: int | None = MAX_EXECUTION_TIME_SECONDS, |
| 1591 | ): |
| 1592 | """ |
| 1593 | Evaluate a python expression using the content of the variables stored in a state and only evaluating a given set |
| 1594 | of functions. |
| 1595 | |
| 1596 | This function will recurse through the nodes of the tree provided. |
| 1597 | |
| 1598 | Args: |
| 1599 | code (`str`): |
| 1600 | The code to evaluate. |
| 1601 | static_tools (`Dict[str, Callable]`): |
| 1602 | The functions that may be called during the evaluation. These can also be agents in a multiagent setting. |
| 1603 | These tools cannot be overwritten in the code: any assignment to their name will raise an error. |
| 1604 | custom_tools (`Dict[str, Callable]`): |
| 1605 | The functions that may be called during the evaluation. |
| 1606 | These tools can be overwritten in the code: any assignment to their name will overwrite them. |
| 1607 | state (`Dict[str, Any]`): |
| 1608 | A dictionary mapping variable names to values. The `state` should contain the initial inputs but will be |
| 1609 | updated by this function to contain all variables as they are evaluated. |
| 1610 | The print outputs will be stored in the state under the key "_print_outputs". |
| 1611 | timeout_seconds (`int`, *optional*, defaults to `MAX_EXECUTION_TIME_SECONDS`): |
| 1612 | Maximum time in seconds allowed for code execution. Set to `None` to disable timeout. |
| 1613 | """ |
| 1614 | try: |
| 1615 | expression = ast.parse(code) |
| 1616 | except SyntaxError as e: |
| 1617 | raise InterpreterError( |
| 1618 | f"Code parsing failed on line {e.lineno} due to: {type(e).__name__}: {str(e)}\n" |
| 1619 | f"{e.text}" |
| 1620 | f"{' ' * (e.offset or 0)}^" |
| 1621 | ) |
| 1622 | |
| 1623 | if state is None: |
| 1624 | state = {} |
| 1625 | static_tools = static_tools.copy() if static_tools is not None else {} |
| 1626 | custom_tools = custom_tools if custom_tools is not None else {} |
| 1627 | state["_print_outputs"] = PrintContainer() |
| 1628 | state["_operations_count"] = {"counter": 0} |
| 1629 | |
| 1630 | if "final_answer" in static_tools: |
| 1631 | previous_final_answer = static_tools["final_answer"] |
| 1632 | |
| 1633 | def final_answer(*args, **kwargs): # Allow arbitrary arguments to be passed |
| 1634 | raise FinalAnswerException(previous_final_answer(*args, **kwargs)) |
| 1635 | |
| 1636 | static_tools["final_answer"] = final_answer |
| 1637 | |
| 1638 | # Define the actual execution logic |
| 1639 | def _execute_code(): |
| 1640 | result = None |
searching dependent graphs…