refactor!: Migrate python code executor tool to autogen-ext (#4824)

migrate python code exec to autogen-ext

Co-authored-by: Leonardo Pinheiro <lpinheiro@microsoft.com>
Co-authored-by: Jack Gerrits <jackgerrits@users.noreply.github.com>
This commit is contained in:
Leonardo Pinheiro
2024-12-27 23:57:55 +10:00
committed by GitHub
parent d1dff316bc
commit c078b252fb
6 changed files with 11 additions and 45 deletions

View File

@@ -36,9 +36,10 @@
" UserMessage,\n",
")\n",
"from autogen_core.tool_agent import ToolAgent, ToolException, tool_agent_caller_loop\n",
"from autogen_core.tools import PythonCodeExecutionTool, ToolSchema\n",
"from autogen_core.tools import ToolSchema\n",
"from autogen_ext.code_executors.docker import DockerCommandLineCodeExecutor\n",
"from autogen_ext.models.openai import OpenAIChatCompletionClient"
"from autogen_ext.models.openai import OpenAIChatCompletionClient\n",
"from autogen_ext.tools.code_execution import PythonCodeExecutionTool"
]
},
{

View File

@@ -22,7 +22,7 @@
"source": [
"## Built-in Tools\n",
"\n",
"One of the built-in tools is the {py:class}`~autogen_core.tools.PythonCodeExecutionTool`,\n",
"One of the built-in tools is the {py:class}`~autogen_ext.tools.code_execution.PythonCodeExecutionTool`,\n",
"which allows agents to execute Python code snippets.\n",
"\n",
"Here is how you create the tool and use it."
@@ -44,8 +44,8 @@
],
"source": [
"from autogen_core import CancellationToken\n",
"from autogen_core.tools import PythonCodeExecutionTool\n",
"from autogen_ext.code_executors.docker import DockerCommandLineCodeExecutor\n",
"from autogen_ext.tools.code_execution import PythonCodeExecutionTool\n",
"\n",
"# Create the tool.\n",
"code_executor = DockerCommandLineCodeExecutor()\n",

View File

@@ -9,19 +9,10 @@ from ...tools import (
from ...tools import (
BaseToolWithState as BaseToolWithStateAlias,
)
from ...tools import (
CodeExecutionInput as CodeExecutionInputAlias,
)
from ...tools import (
CodeExecutionResult as CodeExecutionResultAlias,
)
from ...tools import FunctionTool as FunctionToolAlias
from ...tools import (
ParametersSchema as ParametersSchemaAlias,
)
from ...tools import (
PythonCodeExecutionTool as PythonCodeExecutionToolAlias,
)
from ...tools import (
Tool as ToolAlias,
)
@@ -35,9 +26,6 @@ __all__ = [
"ParametersSchema",
"BaseTool",
"BaseToolWithState",
"PythonCodeExecutionTool",
"CodeExecutionInput",
"CodeExecutionResult",
"FunctionTool",
]
@@ -80,27 +68,6 @@ class BaseToolWithState(BaseToolWithStateAlias[ArgsT, ReturnT, StateT]):
pass
@deprecated(
"autogen_core.tools.PythonCodeExecutionToolAlias moved to autogen_core.tools.PythonCodeExecutionToolAlias. This alias will be removed in 0.4.0."
)
class PythonCodeExecutionTool(PythonCodeExecutionToolAlias):
pass
@deprecated(
"autogen_core.tools.CodeExecutionInputAlias moved to autogen_core.tools.CodeExecutionInputAlias. This alias will be removed in 0.4.0."
)
class CodeExecutionInput(CodeExecutionInputAlias):
pass
@deprecated(
"autogen_core.tools.CodeExecutionResultAlias moved to autogen_core.tools.CodeExecutionResultAlias. This alias will be removed in 0.4.0."
)
class CodeExecutionResult(CodeExecutionResultAlias):
pass
@deprecated(
"autogen_core.tools.FunctionToolAlias moved to autogen_core.tools.FunctionToolAlias. This alias will be removed in 0.4.0."
)

View File

@@ -1,5 +1,4 @@
from ._base import BaseTool, BaseToolWithState, ParametersSchema, Tool, ToolSchema
from ._code_execution import CodeExecutionInput, CodeExecutionResult, PythonCodeExecutionTool
from ._function_tool import FunctionTool
__all__ = [
@@ -8,8 +7,5 @@ __all__ = [
"ParametersSchema",
"BaseTool",
"BaseToolWithState",
"PythonCodeExecutionTool",
"CodeExecutionInput",
"CodeExecutionResult",
"FunctionTool",
]

View File

@@ -1,32 +0,0 @@
from pydantic import BaseModel, Field, model_serializer
from .. import CancellationToken
from ..code_executor import CodeBlock, CodeExecutor
from ._base import BaseTool
class CodeExecutionInput(BaseModel):
code: str = Field(description="The contents of the Python code block that should be executed")
class CodeExecutionResult(BaseModel):
success: bool
output: str
@model_serializer
def ser_model(self) -> str:
return self.output
class PythonCodeExecutionTool(BaseTool[CodeExecutionInput, CodeExecutionResult]):
def __init__(self, executor: CodeExecutor):
super().__init__(CodeExecutionInput, CodeExecutionResult, "CodeExecutor", "Execute Python code blocks.")
self._executor = executor
async def run(self, args: CodeExecutionInput, cancellation_token: CancellationToken) -> CodeExecutionResult:
code_blocks = [CodeBlock(code=args.code, language="python")]
result = await self._executor.execute_code_blocks(
code_blocks=code_blocks, cancellation_token=cancellation_token
)
return CodeExecutionResult(success=result.exit_code == 0, output=result.output)