Skip to content

Commit

Permalink
Reasoning Agent can execute code now. (#545)
Browse files Browse the repository at this point in the history
* Enable code execution in reasoning agent

1. enable code execution
2. remove the "verbose" parameter, and replace with AG2's default "silent" parameter.

* init _user_proxy

* notebook update

* notebook update

* add test case for running code in reasoning agent

* use mock credential for reasoning test

* reasoning prompt update

* mock credentials for more tests

* Update .secrets.baseline

* clear notebook outputs

* remove variable F
  • Loading branch information
BabyCNM committed Feb 26, 2025
1 parent a1cb676 commit 5cc5de3
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions test/agentchat/contrib/test_reasoning_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,5 +533,54 @@ def mock_response(*args, **kwargs):
assert "Paris" in ground_truth


def test_reasoning_agent_code_execution(mock_credentials: Credentials):
"""Test that ReasoningAgent properly executes code in responses"""

# Create agent with code execution enabled
with patch("autogen.agentchat.conversable_agent.ConversableAgent.generate_oai_reply") as mock_oai_reply:
agent = ReasoningAgent(
"test_agent",
llm_config=mock_credentials.llm_config,
code_execution_config={"use_docker": False, "work_dir": "mypy_cache"},
)

def mock_response(*args, **kwargs):
instance = args[0]
if instance.name == "tot_thinker":
return True, {
"content": """Reflection
Let's solve this with Python.
Possible Options:
Option 1: Calculate factorial with Python
```python
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
print(f"Factorial of 5 is {factorial(5)}")
```
Option 2: TERMINATE"""
}
elif instance.name == "reasoner_user_proxy":
# Mock the code execution result
return True, {"content": "Factorial of 5 is 120"}
elif instance.name == "test_agent":
return True, {"content": "The factorial of 5 is 120"}
return True, {"content": "5"}

mock_oai_reply.side_effect = mock_response

# Test code execution
response = agent._beam_reply("Calculate factorial of 5")

# Verify code was executed
assert "Factorial of 5 is 120" in agent._root.children[0].content
assert "Code Execution Result:" in agent._root.children[0].content
assert response == "The factorial of 5 is 120"


if __name__ == "__main__":
pytest.main([__file__])

0 comments on commit 5cc5de3

Please sign in to comment.