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_math_user_proxy_agent.py 3.7 kB

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
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
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import pytest
  2. import sys
  3. import autogen
  4. from autogen.agentchat.contrib.math_user_proxy_agent import (
  5. MathUserProxyAgent,
  6. _remove_print,
  7. _add_print_to_last_line,
  8. )
  9. from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST
  10. try:
  11. from openai import OpenAI
  12. except ImportError:
  13. skip = True
  14. else:
  15. skip = False
  16. @pytest.mark.skipif(
  17. skip or sys.platform in ["darwin", "win32"],
  18. reason="do not run on MacOS or windows",
  19. )
  20. def test_math_user_proxy_agent():
  21. from autogen.agentchat.assistant_agent import AssistantAgent
  22. conversations = {}
  23. # autogen.ChatCompletion.start_logging(conversations)
  24. config_list = autogen.config_list_from_json(
  25. OAI_CONFIG_LIST,
  26. file_location=KEY_LOC,
  27. filter_dict={
  28. "model": ["gpt-4", "gpt4", "gpt-4-32k", "gpt-4-32k-0314", "gpt-4-32k-v0314"],
  29. },
  30. )
  31. assistant = AssistantAgent(
  32. "assistant",
  33. system_message="You are a helpful assistant.",
  34. llm_config={
  35. "timeout": 600,
  36. "seed": 42,
  37. "config_list": config_list,
  38. },
  39. )
  40. mathproxyagent = MathUserProxyAgent(name="MathChatAgent", human_input_mode="NEVER")
  41. assistant.reset()
  42. math_problem = "$x^3=125$. What is x?"
  43. # assistant.receive(
  44. # message=mathproxyagent.generate_init_message(math_problem),
  45. # sender=mathproxyagent,
  46. # )
  47. mathproxyagent.initiate_chat(assistant, problem=math_problem)
  48. print(conversations)
  49. def test_add_remove_print():
  50. # test add print
  51. code = "a = 4\nb = 5\na,b"
  52. assert _add_print_to_last_line(code) == "a = 4\nb = 5\nprint(a,b)"
  53. # test remove print
  54. code = """print("hello")\na = 4*5\nprint("wolrld")"""
  55. assert _remove_print(code) == "a = 4*5"
  56. # test remove print. Only remove prints without indentation
  57. code = "if 4 > 5:\n\tprint('True')"
  58. assert _remove_print(code) == code
  59. @pytest.mark.skipif(
  60. sys.platform in ["darwin", "win32"],
  61. reason="do not run on MacOS or windows",
  62. )
  63. def test_execute_one_python_code():
  64. mathproxyagent = MathUserProxyAgent(name="MathChatAgent", human_input_mode="NEVER")
  65. # no output found 1
  66. code = "x=3"
  67. assert mathproxyagent.execute_one_python_code(code)[0] == "No output found. Make sure you print the results."
  68. # no output found 2
  69. code = "if 4 > 5:\n\tprint('True')"
  70. assert mathproxyagent.execute_one_python_code(code)[0] == "No output found."
  71. # return error
  72. code = "2+'2'"
  73. assert "Error:" in mathproxyagent.execute_one_python_code(code)[0]
  74. # save previous status
  75. mathproxyagent.execute_one_python_code("x=3\ny=x*2")
  76. assert mathproxyagent.execute_one_python_code("print(y)")[0].strip() == "6"
  77. code = "print('*'*2001)"
  78. assert (
  79. mathproxyagent.execute_one_python_code(code)[0]
  80. == "Your requested query response is too long. You might have made a mistake. Please revise your reasoning and query."
  81. )
  82. def test_execute_one_wolfram_query():
  83. mathproxyagent = MathUserProxyAgent(name="MathChatAgent", human_input_mode="NEVER")
  84. code = "2x=3"
  85. try:
  86. mathproxyagent.execute_one_wolfram_query(code)[0]
  87. except ValueError:
  88. print("Wolfrma API key not found. Skip test.")
  89. def test_generate_prompt():
  90. mathproxyagent = MathUserProxyAgent(name="MathChatAgent", human_input_mode="NEVER")
  91. assert "customized" in mathproxyagent.generate_init_message(
  92. problem="2x=4", prompt_type="python", customized_prompt="customized"
  93. )
  94. if __name__ == "__main__":
  95. # test_add_remove_print()
  96. # test_execute_one_python_code()
  97. # test_generate_prompt()
  98. test_math_user_proxy_agent()