-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/add-remove-assistant-agent-tools
- Loading branch information
Showing
4 changed files
with
586 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
python/packages/autogen-agentchat/tests/test_code_executor_agent.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import pytest | ||
from autogen_agentchat.agents import CodeExecutorAgent | ||
from autogen_agentchat.base import Response | ||
from autogen_agentchat.messages import TextMessage | ||
from autogen_core import CancellationToken | ||
from autogen_core.components.code_executor import LocalCommandLineCodeExecutor | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_basic_code_execution() -> None: | ||
"""Test basic code execution""" | ||
|
||
agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor()) | ||
|
||
messages = [ | ||
TextMessage( | ||
content=""" | ||
```python | ||
import math | ||
number = 42 | ||
square_root = math.sqrt(number) | ||
print("%0.3f" % (square_root,)) | ||
``` | ||
""".strip(), | ||
source="assistant", | ||
) | ||
] | ||
response = await agent.on_messages(messages, CancellationToken()) | ||
|
||
assert isinstance(response, Response) | ||
assert isinstance(response.chat_message, TextMessage) | ||
assert response.chat_message.content.strip() == "6.481" | ||
assert response.chat_message.source == "code_executor" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_code_execution_error() -> None: | ||
"""Test basic code execution""" | ||
|
||
agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor()) | ||
|
||
messages = [ | ||
TextMessage( | ||
content=""" | ||
```python | ||
import math | ||
number = -1.0 | ||
square_root = math.sqrt(number) | ||
print("%0.3f" % (square_root,)) | ||
``` | ||
""".strip(), | ||
source="assistant", | ||
) | ||
] | ||
response = await agent.on_messages(messages, CancellationToken()) | ||
|
||
assert isinstance(response, Response) | ||
assert isinstance(response.chat_message, TextMessage) | ||
assert "The script ran, then exited with an error (POSIX exit code: 1)" in response.chat_message.content | ||
assert "ValueError: math domain error" in response.chat_message.content | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_code_execution_no_output() -> None: | ||
"""Test basic code execution""" | ||
|
||
agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor()) | ||
|
||
messages = [ | ||
TextMessage( | ||
content=""" | ||
```python | ||
import math | ||
number = 42 | ||
square_root = math.sqrt(number) | ||
``` | ||
""".strip(), | ||
source="assistant", | ||
) | ||
] | ||
response = await agent.on_messages(messages, CancellationToken()) | ||
|
||
assert isinstance(response, Response) | ||
assert isinstance(response.chat_message, TextMessage) | ||
assert ( | ||
"The script ran but produced no output to console. The POSIX exit code was: 0. If you were expecting output, consider revising the script to ensure content is printed to stdout." | ||
in response.chat_message.content | ||
) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_code_execution_no_block() -> None: | ||
"""Test basic code execution""" | ||
|
||
agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor()) | ||
|
||
messages = [ | ||
TextMessage( | ||
content=""" | ||
import math | ||
number = 42 | ||
square_root = math.sqrt(number) | ||
""".strip(), | ||
source="assistant", | ||
) | ||
] | ||
response = await agent.on_messages(messages, CancellationToken()) | ||
|
||
assert isinstance(response, Response) | ||
assert isinstance(response.chat_message, TextMessage) | ||
assert ( | ||
"No code blocks found in the thread. Please provide at least one markdown-encoded code block" | ||
in response.chat_message.content | ||
) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_code_execution_multiple_blocks() -> None: | ||
"""Test basic code execution""" | ||
|
||
agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor()) | ||
|
||
messages = [ | ||
TextMessage( | ||
content=""" | ||
```python | ||
import math | ||
number = 42 | ||
square_root = math.sqrt(number) | ||
print("%0.3f" % (square_root,)) | ||
``` | ||
And also: | ||
```python | ||
import time | ||
print(f"The current time is: {time.time()}") | ||
``` | ||
And this should result in an error: | ||
```python | ||
import math | ||
number = -1.0 | ||
square_root = math.sqrt(number) | ||
print("%0.3f" % (square_root,)) | ||
``` | ||
""".strip(), | ||
source="assistant", | ||
) | ||
] | ||
response = await agent.on_messages(messages, CancellationToken()) | ||
|
||
assert isinstance(response, Response) | ||
assert isinstance(response.chat_message, TextMessage) | ||
assert "6.481" in response.chat_message.content | ||
assert "The current time is:" in response.chat_message.content | ||
assert "The script ran, then exited with an error (POSIX exit code: 1)" in response.chat_message.content | ||
assert "ValueError: math domain error" in response.chat_message.content |
Oops, something went wrong.