Skip to content

Commit d527573

Browse files
authored
support async func in custom_build_fn (#195)
support async func in `custom_build_fn`
1 parent 4eb3834 commit d527573

4 files changed

Lines changed: 28 additions & 6 deletions

File tree

src/agentscope_runtime/engine/agents/agentscope_agent.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
# pylint:disable=too-many-nested-blocks, too-many-branches, too-many-statements
33
# pylint:disable=line-too-long, protected-access
4+
import inspect
45
import copy
56
import logging
67
import json
@@ -250,7 +251,12 @@ async def run_async(
250251
# We should always build a new agent since the state is manage outside
251252
# the agent
252253
if self._attr.get("custom_build_fn"):
253-
_agent = self._attr["custom_build_fn"](as_context, **kwargs)
254+
build_fn = self._attr["custom_build_fn"]
255+
256+
if inspect.iscoroutinefunction(build_fn):
257+
_agent = await build_fn(as_context, **kwargs)
258+
else:
259+
_agent = build_fn(as_context, **kwargs)
254260
else:
255261
_agent = self.build(as_context)
256262

src/agentscope_runtime/engine/agents/agno_agent.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# pylint:disable=too-many-nested-blocks, too-many-branches, too-many-statements
3+
import inspect
34
import json
45
from typing import Optional, Type, Callable
56

@@ -164,7 +165,12 @@ async def run_async(
164165
# We should always build a new agent since the state is manage outside
165166
# the agent
166167
if self._attr.get("custom_build_fn"):
167-
_agent = self._attr["custom_build_fn"](ag_context, **kwargs)
168+
build_fn = self._attr["custom_build_fn"]
169+
170+
if inspect.iscoroutinefunction(build_fn):
171+
_agent = await build_fn(ag_context, **kwargs)
172+
else:
173+
_agent = build_fn(ag_context, **kwargs)
168174
else:
169175
_agent = self.build(ag_context)
170176

src/agentscope_runtime/engine/agents/autogen_agent.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
import inspect
23
from typing import Optional, Type, Callable
34

45
from autogen_core.models import ChatCompletionClient
@@ -169,7 +170,12 @@ async def run_async(
169170
# We should always build a new agent since the state is manage outside
170171
# the agent
171172
if self._attr.get("custom_build_fn"):
172-
_agent = self._attr["custom_build_fn"](ag_context, **kwargs)
173+
build_fn = self._attr["custom_build_fn"]
174+
175+
if inspect.iscoroutinefunction(build_fn):
176+
_agent = await build_fn(ag_context, **kwargs)
177+
else:
178+
_agent = build_fn(ag_context, **kwargs)
173179
else:
174180
_agent = self.build(ag_context)
175181

src/agentscope_runtime/engine/deployers/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
from .modelstudio_deployer import (
88
ModelstudioDeployManager,
99
)
10-
from .agentrun_deployer import (
11-
AgentRunDeployManager,
12-
)
10+
11+
try:
12+
from .agentrun_deployer import (
13+
AgentRunDeployManager,
14+
)
15+
except ImportError:
16+
AgentRunDeployManager = None # type: ignore
1317

1418
__all__ = [
1519
"DeployManager",

0 commit comments

Comments
 (0)