python_interpreter

Module Contents

Classes

GenericRuntime

PythonInterpreter

A Python executor that can execute Python scripts.

class lagent.actions.python_interpreter.GenericRuntime
GLOBAL_DICT
LOCAL_DICT
HEADERS = []
exec_code(code_piece)
Parameters:

code_piece (str) –

Return type:

None

eval_code(expr)
Parameters:

expr (str) –

Return type:

Any

class lagent.actions.python_interpreter.PythonInterpreter(answer_symbol=None, answer_expr='solution()', answer_from_stdout=False, timeout=20, description=None, parser=JsonParser, enable=True)

Bases: lagent.actions.base_action.BaseAction

A Python executor that can execute Python scripts.

Parameters:
  • answer_symbol (str, Optional) – the answer symbol from LLM. Defaults to None.

  • answer_expr (str, Optional) – the answer function name of the Python script. Defaults to 'solution()'.

  • answer_from_stdout (boolean, Optional) – whether the execution results is from stdout. Defaults to False.

  • timeout (int, Optional) – Upper bound of waiting time for Python script execution. Defaults to 20.

  • description (dict, Optional) – The description of the action. Defaults to None.

  • parser (Type[BaseParser]) – The parser class to process the action’s inputs and outputs. Defaults to JsonParser.

  • enable (bool, optional) – Whether the action is enabled. Defaults to True.

run(command)

用来执行Python代码。代码必须是一个函数,函数名必须得是 ‘solution’,代码对应你的思考过程。代码实例格式如下: ```python # import 依赖包 import xxx def solution():

# 初始化一些变量 variable_names_with_real_meaning = xxx # 步骤一 mid_variable = func(variable_names_with_real_meaning) # 步骤 x mid_variable = func(mid_variable) # 最后结果 final_answer = func(mid_variable) return final_answer

```

Parameters:

command (str) – Python code snippet

Return type:

lagent.schema.ActionReturn