autogen/test/agentchat/test_math_user_proxy_agent.py

124 lines
3.6 KiB
Python
Raw Normal View History

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>
2023-07-31 19:22:30 -07:00
import pytest
import sys
from flaml import autogen
from flaml.autogen.agentchat.contrib.math_user_proxy_agent import (
MathUserProxyAgent,
_remove_print,
_add_print_to_last_line,
)
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>
2023-07-31 19:22:30 -07:00
from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST
@pytest.mark.skipif(
sys.platform in ["darwin", "win32"],
reason="do not run on MacOS or windows",
)
def test_math_user_proxy_agent():
try:
import openai
except ImportError:
return
from flaml.autogen.agentchat.assistant_agent import AssistantAgent
conversations = {}
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>
2023-07-31 19:22:30 -07:00
autogen.ChatCompletion.start_logging(conversations)
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>
2023-07-31 19:22:30 -07:00
config_list = autogen.config_list_from_json(
OAI_CONFIG_LIST,
file_location=KEY_LOC,
filter_dict={
"model": ["gpt-4", "gpt4", "gpt-4-32k", "gpt-4-32k-0314", "gpt-4-32k-v0314"],
},
)
assistant = AssistantAgent(
"assistant",
system_message="You are a helpful assistant.",
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>
2023-07-31 19:22:30 -07:00
llm_config={
"request_timeout": 600,
"seed": 42,
"config_list": config_list,
},
)
mathproxyagent = MathUserProxyAgent(name="MathChatAgent", human_input_mode="NEVER")
assistant.reset()
math_problem = "$x^3=125$. What is x?"
# assistant.receive(
# message=mathproxyagent.generate_init_message(math_problem),
# sender=mathproxyagent,
# )
mathproxyagent.initiate_chat(assistant, problem=math_problem)
print(conversations)
def test_add_remove_print():
# test add print
code = "a = 4\nb = 5\na,b"
assert _add_print_to_last_line(code) == "a = 4\nb = 5\nprint(a,b)"
# test remove print
code = """print("hello")\na = 4*5\nprint("wolrld")"""
assert _remove_print(code) == "a = 4*5"
# test remove print. Only remove prints without indentation
code = "if 4 > 5:\n\tprint('True')"
assert _remove_print(code) == code
@pytest.mark.skipif(
sys.platform in ["darwin", "win32"],
reason="do not run on MacOS or windows",
)
def test_execute_one_python_code():
mathproxyagent = MathUserProxyAgent(name="MathChatAgent", human_input_mode="NEVER")
# no output found 1
code = "x=3"
assert mathproxyagent.execute_one_python_code(code)[0] == "No output found. Make sure you print the results."
# no output found 2
code = "if 4 > 5:\n\tprint('True')"
assert mathproxyagent.execute_one_python_code(code)[0] == "No output found."
# return error
code = "2+'2'"
assert "Error:" in mathproxyagent.execute_one_python_code(code)[0]
# save previous status
mathproxyagent.execute_one_python_code("x=3\ny=x*2")
assert mathproxyagent.execute_one_python_code("print(y)")[0].strip() == "6"
code = "print('*'*2001)"
assert (
mathproxyagent.execute_one_python_code(code)[0]
== "Your requested query response is too long. You might have made a mistake. Please revise your reasoning and query."
)
def test_execute_one_wolfram_query():
mathproxyagent = MathUserProxyAgent(name="MathChatAgent", human_input_mode="NEVER")
code = "2x=3"
try:
mathproxyagent.execute_one_wolfram_query(code)[0]
except ValueError:
print("Wolfrma API key not found. Skip test.")
def test_generate_prompt():
mathproxyagent = MathUserProxyAgent(name="MathChatAgent", human_input_mode="NEVER")
assert "customized" in mathproxyagent.generate_init_message(
problem="2x=4", prompt_type="python", customized_prompt="customized"
)
if __name__ == "__main__":
# test_add_remove_print()
# test_execute_one_python_code()
# test_generate_prompt()
test_math_user_proxy_agent()