Skip to content

Commit 62c7ac3

Browse files
authored
Allow SocietyOfMind agent to work with function calling and tools. (microsoft#1569)
* Allow SocietyOfMind agent to work with function calling and tools. * Correctly skip OpenAI tests.
1 parent 2c52368 commit 62c7ac3

File tree

2 files changed

+169
-2
lines changed

2 files changed

+169
-2
lines changed

autogen/agentchat/contrib/society_of_mind_agent.py

+20
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,26 @@ def _llm_response_preparer(self, prompt, messages):
9595
for message in messages:
9696
message = copy.deepcopy(message)
9797
message["role"] = "user"
98+
99+
# Convert tool and function calls to basic messages to avoid an error on the LLM call
100+
if "content" not in message:
101+
message["content"] = ""
102+
103+
if "tool_calls" in message:
104+
del message["tool_calls"]
105+
if "tool_responses" in message:
106+
del message["tool_responses"]
107+
if "function_call" in message:
108+
if message["content"] == "":
109+
try:
110+
message["content"] = (
111+
message["function_call"]["name"] + "(" + message["function_call"]["arguments"] + ")"
112+
)
113+
except KeyError:
114+
pass
115+
del message["function_call"]
116+
117+
# Add the modified message to the transcript
98118
_messages.append(message)
99119

100120
_messages.append(

test/agentchat/contrib/test_society_of_mind_agent.py

+149-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@
22
import sys
33
import autogen
44
import os
5+
from typing_extensions import Annotated
56
from autogen.agentchat.contrib.society_of_mind_agent import SocietyOfMindAgent
67

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+
717

818
def test_society_of_mind_agent():
919
external_agent = autogen.ConversableAgent(
@@ -195,6 +205,143 @@ def custom_preparer(self, messages):
195205
assert external_agent.chat_messages[soc][-1]["content"] == "All tests passed."
196206

197207

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+
198343
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

Comments
 (0)