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