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.py 12 kB

2 years ago
Support function_call in `autogen/agent` (#1091) * update funccall * code format * update to comments * update notebook * remove test for py3.7 * allow funccall to class functions * add test and clean up notebook * revise notebook and test * update * update mathagent * Update flaml/autogen/agent/agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * revise to comments * revise function call design, notebook and test. add doc * code format * ad message_to_dict function * update mathproxyagent * revise docstr * update * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * simply funccall in userproxyagent, rewind auto-gen.md, revise to comments * code format * update * remove notebook for another pr * revise oai_conversation part in agent, revise function exec in user_proxy_agent * update test_funccall * update * update * fix pydantic version * Update test/autogen/test_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * fix bug * fix bug * update * update is_termination_msg to accept dict --------- Co-authored-by: Chi Wang <wang.chi@microsoft.com> Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> Co-authored-by: Li Jiang <bnujli@gmail.com>
3 years ago
2 years ago
Support function_call in `autogen/agent` (#1091) * update funccall * code format * update to comments * update notebook * remove test for py3.7 * allow funccall to class functions * add test and clean up notebook * revise notebook and test * update * update mathagent * Update flaml/autogen/agent/agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * revise to comments * revise function call design, notebook and test. add doc * code format * ad message_to_dict function * update mathproxyagent * revise docstr * update * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * simply funccall in userproxyagent, rewind auto-gen.md, revise to comments * code format * update * remove notebook for another pr * revise oai_conversation part in agent, revise function exec in user_proxy_agent * update test_funccall * update * update * fix pydantic version * Update test/autogen/test_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * fix bug * fix bug * update * update is_termination_msg to accept dict --------- Co-authored-by: Chi Wang <wang.chi@microsoft.com> Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> Co-authored-by: Li Jiang <bnujli@gmail.com>
3 years ago
Support function_call in `autogen/agent` (#1091) * update funccall * code format * update to comments * update notebook * remove test for py3.7 * allow funccall to class functions * add test and clean up notebook * revise notebook and test * update * update mathagent * Update flaml/autogen/agent/agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * revise to comments * revise function call design, notebook and test. add doc * code format * ad message_to_dict function * update mathproxyagent * revise docstr * update * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * simply funccall in userproxyagent, rewind auto-gen.md, revise to comments * code format * update * remove notebook for another pr * revise oai_conversation part in agent, revise function exec in user_proxy_agent * update test_funccall * update * update * fix pydantic version * Update test/autogen/test_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * fix bug * fix bug * update * update is_termination_msg to accept dict --------- Co-authored-by: Chi Wang <wang.chi@microsoft.com> Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> Co-authored-by: Li Jiang <bnujli@gmail.com>
3 years ago
Support function_call in `autogen/agent` (#1091) * update funccall * code format * update to comments * update notebook * remove test for py3.7 * allow funccall to class functions * add test and clean up notebook * revise notebook and test * update * update mathagent * Update flaml/autogen/agent/agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * revise to comments * revise function call design, notebook and test. add doc * code format * ad message_to_dict function * update mathproxyagent * revise docstr * update * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * simply funccall in userproxyagent, rewind auto-gen.md, revise to comments * code format * update * remove notebook for another pr * revise oai_conversation part in agent, revise function exec in user_proxy_agent * update test_funccall * update * update * fix pydantic version * Update test/autogen/test_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * fix bug * fix bug * update * update is_termination_msg to accept dict --------- Co-authored-by: Chi Wang <wang.chi@microsoft.com> Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> Co-authored-by: Li Jiang <bnujli@gmail.com>
3 years ago
2 years ago
Support function_call in `autogen/agent` (#1091) * update funccall * code format * update to comments * update notebook * remove test for py3.7 * allow funccall to class functions * add test and clean up notebook * revise notebook and test * update * update mathagent * Update flaml/autogen/agent/agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * revise to comments * revise function call design, notebook and test. add doc * code format * ad message_to_dict function * update mathproxyagent * revise docstr * update * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * simply funccall in userproxyagent, rewind auto-gen.md, revise to comments * code format * update * remove notebook for another pr * revise oai_conversation part in agent, revise function exec in user_proxy_agent * update test_funccall * update * update * fix pydantic version * Update test/autogen/test_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * fix bug * fix bug * update * update is_termination_msg to accept dict --------- Co-authored-by: Chi Wang <wang.chi@microsoft.com> Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> Co-authored-by: Li Jiang <bnujli@gmail.com>
3 years ago
Support function_call in `autogen/agent` (#1091) * update funccall * code format * update to comments * update notebook * remove test for py3.7 * allow funccall to class functions * add test and clean up notebook * revise notebook and test * update * update mathagent * Update flaml/autogen/agent/agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * revise to comments * revise function call design, notebook and test. add doc * code format * ad message_to_dict function * update mathproxyagent * revise docstr * update * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * simply funccall in userproxyagent, rewind auto-gen.md, revise to comments * code format * update * remove notebook for another pr * revise oai_conversation part in agent, revise function exec in user_proxy_agent * update test_funccall * update * update * fix pydantic version * Update test/autogen/test_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * fix bug * fix bug * update * update is_termination_msg to accept dict --------- Co-authored-by: Chi Wang <wang.chi@microsoft.com> Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> Co-authored-by: Li Jiang <bnujli@gmail.com>
3 years ago
Support function_call in `autogen/agent` (#1091) * update funccall * code format * update to comments * update notebook * remove test for py3.7 * allow funccall to class functions * add test and clean up notebook * revise notebook and test * update * update mathagent * Update flaml/autogen/agent/agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * revise to comments * revise function call design, notebook and test. add doc * code format * ad message_to_dict function * update mathproxyagent * revise docstr * update * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * simply funccall in userproxyagent, rewind auto-gen.md, revise to comments * code format * update * remove notebook for another pr * revise oai_conversation part in agent, revise function exec in user_proxy_agent * update test_funccall * update * update * fix pydantic version * Update test/autogen/test_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * fix bug * fix bug * update * update is_termination_msg to accept dict --------- Co-authored-by: Chi Wang <wang.chi@microsoft.com> Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> Co-authored-by: Li Jiang <bnujli@gmail.com>
3 years ago
Support function_call in `autogen/agent` (#1091) * update funccall * code format * update to comments * update notebook * remove test for py3.7 * allow funccall to class functions * add test and clean up notebook * revise notebook and test * update * update mathagent * Update flaml/autogen/agent/agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * revise to comments * revise function call design, notebook and test. add doc * code format * ad message_to_dict function * update mathproxyagent * revise docstr * update * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * simply funccall in userproxyagent, rewind auto-gen.md, revise to comments * code format * update * remove notebook for another pr * revise oai_conversation part in agent, revise function exec in user_proxy_agent * update test_funccall * update * update * fix pydantic version * Update test/autogen/test_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * fix bug * fix bug * update * update is_termination_msg to accept dict --------- Co-authored-by: Chi Wang <wang.chi@microsoft.com> Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> Co-authored-by: Li Jiang <bnujli@gmail.com>
3 years ago
Support function_call in `autogen/agent` (#1091) * update funccall * code format * update to comments * update notebook * remove test for py3.7 * allow funccall to class functions * add test and clean up notebook * revise notebook and test * update * update mathagent * Update flaml/autogen/agent/agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * revise to comments * revise function call design, notebook and test. add doc * code format * ad message_to_dict function * update mathproxyagent * revise docstr * update * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * simply funccall in userproxyagent, rewind auto-gen.md, revise to comments * code format * update * remove notebook for another pr * revise oai_conversation part in agent, revise function exec in user_proxy_agent * update test_funccall * update * update * fix pydantic version * Update test/autogen/test_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * fix bug * fix bug * update * update is_termination_msg to accept dict --------- Co-authored-by: Chi Wang <wang.chi@microsoft.com> Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> Co-authored-by: Li Jiang <bnujli@gmail.com>
3 years ago
Support function_call in `autogen/agent` (#1091) * update funccall * code format * update to comments * update notebook * remove test for py3.7 * allow funccall to class functions * add test and clean up notebook * revise notebook and test * update * update mathagent * Update flaml/autogen/agent/agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * revise to comments * revise function call design, notebook and test. add doc * code format * ad message_to_dict function * update mathproxyagent * revise docstr * update * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * simply funccall in userproxyagent, rewind auto-gen.md, revise to comments * code format * update * remove notebook for another pr * revise oai_conversation part in agent, revise function exec in user_proxy_agent * update test_funccall * update * update * fix pydantic version * Update test/autogen/test_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * fix bug * fix bug * update * update is_termination_msg to accept dict --------- Co-authored-by: Chi Wang <wang.chi@microsoft.com> Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> Co-authored-by: Li Jiang <bnujli@gmail.com>
3 years ago
Support function_call in `autogen/agent` (#1091) * update funccall * code format * update to comments * update notebook * remove test for py3.7 * allow funccall to class functions * add test and clean up notebook * revise notebook and test * update * update mathagent * Update flaml/autogen/agent/agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * revise to comments * revise function call design, notebook and test. add doc * code format * ad message_to_dict function * update mathproxyagent * revise docstr * update * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * simply funccall in userproxyagent, rewind auto-gen.md, revise to comments * code format * update * remove notebook for another pr * revise oai_conversation part in agent, revise function exec in user_proxy_agent * update test_funccall * update * update * fix pydantic version * Update test/autogen/test_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * fix bug * fix bug * update * update is_termination_msg to accept dict --------- Co-authored-by: Chi Wang <wang.chi@microsoft.com> Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> Co-authored-by: Li Jiang <bnujli@gmail.com>
3 years ago
Support function_call in `autogen/agent` (#1091) * update funccall * code format * update to comments * update notebook * remove test for py3.7 * allow funccall to class functions * add test and clean up notebook * revise notebook and test * update * update mathagent * Update flaml/autogen/agent/agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * revise to comments * revise function call design, notebook and test. add doc * code format * ad message_to_dict function * update mathproxyagent * revise docstr * update * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/autogen/agent/math_user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * Update flaml/autogen/agent/user_proxy_agent.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> * simply funccall in userproxyagent, rewind auto-gen.md, revise to comments * code format * update * remove notebook for another pr * revise oai_conversation part in agent, revise function exec in user_proxy_agent * update test_funccall * update * update * fix pydantic version * Update test/autogen/test_agent.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * fix bug * fix bug * update * update is_termination_msg to accept dict --------- Co-authored-by: Chi Wang <wang.chi@microsoft.com> Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> Co-authored-by: Li Jiang <bnujli@gmail.com>
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #!/usr/bin/env python3 -m pytest
  2. import asyncio
  3. import json
  4. import os
  5. import sys
  6. import pytest
  7. from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST
  8. import autogen
  9. from autogen.math_utils import eval_math_responses
  10. sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
  11. from conftest import skip_openai # noqa: E402
  12. try:
  13. from openai import OpenAI
  14. except ImportError:
  15. skip = True
  16. else:
  17. skip = False or skip_openai
  18. @pytest.mark.skipif(skip, reason="openai not installed OR requested to skip")
  19. def test_eval_math_responses():
  20. config_list = autogen.config_list_from_json(
  21. OAI_CONFIG_LIST,
  22. filter_dict={
  23. "tags": ["gpt-4", "gpt-3.5-turbo", "gpt-3.5-turbo-16k"],
  24. },
  25. file_location=KEY_LOC,
  26. )
  27. functions = [
  28. {
  29. "name": "eval_math_responses",
  30. "description": "Select a response for a math problem using voting, and check if the response is correct if the solution is provided",
  31. "parameters": {
  32. "type": "object",
  33. "properties": {
  34. "responses": {
  35. "type": "array",
  36. "items": {"type": "string"},
  37. "description": "The responses in a list",
  38. },
  39. "solution": {
  40. "type": "string",
  41. "description": "The canonical solution",
  42. },
  43. },
  44. "required": ["responses"],
  45. },
  46. },
  47. ]
  48. client = autogen.OpenAIWrapper(config_list=config_list)
  49. response = client.create(
  50. messages=[
  51. {
  52. "role": "user",
  53. "content": 'evaluate the math responses ["1", "5/2", "5/2"] against the true answer \\frac{5}{2}',
  54. },
  55. ],
  56. functions=functions,
  57. )
  58. print(response)
  59. responses = client.extract_text_or_completion_object(response)
  60. print(responses[0])
  61. function_call = responses[0].function_call
  62. name, arguments = function_call.name, json.loads(function_call.arguments)
  63. assert name == "eval_math_responses"
  64. print(arguments["responses"])
  65. # if isinstance(arguments["responses"], str):
  66. # arguments["responses"] = json.loads(arguments["responses"])
  67. arguments["responses"] = [f"\\boxed{{{x}}}" for x in arguments["responses"]]
  68. print(arguments["responses"])
  69. arguments["solution"] = f"\\boxed{{{arguments['solution']}}}"
  70. print(eval_math_responses(**arguments))
  71. def test_json_extraction():
  72. from autogen.agentchat import UserProxyAgent
  73. user = UserProxyAgent(name="test", code_execution_config={"use_docker": False})
  74. jstr = '{\n"location": "Boston, MA"\n}'
  75. assert user._format_json_str(jstr) == '{"location": "Boston, MA"}'
  76. jstr = '{\n"code": "python",\n"query": "x=3\nprint(x)"}'
  77. assert user._format_json_str(jstr) == '{"code": "python","query": "x=3\\nprint(x)"}'
  78. jstr = '{"code": "a=\\"hello\\""}'
  79. assert user._format_json_str(jstr) == '{"code": "a=\\"hello\\""}'
  80. jstr = '{\n"tool": "python",\n"query": "print(\'hello\')\n\tprint(\'world\')"\n}' # mixed newlines and tabs
  81. assert user._format_json_str(jstr) == '{"tool": "python","query": "print(\'hello\')\\n\\tprint(\'world\')"}'
  82. jstr = "{}" # empty json
  83. assert user._format_json_str(jstr) == "{}"
  84. def test_execute_function():
  85. from autogen.agentchat import UserProxyAgent
  86. # 1. test calling a simple function
  87. def add_num(num_to_be_added):
  88. given_num = 10
  89. return num_to_be_added + given_num
  90. user = UserProxyAgent(name="test", function_map={"add_num": add_num})
  91. # correct execution
  92. correct_args = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'}
  93. assert user.execute_function(func_call=correct_args)[1]["content"] == "15"
  94. # function name called is wrong or doesn't exist
  95. wrong_func_name = {"name": "subtract_num", "arguments": '{ "num_to_be_added": 5 }'}
  96. assert "Error: Function" in user.execute_function(func_call=wrong_func_name)[1]["content"]
  97. # arguments passed is not in correct json format
  98. wrong_json_format = {
  99. "name": "add_num",
  100. "arguments": '{ "num_to_be_added": 5, given_num: 10 }',
  101. } # should be "given_num" with quotes
  102. assert "The argument must be in JSON format." in user.execute_function(func_call=wrong_json_format)[1]["content"]
  103. # function execution error with extra arguments
  104. wrong_args = {"name": "add_num", "arguments": '{ "num_to_be_added": 5, "given_num": 10 }'}
  105. assert "Error: " in user.execute_function(func_call=wrong_args)[1]["content"]
  106. # 2. test calling a class method
  107. class AddNum:
  108. def __init__(self, given_num):
  109. self.given_num = given_num
  110. def add(self, num_to_be_added):
  111. self.given_num = num_to_be_added + self.given_num
  112. return self.given_num
  113. user = UserProxyAgent(name="test", function_map={"add_num": AddNum(given_num=10).add})
  114. func_call = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'}
  115. assert user.execute_function(func_call=func_call)[1]["content"] == "15"
  116. assert user.execute_function(func_call=func_call)[1]["content"] == "20"
  117. # 3. test calling a function with no arguments
  118. def get_number():
  119. return 42
  120. user = UserProxyAgent("user", function_map={"get_number": get_number})
  121. func_call = {"name": "get_number", "arguments": "{}"}
  122. assert user.execute_function(func_call)[1]["content"] == "42"
  123. # 4. test with a non-existent function
  124. user = UserProxyAgent(name="test", function_map={})
  125. func_call = {"name": "nonexistent_function", "arguments": "{}"}
  126. assert "Error: Function" in user.execute_function(func_call=func_call)[1]["content"]
  127. # 5. test calling a function that raises an exception
  128. def raise_exception():
  129. raise ValueError("This is an error")
  130. user = UserProxyAgent(name="test", function_map={"raise_exception": raise_exception})
  131. func_call = {"name": "raise_exception", "arguments": "{}"}
  132. assert "Error: " in user.execute_function(func_call=func_call)[1]["content"]
  133. @pytest.mark.asyncio
  134. async def test_a_execute_function():
  135. import time
  136. from autogen.agentchat import UserProxyAgent
  137. # Create an async function
  138. async def add_num(num_to_be_added):
  139. given_num = 10
  140. time.sleep(1)
  141. return num_to_be_added + given_num
  142. user = UserProxyAgent(name="test", function_map={"add_num": add_num})
  143. correct_args = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'}
  144. # Asset coroutine doesn't match.
  145. assert user.execute_function(func_call=correct_args)[1]["content"] != "15"
  146. # Asset awaited coroutine does match.
  147. assert (await user.a_execute_function(func_call=correct_args))[1]["content"] == "15"
  148. # function name called is wrong or doesn't exist
  149. wrong_func_name = {"name": "subtract_num", "arguments": '{ "num_to_be_added": 5 }'}
  150. assert "Error: Function" in (await user.a_execute_function(func_call=wrong_func_name))[1]["content"]
  151. # arguments passed is not in correct json format
  152. wrong_json_format = {
  153. "name": "add_num",
  154. "arguments": '{ "num_to_be_added": 5, given_num: 10 }',
  155. } # should be "given_num" with quotes
  156. assert (
  157. "The argument must be in JSON format."
  158. in (await user.a_execute_function(func_call=wrong_json_format))[1]["content"]
  159. )
  160. # function execution error with wrong arguments passed
  161. wrong_args = {"name": "add_num", "arguments": '{ "num_to_be_added": 5, "given_num": 10 }'}
  162. assert "Error: " in (await user.a_execute_function(func_call=wrong_args))[1]["content"]
  163. # 2. test calling a class method
  164. class AddNum:
  165. def __init__(self, given_num):
  166. self.given_num = given_num
  167. def add(self, num_to_be_added):
  168. self.given_num = num_to_be_added + self.given_num
  169. return self.given_num
  170. user = UserProxyAgent(name="test", function_map={"add_num": AddNum(given_num=10).add})
  171. func_call = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'}
  172. assert (await user.a_execute_function(func_call=func_call))[1]["content"] == "15"
  173. assert (await user.a_execute_function(func_call=func_call))[1]["content"] == "20"
  174. # 3. test calling a function with no arguments
  175. def get_number():
  176. return 42
  177. user = UserProxyAgent("user", function_map={"get_number": get_number})
  178. func_call = {"name": "get_number", "arguments": "{}"}
  179. assert (await user.a_execute_function(func_call))[1]["content"] == "42"
  180. @pytest.mark.skipif(
  181. skip or not sys.version.startswith("3.10"),
  182. reason="do not run if openai is not installed OR reeusted to skip OR py!=3.10",
  183. )
  184. def test_update_function():
  185. config_list_gpt4 = autogen.config_list_from_json(
  186. OAI_CONFIG_LIST,
  187. filter_dict={
  188. "tags": ["gpt-4", "gpt-4-32k", "gpt-4o", "gpt-4o-mini"],
  189. },
  190. file_location=KEY_LOC,
  191. )
  192. llm_config = {
  193. "config_list": config_list_gpt4,
  194. "seed": 42,
  195. "functions": [],
  196. }
  197. user_proxy = autogen.UserProxyAgent(
  198. name="user_proxy",
  199. human_input_mode="NEVER",
  200. is_termination_msg=lambda x: True if "TERMINATE" in x.get("content") else False,
  201. )
  202. assistant = autogen.AssistantAgent(name="test", llm_config=llm_config)
  203. # Define a new function *after* the assistant has been created
  204. assistant.update_function_signature(
  205. {
  206. "name": "greet_user",
  207. "description": "Greets the user.",
  208. "parameters": {
  209. "type": "object",
  210. "properties": {},
  211. "required": [],
  212. },
  213. },
  214. is_remove=False,
  215. )
  216. res1 = user_proxy.initiate_chat(
  217. assistant,
  218. message="What functions do you know about in the context of this conversation? End your response with 'TERMINATE'.",
  219. summary_method="reflection_with_llm",
  220. )
  221. messages1 = assistant.chat_messages[user_proxy][-1]["content"]
  222. print(messages1)
  223. print("Chat summary and cost", res1.summary, res1.cost)
  224. assistant.update_function_signature("greet_user", is_remove=True)
  225. res2 = user_proxy.initiate_chat(
  226. assistant,
  227. message="What functions do you know about in the context of this conversation? End your response with 'TERMINATE'.",
  228. summary_method="reflection_with_llm",
  229. )
  230. messages2 = assistant.chat_messages[user_proxy][-1]["content"]
  231. print(messages2)
  232. # The model should know about the function in the context of the conversation
  233. assert "greet_user" in messages1
  234. assert "greet_user" not in messages2
  235. print("Chat summary and cost", res2.summary, res2.cost)
  236. with pytest.raises(
  237. AssertionError,
  238. match="summary_method must be a string chosen from 'reflection_with_llm' or 'last_msg' or a callable, or None.",
  239. ):
  240. user_proxy.initiate_chat(
  241. assistant,
  242. message="What functions do you know about in the context of this conversation? End your response with 'TERMINATE'.",
  243. summary_method="llm",
  244. )
  245. with pytest.raises(
  246. AssertionError,
  247. match="llm client must be set in either the recipient or sender when summary_method is reflection_with_llm.",
  248. ):
  249. user_proxy.initiate_chat(
  250. recipient=user_proxy,
  251. message="What functions do you know about in the context of this conversation? End your response with 'TERMINATE'.",
  252. summary_method="reflection_with_llm",
  253. )
  254. if __name__ == "__main__":
  255. # test_json_extraction()
  256. # test_execute_function()
  257. test_update_function()
  258. # asyncio.run(test_a_execute_function())
  259. # test_eval_math_responses()