|
2 | 2 | import sys
|
3 | 3 | import autogen
|
4 | 4 | import os
|
| 5 | +from typing_extensions import Annotated |
5 | 6 | from autogen.agentchat.contrib.society_of_mind_agent import SocietyOfMindAgent
|
6 | 7 |
|
| 8 | +sys.path.append(os.path.join(os.path.dirname(__file__), "..")) |
| 9 | +from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST # noqa: E402 |
| 10 | + |
| 11 | +sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) |
| 12 | +from conftest import skip_openai # noqa: E402 |
| 13 | + |
| 14 | +if not skip_openai: |
| 15 | + config_list = autogen.config_list_from_json(env_or_file=OAI_CONFIG_LIST, file_location=KEY_LOC) |
| 16 | + |
7 | 17 |
|
8 | 18 | def test_society_of_mind_agent():
|
9 | 19 | external_agent = autogen.ConversableAgent(
|
@@ -195,6 +205,143 @@ def custom_preparer(self, messages):
|
195 | 205 | assert external_agent.chat_messages[soc][-1]["content"] == "All tests passed."
|
196 | 206 |
|
197 | 207 |
|
| 208 | +@pytest.mark.skipif( |
| 209 | + skip_openai, |
| 210 | + reason="do not run openai tests", |
| 211 | +) |
| 212 | +def test_function_calling(): |
| 213 | + llm_config = {"config_list": config_list} |
| 214 | + inner_llm_config = { |
| 215 | + "config_list": config_list, |
| 216 | + "functions": [ |
| 217 | + { |
| 218 | + "name": "reverse_print", |
| 219 | + "description": "Prints a string in reverse", |
| 220 | + "parameters": { |
| 221 | + "type": "object", |
| 222 | + "properties": { |
| 223 | + "str": { |
| 224 | + "type": "string", |
| 225 | + "description": "The string to reverse-print", |
| 226 | + } |
| 227 | + }, |
| 228 | + }, |
| 229 | + "required": ["str"], |
| 230 | + } |
| 231 | + ], |
| 232 | + } |
| 233 | + |
| 234 | + external_agent = autogen.ConversableAgent( |
| 235 | + "external_agent", |
| 236 | + max_consecutive_auto_reply=10, |
| 237 | + human_input_mode="NEVER", |
| 238 | + llm_config=False, |
| 239 | + default_auto_reply="This is an external agent speaking.", |
| 240 | + ) |
| 241 | + |
| 242 | + agent1 = autogen.ConversableAgent( |
| 243 | + "alice", |
| 244 | + max_consecutive_auto_reply=10, |
| 245 | + human_input_mode="NEVER", |
| 246 | + llm_config=inner_llm_config, |
| 247 | + default_auto_reply="This is alice speaking.", |
| 248 | + ) |
| 249 | + agent2 = autogen.ConversableAgent( |
| 250 | + "bob", |
| 251 | + max_consecutive_auto_reply=10, |
| 252 | + human_input_mode="NEVER", |
| 253 | + llm_config=False, |
| 254 | + default_auto_reply="This is bob speaking.", |
| 255 | + ) |
| 256 | + agent3 = autogen.ConversableAgent( |
| 257 | + "sam", |
| 258 | + max_consecutive_auto_reply=10, |
| 259 | + human_input_mode="NEVER", |
| 260 | + llm_config=False, |
| 261 | + default_auto_reply="TERMINATE", |
| 262 | + ) |
| 263 | + |
| 264 | + agent2.register_function( |
| 265 | + function_map={ |
| 266 | + "reverse_print": lambda str: str[::-1], |
| 267 | + } |
| 268 | + ) |
| 269 | + |
| 270 | + groupchat = autogen.GroupChat( |
| 271 | + agents=[agent1, agent2, agent3], messages=[], speaker_selection_method="round_robin", max_round=10 |
| 272 | + ) |
| 273 | + |
| 274 | + group_chat_manager = autogen.GroupChatManager( |
| 275 | + groupchat=groupchat, |
| 276 | + llm_config=False, |
| 277 | + is_termination_msg=lambda x: x.get("content", "").find("TERMINATE") >= 0, |
| 278 | + ) |
| 279 | + |
| 280 | + soc = SocietyOfMindAgent("soc_agent", chat_manager=group_chat_manager, llm_config=llm_config) |
| 281 | + external_agent.send( |
| 282 | + "Call the reverse_print function with the str 'Hello world.'", soc, request_reply=True, silent=False |
| 283 | + ) |
| 284 | + |
| 285 | + |
| 286 | +@pytest.mark.skipif( |
| 287 | + skip_openai, |
| 288 | + reason="do not run openai tests", |
| 289 | +) |
| 290 | +def test_tool_use(): |
| 291 | + llm_config = {"config_list": config_list} |
| 292 | + inner_llm_config = {"config_list": config_list} |
| 293 | + |
| 294 | + external_agent = autogen.ConversableAgent( |
| 295 | + "external_agent", |
| 296 | + max_consecutive_auto_reply=10, |
| 297 | + human_input_mode="NEVER", |
| 298 | + llm_config=False, |
| 299 | + default_auto_reply="This is an external agent speaking.", |
| 300 | + ) |
| 301 | + |
| 302 | + agent1 = autogen.ConversableAgent( |
| 303 | + "alice", |
| 304 | + max_consecutive_auto_reply=10, |
| 305 | + human_input_mode="NEVER", |
| 306 | + llm_config=inner_llm_config, |
| 307 | + default_auto_reply="This is alice speaking.", |
| 308 | + ) |
| 309 | + agent2 = autogen.ConversableAgent( |
| 310 | + "bob", |
| 311 | + max_consecutive_auto_reply=10, |
| 312 | + human_input_mode="NEVER", |
| 313 | + llm_config=False, |
| 314 | + default_auto_reply="This is bob speaking.", |
| 315 | + ) |
| 316 | + agent3 = autogen.ConversableAgent( |
| 317 | + "sam", |
| 318 | + max_consecutive_auto_reply=10, |
| 319 | + human_input_mode="NEVER", |
| 320 | + llm_config=False, |
| 321 | + default_auto_reply="TERMINATE", |
| 322 | + ) |
| 323 | + |
| 324 | + @agent2.register_for_execution() |
| 325 | + @agent1.register_for_llm(name="reverse_print", description="Prints a string in reverse") |
| 326 | + def _reverse_print(s: Annotated[str, "The string to reverse-print."]) -> str: |
| 327 | + return s[::-1] |
| 328 | + |
| 329 | + groupchat = autogen.GroupChat( |
| 330 | + agents=[agent1, agent2, agent3], messages=[], speaker_selection_method="round_robin", max_round=10 |
| 331 | + ) |
| 332 | + |
| 333 | + group_chat_manager = autogen.GroupChatManager( |
| 334 | + groupchat=groupchat, |
| 335 | + llm_config=False, |
| 336 | + is_termination_msg=lambda x: x.get("content", "").find("TERMINATE") >= 0, |
| 337 | + ) |
| 338 | + |
| 339 | + soc = SocietyOfMindAgent("soc_agent", chat_manager=group_chat_manager, llm_config=llm_config) |
| 340 | + external_agent.send("Call reverse_print with the str 'Hello world.'", soc, request_reply=True, silent=False) |
| 341 | + |
| 342 | + |
198 | 343 | if __name__ == "__main__":
|
199 |
| - test_society_of_mind_agent() |
200 |
| - test_custom_preparer() |
| 344 | + # test_society_of_mind_agent() |
| 345 | + # test_custom_preparer() |
| 346 | + test_function_calling() |
| 347 | + test_tool_use() |
0 commit comments