2024-02-28 17:11:08 -08:00
|
|
|
#!/usr/bin/env python3 -m pytest
|
|
|
|
|
2023-12-04 11:12:26 +09:00
|
|
|
import json
|
2024-04-05 10:26:06 +08:00
|
|
|
import os
|
2023-12-04 11:12:26 +09:00
|
|
|
import sys
|
2024-04-05 10:26:06 +08:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2023-12-04 11:12:26 +09:00
|
|
|
from autogen.agentchat.contrib.agent_builder import AgentBuilder
|
2024-01-05 17:24:49 +03:00
|
|
|
|
2024-01-07 02:23:23 +09:00
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
|
2024-01-05 17:24:49 +03:00
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), "../.."))
|
2024-04-15 05:34:26 -07:00
|
|
|
from conftest import reason, skip_openai # noqa: E402
|
2024-04-05 10:26:06 +08:00
|
|
|
from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST # noqa: E402
|
2023-12-04 11:12:26 +09:00
|
|
|
|
2024-04-15 05:34:26 -07:00
|
|
|
try:
|
|
|
|
import chromadb
|
|
|
|
import huggingface_hub
|
|
|
|
except ImportError:
|
|
|
|
skip = True
|
|
|
|
else:
|
|
|
|
skip = False
|
|
|
|
|
2023-12-04 11:12:26 +09:00
|
|
|
here = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
|
|
|
|
|
2024-01-07 02:23:23 +09:00
|
|
|
def _config_check(config):
|
|
|
|
# check config loading
|
|
|
|
assert config.get("coding", None) is not None
|
|
|
|
assert config.get("default_llm_config", None) is not None
|
|
|
|
assert config.get("code_execution_config", None) is not None
|
|
|
|
|
|
|
|
for agent_config in config["agent_configs"]:
|
|
|
|
assert agent_config.get("name", None) is not None
|
|
|
|
assert agent_config.get("model", None) is not None
|
|
|
|
assert agent_config.get("description", None) is not None
|
|
|
|
assert agent_config.get("system_message", None) is not None
|
|
|
|
|
|
|
|
|
2023-12-04 11:12:26 +09:00
|
|
|
@pytest.mark.skipif(
|
2024-04-15 05:34:26 -07:00
|
|
|
skip_openai,
|
|
|
|
reason=reason,
|
2023-12-04 11:12:26 +09:00
|
|
|
)
|
|
|
|
def test_build():
|
2024-01-07 02:23:23 +09:00
|
|
|
builder = AgentBuilder(
|
2024-06-14 22:14:08 +08:00
|
|
|
config_file_or_env=OAI_CONFIG_LIST,
|
|
|
|
config_file_location=KEY_LOC,
|
|
|
|
builder_model=["gpt-4", "gpt-4-1106-preview"],
|
|
|
|
agent_model=["gpt-4", "gpt-4-1106-preview"],
|
2024-01-07 02:23:23 +09:00
|
|
|
)
|
2023-12-04 11:12:26 +09:00
|
|
|
building_task = (
|
|
|
|
"Find a paper on arxiv by programming, and analyze its application in some domain. "
|
|
|
|
"For example, find a recent paper about gpt-4 on arxiv "
|
|
|
|
"and find its potential applications in software."
|
|
|
|
)
|
2024-01-07 02:23:23 +09:00
|
|
|
agent_list, agent_config = builder.build(
|
2023-12-04 11:12:26 +09:00
|
|
|
building_task=building_task,
|
|
|
|
default_llm_config={"temperature": 0},
|
2023-12-06 03:50:17 +09:00
|
|
|
code_execution_config={
|
|
|
|
"last_n_messages": 2,
|
|
|
|
"work_dir": f"{here}/test_agent_scripts",
|
|
|
|
"timeout": 60,
|
|
|
|
"use_docker": "python:3",
|
|
|
|
},
|
2023-12-04 11:12:26 +09:00
|
|
|
)
|
2024-01-07 02:23:23 +09:00
|
|
|
_config_check(agent_config)
|
2023-12-04 11:12:26 +09:00
|
|
|
|
|
|
|
# check number of agents
|
2024-01-07 02:23:23 +09:00
|
|
|
assert len(agent_config["agent_configs"]) <= builder.max_agents
|
2023-12-04 11:12:26 +09:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
2024-04-15 05:34:26 -07:00
|
|
|
skip_openai or skip,
|
|
|
|
reason=reason + "OR dependency not installed",
|
2024-01-07 02:23:23 +09:00
|
|
|
)
|
|
|
|
def test_build_from_library():
|
|
|
|
builder = AgentBuilder(
|
2024-06-14 22:14:08 +08:00
|
|
|
config_file_or_env=OAI_CONFIG_LIST,
|
|
|
|
config_file_location=KEY_LOC,
|
|
|
|
builder_model=["gpt-4", "gpt-4-1106-preview"],
|
|
|
|
agent_model=["gpt-4", "gpt-4-1106-preview"],
|
2024-01-07 02:23:23 +09:00
|
|
|
)
|
|
|
|
building_task = (
|
|
|
|
"Find a paper on arxiv by programming, and analyze its application in some domain. "
|
|
|
|
"For example, find a recent paper about gpt-4 on arxiv "
|
|
|
|
"and find its potential applications in software."
|
|
|
|
)
|
|
|
|
agent_list, agent_config = builder.build_from_library(
|
|
|
|
building_task=building_task,
|
|
|
|
library_path_or_json=f"{here}/example_agent_builder_library.json",
|
|
|
|
default_llm_config={"temperature": 0},
|
|
|
|
code_execution_config={
|
|
|
|
"last_n_messages": 2,
|
|
|
|
"work_dir": f"{here}/test_agent_scripts",
|
|
|
|
"timeout": 60,
|
|
|
|
"use_docker": "python:3",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
_config_check(agent_config)
|
|
|
|
|
|
|
|
# check number of agents
|
|
|
|
assert len(agent_config["agent_configs"]) <= builder.max_agents
|
|
|
|
|
|
|
|
builder.clear_all_agents()
|
|
|
|
|
|
|
|
# test embedding similarity selection
|
|
|
|
agent_list, agent_config = builder.build_from_library(
|
|
|
|
building_task=building_task,
|
|
|
|
library_path_or_json=f"{here}/example_agent_builder_library.json",
|
|
|
|
default_llm_config={"temperature": 0},
|
|
|
|
embedding_model="all-mpnet-base-v2",
|
|
|
|
code_execution_config={
|
|
|
|
"last_n_messages": 2,
|
|
|
|
"work_dir": f"{here}/test_agent_scripts",
|
|
|
|
"timeout": 60,
|
|
|
|
"use_docker": "python:3",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
_config_check(agent_config)
|
|
|
|
|
|
|
|
# check number of agents
|
|
|
|
assert len(agent_config["agent_configs"]) <= builder.max_agents
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
2024-04-15 05:34:26 -07:00
|
|
|
skip_openai,
|
|
|
|
reason=reason,
|
2023-12-04 11:12:26 +09:00
|
|
|
)
|
|
|
|
def test_save():
|
2024-01-07 02:23:23 +09:00
|
|
|
builder = AgentBuilder(
|
2024-06-14 22:14:08 +08:00
|
|
|
config_file_or_env=OAI_CONFIG_LIST,
|
|
|
|
config_file_location=KEY_LOC,
|
|
|
|
builder_model=["gpt-4", "gpt-4-1106-preview"],
|
|
|
|
agent_model=["gpt-4", "gpt-4-1106-preview"],
|
2024-01-07 02:23:23 +09:00
|
|
|
)
|
2023-12-04 11:12:26 +09:00
|
|
|
building_task = (
|
|
|
|
"Find a paper on arxiv by programming, and analyze its application in some domain. "
|
|
|
|
"For example, find a recent paper about gpt-4 on arxiv "
|
|
|
|
"and find its potential applications in software."
|
|
|
|
)
|
|
|
|
|
|
|
|
builder.build(
|
|
|
|
building_task=building_task,
|
|
|
|
default_llm_config={"temperature": 0},
|
2023-12-06 03:50:17 +09:00
|
|
|
code_execution_config={
|
|
|
|
"last_n_messages": 2,
|
|
|
|
"work_dir": f"{here}/test_agent_scripts",
|
|
|
|
"timeout": 60,
|
|
|
|
"use_docker": "python:3",
|
|
|
|
},
|
2023-12-04 11:12:26 +09:00
|
|
|
)
|
|
|
|
saved_files = builder.save(f"{here}/example_save_agent_builder_config.json")
|
|
|
|
|
|
|
|
# check config file path
|
|
|
|
assert os.path.isfile(saved_files)
|
|
|
|
|
|
|
|
saved_configs = json.load(open(saved_files))
|
|
|
|
|
2024-01-07 02:23:23 +09:00
|
|
|
_config_check(saved_configs)
|
2023-12-04 11:12:26 +09:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
2024-04-15 05:34:26 -07:00
|
|
|
skip_openai,
|
|
|
|
reason=reason,
|
2023-12-04 11:12:26 +09:00
|
|
|
)
|
|
|
|
def test_load():
|
2024-01-07 02:23:23 +09:00
|
|
|
builder = AgentBuilder(
|
2024-06-14 22:14:08 +08:00
|
|
|
config_file_or_env=OAI_CONFIG_LIST,
|
|
|
|
config_file_location=KEY_LOC,
|
|
|
|
builder_model=["gpt-4", "gpt-4-1106-preview"],
|
|
|
|
agent_model=["gpt-4", "gpt-4-1106-preview"],
|
2024-01-07 02:23:23 +09:00
|
|
|
)
|
2023-12-04 11:12:26 +09:00
|
|
|
|
|
|
|
config_save_path = f"{here}/example_test_agent_builder_config.json"
|
2024-01-07 02:23:23 +09:00
|
|
|
json.load(open(config_save_path, "r"))
|
2023-12-04 11:12:26 +09:00
|
|
|
|
|
|
|
agent_list, loaded_agent_configs = builder.load(
|
|
|
|
config_save_path,
|
2023-12-06 03:50:17 +09:00
|
|
|
code_execution_config={
|
|
|
|
"last_n_messages": 2,
|
|
|
|
"work_dir": f"{here}/test_agent_scripts",
|
|
|
|
"timeout": 60,
|
|
|
|
"use_docker": "python:3",
|
|
|
|
},
|
2023-12-04 11:12:26 +09:00
|
|
|
)
|
2024-01-07 02:23:23 +09:00
|
|
|
print(loaded_agent_configs)
|
2023-12-04 11:12:26 +09:00
|
|
|
|
2024-01-07 02:23:23 +09:00
|
|
|
_config_check(loaded_agent_configs)
|
2023-12-04 11:12:26 +09:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
2024-04-15 05:34:26 -07:00
|
|
|
skip_openai,
|
|
|
|
reason=reason,
|
2023-12-04 11:12:26 +09:00
|
|
|
)
|
|
|
|
def test_clear_agent():
|
2024-01-07 02:23:23 +09:00
|
|
|
builder = AgentBuilder(
|
2024-06-14 22:14:08 +08:00
|
|
|
config_file_or_env=OAI_CONFIG_LIST,
|
|
|
|
config_file_location=KEY_LOC,
|
|
|
|
builder_model=["gpt-4", "gpt-4-1106-preview"],
|
|
|
|
agent_model=["gpt-4", "gpt-4-1106-preview"],
|
2024-01-07 02:23:23 +09:00
|
|
|
)
|
2023-12-04 11:12:26 +09:00
|
|
|
|
|
|
|
config_save_path = f"{here}/example_test_agent_builder_config.json"
|
|
|
|
builder.load(
|
|
|
|
config_save_path,
|
2023-12-06 03:50:17 +09:00
|
|
|
code_execution_config={
|
|
|
|
"last_n_messages": 2,
|
|
|
|
"work_dir": f"{here}/test_agent_scripts",
|
|
|
|
"timeout": 60,
|
|
|
|
"use_docker": "python:3",
|
|
|
|
},
|
2023-12-04 11:12:26 +09:00
|
|
|
)
|
|
|
|
builder.clear_all_agents()
|
|
|
|
|
|
|
|
# check if the agent cleared
|
|
|
|
assert len(builder.agent_procs_assign) == 0
|
2024-01-07 02:23:23 +09:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_build()
|
|
|
|
test_build_from_library()
|
|
|
|
test_save()
|
|
|
|
test_load()
|
|
|
|
test_clear_agent()
|