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_async_get_human_input.py 2.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env python3 -m pytest
  2. import asyncio
  3. import os
  4. import sys
  5. from unittest.mock import AsyncMock
  6. import autogen
  7. import pytest
  8. from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST
  9. sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
  10. from conftest import skip_openai # noqa: E402
  11. try:
  12. from openai import OpenAI
  13. except ImportError:
  14. skip = True
  15. else:
  16. skip = False or skip_openai
  17. @pytest.mark.skipif(skip, reason="openai not installed OR requested to skip")
  18. @pytest.mark.asyncio
  19. async def test_async_get_human_input():
  20. config_list = autogen.config_list_from_json(OAI_CONFIG_LIST, KEY_LOC)
  21. # create an AssistantAgent instance named "assistant"
  22. assistant = autogen.AssistantAgent(
  23. name="assistant",
  24. max_consecutive_auto_reply=2,
  25. llm_config={"seed": 41, "config_list": config_list, "temperature": 0},
  26. )
  27. user_proxy = autogen.UserProxyAgent(name="user", human_input_mode="ALWAYS", code_execution_config=False)
  28. user_proxy.a_get_human_input = AsyncMock(return_value="This is a test")
  29. user_proxy.register_reply([autogen.Agent, None], autogen.ConversableAgent.a_check_termination_and_human_reply)
  30. await user_proxy.a_initiate_chat(assistant, clear_history=True, message="Hello.")
  31. # Test without message
  32. res = await user_proxy.a_initiate_chat(assistant, clear_history=True, summary_method="reflection_with_llm")
  33. # Assert that custom a_get_human_input was called at least once
  34. user_proxy.a_get_human_input.assert_called()
  35. print("Result summary:", res.summary)
  36. print("Human input:", res.human_input)
  37. @pytest.mark.skipif(skip, reason="openai not installed OR requested to skip")
  38. @pytest.mark.asyncio
  39. async def test_async_max_turn():
  40. config_list = autogen.config_list_from_json(OAI_CONFIG_LIST, KEY_LOC)
  41. # create an AssistantAgent instance named "assistant"
  42. assistant = autogen.AssistantAgent(
  43. name="assistant",
  44. max_consecutive_auto_reply=10,
  45. llm_config={
  46. "seed": 41,
  47. "config_list": config_list,
  48. },
  49. )
  50. user_proxy = autogen.UserProxyAgent(name="user", human_input_mode="ALWAYS", code_execution_config=False)
  51. user_proxy.a_get_human_input = AsyncMock(return_value="Not funny. Try again.")
  52. res = await user_proxy.a_initiate_chat(
  53. assistant, clear_history=True, max_turns=3, message="Hello, make a joke about AI."
  54. )
  55. print("Result summary:", res.summary)
  56. print("Human input:", res.human_input)
  57. print("chat history:", res.chat_history)
  58. assert (
  59. len(res.chat_history) == 6
  60. ), f"Chat history should have 6 messages because max_turns is set to 3 (and user keep request try again) but has {len(res.chat_history)}."
  61. if __name__ == "__main__":
  62. asyncio.run(test_async_get_human_input())
  63. asyncio.run(test_async_max_turn())