LocalExecutorWithTools

LocalExecutorWithTools(tools: list[Tool] | None = None, work_dir: Path | str = PosixPath('.'))

An executor that executes code blocks with injected tools. In this executor, the func within the tools can be called directly without declaring in the code block.
For example, for a tool converted from langchain, the relevant functions can be called directly.

from langchain_community.tools import WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper
from autogen.interop import Interoperability

api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=3000)
langchain_tool = WikipediaQueryRun(api_wrapper=api_wrapper)
interop = Interoperability()
ag2_tool = interop.convert_tool(tool=langchain_tool, type="langchain")

# `ag2_tool.name` is wikipedia
local_executor = LocalExecutorWithTools(tools=[ag2_tool], work_dir="./")

code = '''
result = wikipedia(tool_input=\{"query":"Christmas"})
print(result)
'''
print(
    local_executor.execute_code_blocks(
        code_blocks=[
            CodeBlock(language="python", code=code),
        ]
    )
)

In this case, the wikipedia function can be called directly in the code block. This hides the complexity of the tool.

Parameters:
NameDescription
toolsType: list[Tool] | None

Default: None
work_dirType: pathlib.Path | str

Default: PosixPath(’.’)

Instance Attributes

code_extractor


(Experimental) Export a code extractor that can be used by an agent.

Instance Methods

execute_code_blocks

execute_code_blocks(self, code_blocks: list[CodeBlock]) -> CodeResult

Execute code blocks and return the result.

Parameters:
NameDescription
code_blocksThe code blocks to execute.

Type: list[CodeBlock]
Returns:
TypeDescription
CodeResultCodeResult: The result of the code execution.

restart

restart(self) -> 

Restart the code executor. Since this executor is stateless, no action is needed.