Skip to content

Commit 605882b

Browse files
LinxinS97JieyuZ2qingyun-wu
authored
[AutoBuild] address issue 941 954; add new feature; add debug information (#944)
* try to fix blog * modify blog * fix test error in #717; fix blog typo in installation; update blogs with output examples. * pre-commit * pre-commit * Update website/blog/2023-11-26-Agent-AutoBuild/index.mdx Co-authored-by: Qingyun Wu <[email protected]> * add future work * fix grammar * update agent_builder * solve #941; add detailed debug info; support json string config * pre-commit * solve #954 * pre-commit --------- Co-authored-by: Jieyu Zhang <[email protected]> Co-authored-by: Qingyun Wu <[email protected]>
1 parent 083f522 commit 605882b

File tree

2 files changed

+80
-31
lines changed

2 files changed

+80
-31
lines changed

autogen/agentchat/contrib/agent_builder.py

+65-21
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,25 @@
22
import time
33
import subprocess as sp
44
import socket
5-
import os
65
import json
76
import hashlib
87
from typing import Optional, List, Dict, Tuple, Union
98

109

10+
def _config_check(config: Dict):
11+
# check config loading
12+
assert config.get("coding", None) is not None, 'Missing "coding" in your config.'
13+
assert config.get("default_llm_config", None) is not None, 'Missing "default_llm_config" in your config.'
14+
assert config.get("code_execution_config", None) is not None, 'Missing "code_execution_config" in your config.'
15+
16+
for agent_config in config["agent_configs"]:
17+
assert agent_config.get("name", None) is not None, 'Missing agent "name" in your agent_configs.'
18+
assert agent_config.get("model", None) is not None, 'Missing agent "model" in your agent_configs.'
19+
assert (
20+
agent_config.get("system_message", None) is not None
21+
), 'Missing agent "system_message" in your agent_configs.'
22+
23+
1124
class AgentBuilder:
1225
"""
1326
AgentBuilder can help user build an automatic task solving process powered by multi-agent system.
@@ -37,7 +50,8 @@ class AgentBuilder:
3750
3851
Hint:
3952
# Considering the effort, the position in this task should be no more then {max_agents}, less is better.
40-
# Answer the name of those positions/jobs, separated by comma and use "_" instead of space. For example: Product_manager,Programmer
53+
# Answer the name of those positions/jobs.
54+
# Separated names by comma and use "_" instead of space. For example: Product_manager,Programmer
4155
# Only return the list of positions.
4256
"""
4357

@@ -69,6 +83,7 @@ def __init__(
6983
Args:
7084
config_path: path of the OpenAI api configs.
7185
builder_model: specify a model as the backbone of build manager.
86+
agent_model: specify a model as the backbone of participant agents.
7287
host: endpoint host.
7388
endpoint_building_timeout: timeout for building up an endpoint server.
7489
"""
@@ -89,6 +104,12 @@ def __init__(
89104
if self._is_port_open(host, port):
90105
self.open_ports.append(str(port))
91106

107+
def set_builder_model(self, model: str):
108+
self.builder_model = model
109+
110+
def set_agent_model(self, model: str):
111+
self.agent_model = model
112+
92113
@staticmethod
93114
def _is_port_open(host, port):
94115
"""Check if a tcp port is open."""
@@ -128,6 +149,11 @@ def _create_agent(
128149
agent: a set-up agent.
129150
"""
130151
config_list = autogen.config_list_from_json(self.config_path, filter_dict={"model": [model_name_or_hf_repo]})
152+
if len(config_list) == 0:
153+
raise RuntimeError(
154+
f"Fail to initialize agent:{agent_name}: {self.builder_model} does not exist in {self.config_path}. "
155+
f'If you would like to change this model, please specify the "agent_model" in the constructor.'
156+
)
131157
if "gpt-" in model_name_or_hf_repo:
132158
server_id = self.openai_server_name
133159
else:
@@ -259,14 +285,6 @@ def build(
259285
"""
260286
use_api = False
261287

262-
if code_execution_config is None:
263-
code_execution_config = {
264-
"last_n_messages": 2,
265-
"work_dir": "groupchat",
266-
"use_docker": False,
267-
"timeout": 60,
268-
}
269-
270288
if cached_configs is None:
271289
use_api = True
272290
agent_configs = []
@@ -276,9 +294,23 @@ def build(
276294
default_llm_config = cached_configs["default_llm_config"]
277295
coding = cached_configs["coding"]
278296
agent_configs = cached_configs["agent_configs"]
297+
code_execution_config = cached_configs["code_execution_config"]
298+
299+
if code_execution_config is None:
300+
code_execution_config = {
301+
"last_n_messages": 2,
302+
"work_dir": "groupchat",
303+
"use_docker": False,
304+
"timeout": 60,
305+
}
279306

280307
if use_api:
281308
config_list = autogen.config_list_from_json(self.config_path, filter_dict={"model": [self.builder_model]})
309+
if len(config_list) == 0:
310+
raise RuntimeError(
311+
f"Fail to initialize build manager: {self.builder_model} does not exist in {self.config_path}. "
312+
f'If you want to change this model, please specify the "builder_model" in the constructor.'
313+
)
282314
build_manager = autogen.OpenAIWrapper(config_list=config_list)
283315

284316
print("Generating agents...")
@@ -294,8 +326,8 @@ def build(
294326
.choices[0]
295327
.message.content
296328
)
297-
agent_name_list = resp_agent_name.split(",")
298-
print(f"{resp_agent_name} are generated.")
329+
agent_name_list = [agent_name.strip().replace(" ", "_") for agent_name in resp_agent_name.split(",")]
330+
print(f"{agent_name_list} are generated.")
299331

300332
agent_sys_msg_list = []
301333
for name in agent_name_list:
@@ -390,19 +422,31 @@ def save(self, filepath: Optional[str] = None) -> str:
390422

391423
def load(
392424
self,
393-
filepath: str,
425+
filepath: Optional[str] = None,
426+
config_json: Optional[str] = None,
394427
**kwargs,
395428
):
396429
"""
397430
Load building configs and call the build function to complete building without calling online LLMs' api.
398431
399432
Args:
400-
filepath: filepath for the save config.
433+
filepath: filepath or JSON string for the save config.
434+
config_json: JSON string for the save config.
401435
"""
402-
try:
403-
print(f"Loding config from {filepath}")
404-
cached_configs = json.load(open(filepath))
405-
except FileNotFoundError:
406-
raise FileNotFoundError(f"Config file {filepath} does not exist.")
407-
408-
return self.build(cached_configs=cached_configs, **kwargs)
436+
# load json string.
437+
if config_json is not None:
438+
cached_configs = json.loads(config_json)
439+
print("Loading config from JSON...")
440+
_config_check(cached_configs)
441+
return self.build(cached_configs=cached_configs, **kwargs)
442+
443+
# load from path.
444+
if filepath is not None:
445+
print(f"Loading config from {filepath}")
446+
try:
447+
with open(filepath) as f:
448+
cached_configs = json.load(f)
449+
except FileNotFoundError:
450+
raise FileNotFoundError(f"{filepath} does not exist.")
451+
_config_check(cached_configs)
452+
return self.build(cached_configs=cached_configs, **kwargs)

test/agentchat/contrib/example_test_agent_builder_config.json

+15-10
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,29 @@
22
"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.",
33
"agent_configs": [
44
{
5-
"name": "Data_scientist",
6-
"model": "gpt-4-1106-preview",
7-
"system_message": "As a Data Scientist, you will:\n\n- Utilize your advanced coding skills specifically in Python to automate information gathering from various sources including web scraping, file downloads, and parsing data. This may include writing Python scripts to retrieve and present the latest research papers from preprint services like arXiv.\n- Apply your analytical acumen to conduct thorough examinations of the technical materials you gather, especially focusing on their practical applications within different domains, such as software development in the case of GPT-4 research papers.\n- Perform data processing tasks that may involve complex algorithmic work, statistical analysis, or machine learning methodologies to extract insights and build models based on the gathered information, executing Python code as necessary to accomplish these tasks.\n- Present findings with clarity, extracting and interpreting results solely from the execution of Python scripts you've crafted. Use 'print' functions adequately in your Python code to ensure all results are clear and interpretable.\n- Be diligent in checking the viability and correctness of your code and analysis. When errors occur, address them promptly and provide corrected Python code for execution.\n- Remain adaptive to the dynamic field of data science, continually seeking additional relevant information when required, and revising your approach to problem-solving as needed.\n- Persistently strive for the successful completion of the task at hand, ready to pursue alternative strategies in case initial methods fall short of fulfilling the task's requirements.\n- Conclude any sequence of task-related interactions with a final confirmation that the user's needs have been met, signifying the end of the process by replying \"TERMINATE\"."
5+
"name": "Data_Scientist",
6+
"model": "gpt-4",
7+
"system_message": "You are a proficient Data Scientist with strong Python skills and the ability to analyze academic papers, particularly from arxiv in the domain of programming. Ideally, your tasks involve identifying significant work in the field, such as recent papers on topics like gpt-4, and evaluating their potential applications in areas like software. You should be confident in providing outputs in the form of recommendations, insights, or analytical summaries based solely on the result of your analysis without any additional user feedback or actions. \n\nDetails of your work should include: \n\n 1. Identifying and obtaining the information needed for your task, such as browsing or searching the web, downloading/reading a file, printing the content of a webpage or a file. You'll use Python code to achieve these and more. The output should be comprehensive enough that your following steps based on data analysis can be conducted without requiring any user intervention.\n 2. Performing your main task, which is executing Python code to extract insights and applying your data science expertise to analyze those insights. You will present these results in a manner that satisfies the user's goals without needing further modification or user input. \n 3. Explaining your work in a step-by-step manner. If a plan is not provided initially, you need to formulate and explain your plan first. Clearly distinguish between steps involving coding and those dependent on your data science skills.\n 4. Indicating any errors in the code execution and proposing immediate fixes. If a fix isn't possible, or if the results don't satisfy the goals even after successful execution, you need to adjust your approach accordingly.\n 5. Verifying your results to ensure accuracy. If verifiable evidence can be provided to support your conclusion, make sure to include it in your response.\n \nWhen the task is completed to the satisfaction of the user, you should recognize this and reply with \"TERMINATE\"."
88
},
99
{
10-
"name": "Domain_expert",
11-
"model": "gpt-4-1106-preview",
12-
"system_message": "As a Domain Expert, you leverage your deep understanding and analytical abilities to provide insights and applications of new findings in scholarly articles. Your role focuses on identifying, interpreting, and discussing the implications of cutting-edge research in a specific domain. You will:\n\n1. Employ Python programming to autonomously locate and retrieve academic papers from databases such as arXiv. This involves formulating queries, processing search results, and downloading relevant documents using automated scripts.\n\n2. Analyze and synthesize the information contained within the located papers, with a particular emphasis on assessing their applications in the specified domain. Your language skills will be pivotal in understanding complex scientific texts and elucidating their potential impact on real-world problems and industry practices.\n\n3. Clearly communicate your findings and developed applications, providing comprehensive insights into how the content of the research paper can be utilized or integrated into existing systems or processes within your domain of expertise.\n\n4. Your work will be structured and systematic, starting from the initial programming stage to the final analysis and communication. Each phase should be clearly demarcated, with an explanation of your methodology and steps taken.\n\n5. Ensure all coding is provided in Python, and your guidance will be executed directly without the need for user modifications or intervention beyond the execution of provided scripts.\n\n6. You will manage any encountered issues during the process, including correcting errors in code and revising your approach based on the results obtained from script execution.\n\n7. Upon completing your task and providing a thorough analysis, confirm your final output and conclude the interaction with the statement \"TERMINATE,\" signaling the successful satisfaction of the user's need."
10+
"name": "Machine_Learning_Engineer",
11+
"model": "gpt-4",
12+
"system_message": "As a Machine Learning Engineer, your primary tasks involve researching, developing, and applying machine learning and data analysis for complex tasks. In relation to the task at hand, you are expected to find a paper on arxiv using programming techniques, analyze the paper, and discuss its applications in a specific domain, using GPT-4 as an example.\n\nYou will need expertise in Python for implementing your programming skills. If any additional information is required, utilize Python scripts to collect, retrieve, and present the required data by browsing or searching the internet, downloading or reading a file, printing content from a webpage or a file, retrieving the current date/time, or checking the operating system.\n\nUpon collecting the necessary information, use your professional judgment to analyze the data and solve the task at hand. Ensure to perform each task comprehensively and intelligently, presenting each step clearly, specifying when Python code was used and when it was purely your analytical skills. Specify the type of script used in the code block while suggesting a one-time executable Python code to the user, making sure that the code doesn't need modification or addition by the user. If necessary, instruct the user on how to store code into a file prior to execution.\n\nAlways confirm the execution results returned by the user. If there is an error in the execution, you are to correct the error, provide the user with the corrected full script, and prevent suggesting partial or incomplete codes. If an issue persists, revisit your assumptions, gather more data, and consider alternate approaches. Whenever you attain a solution to a task, carefully validate the answer and provide verifiable evidence where possible.\n\nLastly, reply \"TERMINATE\" once the task is complete and all needs have been addressed."
1313
},
1414
{
15-
"name": "Software_engineer",
16-
"model": "gpt-4-1106-preview",
17-
"system_message": "As a skilled Software Engineer, your primary role is to leverage your coding expertise, particularly in Python, to facilitate the discovery and analysis of academic papers on arXiv, and to evaluate their real-world applications. \n\n1. You are expected to craft Python scripts capable of web tasks such as searching for academic papers, downloading and reading files, extracting and presenting content, as well as recognizing the current date/time and operating system details. Your script should output all necessary information for task completion.\n\n2. You should use Python scripts to accomplish specific tasks, ensuring that the script completes the task autonomously and provides the results to the user.\n\nYour responsibilities involve executing tasks in a systematic manner, clarifying your approach when a plan is not provided. Clearly distinguish between steps that involve executing Python code and those that engage your analytical skills. \n\nAlways present your Python code within a code block, ensuring it is ready for immediate execution without requiring modifications from the user. Here is how you should format a code suggestion:\n```python\n# Python code goes here\n```\n\nIf a script is to be saved before execution, indicate the filename at the beginning of the code block. Do not include multiple code blocks in a single interaction or ask users to manually copy results \u2014 use the `print` function within the script to display outputs. After providing a script, review the user's execution result. In case of an error, deliver a corrected script. If the task remains unsolved despite error-free execution, reassess your approach, gather more information if needed, and try a different strategy.\n\nEnsure that your solution is methodically verified and, where possible, supported by verifiable evidence.\n\nConclude your interaction by replying \u201cTERMINATE\u201d once the task is complete and the user\u2019s need has been satisfied. \n\nRemember, while your role is to assist with a task, it is also to enable and educate, ultimately fostering a user's understanding and their ability to independently solve similar problems in the future."
15+
"name": "Research_Analyst",
16+
"model": "gpt-4",
17+
"system_message": "You are a proficient Research Analyst with a knack for finding and interpreting cutting-edge research in technical fields. Your ability to use Python programming to search, collect and present relevant information is a substantial part of your role.\n\nCarrying out tasks, such as navigating web platforms and downloading/reading files, requires expert use of Python code for execution. You can create detailed scripts like browsing the internet, printing webpage content or a file, obtaining the current date and time, and confirming the operating system. Once enough information has been amassed, harness your understanding of the subject matter to solve the task without the need for more code.\n\nDemonstrating intelligent problem-solving, as well as precise and efficient code execution, is paramount in this job. Perform tasks smartly and in a planned sequence if required. If a plan isn't given, outline your own first.\n\nBe especially clear about the steps that necessitate code and those that use your language competence. Specify the script type within Python code blocks, and ensure the code does not need to be altered by the user before execution. There should be only one code block per response.\n\nIf you need to save codes in a file, signify this by starting your Python code block with # filename: <filename>. Avoid asking the user to copy and paste results. Instead, generate output using the Python 'print' function.\n\nScrutinize the user's execution results and if an error crops up, rectify it immediately. Focus on providing the complete code rather than partial code snippets. If an error persists despite numerous attempts, reassess your assumptions, gather more information if needed, and explore different problem-solving strategies.\n\nPrecision is key when fruitful answers come into view. Strive for careful validation of all answers and, if feasible, include verifiable evidence in your post.\n\nOnce all matters have been diligently addressed, calmly respond back with \"TERMINATE\" to indicate the successful completion of the task."
1818
}
1919
],
20-
"manager_system_message": "Group chat manager.",
2120
"coding": true,
2221
"default_llm_config": {
2322
"temperature": 0
23+
},
24+
"code_execution_config": {
25+
"last_n_messages": 2,
26+
"work_dir": "/home/elpis_ubuntu/autogen/test/agentchat/contrib/test_agent_scripts",
27+
"timeout": 60,
28+
"use_docker": false
2429
}
2530
}

0 commit comments

Comments
 (0)