2023-05-21 15:22:29 -07:00
|
|
|
import os
|
2023-07-25 16:46:11 -07:00
|
|
|
import sys
|
|
|
|
import pytest
|
2023-09-16 16:34:16 +00:00
|
|
|
import autogen
|
|
|
|
from autogen.agentchat import AssistantAgent, UserProxyAgent
|
2023-05-02 13:38:23 -07:00
|
|
|
|
2023-07-31 19:22:30 -07:00
|
|
|
KEY_LOC = "notebook"
|
2023-07-23 06:23:09 -07:00
|
|
|
OAI_CONFIG_LIST = "OAI_CONFIG_LIST"
|
2023-05-21 15:22:29 -07:00
|
|
|
here = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
|
2023-05-02 13:38:23 -07:00
|
|
|
|
2023-07-25 16:46:11 -07:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.platform in ["darwin", "win32"],
|
|
|
|
reason="do not run on MacOS or windows",
|
|
|
|
)
|
|
|
|
def test_ai_user_proxy_agent():
|
|
|
|
try:
|
|
|
|
import openai
|
|
|
|
except ImportError:
|
|
|
|
return
|
|
|
|
|
|
|
|
conversations = {}
|
2023-07-31 19:22:30 -07:00
|
|
|
autogen.ChatCompletion.start_logging(conversations)
|
2023-07-25 16:46:11 -07:00
|
|
|
|
2023-07-31 19:22:30 -07:00
|
|
|
config_list = autogen.config_list_from_json(
|
2023-07-25 16:46:11 -07:00
|
|
|
OAI_CONFIG_LIST,
|
|
|
|
file_location=KEY_LOC,
|
|
|
|
)
|
|
|
|
assistant = AssistantAgent(
|
|
|
|
"assistant",
|
|
|
|
system_message="You are a helpful assistant.",
|
2023-07-31 19:22:30 -07:00
|
|
|
llm_config={
|
2023-07-25 16:46:11 -07:00
|
|
|
"request_timeout": 600,
|
|
|
|
"seed": 42,
|
|
|
|
"config_list": config_list,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
ai_user_proxy = UserProxyAgent(
|
|
|
|
name="ai_user",
|
|
|
|
human_input_mode="NEVER",
|
|
|
|
max_consecutive_auto_reply=2,
|
|
|
|
code_execution_config=False,
|
2023-07-31 19:22:30 -07:00
|
|
|
llm_config={
|
2023-07-25 16:46:11 -07:00
|
|
|
"config_list": config_list,
|
|
|
|
},
|
|
|
|
# In the system message the "user" always refers to ther other agent.
|
|
|
|
system_message="You ask a user for help. You check the answer from the user and provide feedback.",
|
|
|
|
)
|
|
|
|
assistant.reset()
|
|
|
|
|
|
|
|
math_problem = "$x^3=125$. What is x?"
|
|
|
|
ai_user_proxy.initiate_chat(
|
|
|
|
assistant,
|
|
|
|
message=math_problem,
|
|
|
|
)
|
|
|
|
print(conversations)
|
|
|
|
|
|
|
|
|
2023-06-09 11:40:04 -07:00
|
|
|
def test_gpt35(human_input_mode="NEVER", max_consecutive_auto_reply=5):
|
|
|
|
try:
|
|
|
|
import openai
|
|
|
|
except ImportError:
|
|
|
|
return
|
2023-07-31 19:22:30 -07:00
|
|
|
config_list = autogen.config_list_from_json(
|
2023-07-23 06:23:09 -07:00
|
|
|
OAI_CONFIG_LIST,
|
|
|
|
file_location=KEY_LOC,
|
|
|
|
filter_dict={
|
|
|
|
"model": {
|
|
|
|
"gpt-3.5-turbo",
|
|
|
|
"gpt-3.5-turbo-16k",
|
2023-09-03 19:32:51 -07:00
|
|
|
"gpt-3.5-turbo-16k-0613",
|
2023-07-23 06:23:09 -07:00
|
|
|
"gpt-3.5-turbo-0301",
|
|
|
|
"chatgpt-35-turbo-0301",
|
|
|
|
"gpt-35-turbo-v0301",
|
2023-08-14 00:09:45 -07:00
|
|
|
"gpt",
|
2023-07-23 06:23:09 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2023-07-31 19:22:30 -07:00
|
|
|
llm_config = {
|
|
|
|
"seed": 42,
|
|
|
|
"config_list": config_list,
|
|
|
|
"max_tokens": 1024,
|
|
|
|
}
|
2023-06-09 11:40:04 -07:00
|
|
|
assistant = AssistantAgent(
|
|
|
|
"coding_agent",
|
2023-07-31 19:22:30 -07:00
|
|
|
llm_config=llm_config,
|
2023-06-09 11:40:04 -07:00
|
|
|
)
|
|
|
|
user = UserProxyAgent(
|
|
|
|
"user",
|
|
|
|
human_input_mode=human_input_mode,
|
2023-07-06 06:08:44 +08:00
|
|
|
is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
|
2023-06-09 11:40:04 -07:00
|
|
|
max_consecutive_auto_reply=max_consecutive_auto_reply,
|
2023-07-23 06:23:09 -07:00
|
|
|
code_execution_config={
|
|
|
|
"work_dir": f"{here}/test_agent_scripts",
|
|
|
|
"use_docker": "python:3",
|
|
|
|
"timeout": 60,
|
|
|
|
},
|
2023-07-31 19:22:30 -07:00
|
|
|
llm_config=llm_config,
|
|
|
|
system_message="""Reply TERMINATE to end the conversation.""",
|
2023-06-09 11:40:04 -07:00
|
|
|
)
|
2023-07-23 06:23:09 -07:00
|
|
|
user.initiate_chat(assistant, message="TERMINATE")
|
|
|
|
# should terminate without sending any message
|
2023-07-28 21:17:51 -07:00
|
|
|
assert assistant.last_message()["content"] == assistant.last_message(user)["content"] == "TERMINATE"
|
2023-06-09 11:40:04 -07:00
|
|
|
coding_task = "Print hello world to a file called hello.txt"
|
2023-07-23 06:23:09 -07:00
|
|
|
user.initiate_chat(assistant, message=coding_task)
|
2023-06-09 11:40:04 -07:00
|
|
|
# coding_task = "Create a powerpoint with the text hello world in it."
|
|
|
|
# assistant.receive(coding_task, user)
|
|
|
|
coding_task = "Save a pandas df with 3 rows and 3 columns to disk."
|
2023-07-23 06:23:09 -07:00
|
|
|
user.initiate_chat(assistant, message=coding_task)
|
2023-07-04 13:29:32 -07:00
|
|
|
assert not isinstance(user.use_docker, bool) # None or str
|
2023-05-02 13:38:23 -07:00
|
|
|
|
|
|
|
|
2023-06-09 11:40:04 -07:00
|
|
|
def test_create_execute_script(human_input_mode="NEVER", max_consecutive_auto_reply=10):
|
2023-05-02 13:38:23 -07:00
|
|
|
try:
|
|
|
|
import openai
|
|
|
|
except ImportError:
|
|
|
|
return
|
|
|
|
|
2023-07-31 19:22:30 -07:00
|
|
|
config_list = autogen.config_list_from_json(OAI_CONFIG_LIST, file_location=KEY_LOC)
|
2023-05-02 13:38:23 -07:00
|
|
|
conversations = {}
|
2023-07-31 19:22:30 -07:00
|
|
|
autogen.ChatCompletion.start_logging(conversations)
|
|
|
|
llm_config = {
|
|
|
|
"request_timeout": 600,
|
|
|
|
"seed": 42,
|
|
|
|
"config_list": config_list,
|
|
|
|
}
|
2023-07-25 16:46:11 -07:00
|
|
|
assistant = AssistantAgent(
|
|
|
|
"assistant",
|
2023-07-31 19:22:30 -07:00
|
|
|
llm_config=llm_config,
|
2023-07-25 16:46:11 -07:00
|
|
|
)
|
2023-05-21 15:22:29 -07:00
|
|
|
user = UserProxyAgent(
|
2023-05-15 20:37:38 -04:00
|
|
|
"user",
|
|
|
|
human_input_mode=human_input_mode,
|
|
|
|
max_consecutive_auto_reply=max_consecutive_auto_reply,
|
2023-07-06 06:08:44 +08:00
|
|
|
is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
|
2023-05-15 20:37:38 -04:00
|
|
|
)
|
2023-07-17 20:40:41 -07:00
|
|
|
user.initiate_chat(
|
|
|
|
assistant,
|
2023-07-23 06:23:09 -07:00
|
|
|
message="""Create and execute a script to plot a rocket without using matplotlib""",
|
2023-05-15 20:37:38 -04:00
|
|
|
)
|
2023-06-09 11:40:04 -07:00
|
|
|
assistant.reset()
|
2023-07-17 20:40:41 -07:00
|
|
|
user.initiate_chat(
|
|
|
|
assistant,
|
2023-07-23 06:23:09 -07:00
|
|
|
message="""Create a temp.py file with the following content:
|
2023-05-02 13:38:23 -07:00
|
|
|
```
|
|
|
|
print('Hello world!')
|
|
|
|
```""",
|
|
|
|
)
|
|
|
|
print(conversations)
|
2023-07-31 19:22:30 -07:00
|
|
|
autogen.ChatCompletion.start_logging(compact=False)
|
2023-07-17 20:40:41 -07:00
|
|
|
user.send("""Execute temp.py""", assistant)
|
2023-07-31 19:22:30 -07:00
|
|
|
print(autogen.ChatCompletion.logged_history)
|
|
|
|
autogen.ChatCompletion.stop_logging()
|
2023-05-02 13:38:23 -07:00
|
|
|
|
|
|
|
|
2023-05-15 20:37:38 -04:00
|
|
|
def test_tsp(human_input_mode="NEVER", max_consecutive_auto_reply=10):
|
2023-05-02 13:38:23 -07:00
|
|
|
try:
|
|
|
|
import openai
|
|
|
|
except ImportError:
|
|
|
|
return
|
|
|
|
|
2023-07-31 19:22:30 -07:00
|
|
|
config_list = autogen.config_list_from_json(
|
2023-07-23 06:23:09 -07:00
|
|
|
OAI_CONFIG_LIST,
|
|
|
|
file_location=KEY_LOC,
|
|
|
|
filter_dict={
|
2023-08-14 00:09:45 -07:00
|
|
|
"model": ["gpt-4", "gpt4", "gpt-4-32k", "gpt-4-32k-0314", "gpt-4-32k-v0314"],
|
2023-07-23 06:23:09 -07:00
|
|
|
},
|
|
|
|
)
|
2023-05-02 13:38:23 -07:00
|
|
|
hard_questions = [
|
|
|
|
"What if we must go from node 1 to node 2?",
|
|
|
|
"Can we double all distances?",
|
|
|
|
"Can we add a new point to the graph? It's distance should be randomly between 0 - 5 to each of the existing points.",
|
|
|
|
]
|
|
|
|
|
2023-07-17 20:40:41 -07:00
|
|
|
class TSPUserProxyAgent(UserProxyAgent):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
with open(f"{here}/tsp_prompt.txt", "r") as f:
|
|
|
|
self._prompt = f.read()
|
|
|
|
|
2023-07-23 06:23:09 -07:00
|
|
|
def generate_init_message(self, question) -> str:
|
2023-07-17 20:40:41 -07:00
|
|
|
return self._prompt.format(question=question)
|
|
|
|
|
2023-07-31 19:22:30 -07:00
|
|
|
autogen.ChatCompletion.start_logging()
|
|
|
|
assistant = AssistantAgent("assistant", llm_config={"temperature": 0, "config_list": config_list})
|
2023-07-17 20:40:41 -07:00
|
|
|
user = TSPUserProxyAgent(
|
2023-05-15 20:37:38 -04:00
|
|
|
"user",
|
2023-07-23 06:23:09 -07:00
|
|
|
code_execution_config={"work_dir": here},
|
2023-05-15 20:37:38 -04:00
|
|
|
human_input_mode=human_input_mode,
|
|
|
|
max_consecutive_auto_reply=max_consecutive_auto_reply,
|
|
|
|
)
|
2023-07-17 20:40:41 -07:00
|
|
|
user.initiate_chat(assistant, question=hard_questions[2])
|
2023-07-31 19:22:30 -07:00
|
|
|
print(autogen.ChatCompletion.logged_history)
|
|
|
|
autogen.ChatCompletion.stop_logging()
|
2023-05-02 13:38:23 -07:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-07-23 06:23:09 -07:00
|
|
|
test_gpt35()
|
2023-07-17 20:40:41 -07:00
|
|
|
# test_create_execute_script(human_input_mode="TERMINATE")
|
2023-05-15 20:37:38 -04:00
|
|
|
# when GPT-4, i.e., the DEFAULT_MODEL, is used, conversation in the following test
|
|
|
|
# should terminate in 2-3 rounds of interactions (because is_termination_msg should be true after 2-3 rounds)
|
|
|
|
# although the max_consecutive_auto_reply is set to 10.
|
2023-07-23 06:23:09 -07:00
|
|
|
# test_tsp(human_input_mode="NEVER", max_consecutive_auto_reply=10)
|