autogen/test/agentchat/contrib/test_agent_builder.py
Linxin Song 83f0c744b2
Testing AutoBuild (#846)
* init notebook for agent set up

* complete AgentCreator

* AgentCreator first step features completed.

* update AgentCreator

* update AgentCreator

* update AgentCreator (modify params of build)

* [update AgentCreator] add auto judgement of coding

* add autobuild

* rename autobuild notebook

* Add step-by-step command.

* modify name

* fix bugs

* update for new openai api

* add example

* add load_config, save_config, and add task in build and start

* modify notebook

* rewrite save and load function; update notebook

* update description

* update description

* update description

* change member variable of agent_creator.py

* update notebook

* new feature: auto-generate agent name and system message

* new feature: add gpts support

* update notebook

* update notebook

* beautify; add docstring for build

* Update notebook; PR version

* typo

* update notebook

* fix typo

* try to run llama

* try to run llama

* switch api_base to base_url

* add details for Step 6; add print in clear_all_agents()

* Change description of Step 5

* delete ASSISTANT_CONFIG_LIST

* add Linxin to blog authors

* add blog

* Update index.mdx

* add test; add user proxy constrain; change show case in notebook

* modify blog for test

* change test file name; modify test

* modify test

* modify test

* add try/catch for dependency

* add version requirement for openai

* add exception for DistributionNotFound error

* add requirement

* change assertion

* rename test; modify index.mdx

* change config file name

* Update agent_chat.md

* Update agent_chat.md

* Update AgentChat.md

* complete blog; fix typos in notebook

* add autobuild banner

* try to merge

* Update Examples.md

* update test

* skip if openai not installed

* pre-commit

* Update website/blog/2023-11-26-Agent-AutoBuild/index.mdx

Co-authored-by: Chi Wang <wang.chi@microsoft.com>

* update contrib-openai.yml

* change pull_request_target to pull_request

* disable other openai tests

* address issues from ekzhu; modify notebook; modify blog; modify test

* update test example

* update test

* Update agent_chat.md

* Update Examples.md

* address issues from qingyun; update agent_builder.py; update notebook

* recover contrib-openai.yml

* pre-commit solve

---------

Co-authored-by: Jieyu Zhang <jieyuz2@cs.washington.edu>
Co-authored-by: JieyuZ2 <jieyuzhang97@gmail.com>
Co-authored-by: Chi Wang <wang.chi@microsoft.com>
Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu>
2023-12-04 02:12:26 +00:00

135 lines
4.5 KiB
Python

import pytest
import os
import json
import sys
from packaging.requirements import Requirement
from autogen.agentchat.contrib.agent_builder import AgentBuilder
from autogen import UserProxyAgent
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST # noqa: E402
here = os.path.abspath(os.path.dirname(__file__))
oai_config_path = OAI_CONFIG_LIST
# openai>=1 required
try:
from openai import OpenAI, APIError
from openai.types.chat import ChatCompletion
from openai.types.chat.chat_completion import ChatCompletionMessage, Choice
from openai.types.completion import Completion
from openai.types.completion_usage import CompletionUsage
import diskcache
OPENAI_INSTALLED = True
except ImportError:
OPENAI_INSTALLED = False
@pytest.mark.skipif(
not OPENAI_INSTALLED,
reason="do not run when dependency is not installed",
)
def test_build():
builder = AgentBuilder(config_path=oai_config_path, builder_model="gpt-4", agent_model="gpt-4")
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},
user_proxy_work_dir=f"{here}/test_agent_scripts",
docker="python:3",
)
# check number of agents
assert len(builder.agent_procs_assign.keys()) <= builder.max_agents
# check system message
for agent, proc in builder.agent_procs_assign.values():
assert "TERMINATE" in agent.system_message
@pytest.mark.skipif(
not OPENAI_INSTALLED,
reason="do not run when dependency is not installed",
)
def test_save():
builder = AgentBuilder(config_path=oai_config_path, builder_model="gpt-4", agent_model="gpt-4")
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},
user_proxy_work_dir=f"{here}/test_agent_scripts",
docker="python:3",
)
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))
# check config format
assert saved_configs.get("building_task", None) is not None
assert saved_configs.get("agent_configs", None) is not None
assert saved_configs.get("coding", None) is not None
assert saved_configs.get("default_llm_config", None) is not None
@pytest.mark.skipif(
not OPENAI_INSTALLED,
reason="do not run when dependency is not installed",
)
def test_load():
builder = AgentBuilder(config_path=oai_config_path, builder_model="gpt-4", agent_model="gpt-4")
config_save_path = f"{here}/example_test_agent_builder_config.json"
configs = json.load(open(config_save_path))
agent_configs = {
e["name"]: {"model": e["model"], "system_message": e["system_message"]} for e in configs["agent_configs"]
}
agent_list, loaded_agent_configs = builder.load(
config_save_path,
user_proxy_work_dir=f"{here}/test_agent_scripts",
docker="python:3",
)
# check config loading
assert loaded_agent_configs["coding"] == configs["coding"]
if loaded_agent_configs["coding"] is True:
assert isinstance(agent_list[0], UserProxyAgent)
agent_list = agent_list[1:]
for agent in agent_list:
agent_name = agent.name
assert agent_configs.get(agent_name, None) is not None
assert agent_configs[agent_name]["model"] == agent.llm_config["model"]
assert agent_configs[agent_name]["system_message"] == agent.system_message
@pytest.mark.skipif(
not OPENAI_INSTALLED,
reason="do not run when dependency is not installed",
)
def test_clear_agent():
builder = AgentBuilder(config_path=oai_config_path, builder_model="gpt-4", agent_model="gpt-4")
config_save_path = f"{here}/example_test_agent_builder_config.json"
builder.load(
config_save_path,
user_proxy_work_dir=f"{here}/test_agent_scripts",
docker="python:3",
)
builder.clear_all_agents()
# check if the agent cleared
assert len(builder.agent_procs_assign) == 0