autogen_ext.tools.code_execution#
- pydantic 模型 CodeExecutionInput[源代码]#
基类:
BaseModel
显示 JSON 模式
{ "title": "CodeExecutionInput", "type": "object", "properties": { "code": { "description": "The contents of the Python code block that should be executed", "title": "Code", "type": "string" } }, "required": [ "code" ] }
- 字段:
code (str)
- pydantic 模型 CodeExecutionResult[源代码]#
基类:
BaseModel
显示 JSON 模式
{ "title": "CodeExecutionResult", "type": "object", "properties": { "success": { "title": "Success", "type": "boolean" }, "output": { "title": "Output", "type": "string" } }, "required": [ "success", "output" ] }
- 字段:
output (str)
success (bool)
- class PythonCodeExecutionTool(executor: CodeExecutor)[源代码]#
基类:
BaseTool
[CodeExecutionInput
,CodeExecutionResult
],Component
[PythonCodeExecutionToolConfig
]一个在代码执行器中执行 Python 代码并返回输出的工具。
示例执行器
autogen_ext.code_executors.local.LocalCommandLineCodeExecutor
autogen_ext.code_executors.docker.DockerCommandLineCodeExecutor
autogen_ext.code_executors.azure.ACADynamicSessionsCodeExecutor
使用示例
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())
- 参数:
executor (CodeExecutor) – 将用于执行代码块的代码执行器。
- component_config_schema#
PythonCodeExecutionToolConfig
的别名
- component_provider_override: ClassVar[str | None] = 'autogen_ext.tools.code_execution.PythonCodeExecutionTool'#
覆盖组件的提供程序字符串。这应该用于防止内部模块名称成为模块名称的一部分。
- async run(args: CodeExecutionInput, cancellation_token: CancellationToken) CodeExecutionResult [源代码]#