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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import autogen
  2. import pytest
  3. import sys
  4. from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST
  5. try:
  6. from openai import OpenAI
  7. except ImportError:
  8. skip = True
  9. else:
  10. skip = False
  11. @pytest.mark.skipif(
  12. skip or not sys.version.startswith("3.10"),
  13. reason="do not run if openai is not installed or py!=3.10",
  14. )
  15. def test_function_call_groupchat():
  16. import random
  17. def get_random_number():
  18. return random.randint(0, 100)
  19. config_list_gpt4 = autogen.config_list_from_json(
  20. OAI_CONFIG_LIST,
  21. filter_dict={
  22. "model": ["gpt-4", "gpt-4-0314", "gpt4", "gpt-4-32k", "gpt-4-32k-0314", "gpt-4-32k-v0314"],
  23. },
  24. file_location=KEY_LOC,
  25. )
  26. llm_config = {
  27. "config_list": config_list_gpt4,
  28. "cache_seed": 42,
  29. "functions": [
  30. {
  31. "name": "get_random_number",
  32. "description": "Get a random number between 0 and 100",
  33. "parameters": {
  34. "type": "object",
  35. "properties": {},
  36. },
  37. },
  38. ],
  39. }
  40. user_proxy = autogen.UserProxyAgent(
  41. name="User_proxy",
  42. system_message="A human admin that will execute function_calls.",
  43. function_map={"get_random_number": get_random_number},
  44. human_input_mode="NEVER",
  45. )
  46. coder = autogen.AssistantAgent(
  47. name="Player",
  48. 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.",
  49. llm_config=llm_config,
  50. )
  51. groupchat = autogen.GroupChat(agents=[user_proxy, coder], messages=[], max_round=7)
  52. # pass in llm_config with functions
  53. with pytest.raises(
  54. ValueError,
  55. match="GroupChatManager is not allowed to make function/tool calls. Please remove the 'functions' or 'tools' config in 'llm_config' you passed in.",
  56. ):
  57. manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)
  58. # pass in llm_config without functions
  59. llm_config_manager = llm_config.copy()
  60. del llm_config_manager["functions"]
  61. manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config_manager)
  62. user_proxy.initiate_chat(manager, message="Let's start the game!")
  63. if __name__ == "__main__":
  64. test_function_call_groupchat()