-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Copy pathtest_agent_builder.py
executable file
·331 lines (271 loc) · 10.7 KB
/
test_agent_builder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python3 -m pytest
import json
import os
import sys
from unittest.mock import MagicMock, patch
import pytest
import autogen
from autogen.agentchat.contrib.agent_builder import AgentBuilder
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
sys.path.append(os.path.join(os.path.dirname(__file__), "../.."))
from conftest import reason, skip_openai # noqa: E402
from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST # noqa: E402 # noqa: E402
try:
import chromadb
import huggingface_hub
except ImportError:
skip = True
else:
skip = False
here = os.path.abspath(os.path.dirname(__file__))
llm_config = {"temperature": 0}
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
# Function initializes a group chat with agents and starts a execution_task.
def start_task(execution_task: str, agent_list: list):
group_chat = autogen.GroupChat(agents=agent_list, messages=[], max_round=12)
manager = autogen.GroupChatManager(
groupchat=group_chat,
llm_config={"config_list": autogen.config_list_from_json(f"{KEY_LOC}/{OAI_CONFIG_LIST}"), **llm_config},
)
agent_list[0].initiate_chat(manager, message=execution_task)
ask_ossinsight_mock = MagicMock()
# Function to test function calling
def ask_ossinsight(question: str) -> str:
ask_ossinsight_mock(question)
return "The repository microsoft/autogen has 123,456 stars on GitHub."
@pytest.mark.skipif(skip_openai, reason=reason)
def test_build():
builder = AgentBuilder(
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"],
)
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(
building_task=building_task,
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
@pytest.mark.skipif(skip_openai or skip, reason=reason + "OR dependency not installed")
def test_build_assistant_with_function_calling():
list_of_functions = [
{
"name": "ossinsight_data_api",
"description": "This is an API endpoint allowing users (analysts) to input question about GitHub in text format to retrieve the related and structured data.",
"function": ask_ossinsight,
}
]
builder = AgentBuilder(
config_file_or_env=OAI_CONFIG_LIST, config_file_location=KEY_LOC, builder_model="gpt-4", agent_model="gpt-4"
)
building_task = "How many stars microsoft/autogen has on GitHub?"
agent_list, agent_config = builder.build(
building_task=building_task,
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",
},
list_of_functions=list_of_functions,
)
_config_check(agent_config)
# check number of agents
assert len(agent_config["agent_configs"]) <= builder.max_agents
# Mock the 'ask_ossinsight' function in the '_main_' module using a context manager.
with patch(f"{__name__}.ask_ossinsight") as mocked_function:
# Execute 'start_task' which should trigger 'ask_ossinsight' due to the given execution task.
start_task(
execution_task="How many stars microsoft/autogen has on GitHub?",
agent_list=agent_list,
)
# Verify that 'ask_ossinsight' was called exactly once during the task execution.
mocked_function.assert_called()
@pytest.mark.skipif(
skip_openai,
reason="requested to skip",
)
def test_build_gpt_assistant_with_function_calling():
list_of_functions = [
{
"name": "ossinsight_data_api",
"description": "This is an API endpoint allowing users (analysts) to input question about GitHub in text format to retrieve the related and structured data.",
"function": ask_ossinsight,
}
]
builder = AgentBuilder(
config_file_or_env=OAI_CONFIG_LIST, config_file_location=KEY_LOC, builder_model="gpt-4", agent_model="gpt-4"
)
building_task = "Determine number of stars of GitHub repositories"
agent_list, agent_config = builder.build(
building_task=building_task,
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",
},
list_of_functions=list_of_functions,
use_oai_assistant=True,
)
_config_check(agent_config)
# check number of agents
assert len(agent_config["agent_configs"]) <= builder.max_agents
# Mock the 'ask_ossinsight' function in the '_main_' module using a context manager.
with patch(f"{__name__}.ask_ossinsight") as mocked_function:
# Execute 'start_task' which should trigger 'ask_ossinsight' due to the given execution task.
start_task(
execution_task="How many stars microsoft/autogen has on GitHub?",
agent_list=agent_list,
)
# Verify that 'ask_ossinsight' was called exactly once during the task execution.
mocked_function.assert_called()
@pytest.mark.skipif(
skip_openai or skip,
reason=reason + "OR dependency not installed",
)
def test_build_from_library():
builder = AgentBuilder(
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"],
)
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(skip_openai, reason=reason)
def test_save():
builder = AgentBuilder(
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"],
)
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},
code_execution_config={
"last_n_messages": 2,
"work_dir": f"{here}/test_agent_scripts",
"timeout": 60,
"use_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))
_config_check(saved_configs)
@pytest.mark.skipif(skip_openai, reason=reason)
def test_load():
builder = AgentBuilder(
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"],
)
config_save_path = f"{here}/example_test_agent_builder_config.json"
json.load(open(config_save_path, "r"))
agent_list, loaded_agent_configs = builder.load(
config_save_path,
code_execution_config={
"last_n_messages": 2,
"work_dir": f"{here}/test_agent_scripts",
"timeout": 60,
"use_docker": "python:3",
},
)
print(loaded_agent_configs)
_config_check(loaded_agent_configs)
@pytest.mark.skipif(skip_openai, reason=reason)
def test_clear_agent():
builder = AgentBuilder(
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"],
)
config_save_path = f"{here}/example_test_agent_builder_config.json"
builder.load(
config_save_path,
code_execution_config={
"last_n_messages": 2,
"work_dir": f"{here}/test_agent_scripts",
"timeout": 60,
"use_docker": "python:3",
},
)
builder.clear_all_agents()
# check if the agent cleared
assert len(builder.agent_procs_assign) == 0
if __name__ == "__main__":
test_build()
test_build_assistant_with_function_calling()
test_build_gpt_assistant_with_function_calling()
test_build_from_library()
test_save()
test_load()
test_clear_agent()