tools.code_execution
- class agentopera.tools.code_execution.CodeExecutionInput(*, code: str)[source]
Bases:
BaseModel
- code: str
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentopera.tools.code_execution.CodeExecutionResult(*, success: bool, output: str)[source]
Bases:
BaseModel
- success: bool
- output: str
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentopera.tools.code_execution.PythonCodeExecutionTool(executor: CodeExecutor)[source]
Bases:
BaseTool
[CodeExecutionInput
,CodeExecutionResult
]A tool that executes Python code in a code executor and returns output.
Example executors:
agentopera.agents.code_executors.local.LocalCommandLineCodeExecutor
agentopera.agents.code_executors.docker.DockerCommandLineCodeExecutor
agentopera.agents.code_executors.azure.ACADynamicSessionsCodeExecutor
Example usage:
pip install -U "agentopera" "agentopera[openai]" "yfinance" "matplotlib"
import asyncio from agentopera.chatflow.agents import AssistantAgent from agentopera.chatflow.ui import Console from agentopera.models.openai import OpenAIChatCompletionClient from agentopera.agents.code_executors.local import LocalCommandLineCodeExecutor from agentopera.agents.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]