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.5 kB

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