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)
- 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)
- class PythonCodeExecutionTool(executor: CodeExecutor)[source]#
Bases:
BaseTool
[CodeExecutionInput
,CodeExecutionResult
],Component
[PythonCodeExecutionToolConfig
]A tool that executes Python code in a code executor and returns output.
Example executors:
autogen_ext.code_executors.local.LocalCommandLineCodeExecutor
autogen_ext.code_executors.docker.DockerCommandLineCodeExecutor
autogen_ext.code_executors.azure.ACADynamicSessionsCodeExecutor
Example usage:
pip install -U "autogen-agentchat" "autogen-ext[openai]" "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.
- component_config_schema#
alias of
PythonCodeExecutionToolConfig
- component_provider_override: ClassVar[str | None] = 'autogen_ext.tools.code_execution.PythonCodeExecutionTool'#
Override the provider string for the component. This should be used to prevent internal module names being a part of the module name.
- async run(args: CodeExecutionInput, cancellation_token: CancellationToken) CodeExecutionResult [source]#