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 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import autogen
  2. import pytest
  3. import sys
  4. import os
  5. from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST
  6. sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
  7. from conftest import skip_openai # noqa: E402
  8. try:
  9. from openai import OpenAI
  10. except ImportError:
  11. skip = True
  12. else:
  13. skip = False or skip_openai
  14. @pytest.mark.skipif(
  15. skip or not sys.version.startswith("3.10"),
  16. reason="do not run if openai is not installed or py!=3.10",
  17. )
  18. def test_function_call_groupchat():
  19. import random
  20. def get_random_number():
  21. return random.randint(0, 100)
  22. config_list_gpt4 = autogen.config_list_from_json(
  23. OAI_CONFIG_LIST,
  24. filter_dict={
  25. "model": ["gpt-4", "gpt-4-0314", "gpt4", "gpt-4-32k", "gpt-4-32k-0314", "gpt-4-32k-v0314"],
  26. },
  27. file_location=KEY_LOC,
  28. )
  29. llm_config = {
  30. "config_list": config_list_gpt4,
  31. "cache_seed": 42,
  32. "functions": [
  33. {
  34. "name": "get_random_number",
  35. "description": "Get a random number between 0 and 100",
  36. "parameters": {
  37. "type": "object",
  38. "properties": {},
  39. },
  40. },
  41. ],
  42. }
  43. user_proxy = autogen.UserProxyAgent(
  44. name="User_proxy",
  45. system_message="A human admin that will execute function_calls.",
  46. function_map={"get_random_number": get_random_number},
  47. human_input_mode="NEVER",
  48. )
  49. coder = autogen.AssistantAgent(
  50. name="Player",
  51. system_message="You will can 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.",
  52. llm_config=llm_config,
  53. )
  54. groupchat = autogen.GroupChat(agents=[user_proxy, coder], messages=[], max_round=7)
  55. # pass in llm_config with functions
  56. with pytest.raises(
  57. ValueError,
  58. match="GroupChatManager is not allowed to make function/tool calls. Please remove the 'functions' or 'tools' config in 'llm_config' you passed in.",
  59. ):
  60. manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)
  61. # pass in llm_config without functions
  62. llm_config_manager = llm_config.copy()
  63. del llm_config_manager["functions"]
  64. manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config_manager)
  65. user_proxy.initiate_chat(manager, message="Let's start the game!")
  66. def test_no_function_map():
  67. dummy1 = autogen.UserProxyAgent(
  68. name="User_proxy",
  69. system_message="A human admin that will execute function_calls.",
  70. human_input_mode="NEVER",
  71. )
  72. dummy2 = autogen.UserProxyAgent(
  73. name="User_proxy",
  74. system_message="A human admin that will execute function_calls.",
  75. human_input_mode="NEVER",
  76. )
  77. groupchat = autogen.GroupChat(agents=[dummy1, dummy2], messages=[], max_round=7)
  78. groupchat.messages = [
  79. {
  80. "role": "assistant",
  81. "content": None,
  82. "function_call": {"name": "get_random_number", "arguments": "{}"},
  83. }
  84. ]
  85. with pytest.raises(
  86. ValueError,
  87. match="No agent can execute the function get_random_number. Please check the function_map of the agents.",
  88. ):
  89. groupchat._prepare_and_select_agents(dummy2)
  90. if __name__ == "__main__":
  91. test_function_call_groupchat()
  92. test_no_function_map()