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 10 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import pytest
  2. import asyncio
  3. import json
  4. import autogen
  5. from autogen.math_utils import eval_math_responses
  6. from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST
  7. import sys
  8. import os
  9. sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
  10. from conftest import skip_openai # noqa: E402
  11. try:
  12. from openai import OpenAI
  13. except ImportError:
  14. skip = True
  15. else:
  16. skip = False or skip_openai
  17. @pytest.mark.skipif(skip, reason="openai not installed OR requested to skip")
  18. def test_eval_math_responses():
  19. config_list = autogen.config_list_from_models(
  20. KEY_LOC, model_list=["gpt-4-0613", "gpt-3.5-turbo-0613", "gpt-3.5-turbo-16k"]
  21. )
  22. functions = [
  23. {
  24. "name": "eval_math_responses",
  25. "description": "Select a response for a math problem using voting, and check if the response is correct if the solution is provided",
  26. "parameters": {
  27. "type": "object",
  28. "properties": {
  29. "responses": {
  30. "type": "array",
  31. "items": {"type": "string"},
  32. "description": "The responses in a list",
  33. },
  34. "solution": {
  35. "type": "string",
  36. "description": "The canonical solution",
  37. },
  38. },
  39. "required": ["responses"],
  40. },
  41. },
  42. ]
  43. client = autogen.OpenAIWrapper(config_list=config_list)
  44. response = client.create(
  45. messages=[
  46. {
  47. "role": "user",
  48. "content": 'evaluate the math responses ["1", "5/2", "5/2"] against the true answer \\frac{5}{2}',
  49. },
  50. ],
  51. functions=functions,
  52. )
  53. print(response)
  54. responses = client.extract_text_or_completion_object(response)
  55. print(responses[0])
  56. function_call = responses[0].function_call
  57. name, arguments = function_call.name, json.loads(function_call.arguments)
  58. assert name == "eval_math_responses"
  59. print(arguments["responses"])
  60. # if isinstance(arguments["responses"], str):
  61. # arguments["responses"] = json.loads(arguments["responses"])
  62. arguments["responses"] = [f"\\boxed{{{x}}}" for x in arguments["responses"]]
  63. print(arguments["responses"])
  64. arguments["solution"] = f"\\boxed{{{arguments['solution']}}}"
  65. print(eval_math_responses(**arguments))
  66. def test_json_extraction():
  67. from autogen.agentchat import UserProxyAgent
  68. user = UserProxyAgent(name="test", code_execution_config={"use_docker": False})
  69. jstr = '{\n"location": "Boston, MA"\n}'
  70. assert user._format_json_str(jstr) == '{"location": "Boston, MA"}'
  71. jstr = '{\n"code": "python",\n"query": "x=3\nprint(x)"}'
  72. assert user._format_json_str(jstr) == '{"code": "python","query": "x=3\\nprint(x)"}'
  73. jstr = '{"code": "a=\\"hello\\""}'
  74. assert user._format_json_str(jstr) == '{"code": "a=\\"hello\\""}'
  75. def test_execute_function():
  76. from autogen.agentchat import UserProxyAgent
  77. # 1. test calling a simple function
  78. def add_num(num_to_be_added):
  79. given_num = 10
  80. return num_to_be_added + given_num
  81. user = UserProxyAgent(name="test", function_map={"add_num": add_num})
  82. # correct execution
  83. correct_args = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'}
  84. assert user.execute_function(func_call=correct_args)[1]["content"] == "15"
  85. # function name called is wrong or doesn't exist
  86. wrong_func_name = {"name": "subtract_num", "arguments": '{ "num_to_be_added": 5 }'}
  87. assert "Error: Function" in user.execute_function(func_call=wrong_func_name)[1]["content"]
  88. # arguments passed is not in correct json format
  89. wrong_json_format = {
  90. "name": "add_num",
  91. "arguments": '{ "num_to_be_added": 5, given_num: 10 }',
  92. } # should be "given_num" with quotes
  93. assert "You argument should follow json format." in user.execute_function(func_call=wrong_json_format)[1]["content"]
  94. # function execution error with wrong arguments passed
  95. wrong_args = {"name": "add_num", "arguments": '{ "num_to_be_added": 5, "given_num": 10 }'}
  96. assert "Error: " in user.execute_function(func_call=wrong_args)[1]["content"]
  97. # 2. test calling a class method
  98. class AddNum:
  99. def __init__(self, given_num):
  100. self.given_num = given_num
  101. def add(self, num_to_be_added):
  102. self.given_num = num_to_be_added + self.given_num
  103. return self.given_num
  104. user = UserProxyAgent(name="test", function_map={"add_num": AddNum(given_num=10).add})
  105. func_call = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'}
  106. assert user.execute_function(func_call=func_call)[1]["content"] == "15"
  107. assert user.execute_function(func_call=func_call)[1]["content"] == "20"
  108. # 3. test calling a function with no arguments
  109. def get_number():
  110. return 42
  111. user = UserProxyAgent("user", function_map={"get_number": get_number})
  112. func_call = {"name": "get_number", "arguments": "{}"}
  113. assert user.execute_function(func_call)[1]["content"] == "42"
  114. @pytest.mark.asyncio
  115. async def test_a_execute_function():
  116. from autogen.agentchat import UserProxyAgent
  117. import time
  118. # Create an async function
  119. async def add_num(num_to_be_added):
  120. given_num = 10
  121. time.sleep(1)
  122. return num_to_be_added + given_num
  123. user = UserProxyAgent(name="test", function_map={"add_num": add_num})
  124. correct_args = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'}
  125. # Asset coroutine doesn't match.
  126. assert user.execute_function(func_call=correct_args)[1]["content"] != "15"
  127. # Asset awaited coroutine does match.
  128. assert (await user.a_execute_function(func_call=correct_args))[1]["content"] == "15"
  129. # function name called is wrong or doesn't exist
  130. wrong_func_name = {"name": "subtract_num", "arguments": '{ "num_to_be_added": 5 }'}
  131. assert "Error: Function" in (await user.a_execute_function(func_call=wrong_func_name))[1]["content"]
  132. # arguments passed is not in correct json format
  133. wrong_json_format = {
  134. "name": "add_num",
  135. "arguments": '{ "num_to_be_added": 5, given_num: 10 }',
  136. } # should be "given_num" with quotes
  137. assert (
  138. "You argument should follow json format."
  139. in (await user.a_execute_function(func_call=wrong_json_format))[1]["content"]
  140. )
  141. # function execution error with wrong arguments passed
  142. wrong_args = {"name": "add_num", "arguments": '{ "num_to_be_added": 5, "given_num": 10 }'}
  143. assert "Error: " in (await user.a_execute_function(func_call=wrong_args))[1]["content"]
  144. # 2. test calling a class method
  145. class AddNum:
  146. def __init__(self, given_num):
  147. self.given_num = given_num
  148. def add(self, num_to_be_added):
  149. self.given_num = num_to_be_added + self.given_num
  150. return self.given_num
  151. user = UserProxyAgent(name="test", function_map={"add_num": AddNum(given_num=10).add})
  152. func_call = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'}
  153. assert (await user.a_execute_function(func_call=func_call))[1]["content"] == "15"
  154. assert (await user.a_execute_function(func_call=func_call))[1]["content"] == "20"
  155. # 3. test calling a function with no arguments
  156. def get_number():
  157. return 42
  158. user = UserProxyAgent("user", function_map={"get_number": get_number})
  159. func_call = {"name": "get_number", "arguments": "{}"}
  160. assert (await user.a_execute_function(func_call))[1]["content"] == "42"
  161. @pytest.mark.skipif(
  162. skip or not sys.version.startswith("3.10"),
  163. reason="do not run if openai is not installed OR reeusted to skip OR py!=3.10",
  164. )
  165. def test_update_function():
  166. config_list_gpt4 = autogen.config_list_from_json(
  167. OAI_CONFIG_LIST,
  168. filter_dict={
  169. "model": ["gpt-4", "gpt-4-0314", "gpt4", "gpt-4-32k", "gpt-4-32k-0314", "gpt-4-32k-v0314"],
  170. },
  171. file_location=KEY_LOC,
  172. )
  173. llm_config = {
  174. "config_list": config_list_gpt4,
  175. "seed": 42,
  176. "functions": [],
  177. }
  178. user_proxy = autogen.UserProxyAgent(
  179. name="user_proxy",
  180. human_input_mode="NEVER",
  181. is_termination_msg=lambda x: True if "TERMINATE" in x.get("content") else False,
  182. )
  183. assistant = autogen.AssistantAgent(name="test", llm_config=llm_config)
  184. # Define a new function *after* the assistant has been created
  185. assistant.update_function_signature(
  186. {
  187. "name": "greet_user",
  188. "description": "Greets the user.",
  189. "parameters": {
  190. "type": "object",
  191. "properties": {},
  192. "required": [],
  193. },
  194. },
  195. is_remove=False,
  196. )
  197. res1 = user_proxy.initiate_chat(
  198. assistant,
  199. message="What functions do you know about in the context of this conversation? End your response with 'TERMINATE'.",
  200. summary_method="reflection_with_llm",
  201. )
  202. messages1 = assistant.chat_messages[user_proxy][-1]["content"]
  203. print(messages1)
  204. print("Chat summary and cost", res1.summary, res1.cost)
  205. assistant.update_function_signature("greet_user", is_remove=True)
  206. res2 = user_proxy.initiate_chat(
  207. assistant,
  208. message="What functions do you know about in the context of this conversation? End your response with 'TERMINATE'.",
  209. summary_method="reflection_with_llm",
  210. )
  211. messages2 = assistant.chat_messages[user_proxy][-1]["content"]
  212. print(messages2)
  213. # The model should know about the function in the context of the conversation
  214. assert "greet_user" in messages1
  215. assert "greet_user" not in messages2
  216. print("Chat summary and cost", res2.summary, res2.cost)
  217. if __name__ == "__main__":
  218. # test_json_extraction()
  219. # test_execute_function()
  220. test_update_function()
  221. asyncio.run(test_a_execute_function())
  222. test_eval_math_responses()