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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. config_list_gpt4 = autogen.config_list_from_json(
  39. OAI_CONFIG_LIST,
  40. filter_dict={
  41. "model": ["gpt-4", "gpt-4-0314", "gpt4", "gpt-4-32k", "gpt-4-32k-0314", "gpt-4-32k-v0314"],
  42. },
  43. file_location=KEY_LOC,
  44. )
  45. llm_config = {
  46. "config_list": config_list_gpt4,
  47. "cache_seed": 42,
  48. key: value,
  49. }
  50. # llm_config without functions
  51. llm_config_no_function = llm_config.copy()
  52. del llm_config_no_function[key]
  53. func = Function()
  54. user_proxy = autogen.UserProxyAgent(
  55. name="Executor",
  56. description="An executor that executes function_calls.",
  57. function_map={"get_random_number": func.get_random_number},
  58. human_input_mode="NEVER",
  59. )
  60. player = autogen.AssistantAgent(
  61. name="Player",
  62. 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.",
  63. description="A player that makes function_calls.",
  64. llm_config=llm_config,
  65. )
  66. observer = autogen.AssistantAgent(
  67. name="Observer",
  68. system_message="You observe the the player's actions and results. Summarize in 1 sentence.",
  69. description="An observer.",
  70. llm_config=llm_config_no_function,
  71. )
  72. groupchat = autogen.GroupChat(
  73. agents=[player, user_proxy, observer], messages=[], max_round=7, speaker_selection_method="round_robin"
  74. )
  75. # pass in llm_config with functions
  76. with pytest.raises(
  77. ValueError,
  78. match="GroupChatManager is not allowed to make function/tool calls. Please remove the 'functions' or 'tools' config in 'llm_config' you passed in.",
  79. ):
  80. manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)
  81. manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config_no_function)
  82. if sync:
  83. res = observer.initiate_chat(manager, message="Let's start the game!", summary_method="reflection_with_llm")
  84. else:
  85. res = await observer.a_initiate_chat(
  86. manager, message="Let's start the game!", summary_method="reflection_with_llm"
  87. )
  88. assert func.call_count >= 1, "The function get_random_number should be called at least once."
  89. print("Chat summary:", res.summary)
  90. print("Chat cost:", res.cost)
  91. def test_no_function_map():
  92. dummy1 = autogen.UserProxyAgent(
  93. name="User_proxy",
  94. system_message="A human admin that will execute function_calls.",
  95. human_input_mode="NEVER",
  96. )
  97. dummy2 = autogen.UserProxyAgent(
  98. name="User_proxy",
  99. system_message="A human admin that will execute function_calls.",
  100. human_input_mode="NEVER",
  101. )
  102. groupchat = autogen.GroupChat(agents=[dummy1, dummy2], messages=[], max_round=7)
  103. groupchat.messages = [
  104. {
  105. "role": "assistant",
  106. "content": None,
  107. "function_call": {"name": "get_random_number", "arguments": "{}"},
  108. }
  109. ]
  110. with pytest.raises(
  111. ValueError,
  112. match="No agent can execute the function get_random_number. Please check the function_map of the agents.",
  113. ):
  114. groupchat._prepare_and_select_agents(dummy2)
  115. if __name__ == "__main__":
  116. asyncio.run(test_function_call_groupchat("functions", [func_def], True))
  117. # test_no_function_map()