2023-05-21 15:22:29 -07:00
|
|
|
import os
|
2023-05-02 13:38:23 -07:00
|
|
|
from flaml import oai
|
2023-06-09 11:40:04 -07:00
|
|
|
from flaml.autogen.agent import AssistantAgent, UserProxyAgent
|
2023-05-02 13:38:23 -07:00
|
|
|
|
2023-05-21 15:22:29 -07:00
|
|
|
KEY_LOC = "test/autogen"
|
|
|
|
here = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
|
2023-05-02 13:38:23 -07:00
|
|
|
|
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-06-17 06:11:22 -07:00
|
|
|
config_list = oai.config_list_from_models(key_file_path=KEY_LOC, model_list=["gpt-3.5-turbo-0613"], exclude="aoai")
|
2023-06-09 11:40:04 -07:00
|
|
|
assistant = AssistantAgent(
|
|
|
|
"coding_agent",
|
2023-06-15 17:58:44 -07:00
|
|
|
# request_timeout=600,
|
2023-06-09 11:40:04 -07:00
|
|
|
seed=40,
|
|
|
|
max_tokens=1024,
|
|
|
|
config_list=config_list,
|
|
|
|
)
|
|
|
|
user = UserProxyAgent(
|
|
|
|
"user",
|
|
|
|
work_dir=f"{here}/test_agent_scripts",
|
|
|
|
human_input_mode=human_input_mode,
|
|
|
|
is_termination_msg=lambda x: x.rstrip().endswith("TERMINATE"),
|
|
|
|
max_consecutive_auto_reply=max_consecutive_auto_reply,
|
|
|
|
use_docker="python:3",
|
|
|
|
)
|
|
|
|
coding_task = "Print hello world to a file called hello.txt"
|
|
|
|
assistant.receive(coding_task, user)
|
|
|
|
# coding_task = "Create a powerpoint with the text hello world in it."
|
|
|
|
# assistant.receive(coding_task, user)
|
|
|
|
assistant.reset()
|
|
|
|
coding_task = "Save a pandas df with 3 rows and 3 columns to disk."
|
|
|
|
assistant.receive(coding_task, user)
|
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-05-21 15:22:29 -07:00
|
|
|
config_list = oai.config_list_gpt4_gpt35(key_file_path=KEY_LOC)
|
2023-05-02 13:38:23 -07:00
|
|
|
conversations = {}
|
|
|
|
oai.ChatCompletion.start_logging(conversations)
|
2023-06-09 11:40:04 -07:00
|
|
|
assistant = AssistantAgent("assistant", request_timeout=600, seed=42, config_list=config_list)
|
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,
|
|
|
|
is_termination_msg=lambda x: x.rstrip().endswith("TERMINATE"),
|
|
|
|
)
|
2023-06-09 11:40:04 -07:00
|
|
|
assistant.receive(
|
2023-05-15 20:37:38 -04:00
|
|
|
"""Create and execute a script to plot a rocket without using matplotlib""",
|
|
|
|
user,
|
|
|
|
)
|
2023-06-09 11:40:04 -07:00
|
|
|
assistant.reset()
|
|
|
|
assistant.receive(
|
2023-05-02 13:38:23 -07:00
|
|
|
"""Create a temp.py file with the following content:
|
|
|
|
```
|
|
|
|
print('Hello world!')
|
|
|
|
```""",
|
|
|
|
user,
|
|
|
|
)
|
|
|
|
print(conversations)
|
|
|
|
oai.ChatCompletion.start_logging(compact=False)
|
2023-06-09 11:40:04 -07:00
|
|
|
assistant.receive("""Execute temp.py""", user)
|
2023-05-02 13:38:23 -07:00
|
|
|
print(oai.ChatCompletion.logged_history)
|
|
|
|
oai.ChatCompletion.stop_logging()
|
|
|
|
|
|
|
|
|
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-05-21 15:22:29 -07:00
|
|
|
config_list = oai.config_list_openai_aoai(key_file_path=KEY_LOC)
|
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.",
|
|
|
|
]
|
|
|
|
|
|
|
|
oai.ChatCompletion.start_logging()
|
2023-06-09 11:40:04 -07:00
|
|
|
assistant = AssistantAgent("assistant", temperature=0, config_list=config_list)
|
2023-05-21 15:22:29 -07:00
|
|
|
user = UserProxyAgent(
|
2023-05-15 20:37:38 -04:00
|
|
|
"user",
|
2023-05-21 15:22:29 -07:00
|
|
|
work_dir=f"{here}",
|
2023-05-15 20:37:38 -04:00
|
|
|
human_input_mode=human_input_mode,
|
|
|
|
max_consecutive_auto_reply=max_consecutive_auto_reply,
|
|
|
|
)
|
2023-05-21 15:22:29 -07:00
|
|
|
with open(f"{here}/tsp_prompt.txt", "r") as f:
|
2023-05-02 13:38:23 -07:00
|
|
|
prompt = f.read()
|
|
|
|
# agent.receive(prompt.format(question=hard_questions[0]), user)
|
|
|
|
# agent.receive(prompt.format(question=hard_questions[1]), user)
|
2023-06-09 11:40:04 -07:00
|
|
|
assistant.receive(prompt.format(question=hard_questions[2]), user)
|
2023-05-02 13:38:23 -07:00
|
|
|
print(oai.ChatCompletion.logged_history)
|
|
|
|
oai.ChatCompletion.stop_logging()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-06-09 11:40:04 -07:00
|
|
|
test_gpt35()
|
|
|
|
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.
|
|
|
|
test_tsp(human_input_mode="NEVER", max_consecutive_auto_reply=10)
|