You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_code_executor_agent.py 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import pytest
  2. from autogen_agentchat.agents import CodeExecutorAgent
  3. from autogen_agentchat.base import Response
  4. from autogen_agentchat.messages import TextMessage
  5. from autogen_core import CancellationToken
  6. from autogen_ext.code_executors.local import LocalCommandLineCodeExecutor
  7. @pytest.mark.asyncio
  8. async def test_basic_code_execution() -> None:
  9. """Test basic code execution"""
  10. agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor())
  11. messages = [
  12. TextMessage(
  13. content="""
  14. ```python
  15. import math
  16. number = 42
  17. square_root = math.sqrt(number)
  18. print("%0.3f" % (square_root,))
  19. ```
  20. """.strip(),
  21. source="assistant",
  22. )
  23. ]
  24. response = await agent.on_messages(messages, CancellationToken())
  25. assert isinstance(response, Response)
  26. assert isinstance(response.chat_message, TextMessage)
  27. assert response.chat_message.content.strip() == "6.481"
  28. assert response.chat_message.source == "code_executor"
  29. @pytest.mark.asyncio
  30. async def test_code_execution_error() -> None:
  31. """Test basic code execution"""
  32. agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor())
  33. messages = [
  34. TextMessage(
  35. content="""
  36. ```python
  37. import math
  38. number = -1.0
  39. square_root = math.sqrt(number)
  40. print("%0.3f" % (square_root,))
  41. ```
  42. """.strip(),
  43. source="assistant",
  44. )
  45. ]
  46. response = await agent.on_messages(messages, CancellationToken())
  47. assert isinstance(response, Response)
  48. assert isinstance(response.chat_message, TextMessage)
  49. assert "The script ran, then exited with an error (POSIX exit code: 1)" in response.chat_message.content
  50. assert "ValueError: math domain error" in response.chat_message.content
  51. @pytest.mark.asyncio
  52. async def test_code_execution_no_output() -> None:
  53. """Test basic code execution"""
  54. agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor())
  55. messages = [
  56. TextMessage(
  57. content="""
  58. ```python
  59. import math
  60. number = 42
  61. square_root = math.sqrt(number)
  62. ```
  63. """.strip(),
  64. source="assistant",
  65. )
  66. ]
  67. response = await agent.on_messages(messages, CancellationToken())
  68. assert isinstance(response, Response)
  69. assert isinstance(response.chat_message, TextMessage)
  70. assert (
  71. "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."
  72. in response.chat_message.content
  73. )
  74. @pytest.mark.asyncio
  75. async def test_code_execution_no_block() -> None:
  76. """Test basic code execution"""
  77. agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor())
  78. messages = [
  79. TextMessage(
  80. content="""
  81. import math
  82. number = 42
  83. square_root = math.sqrt(number)
  84. """.strip(),
  85. source="assistant",
  86. )
  87. ]
  88. response = await agent.on_messages(messages, CancellationToken())
  89. assert isinstance(response, Response)
  90. assert isinstance(response.chat_message, TextMessage)
  91. assert (
  92. "No code blocks found in the thread. Please provide at least one markdown-encoded code block"
  93. in response.chat_message.content
  94. )
  95. @pytest.mark.asyncio
  96. async def test_code_execution_multiple_blocks() -> None:
  97. """Test basic code execution"""
  98. agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor())
  99. messages = [
  100. TextMessage(
  101. content="""
  102. ```python
  103. import math
  104. number = 42
  105. square_root = math.sqrt(number)
  106. print("%0.3f" % (square_root,))
  107. ```
  108. And also:
  109. ```python
  110. import time
  111. print(f"The current time is: {time.time()}")
  112. ```
  113. And this should result in an error:
  114. ```python
  115. import math
  116. number = -1.0
  117. square_root = math.sqrt(number)
  118. print("%0.3f" % (square_root,))
  119. ```
  120. """.strip(),
  121. source="assistant",
  122. )
  123. ]
  124. response = await agent.on_messages(messages, CancellationToken())
  125. assert isinstance(response, Response)
  126. assert isinstance(response.chat_message, TextMessage)
  127. assert "6.481" in response.chat_message.content
  128. assert "The current time is:" in response.chat_message.content
  129. assert "The script ran, then exited with an error (POSIX exit code: 1)" in response.chat_message.content
  130. assert "ValueError: math domain error" in response.chat_message.content
  131. @pytest.mark.asyncio
  132. async def test_code_execution_agent_serialization() -> None:
  133. """Test agent config serialization"""
  134. agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor())
  135. # Serialize and deserialize the agent
  136. serialized_agent = agent.dump_component()
  137. deserialized_agent = CodeExecutorAgent.load_component(serialized_agent)
  138. assert isinstance(deserialized_agent, CodeExecutorAgent)
  139. assert deserialized_agent.name == "code_executor"