Skip to content

Commit

Permalink
Fix the bug of react agent in workstation (#379)
Browse files Browse the repository at this point in the history
TODO: This is only a temporary fix, which requires further improvement.
  • Loading branch information
qbc2016 authored Aug 6, 2024
1 parent 1329860 commit 21441b0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
5 changes: 5 additions & 0 deletions src/agentscope/service/service_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ def bing_search(query: str, api_key: str, num_results=10):
"""

# TODO: hotfix for workstation, will be removed in the future
if isinstance(service_func, partial):
self.add(service_func.func, **service_func.keywords)
return

processed_func, json_schema = ServiceToolkit.get(
service_func,
**kwargs,
Expand Down
56 changes: 33 additions & 23 deletions src/agentscope/web/workstation/workflow_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""Workflow node opt."""
from abc import ABC, abstractmethod
from enum import IntEnum
from functools import partial
from typing import List, Optional

from agentscope import msghub
Expand Down Expand Up @@ -33,7 +34,7 @@
read_text_file,
write_text_file,
execute_python_code,
ServiceFactory,
ServiceToolkit,
)

DEFAULT_FLOW_VAR = "flow"
Expand Down Expand Up @@ -120,8 +121,9 @@ def __init__(

def compile(self) -> dict:
return {
"imports": "from agentscope.models import read_model_configs",
"inits": f"read_model_configs([{self.opt_kwargs}])",
"imports": "from agentscope.manager import ModelManager",
"inits": f"ModelManager.get_instance().load_model_configs("
f"[{self.opt_kwargs}])",
"execs": "",
}

Expand Down Expand Up @@ -294,22 +296,32 @@ def __init__(
) -> None:
super().__init__(node_id, opt_kwargs, source_kwargs, dep_opts)
# Build tools
self.tools = []
self.service_toolkit = ServiceToolkit()
for tool in dep_opts:
if not hasattr(tool, "service_func"):
raise TypeError(f"{tool} must be tool!")
self.tools.append(tool.service_func)
self.pipeline = ReActAgent(tools=self.tools, **self.opt_kwargs)
self.service_toolkit.add(tool.service_func)
self.pipeline = ReActAgent(
service_toolkit=self.service_toolkit,
**self.opt_kwargs,
)

def __call__(self, x: dict = None) -> dict:
return self.pipeline(x)

def compile(self) -> dict:
tools = deps_converter(self.dep_vars)[1:-1].split(",")
service_toolkit_code = ";".join(
f"{self.var_name}_service_toolkit.add({tool.strip()})"
for tool in tools
)
return {
"imports": "from agentscope.agents import ReActAgent",
"inits": f"{self.var_name} = ReActAgent"
f"({kwarg_converter(self.opt_kwargs)}, tools"
f"={deps_converter(self.dep_vars)})",
"inits": f"{self.var_name}_service_toolkit = ServiceToolkit()\n"
f" {service_toolkit_code}\n"
f" {self.var_name} = ReActAgent"
f"({kwarg_converter(self.opt_kwargs)}, service_toolkit"
f"={self.var_name}_service_toolkit)",
"execs": f"{DEFAULT_FLOW_VAR} = {self.var_name}"
f"({DEFAULT_FLOW_VAR})",
}
Expand Down Expand Up @@ -701,13 +713,14 @@ def __init__(
dep_opts: list,
) -> None:
super().__init__(node_id, opt_kwargs, source_kwargs, dep_opts)
self.service_func = ServiceFactory.get(bing_search, **self.opt_kwargs)
self.service_func = partial(bing_search, **self.opt_kwargs)

def compile(self) -> dict:
return {
"imports": "from agentscope.service import ServiceFactory\n"
"from functools import partial\n"
"from agentscope.service import bing_search",
"inits": f"{self.var_name} = ServiceFactory.get(bing_search,"
"inits": f"{self.var_name} = partial(bing_search,"
f" {kwarg_converter(self.opt_kwargs)})",
"execs": "",
}
Expand All @@ -728,16 +741,14 @@ def __init__(
dep_opts: list,
) -> None:
super().__init__(node_id, opt_kwargs, source_kwargs, dep_opts)
self.service_func = ServiceFactory.get(
google_search,
**self.opt_kwargs,
)
self.service_func = partial(google_search, **self.opt_kwargs)

def compile(self) -> dict:
return {
"imports": "from agentscope.service import ServiceFactory\n"
"from functools import partial\n"
"from agentscope.service import google_search",
"inits": f"{self.var_name} = ServiceFactory.get(google_search,"
"inits": f"{self.var_name} = partial(google_search,"
f" {kwarg_converter(self.opt_kwargs)})",
"execs": "",
}
Expand All @@ -758,14 +769,13 @@ def __init__(
dep_opts: list,
) -> None:
super().__init__(node_id, opt_kwargs, source_kwargs, dep_opts)
self.service_func = ServiceFactory.get(execute_python_code)
self.service_func = execute_python_code

def compile(self) -> dict:
return {
"imports": "from agentscope.service import ServiceFactory\n"
"from agentscope.service import execute_python_code",
"inits": f"{self.var_name} = ServiceFactory.get("
f"execute_python_code)",
"inits": f"{self.var_name} = execute_python_code",
"execs": "",
}

Expand All @@ -785,13 +795,13 @@ def __init__(
dep_opts: list,
) -> None:
super().__init__(node_id, opt_kwargs, source_kwargs, dep_opts)
self.service_func = ServiceFactory.get(read_text_file)
self.service_func = read_text_file

def compile(self) -> dict:
return {
"imports": "from agentscope.service import ServiceFactory\n"
"from agentscope.service import read_text_file",
"inits": f"{self.var_name} = ServiceFactory.get(read_text_file)",
"inits": f"{self.var_name} = read_text_file",
"execs": "",
}

Expand All @@ -811,13 +821,13 @@ def __init__(
dep_opts: list,
) -> None:
super().__init__(node_id, opt_kwargs, source_kwargs, dep_opts)
self.service_func = ServiceFactory.get(write_text_file)
self.service_func = write_text_file

def compile(self) -> dict:
return {
"imports": "from agentscope.service import ServiceFactory\n"
"from agentscope.service import write_text_file",
"inits": f"{self.var_name} = ServiceFactory.get(write_text_file)",
"inits": f"{self.var_name} = write_text_file",
"execs": "",
}

Expand Down

0 comments on commit 21441b0

Please sign in to comment.