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_function_call_groupchat.py 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env python3 -m pytest
  2. import asyncio
  3. import os
  4. import sys
  5. import pytest
  6. from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST
  7. import autogen
  8. sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
  9. from conftest import reason, skip_openai
  10. func_def = {
  11. "name": "get_random_number",
  12. "description": "Get a random number between 0 and 100",
  13. "parameters": {
  14. "type": "object",
  15. "properties": {},
  16. },
  17. }
  18. @pytest.mark.skipif(
  19. skip_openai,
  20. reason=reason,
  21. )
  22. @pytest.mark.parametrize(
  23. "key, value, sync",
  24. [
  25. ("tools", [{"type": "function", "function": func_def}], False),
  26. ("functions", [func_def], True),
  27. ("tools", [{"type": "function", "function": func_def}], True),
  28. ],
  29. )
  30. @pytest.mark.asyncio
  31. async def test_function_call_groupchat(key, value, sync):
  32. import random
  33. class Function:
  34. call_count = 0
  35. def get_random_number(self):
  36. self.call_count += 1
  37. return random.randint(0, 100)
  38. # llm_config without functions
  39. config_list_35 = autogen.config_list_from_json(
  40. OAI_CONFIG_LIST,
  41. file_location=KEY_LOC,
  42. filter_dict={"tags": ["gpt-3.5-turbo", "gpt-3.5-turbo-16k"]},
  43. )
  44. llm_config_no_function = {"config_list": config_list_35}
  45. config_list_tool = autogen.filter_config(config_list_35, {"tags": ["tool"]})
  46. llm_config = {
  47. "config_list": config_list_tool,
  48. key: value,
  49. }
  50. func = Function()
  51. user_proxy = autogen.UserProxyAgent(
  52. name="Executor",
  53. description="An executor that executes function_calls.",
  54. function_map={"get_random_number": func.get_random_number},
  55. human_input_mode="NEVER",
  56. )
  57. player = autogen.AssistantAgent(
  58. name="Player",
  59. system_message="You will use function `get_random_number` to get a random number. Stop only when you get at least 1 even number and 1 odd number. Reply TERMINATE to stop.",
  60. description="A player that makes function_calls.",
  61. llm_config=llm_config,
  62. )
  63. observer = autogen.AssistantAgent(
  64. name="Observer",
  65. system_message="You observe the the player's actions and results. Summarize in 1 sentence.",
  66. description="An observer.",
  67. llm_config=llm_config_no_function,
  68. )
  69. groupchat = autogen.GroupChat(
  70. agents=[player, user_proxy, observer], messages=[], max_round=7, speaker_selection_method="round_robin"
  71. )
  72. # pass in llm_config with functions
  73. with pytest.raises(
  74. ValueError,
  75. match="GroupChatManager is not allowed to make function/tool calls. Please remove the 'functions' or 'tools' config in 'llm_config' you passed in.",
  76. ):
  77. manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)
  78. manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config_no_function)
  79. if sync:
  80. res = observer.initiate_chat(manager, message="Let's start the game!", summary_method="reflection_with_llm")
  81. else:
  82. res = await observer.a_initiate_chat(
  83. manager, message="Let's start the game!", summary_method="reflection_with_llm"
  84. )
  85. assert func.call_count >= 1, "The function get_random_number should be called at least once."
  86. print("Chat summary:", res.summary)
  87. print("Chat cost:", res.cost)
  88. def test_no_function_map():
  89. dummy1 = autogen.UserProxyAgent(
  90. name="User_proxy",
  91. system_message="A human admin that will execute function_calls.",
  92. human_input_mode="NEVER",
  93. )
  94. dummy2 = autogen.UserProxyAgent(
  95. name="User_proxy",
  96. system_message="A human admin that will execute function_calls.",
  97. human_input_mode="NEVER",
  98. )
  99. groupchat = autogen.GroupChat(agents=[dummy1, dummy2], messages=[], max_round=7)
  100. groupchat.messages = [
  101. {
  102. "role": "assistant",
  103. "content": None,
  104. "function_call": {"name": "get_random_number", "arguments": "{}"},
  105. }
  106. ]
  107. with pytest.raises(
  108. ValueError,
  109. match="No agent can execute the function get_random_number. Please check the function_map of the agents.",
  110. ):
  111. groupchat._prepare_and_select_agents(dummy2)
  112. if __name__ == "__main__":
  113. asyncio.run(test_function_call_groupchat("functions", [func_def], True))
  114. # test_no_function_map()