Skip to content

Commit 2ff543e

Browse files
ekzhujackgerrits
andauthored
Add missing API doc for Python code execution tool (#4901)
* Add missing API doc for Python code execution tool * wip * Add API doc for the executor tool --------- Co-authored-by: Jack Gerrits <[email protected]>
1 parent 4486c67 commit 2ff543e

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

python/packages/autogen-core/docs/src/reference/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ python/autogen_ext.teams.magentic_one
4949
python/autogen_ext.models.openai
5050
python/autogen_ext.models.replay
5151
python/autogen_ext.tools.langchain
52+
python/autogen_ext.tools.code_execution
5253
python/autogen_ext.code_executors.local
5354
python/autogen_ext.code_executors.docker
5455
python/autogen_ext.code_executors.azure
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
autogen\_ext.tools.code\_execution
2+
==================================
3+
4+
5+
.. automodule:: autogen_ext.tools.code_execution
6+
:members:
7+
:undoc-members:
8+
:show-inheritance:

python/packages/autogen-ext/src/autogen_ext/tools/code_execution/_code_execution.py

+43
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,49 @@ def ser_model(self) -> str:
1818

1919

2020
class PythonCodeExecutionTool(BaseTool[CodeExecutionInput, CodeExecutionResult]):
21+
"""A tool that executes Python code in a code executor and returns output.
22+
23+
Example executors:
24+
25+
* :class:`autogen_ext.code_executors.local.LocalCommandLineCodeExecutor`
26+
* :class:`autogen_ext.code_executors.docker.DockerCommandLineCodeExecutor`
27+
* :class:`autogen_ext.code_executors.azure.ACADynamicSessionsCodeExecutor`
28+
29+
Example usage:
30+
31+
.. code-block:: bash
32+
33+
pip install "autogen-agentchat==0.4.0.dev13" "autogen-ext[openai]==0.4.0.dev13" "yfinance" "matplotlib"
34+
35+
.. code-block:: python
36+
37+
import asyncio
38+
from autogen_agentchat.agents import AssistantAgent
39+
from autogen_agentchat.ui import Console
40+
from autogen_ext.models.openai import OpenAIChatCompletionClient
41+
from autogen_ext.code_executors.local import LocalCommandLineCodeExecutor
42+
from autogen_ext.tools.code_execution import PythonCodeExecutionTool
43+
44+
45+
async def main() -> None:
46+
tool = PythonCodeExecutionTool(LocalCommandLineCodeExecutor(work_dir="coding"))
47+
agent = AssistantAgent(
48+
"assistant", OpenAIChatCompletionClient(model="gpt-4o"), tools=[tool], reflect_on_tool_use=True
49+
)
50+
await Console(
51+
agent.run_stream(
52+
task="Create a plot of MSFT stock prices in 2024 and save it to a file. Use yfinance and matplotlib."
53+
)
54+
)
55+
56+
57+
asyncio.run(main())
58+
59+
60+
Args:
61+
executor (CodeExecutor): The code executor that will be used to execute the code blocks.
62+
"""
63+
2164
def __init__(self, executor: CodeExecutor):
2265
super().__init__(CodeExecutionInput, CodeExecutionResult, "CodeExecutor", "Execute Python code blocks.")
2366
self._executor = executor

0 commit comments

Comments
 (0)