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