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

2 years ago
raise error when msg is invalid; fix docstr; improve ResponsiveAgent; update doc and packaging; capture ipython output; find code blocks with llm when regex fails. (#1154) * autogen.agent -> autogen.agentchat * bug fix in portfolio * notebook * timeout * timeout * infer lang; close #1150 * timeout * message context * context handling * add sender to generate_reply * clean up the receive function * move mathchat to contrib * contrib * last_message * Add OptiGuide: agent and notebook * Optiguide notebook: add figures and URL 1. figures and code points to remote URL 2. simplify the prompt for the interpreter, because all information is already in the chat history. * Update name: Agent -> GenericAgent * Update notebook * Rename: GenericAgent -> ResponsiveAgent * Rebase to autogen.agentchat * OptiGuide: Comment, sytle, and notebook updates * simplify optiguide * raise error when msg is invalid; fix docstr * allow return None for generate_reply() * update_system_message * test update_system_message * simplify optiguide * simplify optiguide * simplify optiguide * simplify optiguide * move test * add test and fix bug * doc update * doc update * doc update * color * optiguide * prompt * test danger case * packaging * docker * remove path in traceback * capture ipython output * simplify * find code blocks with llm * find code with llm * order * order * fix bug in context handling * print executing msg * print executing msg * test find code * test find code * disable find_code * default_auto_reply * default auto reply * remove optiguide * remove -e --------- Co-authored-by: Beibin Li <beibin79@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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. try:
  2. from openai import OpenAI
  3. except ImportError:
  4. OpenAI = None
  5. import pytest
  6. import asyncio
  7. import json
  8. import autogen
  9. from autogen.math_utils import eval_math_responses
  10. from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST
  11. import sys
  12. @pytest.mark.skipif(OpenAI is None, reason="openai>=1 not installed")
  13. def test_eval_math_responses():
  14. config_list = autogen.config_list_from_models(
  15. KEY_LOC, exclude="aoai", model_list=["gpt-4-0613", "gpt-3.5-turbo-0613", "gpt-3.5-turbo-16k"]
  16. )
  17. functions = [
  18. {
  19. "name": "eval_math_responses",
  20. "description": "Select a response for a math problem using voting, and check if the response is correct if the solution is provided",
  21. "parameters": {
  22. "type": "object",
  23. "properties": {
  24. "responses": {
  25. "type": "array",
  26. "items": {"type": "string"},
  27. "description": "The responses in a list",
  28. },
  29. "solution": {
  30. "type": "string",
  31. "description": "The canonical solution",
  32. },
  33. },
  34. "required": ["responses"],
  35. },
  36. },
  37. ]
  38. client = autogen.OpenAIWrapper(config_list=config_list)
  39. response = client.create(
  40. messages=[
  41. {
  42. "role": "user",
  43. "content": 'evaluate the math responses ["1", "5/2", "5/2"] against the true answer \\frac{5}{2}',
  44. },
  45. ],
  46. functions=functions,
  47. )
  48. print(response)
  49. responses = client.extract_text_or_completion_object(response)
  50. print(responses[0])
  51. function_call = responses[0].function_call
  52. name, arguments = function_call.name, json.loads(function_call.arguments)
  53. assert name == "eval_math_responses"
  54. print(arguments["responses"])
  55. # if isinstance(arguments["responses"], str):
  56. # arguments["responses"] = json.loads(arguments["responses"])
  57. arguments["responses"] = [f"\\boxed{{{x}}}" for x in arguments["responses"]]
  58. print(arguments["responses"])
  59. arguments["solution"] = f"\\boxed{{{arguments['solution']}}}"
  60. print(eval_math_responses(**arguments))
  61. def test_json_extraction():
  62. from autogen.agentchat import UserProxyAgent
  63. user = UserProxyAgent(name="test", code_execution_config={"use_docker": False})
  64. jstr = '{\n"location": "Boston, MA"\n}'
  65. assert user._format_json_str(jstr) == '{"location": "Boston, MA"}'
  66. jstr = '{\n"code": "python",\n"query": "x=3\nprint(x)"}'
  67. assert user._format_json_str(jstr) == '{"code": "python","query": "x=3\\nprint(x)"}'
  68. jstr = '{"code": "a=\\"hello\\""}'
  69. assert user._format_json_str(jstr) == '{"code": "a=\\"hello\\""}'
  70. def test_execute_function():
  71. from autogen.agentchat import UserProxyAgent
  72. # 1. test calling a simple function
  73. def add_num(num_to_be_added):
  74. given_num = 10
  75. return num_to_be_added + given_num
  76. user = UserProxyAgent(name="test", function_map={"add_num": add_num})
  77. # correct execution
  78. correct_args = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'}
  79. assert user.execute_function(func_call=correct_args)[1]["content"] == "15"
  80. # function name called is wrong or doesn't exist
  81. wrong_func_name = {"name": "subtract_num", "arguments": '{ "num_to_be_added": 5 }'}
  82. assert "Error: Function" in user.execute_function(func_call=wrong_func_name)[1]["content"]
  83. # arguments passed is not in correct json format
  84. wrong_json_format = {
  85. "name": "add_num",
  86. "arguments": '{ "num_to_be_added": 5, given_num: 10 }',
  87. } # should be "given_num" with quotes
  88. assert "You argument should follow json format." in user.execute_function(func_call=wrong_json_format)[1]["content"]
  89. # function execution error with wrong arguments passed
  90. wrong_args = {"name": "add_num", "arguments": '{ "num_to_be_added": 5, "given_num": 10 }'}
  91. assert "Error: " in user.execute_function(func_call=wrong_args)[1]["content"]
  92. # 2. test calling a class method
  93. class AddNum:
  94. def __init__(self, given_num):
  95. self.given_num = given_num
  96. def add(self, num_to_be_added):
  97. self.given_num = num_to_be_added + self.given_num
  98. return self.given_num
  99. user = UserProxyAgent(name="test", function_map={"add_num": AddNum(given_num=10).add})
  100. func_call = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'}
  101. assert user.execute_function(func_call=func_call)[1]["content"] == "15"
  102. assert user.execute_function(func_call=func_call)[1]["content"] == "20"
  103. # 3. test calling a function with no arguments
  104. def get_number():
  105. return 42
  106. user = UserProxyAgent("user", function_map={"get_number": get_number})
  107. func_call = {"name": "get_number", "arguments": "{}"}
  108. assert user.execute_function(func_call)[1]["content"] == "42"
  109. @pytest.mark.asyncio
  110. async def test_a_execute_function():
  111. from autogen.agentchat import UserProxyAgent
  112. import time
  113. # Create an async function
  114. async def add_num(num_to_be_added):
  115. given_num = 10
  116. time.sleep(1)
  117. return num_to_be_added + given_num
  118. user = UserProxyAgent(name="test", function_map={"add_num": add_num})
  119. correct_args = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'}
  120. # Asset coroutine doesn't match.
  121. assert user.execute_function(func_call=correct_args)[1]["content"] != "15"
  122. # Asset awaited coroutine does match.
  123. assert (await user.a_execute_function(func_call=correct_args))[1]["content"] == "15"
  124. # function name called is wrong or doesn't exist
  125. wrong_func_name = {"name": "subtract_num", "arguments": '{ "num_to_be_added": 5 }'}
  126. assert "Error: Function" in (await user.a_execute_function(func_call=wrong_func_name))[1]["content"]
  127. # arguments passed is not in correct json format
  128. wrong_json_format = {
  129. "name": "add_num",
  130. "arguments": '{ "num_to_be_added": 5, given_num: 10 }',
  131. } # should be "given_num" with quotes
  132. assert (
  133. "You argument should follow json format."
  134. in (await user.a_execute_function(func_call=wrong_json_format))[1]["content"]
  135. )
  136. # function execution error with wrong arguments passed
  137. wrong_args = {"name": "add_num", "arguments": '{ "num_to_be_added": 5, "given_num": 10 }'}
  138. assert "Error: " in (await user.a_execute_function(func_call=wrong_args))[1]["content"]
  139. # 2. test calling a class method
  140. class AddNum:
  141. def __init__(self, given_num):
  142. self.given_num = given_num
  143. def add(self, num_to_be_added):
  144. self.given_num = num_to_be_added + self.given_num
  145. return self.given_num
  146. user = UserProxyAgent(name="test", function_map={"add_num": AddNum(given_num=10).add})
  147. func_call = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'}
  148. assert (await user.a_execute_function(func_call=func_call))[1]["content"] == "15"
  149. assert (await user.a_execute_function(func_call=func_call))[1]["content"] == "20"
  150. # 3. test calling a function with no arguments
  151. def get_number():
  152. return 42
  153. user = UserProxyAgent("user", function_map={"get_number": get_number})
  154. func_call = {"name": "get_number", "arguments": "{}"}
  155. assert (await user.a_execute_function(func_call))[1]["content"] == "42"
  156. @pytest.mark.skipif(
  157. not OpenAI or not sys.version.startswith("3.10"),
  158. reason="do not run if openai is not installed or py!=3.10",
  159. )
  160. def test_update_function():
  161. config_list_gpt4 = autogen.config_list_from_json(
  162. OAI_CONFIG_LIST,
  163. filter_dict={
  164. "model": ["gpt-4", "gpt-4-0314", "gpt4", "gpt-4-32k", "gpt-4-32k-0314", "gpt-4-32k-v0314"],
  165. },
  166. file_location=KEY_LOC,
  167. )
  168. llm_config = {
  169. "config_list": config_list_gpt4,
  170. "seed": 42,
  171. "functions": [],
  172. }
  173. user_proxy = autogen.UserProxyAgent(
  174. name="user_proxy",
  175. human_input_mode="NEVER",
  176. is_termination_msg=lambda x: True if "TERMINATE" in x.get("content") else False,
  177. )
  178. assistant = autogen.AssistantAgent(name="test", llm_config=llm_config)
  179. # Define a new function *after* the assistant has been created
  180. assistant.update_function_signature(
  181. {
  182. "name": "greet_user",
  183. "description": "Greets the user.",
  184. "parameters": {
  185. "type": "object",
  186. "properties": {},
  187. "required": [],
  188. },
  189. },
  190. is_remove=False,
  191. )
  192. user_proxy.initiate_chat(
  193. assistant,
  194. message="What functions do you know about in the context of this conversation? End your response with 'TERMINATE'.",
  195. )
  196. messages1 = assistant.chat_messages[user_proxy][-1]["content"]
  197. print(messages1)
  198. assistant.update_function_signature("greet_user", is_remove=True)
  199. user_proxy.initiate_chat(
  200. assistant,
  201. message="What functions do you know about in the context of this conversation? End your response with 'TERMINATE'.",
  202. )
  203. messages2 = assistant.chat_messages[user_proxy][-1]["content"]
  204. print(messages2)
  205. # The model should know about the function in the context of the conversation
  206. assert "greet_user" in messages1
  207. assert "greet_user" not in messages2
  208. if __name__ == "__main__":
  209. # test_json_extraction()
  210. # test_execute_function()
  211. test_update_function()
  212. asyncio.run(test_a_execute_function())
  213. test_eval_math_responses()