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

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