Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced asyncio.coroutines.iscoroutinefunction with inspect.iscoroutinefunction #1267

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion autogen/agentchat/contrib/compressible_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import copy
import asyncio
import logging
import inspect
from autogen.token_count_utils import count_token, get_max_token_limit, num_tokens_from_functions

try:
Expand Down Expand Up @@ -201,7 +202,7 @@ def generate_reply(
reply_func = reply_func_tuple["reply_func"]
if exclude and reply_func in exclude:
continue
if asyncio.coroutines.iscoroutinefunction(reply_func):
if inspect.iscoroutinefunction(reply_func):
continue
if self._match_trigger(reply_func_tuple["trigger"], sender):
final, reply = reply_func(self, messages=messages, sender=sender, config=reply_func_tuple["config"])
Expand Down
16 changes: 8 additions & 8 deletions autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def reply_func(
"reset_config": reset_config,
},
)
if ignore_async_in_sync_chat and asyncio.coroutines.iscoroutinefunction(reply_func):
if ignore_async_in_sync_chat and inspect.iscoroutinefunction(reply_func):
self._ignore_async_func_in_sync_chat_list.append(reply_func)

@property
Expand Down Expand Up @@ -629,7 +629,7 @@ def _raise_exception_on_async_reply_functions(self) -> None:
self._ignore_async_func_in_sync_chat_list
)

async_reply_functions = [f for f in reply_functions if asyncio.coroutines.iscoroutinefunction(f)]
async_reply_functions = [f for f in reply_functions if inspect.iscoroutinefunction(f)]
if async_reply_functions != []:
msg = (
"Async reply functions can only be used with ConversableAgent.a_initiate_chat(). The following async reply functions are found: "
Expand Down Expand Up @@ -849,7 +849,7 @@ def generate_function_call_reply(
if "function_call" in message and message["function_call"]:
func_call = message["function_call"]
func = self._function_map.get(func_call.get("name", None), None)
if asyncio.coroutines.iscoroutinefunction(func):
if inspect.iscoroutinefunction(func):
return False, None

_, func_return = self.execute_function(message["function_call"])
Expand Down Expand Up @@ -877,7 +877,7 @@ async def a_generate_function_call_reply(
func_call = message["function_call"]
func_name = func_call.get("name", "")
func = self._function_map.get(func_name, None)
if func and asyncio.coroutines.iscoroutinefunction(func):
if func and inspect.iscoroutinefunction(func):
_, func_return = await self.a_execute_function(func_call)
return True, func_return

Expand Down Expand Up @@ -906,7 +906,7 @@ def generate_tool_calls_reply(
id = tool_call["id"]
function_call = tool_call.get("function", {})
func = self._function_map.get(function_call.get("name", None), None)
if asyncio.coroutines.iscoroutinefunction(func):
if inspect.iscoroutinefunction(func):
continue
_, func_return = self.execute_function(function_call)
tool_returns.append(
Expand Down Expand Up @@ -1231,7 +1231,7 @@ def generate_reply(
reply_func = reply_func_tuple["reply_func"]
if exclude and reply_func in exclude:
continue
if asyncio.coroutines.iscoroutinefunction(reply_func):
if inspect.iscoroutinefunction(reply_func):
continue
if self._match_trigger(reply_func_tuple["trigger"], sender):
final, reply = reply_func(self, messages=messages, sender=sender, config=reply_func_tuple["config"])
Expand Down Expand Up @@ -1288,7 +1288,7 @@ async def a_generate_reply(
if exclude and reply_func in exclude:
continue
if self._match_trigger(reply_func_tuple["trigger"], sender):
if asyncio.coroutines.iscoroutinefunction(reply_func):
if inspect.iscoroutinefunction(reply_func):
final, reply = await reply_func(
self, messages=messages, sender=sender, config=reply_func_tuple["config"]
)
Expand Down Expand Up @@ -1537,7 +1537,7 @@ async def a_execute_function(self, func_call):
flush=True,
)
try:
if asyncio.coroutines.iscoroutinefunction(func):
if inspect.iscoroutinefunction(func):
content = await func(**arguments)
else:
# Fallback to sync function if the function is not async
Expand Down
5 changes: 3 additions & 2 deletions test/agentchat/test_conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time
from typing import Any, Callable, Dict, Literal
import unittest
import inspect

import pytest
from unittest.mock import patch
Expand Down Expand Up @@ -559,7 +560,7 @@ def currency_calculator(
== '{"currency":"EUR","amount":100.1}'
)

assert not asyncio.coroutines.iscoroutinefunction(currency_calculator)
assert not inspect.iscoroutinefunction(currency_calculator)


@pytest.mark.asyncio
Expand Down Expand Up @@ -597,7 +598,7 @@ async def currency_calculator(
== '{"currency":"EUR","amount":100.1}'
)

assert asyncio.coroutines.iscoroutinefunction(currency_calculator)
assert inspect.iscoroutinefunction(currency_calculator)


def get_origin(d: Dict[str, Callable[..., Any]]) -> Dict[str, Callable[..., Any]]:
Expand Down
4 changes: 2 additions & 2 deletions test/test_function_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def f(
) -> Tuple[Currency, CurrencySymbol]:
return base, quote_currency

assert not asyncio.coroutines.iscoroutinefunction(f)
assert not inspect.iscoroutinefunction(f)

actual = f(base={"currency": "USD", "amount": 123.45}, quote_currency="EUR")
assert isinstance(actual[0], Currency)
Expand All @@ -382,7 +382,7 @@ async def f(
) -> Tuple[Currency, CurrencySymbol]:
return base, quote_currency

assert asyncio.coroutines.iscoroutinefunction(f)
assert inspect.iscoroutinefunction(f)

actual = await f(base={"currency": "USD", "amount": 123.45}, quote_currency="EUR")
assert isinstance(actual[0], Currency)
Expand Down
Loading