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

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