Skip to content

Commit 63a35e7

Browse files
yenifsonichidavorrunje
authored
run sync and async in async (#1242)
* run sync and async in async * test for async multitool call * uncomment notebook --------- Co-authored-by: Chi Wang <[email protected]> Co-authored-by: Davor Runje <[email protected]>
1 parent 1565795 commit 63a35e7

File tree

3 files changed

+190
-24
lines changed

3 files changed

+190
-24
lines changed

autogen/agentchat/conversable_agent.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -950,9 +950,7 @@ async def a_generate_tool_calls_reply(
950950
message = messages[-1]
951951
async_tool_calls = []
952952
for tool_call in message.get("tool_calls", []):
953-
func = self._function_map.get(tool_call.get("function", {}).get("name", None), None)
954-
if func and asyncio.coroutines.iscoroutinefunction(func):
955-
async_tool_calls.append(self._a_execute_tool_call(tool_call))
953+
async_tool_calls.append(self._a_execute_tool_call(tool_call))
956954
if async_tool_calls:
957955
tool_returns = await asyncio.gather(*async_tool_calls)
958956
return True, {

notebook/agentchat_function_call_async.ipynb

+86-21
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
},
6262
{
6363
"cell_type": "code",
64-
"execution_count": 2,
64+
"execution_count": 5,
6565
"id": "dca301a4",
6666
"metadata": {},
6767
"outputs": [],
@@ -119,7 +119,7 @@
119119
},
120120
{
121121
"cell_type": "code",
122-
"execution_count": 3,
122+
"execution_count": 6,
123123
"id": "9fb85afb",
124124
"metadata": {},
125125
"outputs": [
@@ -134,14 +134,22 @@
134134
"--------------------------------------------------------------------------------\n",
135135
"\u001b[33mchatbot\u001b[0m (to user_proxy):\n",
136136
"\n",
137-
"\u001b[32m***** Suggested tool Call (call_thUjscBN349eGd6xh3XrVT18): timer *****\u001b[0m\n",
137+
"\u001b[32m***** Suggested tool Call (call_mZqUtVEf6hwarnCqdiKtVhAE): timer *****\u001b[0m\n",
138138
"Arguments: \n",
139-
"{\"num_seconds\":\"5\"}\n",
139+
"{\n",
140+
" \"num_seconds\": \"5\"\n",
141+
"}\n",
140142
"\u001b[32m**********************************************************************\u001b[0m\n",
141143
"\n",
142144
"--------------------------------------------------------------------------------\n",
143145
"\u001b[35m\n",
144-
">>>>>>>> EXECUTING ASYNC FUNCTION timer...\u001b[0m\n",
146+
">>>>>>>> EXECUTING ASYNC FUNCTION timer...\u001b[0m\n"
147+
]
148+
},
149+
{
150+
"name": "stdout",
151+
"output_type": "stream",
152+
"text": [
145153
"\u001b[33muser_proxy\u001b[0m (to chatbot):\n",
146154
"\n",
147155
"\u001b[33muser_proxy\u001b[0m (to chatbot):\n",
@@ -153,14 +161,16 @@
153161
"--------------------------------------------------------------------------------\n",
154162
"\u001b[33mchatbot\u001b[0m (to user_proxy):\n",
155163
"\n",
156-
"\u001b[32m***** Suggested tool Call (call_ubo7cKE3TKumGHkqGjQtZisy): stopwatch *****\u001b[0m\n",
164+
"\u001b[32m***** Suggested tool Call (call_xHz73PpH4ipdv7kTxK2AiVCf): stopwatch *****\u001b[0m\n",
157165
"Arguments: \n",
158-
"{\"num_seconds\":\"5\"}\n",
166+
"{\n",
167+
" \"num_seconds\": \"5\"\n",
168+
"}\n",
159169
"\u001b[32m**************************************************************************\u001b[0m\n",
160170
"\n",
161171
"--------------------------------------------------------------------------------\n",
162172
"\u001b[35m\n",
163-
">>>>>>>> EXECUTING FUNCTION stopwatch...\u001b[0m\n",
173+
">>>>>>>> EXECUTING ASYNC FUNCTION stopwatch...\u001b[0m\n",
164174
"\u001b[33muser_proxy\u001b[0m (to chatbot):\n",
165175
"\n",
166176
"\u001b[33muser_proxy\u001b[0m (to chatbot):\n",
@@ -172,8 +182,6 @@
172182
"--------------------------------------------------------------------------------\n",
173183
"\u001b[33mchatbot\u001b[0m (to user_proxy):\n",
174184
"\n",
175-
"Both the timer and the stopwatch for 5 seconds have been completed. \n",
176-
"\n",
177185
"TERMINATE\n",
178186
"\n",
179187
"--------------------------------------------------------------------------------\n"
@@ -245,7 +253,7 @@
245253
},
246254
{
247255
"cell_type": "code",
248-
"execution_count": 4,
256+
"execution_count": 7,
249257
"id": "2472f95c",
250258
"metadata": {},
251259
"outputs": [],
@@ -280,20 +288,77 @@
280288
},
281289
{
282290
"cell_type": "code",
283-
"execution_count": 5,
291+
"execution_count": 8,
284292
"id": "e2c9267a",
285293
"metadata": {},
286-
"outputs": [],
294+
"outputs": [
295+
{
296+
"name": "stdout",
297+
"output_type": "stream",
298+
"text": [
299+
"\u001b[33muser_proxy\u001b[0m (to chat_manager):\n",
300+
"\n",
301+
"\n",
302+
"1) Create a timer for 5 seconds.\n",
303+
"2) a stopwatch for 5 seconds.\n",
304+
"3) Pretty print the result as md.\n",
305+
"4) when 1-3 are done, terminate the group chat\n",
306+
"\n",
307+
"--------------------------------------------------------------------------------\n",
308+
"\u001b[33muser_proxy\u001b[0m (to chat_manager):\n",
309+
"\n",
310+
"\n",
311+
"\n",
312+
"--------------------------------------------------------------------------------\n",
313+
"\u001b[33mchatbot\u001b[0m (to chat_manager):\n",
314+
"\n",
315+
"\u001b[32m***** Suggested tool Call (call_rdYofiAgFpRfaZaCT4tzAhOb): timer *****\u001b[0m\n",
316+
"Arguments: \n",
317+
"{\n",
318+
" \"num_seconds\": \"5\"\n",
319+
"}\n",
320+
"\u001b[32m**********************************************************************\u001b[0m\n",
321+
"\n",
322+
"--------------------------------------------------------------------------------\n",
323+
"\u001b[35m\n",
324+
">>>>>>>> EXECUTING ASYNC FUNCTION timer...\u001b[0m\n",
325+
"\u001b[33muser_proxy\u001b[0m (to chat_manager):\n",
326+
"\n",
327+
"\u001b[33muser_proxy\u001b[0m (to chat_manager):\n",
328+
"\n",
329+
"\u001b[32m***** Response from calling tool \"timer\" *****\u001b[0m\n",
330+
"Timer is done!\n",
331+
"\u001b[32m**********************************************\u001b[0m\n",
332+
"\n",
333+
"--------------------------------------------------------------------------------\n",
334+
"\u001b[33muser_proxy\u001b[0m (to chat_manager):\n",
335+
"\n",
336+
"\n",
337+
"\n",
338+
"--------------------------------------------------------------------------------\n",
339+
"\u001b[33muser_proxy\u001b[0m (to chat_manager):\n",
340+
"\n",
341+
"\n",
342+
"\n",
343+
"--------------------------------------------------------------------------------\n",
344+
"\u001b[33muser_proxy\u001b[0m (to chat_manager):\n",
345+
"\n",
346+
"\n",
347+
"\n",
348+
"--------------------------------------------------------------------------------\n"
349+
]
350+
}
351+
],
287352
"source": [
288353
"# todo: remove comment after fixing https://github.com/microsoft/autogen/issues/1205\n",
289-
"# await user_proxy.a_initiate_chat( # noqa: F704\n",
290-
"# manager,\n",
291-
"# message=\"\"\"\n",
292-
"# 1) Create a timer for 5 seconds.\n",
293-
"# 2) a stopwatch for 5 seconds.\n",
294-
"# 3) Pretty print the result as md.\n",
295-
"# 4) when 1-3 are done, terminate the group chat\"\"\",\n",
296-
"# )"
354+
"await user_proxy.a_initiate_chat( # noqa: F704\n",
355+
" manager,\n",
356+
" message=\"\"\"\n",
357+
"1) Create a timer for 5 seconds.\n",
358+
"2) a stopwatch for 5 seconds.\n",
359+
"3) Pretty print the result as md.\n",
360+
"4) when 1-3 are done, terminate the group chat\"\"\",\n",
361+
")"
297362
]
298363
},
299364
{

test/agentchat/test_tool_calls.py

+103
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,109 @@ def receive(
278278
]
279279

280280

281+
@pytest.mark.skipif(not TOOL_ENABLED, reason="openai>=1.1.0 not installed")
282+
@pytest.mark.asyncio
283+
async def test_async_multi_tool_call():
284+
class FakeAgent(autogen.Agent):
285+
def __init__(self, name):
286+
super().__init__(name)
287+
self.received = []
288+
289+
async def a_receive(
290+
self,
291+
message,
292+
sender,
293+
request_reply=None,
294+
silent=False,
295+
):
296+
message = message if isinstance(message, list) else [message]
297+
self.received.extend(message)
298+
return ""
299+
300+
user_proxy = autogen.UserProxyAgent(
301+
name="user_proxy",
302+
human_input_mode="NEVER",
303+
is_termination_msg=lambda x: True if "TERMINATE" in x.get("content") else False,
304+
)
305+
306+
def echo(str):
307+
return str
308+
309+
async def a_echo(str):
310+
return str
311+
312+
user_proxy.register_function({"a_echo": a_echo, "echo": echo})
313+
314+
fake_agent = FakeAgent("fake_agent")
315+
316+
await user_proxy.a_receive(
317+
message={
318+
"content": "test multi tool call",
319+
"tool_calls": [
320+
{
321+
"id": "tool_1",
322+
"type": "function",
323+
"function": {"name": "a_echo", "arguments": json.JSONEncoder().encode({"str": "hello world"})},
324+
},
325+
{
326+
"id": "tool_2",
327+
"type": "function",
328+
"function": {
329+
"name": "echo",
330+
"arguments": json.JSONEncoder().encode({"str": "goodbye and thanks for all the fish"}),
331+
},
332+
},
333+
{
334+
"id": "tool_3",
335+
"type": "function",
336+
"function": {
337+
"name": "multi_tool_call_echo", # normalized "multi_tool_call.echo"
338+
"arguments": json.JSONEncoder().encode({"str": "goodbye and thanks for all the fish"}),
339+
},
340+
},
341+
],
342+
},
343+
sender=fake_agent,
344+
request_reply=True,
345+
)
346+
347+
assert fake_agent.received == [
348+
{
349+
"role": "tool",
350+
"tool_responses": [
351+
{"tool_call_id": "tool_1", "role": "tool", "name": "a_echo", "content": "hello world"},
352+
{
353+
"tool_call_id": "tool_2",
354+
"role": "tool",
355+
"name": "echo",
356+
"content": "goodbye and thanks for all the fish",
357+
},
358+
{
359+
"tool_call_id": "tool_3",
360+
"role": "tool",
361+
"name": "multi_tool_call_echo",
362+
"content": "Error: Function multi_tool_call_echo not found.",
363+
},
364+
],
365+
"content": inspect.cleandoc(
366+
"""
367+
Tool call: a_echo
368+
Id: tool_1
369+
hello world
370+
371+
Tool call: echo
372+
Id: tool_2
373+
goodbye and thanks for all the fish
374+
375+
Tool call: multi_tool_call_echo
376+
Id: tool_3
377+
Error: Function multi_tool_call_echo not found.
378+
"""
379+
),
380+
}
381+
]
382+
383+
281384
if __name__ == "__main__":
282385
# test_update_tool()
283386
# test_eval_math_responses()

0 commit comments

Comments
 (0)