autogen_ext.tools.code_execution#

pydantic model CodeExecutionInput[source]#

Bases: BaseModel

Show JSON schema
{
   "title": "CodeExecutionInput",
   "type": "object",
   "properties": {
      "code": {
         "description": "The contents of the Python code block that should be executed",
         "title": "Code",
         "type": "string"
      }
   },
   "required": [
      "code"
   ]
}

Fields:
  • code (str)

field code: str [Required]#

The contents of the Python code block that should be executed

pydantic model CodeExecutionResult[source]#

Bases: BaseModel

Show JSON schema
{
   "title": "CodeExecutionResult",
   "type": "object",
   "properties": {
      "success": {
         "title": "Success",
         "type": "boolean"
      },
      "output": {
         "title": "Output",
         "type": "string"
      }
   },
   "required": [
      "success",
      "output"
   ]
}

Fields:
  • output (str)

  • success (bool)

field output: str [Required]#
field success: bool [Required]#
ser_model() str[source]#
class PythonCodeExecutionTool(executor: CodeExecutor)[source]#

Bases: BaseTool[CodeExecutionInput, CodeExecutionResult]

A tool that executes Python code in a code executor and returns output.

Example executors:

Example usage:

pip install "autogen-agentchat==0.4.0.dev13" "autogen-ext[openai]==0.4.0.dev13" "yfinance" "matplotlib"
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_ext.code_executors.local import LocalCommandLineCodeExecutor
from autogen_ext.tools.code_execution import PythonCodeExecutionTool


async def main() -> None:
    tool = PythonCodeExecutionTool(LocalCommandLineCodeExecutor(work_dir="coding"))
    agent = AssistantAgent(
        "assistant", OpenAIChatCompletionClient(model="gpt-4o"), tools=[tool], reflect_on_tool_use=True
    )
    await Console(
        agent.run_stream(
            task="Create a plot of MSFT stock prices in 2024 and save it to a file. Use yfinance and matplotlib."
        )
    )


asyncio.run(main())
Parameters:

executor (CodeExecutor) – The code executor that will be used to execute the code blocks.

async run(args: CodeExecutionInput, cancellation_token: CancellationToken) CodeExecutionResult[source]#