diff --git a/en/.doctrees/agentscope.agents.agent.doctree b/en/.doctrees/agentscope.agents.agent.doctree index 69d9e4245..a286facdc 100644 Binary files a/en/.doctrees/agentscope.agents.agent.doctree and b/en/.doctrees/agentscope.agents.agent.doctree differ diff --git a/en/.doctrees/agentscope.agents.doctree b/en/.doctrees/agentscope.agents.doctree index e69224968..4c9629026 100644 Binary files a/en/.doctrees/agentscope.agents.doctree and b/en/.doctrees/agentscope.agents.doctree differ diff --git a/en/.doctrees/agentscope.agents.rpc_agent.doctree b/en/.doctrees/agentscope.agents.rpc_agent.doctree index d840a4249..60610f663 100644 Binary files a/en/.doctrees/agentscope.agents.rpc_agent.doctree and b/en/.doctrees/agentscope.agents.rpc_agent.doctree differ diff --git a/en/.doctrees/agentscope.message.doctree b/en/.doctrees/agentscope.message.doctree index 8038d6da9..271b2b147 100644 Binary files a/en/.doctrees/agentscope.message.doctree and b/en/.doctrees/agentscope.message.doctree differ diff --git a/en/.doctrees/agentscope.server.doctree b/en/.doctrees/agentscope.server.doctree new file mode 100644 index 000000000..7fda2661d Binary files /dev/null and b/en/.doctrees/agentscope.server.doctree differ diff --git a/en/.doctrees/agentscope.server.launcher.doctree b/en/.doctrees/agentscope.server.launcher.doctree new file mode 100644 index 000000000..177bd97e6 Binary files /dev/null and b/en/.doctrees/agentscope.server.launcher.doctree differ diff --git a/en/.doctrees/agentscope.server.servicer.doctree b/en/.doctrees/agentscope.server.servicer.doctree new file mode 100644 index 000000000..1ac656881 Binary files /dev/null and b/en/.doctrees/agentscope.server.servicer.doctree differ diff --git a/en/.doctrees/agentscope.utils.tools.doctree b/en/.doctrees/agentscope.utils.tools.doctree index cbe560e9b..aa85acbd8 100644 Binary files a/en/.doctrees/agentscope.utils.tools.doctree and b/en/.doctrees/agentscope.utils.tools.doctree differ diff --git a/en/.doctrees/environment.pickle b/en/.doctrees/environment.pickle index 81f5bdb5f..83d44a225 100644 Binary files a/en/.doctrees/environment.pickle and b/en/.doctrees/environment.pickle differ diff --git a/en/.doctrees/index.doctree b/en/.doctrees/index.doctree index f39b8de1d..94542b55c 100644 Binary files a/en/.doctrees/index.doctree and b/en/.doctrees/index.doctree differ diff --git a/en/.doctrees/tutorial/208-distribute.doctree b/en/.doctrees/tutorial/208-distribute.doctree index b68ad284e..3c71f7916 100644 Binary files a/en/.doctrees/tutorial/208-distribute.doctree and b/en/.doctrees/tutorial/208-distribute.doctree differ diff --git a/en/_modules/agentscope/_init.html b/en/_modules/agentscope/_init.html index 6da3e2e29..347c46889 100644 --- a/en/_modules/agentscope/_init.html +++ b/en/_modules/agentscope/_init.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/agents/agent.html b/en/_modules/agentscope/agents/agent.html index 558638dce..dbbe104ab 100644 --- a/en/_modules/agentscope/agents/agent.html +++ b/en/_modules/agentscope/agents/agent.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • @@ -123,7 +124,7 @@

    Source code for agentscope.agents.agent

         """
     
         def __init__(cls, name: Any, bases: Any, attrs: Any) -> None:
    -        if not hasattr(cls, "registry"):
    +        if not hasattr(cls, "_registry"):
                 cls._registry = {}
             else:
                 if name in cls._registry:
    @@ -363,7 +364,7 @@ 

    Source code for agentscope.agents.agent

             """
             agent_class_name = agent_class.__name__
             if agent_class_name in cls._registry:
    -            logger.warning(
    +            logger.info(
                     f"Agent class with name [{agent_class_name}] already exists.",
                 )
             else:
    @@ -532,14 +533,22 @@ 

    Source code for agentscope.agents.agent

                 port (`int`, defaults to `None`):
                     Port of the rpc agent server.
                 max_pool_size (`int`, defaults to `8192`):
    -                Max number of task results that the server can accommodate.
    +                Only takes effect when `host` and `port` are not filled in.
    +                The max number of agent reply messages that the started agent
    +                server can accommodate. Note that the oldest message will be
    +                deleted after exceeding the pool size.
                 max_timeout_seconds (`int`, defaults to `1800`):
    -                Timeout for task results.
    +                Only takes effect when `host` and `port` are not filled in.
    +                Maximum time for reply messages to be cached in the launched
    +                agent server. Note that expired messages will be deleted.
                 local_mode (`bool`, defaults to `True`):
    -                Whether the started rpc server only listens to local
    +                Only takes effect when `host` and `port` are not filled in.
    +                Whether the started agent server only listens to local
                     requests.
                 lazy_launch (`bool`, defaults to `True`):
    -                Only launch the server when the agent is called.
    +                Only takes effect when `host` and `port` are not filled in.
    +                If `True`, launch the agent server when the agent is called,
    +                otherwise, launch the agent server immediately.
                 launch_server(`bool`, defaults to `None`):
                     This field has been deprecated and will be removed in
                     future releases.
    diff --git a/en/_modules/agentscope/agents/dialog_agent.html b/en/_modules/agentscope/agents/dialog_agent.html
    index c1b08ab12..75de091fb 100644
    --- a/en/_modules/agentscope/agents/dialog_agent.html
    +++ b/en/_modules/agentscope/agents/dialog_agent.html
    @@ -67,6 +67,7 @@
     
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/agents/dict_dialog_agent.html b/en/_modules/agentscope/agents/dict_dialog_agent.html index e0e97e43b..faf47374a 100644 --- a/en/_modules/agentscope/agents/dict_dialog_agent.html +++ b/en/_modules/agentscope/agents/dict_dialog_agent.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/agents/operator.html b/en/_modules/agentscope/agents/operator.html index b2912fd50..00180cb2b 100644 --- a/en/_modules/agentscope/agents/operator.html +++ b/en/_modules/agentscope/agents/operator.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/agents/react_agent.html b/en/_modules/agentscope/agents/react_agent.html index f2f725d5a..2c61a361a 100644 --- a/en/_modules/agentscope/agents/react_agent.html +++ b/en/_modules/agentscope/agents/react_agent.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/agents/rpc_agent.html b/en/_modules/agentscope/agents/rpc_agent.html index f39feb670..e76ff5a31 100644 --- a/en/_modules/agentscope/agents/rpc_agent.html +++ b/en/_modules/agentscope/agents/rpc_agent.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • @@ -99,67 +100,15 @@

    Source code for agentscope.agents.rpc_agent

     # -*- coding: utf-8 -*-
     """ Base class for Rpc Agent """
    -
    -from multiprocessing import Process, Event, Pipe
    -from multiprocessing.synchronize import Event as EventClass
    -import socket
    -import threading
    -import json
    -import base64
    -import traceback
    -import asyncio
     from typing import Type, Optional, Union, Sequence
    -from concurrent import futures
    -from loguru import logger
    -
    -try:
    -    import dill
    -    import grpc
    -    from grpc import ServicerContext
    -    from expiringdict import ExpiringDict
    -except ImportError as import_error:
    -    from agentscope.utils.tools import ImportErrorReporter
    -
    -    dill = ImportErrorReporter(import_error, "distribute")
    -    grpc = ImportErrorReporter(import_error, "distribute")
    -    ServicerContext = ImportErrorReporter(import_error, "distribute")
    -    ExpiringDict = ImportErrorReporter(import_error, "distribute")
    -
    -from agentscope._init import init_process, _INIT_SETTINGS
    +
     from agentscope.agents.agent import AgentBase
     from agentscope.message import (
    -    Msg,
         PlaceholderMessage,
    -    deserialize,
         serialize,
     )
    -from agentscope.rpc import (
    -    RpcAgentClient,
    -    RpcMsg,
    -    RpcAgentServicer,
    -    add_RpcAgentServicer_to_server,
    -)
    -
    -
    -
    -[docs] -def rpc_servicer_method( # type: ignore[no-untyped-def] - func, -): - """A decorator used to identify that the specific method is an rpc agent - servicer method, which can only be run in the rpc server process. - """ - - def inner(rpc_agent, msg): # type: ignore[no-untyped-def] - if not rpc_agent.is_servicer: - error_msg = f"Detect main process try to use rpc servicer method \ - [{func.__name__}]" - logger.error(error_msg) - raise RuntimeError(error_msg) - return func(rpc_agent, msg) - - return inner
    - +from agentscope.rpc import RpcAgentClient +from agentscope.server.launcher import RpcAgentServerLauncher
    @@ -339,645 +288,6 @@

    Source code for agentscope.agents.rpc_agent

         def __del__(self) -> None:
             self.stop()
    - - -
    -[docs] -def setup_rpc_agent_server( - host: str, - port: int, - init_settings: dict = None, - start_event: EventClass = None, - stop_event: EventClass = None, - pipe: int = None, - local_mode: bool = True, - max_pool_size: int = 8192, - max_timeout_seconds: int = 1800, - custom_agents: list = None, -) -> None: - """Setup gRPC server rpc agent. - - Args: - host (`str`, defaults to `"localhost"`): - Hostname of the rpc agent server. - port (`int`): - The socket port monitored by grpc server. - init_settings (`dict`, defaults to `None`): - Init settings for agentscope.init. - start_event (`EventClass`, defaults to `None`): - An Event instance used to determine whether the child process - has been started. - stop_event (`EventClass`, defaults to `None`): - The stop Event instance used to determine whether the child - process has been stopped. - pipe (`int`, defaults to `None`): - A pipe instance used to pass the actual port of the server. - local_mode (`bool`, defaults to `None`): - Only listen to local requests. - max_pool_size (`int`, defaults to `8192`): - Max number of task results that the server can accommodate. - max_timeout_seconds (`int`, defaults to `1800`): - Timeout for task results. - custom_agents (`list`, defaults to `None`): - A list of custom agent classes that are not in `agentscope.agents`. - """ - asyncio.run( - setup_rpc_agent_server_async( - host=host, - port=port, - init_settings=init_settings, - start_event=start_event, - stop_event=stop_event, - pipe=pipe, - local_mode=local_mode, - max_pool_size=max_pool_size, - max_timeout_seconds=max_timeout_seconds, - custom_agents=custom_agents, - ), - )
    - - - -
    -[docs] -async def setup_rpc_agent_server_async( - host: str, - port: int, - init_settings: dict = None, - start_event: EventClass = None, - stop_event: EventClass = None, - pipe: int = None, - local_mode: bool = True, - max_pool_size: int = 8192, - max_timeout_seconds: int = 1800, - custom_agents: list = None, -) -> None: - """Setup gRPC server rpc agent in an async way. - - Args: - host (`str`, defaults to `"localhost"`): - Hostname of the rpc agent server. - port (`int`): - The socket port monitored by grpc server. - init_settings (`dict`, defaults to `None`): - Init settings for agentscope.init. - start_event (`EventClass`, defaults to `None`): - An Event instance used to determine whether the child process - has been started. - stop_event (`EventClass`, defaults to `None`): - The stop Event instance used to determine whether the child - process has been stopped. - pipe (`int`, defaults to `None`): - A pipe instance used to pass the actual port of the server. - local_mode (`bool`, defaults to `None`): - Only listen to local requests. - max_pool_size (`int`, defaults to `8192`): - Max number of task results that the server can accommodate. - max_timeout_seconds (`int`, defaults to `1800`): - Timeout for task results. - custom_agents (`list`, defaults to `None`): - A list of custom agent classes that are not in `agentscope.agents`. - """ - - if init_settings is not None: - init_process(**init_settings) - servicer = AgentPlatform( - host=host, - port=port, - max_pool_size=max_pool_size, - max_timeout_seconds=max_timeout_seconds, - ) - # update agent registry - if custom_agents is not None: - for agent_class in custom_agents: - AgentBase.register_agent_class(agent_class=agent_class) - while True: - try: - port = check_port(port) - servicer.port = port - logger.info( - f"Starting rpc server at port [{port}]...", - ) - server = grpc.aio.server( - futures.ThreadPoolExecutor(max_workers=None), - ) - add_RpcAgentServicer_to_server(servicer, server) - if local_mode: - server.add_insecure_port(f"localhost:{port}") - else: - server.add_insecure_port(f"0.0.0.0:{port}") - await server.start() - break - except OSError: - logger.warning( - f"Failed to start rpc server at port [{port}]" - f"try another port", - ) - logger.info( - f"rpc server at port [{port}] started successfully", - ) - if start_event is not None: - pipe.send(port) - start_event.set() - while not stop_event.is_set(): - await asyncio.sleep(1) - logger.info( - f"Stopping rpc server at port [{port}]", - ) - await server.stop(10.0) - else: - await server.wait_for_termination() - logger.info( - f"rpc server at port [{port}] stopped successfully", - )
    - - - -
    -[docs] -def find_available_port() -> int: - """Get an unoccupied socket port number.""" - with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: - s.bind(("", 0)) - return s.getsockname()[1]
    - - - -
    -[docs] -def check_port(port: Optional[int] = None) -> int: - """Check if the port is available. - - Args: - port (`int`): - the port number being checked. - - Returns: - `int`: the port number that passed the check. If the port is found - to be occupied, an available port number will be automatically - returned. - """ - if port is None: - new_port = find_available_port() - logger.warning( - "gRpc server port is not provided, automatically select " - f"[{new_port}] as the port number.", - ) - return new_port - with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: - if s.connect_ex(("localhost", port)) == 0: - new_port = find_available_port() - logger.warning( - f"Port [{port}] is occupied, use [{new_port}] instead", - ) - return new_port - return port
    - - - -
    -[docs] -class RpcAgentServerLauncher: - """The launcher of AgentPlatform (formerly RpcAgentServer).""" - -
    -[docs] - def __init__( - self, - host: str = "localhost", - port: int = None, - max_pool_size: int = 8192, - max_timeout_seconds: int = 1800, - local_mode: bool = False, - custom_agents: list = None, - agent_class: Type[AgentBase] = None, - agent_args: tuple = (), - agent_kwargs: dict = None, - ) -> None: - """Init a rpc agent server launcher. - - Args: - host (`str`, defaults to `"localhost"`): - Hostname of the rpc agent server. - port (`int`, defaults to `None`): - Port of the rpc agent server. - max_pool_size (`int`, defaults to `8192`): - Max number of task results that the server can accommodate. - max_timeout_seconds (`int`, defaults to `1800`): - Timeout for task results. - local_mode (`bool`, defaults to `False`): - Whether the started rpc server only listens to local - requests. - custom_agents (`list`, defaults to `None`): - A list of custom agent classes that are not in - `agentscope.agents`. - agent_class (`Type[AgentBase]`, deprecated): - The AgentBase subclass encapsulated by this wrapper. - agent_args (`tuple`, deprecated): The args tuple used to - initialize the agent_class. - agent_kwargs (`dict`, deprecated): The args dict used to - initialize the agent_class. - """ - self.host = host - self.port = check_port(port) - self.max_pool_size = max_pool_size - self.max_timeout_seconds = max_timeout_seconds - self.local_mode = local_mode - self.server = None - self.stop_event = None - self.parent_con = None - self.custom_agents = custom_agents - if ( - agent_class is not None - or len(agent_args) > 0 - or agent_kwargs is not None - ): - logger.warning( - "`agent_class`, `agent_args` and `agent_kwargs` is deprecated" - " in `RpcAgentServerLauncher`", - )
    - - - def _launch_in_main(self) -> None: - """Launch gRPC server in main-process""" - logger.info( - f"Launching agent server at [{self.host}:{self.port}]...", - ) - asyncio.run( - setup_rpc_agent_server_async( - host=self.host, - port=self.port, - max_pool_size=self.max_pool_size, - max_timeout_seconds=self.max_timeout_seconds, - local_mode=self.local_mode, - custom_agents=self.custom_agents, - ), - ) - - def _launch_in_sub(self) -> None: - """Launch gRPC server in sub-process.""" - self.stop_event = Event() - self.parent_con, child_con = Pipe() - start_event = Event() - server_process = Process( - target=setup_rpc_agent_server, - kwargs={ - "host": self.host, - "port": self.port, - "init_settings": _INIT_SETTINGS, - "start_event": start_event, - "stop_event": self.stop_event, - "pipe": child_con, - "max_pool_size": self.max_pool_size, - "max_timeout_seconds": self.max_timeout_seconds, - "local_mode": self.local_mode, - "custom_agents": self.custom_agents, - }, - ) - server_process.start() - self.port = self.parent_con.recv() - start_event.wait() - self.server = server_process - logger.info( - f"Launch agent server at [{self.host}:{self.port}] success", - ) - -
    -[docs] - def launch(self, in_subprocess: bool = True) -> None: - """launch a rpc agent server. - - Args: - in_subprocess (bool, optional): launch the server in subprocess. - Defaults to True. For agents that need to obtain command line - input, such as UserAgent, please set this value to False. - """ - if in_subprocess: - self._launch_in_sub() - else: - self._launch_in_main()
    - - -
    -[docs] - def wait_until_terminate(self) -> None: - """Wait for server process""" - if self.server is not None: - self.server.join()
    - - -
    -[docs] - def shutdown(self) -> None: - """Shutdown the rpc agent server.""" - if self.server is not None: - if self.stop_event is not None: - self.stop_event.set() - self.stop_event = None - self.server.join() - if self.server.is_alive(): - self.server.kill() - logger.info( - f"Agent server at port [{self.port}] is killed.", - ) - self.server = None
    -
    - - - -
    -[docs] -class AgentPlatform(RpcAgentServicer): - """A platform for agent to run on (formerly RpcServerSideWrapper)""" - -
    -[docs] - def __init__( - self, - host: str = "localhost", - port: int = None, - max_pool_size: int = 8192, - max_timeout_seconds: int = 1800, - ): - """Init the AgentPlatform. - - Args: - host (`str`, defaults to "localhost"): - Hostname of the rpc agent server. - port (`int`, defaults to `None`): - Port of the rpc agent server. - max_pool_size (`int`, defaults to `8192`): - The max number of task results that the server can - accommodate. Note that the oldest result will be deleted - after exceeding the pool size. - max_timeout_seconds (`int`, defaults to `1800`): - Timeout for task results. Note that expired results will be - deleted. - """ - self.host = host - self.port = port - self.result_pool = ExpiringDict( - max_len=max_pool_size, - max_age_seconds=max_timeout_seconds, - ) - self.executor = futures.ThreadPoolExecutor(max_workers=None) - self.task_id_lock = threading.Lock() - self.agent_id_lock = threading.Lock() - self.task_id_counter = 0 - self.agent_pool: dict[str, AgentBase] = {}
    - - -
    -[docs] - def get_task_id(self) -> int: - """Get the auto-increment task id.""" - with self.task_id_lock: - self.task_id_counter += 1 - return self.task_id_counter
    - - -
    -[docs] - def agent_exists(self, agent_id: str) -> bool: - """Check whether the agent exists. - - Args: - agent_id (`str`): the agent id. - - Returns: - bool: whether the agent exists. - """ - return agent_id in self.agent_pool
    - - -
    -[docs] - def check_and_generate_agent( - self, - agent_id: str, - agent_configs: dict, - ) -> None: - """ - Check whether the agent exists, and create new agent instance - for new agent. - - Args: - agent_id (`str`): the agent id. - agent_configs (`dict`): configuration used to initialize the agent, - with three fields (generated in `_AgentMeta`): - - .. code-block:: python - - { - "class_name": {name of the agent} - "args": {args in tuple type to init the agent} - "kwargs": {args in dict type to init the agent} - } - - """ - with self.agent_id_lock: - if agent_id not in self.agent_pool: - agent_class_name = agent_configs["class_name"] - agent_instance = AgentBase.get_agent_class(agent_class_name)( - *agent_configs["args"], - **agent_configs["kwargs"], - ) - agent_instance._agent_id = agent_id # pylint: disable=W0212 - self.agent_pool[agent_id] = agent_instance - logger.info(f"create agent instance [{agent_id}]")
    - - -
    -[docs] - def check_and_delete_agent(self, agent_id: str) -> None: - """ - Check whether the agent exists, and delete the agent instance - for the agent_id. - - Args: - agent_id (`str`): the agent id. - """ - with self.agent_id_lock: - if agent_id in self.agent_pool: - self.agent_pool.pop(agent_id) - logger.info(f"delete agent instance [{agent_id}]")
    - - -
    -[docs] - def call_func( # pylint: disable=W0236 - self, - request: RpcMsg, - context: ServicerContext, - ) -> RpcMsg: - """Call the specific servicer function.""" - if hasattr(self, request.target_func): - if request.target_func not in ["_create_agent", "_get"]: - if not self.agent_exists(request.agent_id): - return context.abort( - grpc.StatusCode.INVALID_ARGUMENT, - f"Agent [{request.agent_id}] not exists.", - ) - return getattr(self, request.target_func)(request) - else: - # TODO: support other user defined method - logger.error(f"Unsupported method {request.target_func}") - return context.abort( - grpc.StatusCode.INVALID_ARGUMENT, - f"Unsupported method {request.target_func}", - )
    - - - def _reply(self, request: RpcMsg) -> RpcMsg: - """Call function of RpcAgentService - - Args: - request (`RpcMsg`): - Message containing input parameters or input parameter - placeholders. - - Returns: - `RpcMsg`: A serialized Msg instance with attributes name, host, - port and task_id - """ - if request.value: - msg = deserialize(request.value) - else: - msg = None - task_id = self.get_task_id() - self.result_pool[task_id] = threading.Condition() - self.executor.submit( - self.process_messages, - task_id, - request.agent_id, - msg, # type: ignore[arg-type] - ) - return RpcMsg( - value=Msg( - name=self.agent_pool[request.agent_id].name, - content=None, - task_id=task_id, - ).serialize(), - ) - - def _get(self, request: RpcMsg) -> RpcMsg: - """Get function of RpcAgentService - - Args: - request (`RpcMsg`): - Identifier of message, with json format:: - - { - 'task_id': int - } - - Returns: - `RpcMsg`: Concrete values of the specific message (or part of it). - """ - msg = json.loads(request.value) - while True: - result = self.result_pool.get(msg["task_id"]) - if isinstance(result, threading.Condition): - with result: - result.wait(timeout=1) - else: - break - return RpcMsg(value=result.serialize()) - - def _observe(self, request: RpcMsg) -> RpcMsg: - """Observe function of RpcAgentService - - Args: - request (`RpcMsg`): - The serialized input to be observed. - - Returns: - `RpcMsg`: Empty RpcMsg. - """ - msgs = deserialize(request.value) - for msg in msgs: - if isinstance(msg, PlaceholderMessage): - msg.update_value() - self.agent_pool[request.agent_id].observe(msgs) - return RpcMsg() - - def _create_agent(self, request: RpcMsg) -> RpcMsg: - """Create a new agent instance for the agent_id. - - Args: - request (RpcMsg): request message with a `agent_id` field. - """ - self.check_and_generate_agent( - request.agent_id, - agent_configs=( - dill.loads(base64.b64decode(request.value)) - if request.value - else None - ), - ) - return RpcMsg() - - def _clone_agent(self, request: RpcMsg) -> RpcMsg: - """Clone a new agent instance from the origin instance. - - Args: - request (RpcMsg): The `agent_id` field is the agent_id of the - agent to be cloned. - - Returns: - `RpcMsg`: The `value` field contains the agent_id of generated - agent. - """ - agent_id = request.agent_id - with self.agent_id_lock: - if agent_id not in self.agent_pool: - raise ValueError(f"Agent [{agent_id}] not exists") - ori_agent = self.agent_pool[agent_id] - new_agent = ori_agent.__class__( - *ori_agent._init_settings["args"], # pylint: disable=W0212 - **ori_agent._init_settings["kwargs"], # pylint: disable=W0212 - ) - with self.agent_id_lock: - self.agent_pool[new_agent.agent_id] = new_agent - return RpcMsg(value=new_agent.agent_id) - - def _delete_agent(self, request: RpcMsg) -> RpcMsg: - """Delete the agent instance of the specific sesssion_id. - - Args: - request (RpcMsg): request message with a `agent_id` field. - """ - self.check_and_delete_agent(request.agent_id) - return RpcMsg() - -
    -[docs] - def process_messages( - self, - task_id: int, - agent_id: str, - task_msg: dict = None, - ) -> None: - """Task processing.""" - if isinstance(task_msg, PlaceholderMessage): - task_msg.update_value() - cond = self.result_pool[task_id] - try: - result = self.agent_pool[agent_id].reply(task_msg) - self.result_pool[task_id] = result - except Exception: - error_msg = traceback.format_exc() - logger.error(f"Error in agent [{agent_id}]:\n{error_msg}") - self.result_pool[task_id] = Msg( - name="ERROR", - role="assistant", - __status="ERROR", - content=f"Error in agent [{agent_id}]:\n{error_msg}", - ) - with cond: - cond.notify_all()
    -
    -
    diff --git a/en/_modules/agentscope/agents/text_to_image_agent.html b/en/_modules/agentscope/agents/text_to_image_agent.html index f7bd6429b..fc53653c0 100644 --- a/en/_modules/agentscope/agents/text_to_image_agent.html +++ b/en/_modules/agentscope/agents/text_to_image_agent.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/agents/user_agent.html b/en/_modules/agentscope/agents/user_agent.html index b550b2b17..3edd31484 100644 --- a/en/_modules/agentscope/agents/user_agent.html +++ b/en/_modules/agentscope/agents/user_agent.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/constants.html b/en/_modules/agentscope/constants.html index 81391e4b7..9e9e1a18e 100644 --- a/en/_modules/agentscope/constants.html +++ b/en/_modules/agentscope/constants.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/exception.html b/en/_modules/agentscope/exception.html index 4ff63e050..c26072e9f 100644 --- a/en/_modules/agentscope/exception.html +++ b/en/_modules/agentscope/exception.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/memory/memory.html b/en/_modules/agentscope/memory/memory.html index 8335dd13c..f9d76f403 100644 --- a/en/_modules/agentscope/memory/memory.html +++ b/en/_modules/agentscope/memory/memory.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/memory/temporary_memory.html b/en/_modules/agentscope/memory/temporary_memory.html index 09ed0b6f5..a7304f0d2 100644 --- a/en/_modules/agentscope/memory/temporary_memory.html +++ b/en/_modules/agentscope/memory/temporary_memory.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/message.html b/en/_modules/agentscope/message.html index a253e1508..d6ff18efa 100644 --- a/en/_modules/agentscope/message.html +++ b/en/_modules/agentscope/message.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • @@ -578,7 +579,7 @@

    Source code for agentscope.message

     
     
    [docs] -def deserialize(s: str) -> Union[MessageBase, Sequence]: +def deserialize(s: Union[str, bytes]) -> Union[MessageBase, Sequence]: """Deserialize json string into MessageBase""" js_msg = json.loads(s) msg_type = js_msg.pop("__type") diff --git a/en/_modules/agentscope/models.html b/en/_modules/agentscope/models.html index c9dfb8eed..9329ddcfc 100644 --- a/en/_modules/agentscope/models.html +++ b/en/_modules/agentscope/models.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/models/dashscope_model.html b/en/_modules/agentscope/models/dashscope_model.html index 88bff84be..c2b5959f9 100644 --- a/en/_modules/agentscope/models/dashscope_model.html +++ b/en/_modules/agentscope/models/dashscope_model.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/models/gemini_model.html b/en/_modules/agentscope/models/gemini_model.html index 51a53c432..28d0796d2 100644 --- a/en/_modules/agentscope/models/gemini_model.html +++ b/en/_modules/agentscope/models/gemini_model.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/models/litellm_model.html b/en/_modules/agentscope/models/litellm_model.html index 1fb500f3d..32af55123 100644 --- a/en/_modules/agentscope/models/litellm_model.html +++ b/en/_modules/agentscope/models/litellm_model.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/models/model.html b/en/_modules/agentscope/models/model.html index 059ed7161..9f503e745 100644 --- a/en/_modules/agentscope/models/model.html +++ b/en/_modules/agentscope/models/model.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/models/ollama_model.html b/en/_modules/agentscope/models/ollama_model.html index 557ac284d..4f755787d 100644 --- a/en/_modules/agentscope/models/ollama_model.html +++ b/en/_modules/agentscope/models/ollama_model.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/models/openai_model.html b/en/_modules/agentscope/models/openai_model.html index 82e0d1b4c..d5c78599b 100644 --- a/en/_modules/agentscope/models/openai_model.html +++ b/en/_modules/agentscope/models/openai_model.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/models/post_model.html b/en/_modules/agentscope/models/post_model.html index 91e9a1b03..bab9b5856 100644 --- a/en/_modules/agentscope/models/post_model.html +++ b/en/_modules/agentscope/models/post_model.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/models/response.html b/en/_modules/agentscope/models/response.html index 66af3c2af..721ff97b0 100644 --- a/en/_modules/agentscope/models/response.html +++ b/en/_modules/agentscope/models/response.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/models/zhipu_model.html b/en/_modules/agentscope/models/zhipu_model.html index f8f35fe16..abbcfb7a4 100644 --- a/en/_modules/agentscope/models/zhipu_model.html +++ b/en/_modules/agentscope/models/zhipu_model.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/msghub.html b/en/_modules/agentscope/msghub.html index 047e982a3..76e6601f7 100644 --- a/en/_modules/agentscope/msghub.html +++ b/en/_modules/agentscope/msghub.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/parsers/code_block_parser.html b/en/_modules/agentscope/parsers/code_block_parser.html index 4c3c90c6d..ee5b19178 100644 --- a/en/_modules/agentscope/parsers/code_block_parser.html +++ b/en/_modules/agentscope/parsers/code_block_parser.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/parsers/json_object_parser.html b/en/_modules/agentscope/parsers/json_object_parser.html index d15cab1dd..f668d9276 100644 --- a/en/_modules/agentscope/parsers/json_object_parser.html +++ b/en/_modules/agentscope/parsers/json_object_parser.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/parsers/parser_base.html b/en/_modules/agentscope/parsers/parser_base.html index 8a5d5cd9e..c5d0a6250 100644 --- a/en/_modules/agentscope/parsers/parser_base.html +++ b/en/_modules/agentscope/parsers/parser_base.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/parsers/tagged_content_parser.html b/en/_modules/agentscope/parsers/tagged_content_parser.html index 3f0371206..cd87dc654 100644 --- a/en/_modules/agentscope/parsers/tagged_content_parser.html +++ b/en/_modules/agentscope/parsers/tagged_content_parser.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/pipelines/functional.html b/en/_modules/agentscope/pipelines/functional.html index e6e82d069..8c74904b1 100644 --- a/en/_modules/agentscope/pipelines/functional.html +++ b/en/_modules/agentscope/pipelines/functional.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/pipelines/pipeline.html b/en/_modules/agentscope/pipelines/pipeline.html index 7f1e5b4f2..dc919df9e 100644 --- a/en/_modules/agentscope/pipelines/pipeline.html +++ b/en/_modules/agentscope/pipelines/pipeline.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/prompt.html b/en/_modules/agentscope/prompt.html index 5e5b9f695..6f8ab147e 100644 --- a/en/_modules/agentscope/prompt.html +++ b/en/_modules/agentscope/prompt.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/rpc/rpc_agent_client.html b/en/_modules/agentscope/rpc/rpc_agent_client.html index 58d331202..1af857f6c 100644 --- a/en/_modules/agentscope/rpc/rpc_agent_client.html +++ b/en/_modules/agentscope/rpc/rpc_agent_client.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/rpc/rpc_agent_pb2_grpc.html b/en/_modules/agentscope/rpc/rpc_agent_pb2_grpc.html index 7ce903cd6..42ef42560 100644 --- a/en/_modules/agentscope/rpc/rpc_agent_pb2_grpc.html +++ b/en/_modules/agentscope/rpc/rpc_agent_pb2_grpc.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/server/launcher.html b/en/_modules/agentscope/server/launcher.html new file mode 100644 index 000000000..51fd9d673 --- /dev/null +++ b/en/_modules/agentscope/server/launcher.html @@ -0,0 +1,600 @@ + + + + + + + agentscope.server.launcher — AgentScope documentation + + + + + + + + + + + + + + + + + + + + +
    + + +
    + +
    +
    +
    + +
    +
    +
    +
    + +

    Source code for agentscope.server.launcher

    +# -*- coding: utf-8 -*-
    +""" Server of distributed agent"""
    +import os
    +from multiprocessing import Process, Event, Pipe
    +from multiprocessing.synchronize import Event as EventClass
    +import asyncio
    +import signal
    +import argparse
    +from typing import Type
    +from concurrent import futures
    +from loguru import logger
    +
    +try:
    +    import grpc
    +    from agentscope.rpc.rpc_agent_pb2_grpc import (
    +        add_RpcAgentServicer_to_server,
    +    )
    +except ImportError as import_error:
    +    from agentscope.utils.tools import ImportErrorReporter
    +
    +    grpc = ImportErrorReporter(import_error, "distribute")
    +    add_RpcAgentServicer_to_server = ImportErrorReporter(
    +        import_error,
    +        "distribute",
    +    )
    +
    +import agentscope
    +from agentscope.server.servicer import AgentServerServicer
    +from agentscope.agents.agent import AgentBase
    +from agentscope.utils.tools import (
    +    _get_timestamp,
    +    check_port,
    +)
    +
    +
    +def _setup_agent_server(
    +    host: str,
    +    port: int,
    +    server_id: str,
    +    init_settings: dict = None,
    +    start_event: EventClass = None,
    +    stop_event: EventClass = None,
    +    pipe: int = None,
    +    local_mode: bool = True,
    +    max_pool_size: int = 8192,
    +    max_timeout_seconds: int = 1800,
    +    custom_agents: list = None,
    +) -> None:
    +    """Setup agent server.
    +
    +    Args:
    +        host (`str`, defaults to `"localhost"`):
    +            Hostname of the agent server.
    +        port (`int`):
    +            The socket port monitored by the agent server.
    +        server_id (`str`):
    +            The id of the server.
    +        init_settings (`dict`, defaults to `None`):
    +            Init settings for agentscope.init.
    +        start_event (`EventClass`, defaults to `None`):
    +            An Event instance used to determine whether the child process
    +            has been started.
    +        stop_event (`EventClass`, defaults to `None`):
    +            The stop Event instance used to determine whether the child
    +            process has been stopped.
    +        pipe (`int`, defaults to `None`):
    +            A pipe instance used to pass the actual port of the server.
    +        local_mode (`bool`, defaults to `None`):
    +            Only listen to local requests.
    +        max_pool_size (`int`, defaults to `8192`):
    +            Max number of agent replies that the server can accommodate.
    +        max_timeout_seconds (`int`, defaults to `1800`):
    +            Timeout for agent replies.
    +        custom_agents (`list`, defaults to `None`):
    +            A list of custom agent classes that are not in `agentscope.agents`.
    +    """
    +    asyncio.run(
    +        _setup_agent_server_async(
    +            host=host,
    +            port=port,
    +            server_id=server_id,
    +            init_settings=init_settings,
    +            start_event=start_event,
    +            stop_event=stop_event,
    +            pipe=pipe,
    +            local_mode=local_mode,
    +            max_pool_size=max_pool_size,
    +            max_timeout_seconds=max_timeout_seconds,
    +            custom_agents=custom_agents,
    +        ),
    +    )
    +
    +
    +async def _setup_agent_server_async(
    +    host: str,
    +    port: int,
    +    server_id: str,
    +    init_settings: dict = None,
    +    start_event: EventClass = None,
    +    stop_event: EventClass = None,
    +    pipe: int = None,
    +    local_mode: bool = True,
    +    max_pool_size: int = 8192,
    +    max_timeout_seconds: int = 1800,
    +    custom_agents: list = None,
    +) -> None:
    +    """Setup agent server in an async way.
    +
    +    Args:
    +        host (`str`, defaults to `"localhost"`):
    +            Hostname of the agent server.
    +        port (`int`):
    +            The socket port monitored by the agent server.
    +        server_id (`str`):
    +            The id of the server.
    +        init_settings (`dict`, defaults to `None`):
    +            Init settings for agentscope.init.
    +        start_event (`EventClass`, defaults to `None`):
    +            An Event instance used to determine whether the child process
    +            has been started.
    +        stop_event (`EventClass`, defaults to `None`):
    +            The stop Event instance used to determine whether the child
    +            process has been stopped.
    +        pipe (`int`, defaults to `None`):
    +            A pipe instance used to pass the actual port of the server.
    +        local_mode (`bool`, defaults to `None`):
    +            If `True`, only listen to requests from "localhost", otherwise,
    +            listen to requests from all hosts.
    +        max_pool_size (`int`, defaults to `8192`):
    +            The max number of agent reply messages that the server can
    +            accommodate. Note that the oldest message will be deleted
    +            after exceeding the pool size.
    +        max_timeout_seconds (`int`, defaults to `1800`):
    +            Maximum time for reply messages to be cached in the server.
    +            Note that expired messages will be deleted.
    +        custom_agents (`list`, defaults to `None`):
    +            A list of custom agent classes that are not in `agentscope.agents`.
    +    """
    +    from agentscope._init import init_process
    +
    +    if init_settings is not None:
    +        init_process(**init_settings)
    +    servicer = AgentServerServicer(
    +        host=host,
    +        port=port,
    +        max_pool_size=max_pool_size,
    +        max_timeout_seconds=max_timeout_seconds,
    +    )
    +    # update agent registry
    +    if custom_agents is not None:
    +        for agent_class in custom_agents:
    +            AgentBase.register_agent_class(agent_class=agent_class)
    +
    +    async def shutdown_signal_handler() -> None:
    +        logger.info(
    +            f"Received shutdown signal. Gracefully stopping the server at "
    +            f"[{host}:{port}].",
    +        )
    +        await server.stop(grace=5)
    +
    +    loop = asyncio.get_running_loop()
    +    if os.name != "nt":
    +        # windows does not support add_signal_handler
    +        for sig in (signal.SIGINT, signal.SIGTERM):
    +            loop.add_signal_handler(
    +                sig,
    +                lambda: asyncio.create_task(shutdown_signal_handler()),
    +            )
    +    while True:
    +        try:
    +            port = check_port(port)
    +            servicer.port = port
    +            server = grpc.aio.server(
    +                futures.ThreadPoolExecutor(max_workers=None),
    +            )
    +            add_RpcAgentServicer_to_server(servicer, server)
    +            if local_mode:
    +                server.add_insecure_port(f"localhost:{port}")
    +            else:
    +                server.add_insecure_port(f"0.0.0.0:{port}")
    +            await server.start()
    +            break
    +        except OSError:
    +            logger.warning(
    +                f"Failed to start agent server at port [{port}]"
    +                f"try another port",
    +            )
    +    logger.info(
    +        f"agent server [{server_id}] at {host}:{port} started successfully",
    +    )
    +    if start_event is not None:
    +        pipe.send(port)
    +        start_event.set()
    +        while not stop_event.is_set():
    +            await asyncio.sleep(1)
    +        logger.info(
    +            f"Stopping agent server at [{host}:{port}]",
    +        )
    +        await server.stop(grace=10.0)
    +    else:
    +        await server.wait_for_termination()
    +    logger.info(
    +        f"agent server [{server_id}] at {host}:{port} stopped successfully",
    +    )
    +
    +
    +
    +[docs] +class RpcAgentServerLauncher: + """The launcher of AgentServer.""" + +
    +[docs] + def __init__( + self, + host: str = "localhost", + port: int = None, + max_pool_size: int = 8192, + max_timeout_seconds: int = 1800, + local_mode: bool = False, + custom_agents: list = None, + server_id: str = None, + agent_class: Type[AgentBase] = None, + agent_args: tuple = (), + agent_kwargs: dict = None, + ) -> None: + """Init a launcher of agent server. + + Args: + host (`str`, defaults to `"localhost"`): + Hostname of the agent server. + port (`int`, defaults to `None`): + Socket port of the agent server. + max_pool_size (`int`, defaults to `8192`): + The max number of agent reply messages that the server can + accommodate. Note that the oldest message will be deleted + after exceeding the pool size. + max_timeout_seconds (`int`, defaults to `1800`): + Maximum time for reply messages to be cached in the server. + Note that expired messages will be deleted. + local_mode (`bool`, defaults to `False`): + If `True`, only listen to requests from "localhost", otherwise, + listen to requests from all hosts. + custom_agents (`list`, defaults to `None`): + A list of custom agent classes that are not in + `agentscope.agents`. + server_id (`str`, defaults to `None`): + The id of the agent server. If not specified, a random id + will be generated. + agent_class (`Type[AgentBase]`, deprecated): + The AgentBase subclass encapsulated by this wrapper. + agent_args (`tuple`, deprecated): The args tuple used to + initialize the agent_class. + agent_kwargs (`dict`, deprecated): The args dict used to + initialize the agent_class. + """ + self.host = host + self.port = check_port(port) + self.max_pool_size = max_pool_size + self.max_timeout_seconds = max_timeout_seconds + self.local_mode = local_mode + self.server = None + self.stop_event = None + self.parent_con = None + self.custom_agents = custom_agents + self.server_id = ( + self.generate_server_id() if server_id is None else server_id + ) + if ( + agent_class is not None + or len(agent_args) > 0 + or agent_kwargs is not None + ): + logger.warning( + "`agent_class`, `agent_args` and `agent_kwargs` is deprecated" + " in `RpcAgentServerLauncher`", + )
    + + +
    +[docs] + def generate_server_id(self) -> str: + """Generate server id""" + return f"{self.host}:{self.port}-{_get_timestamp('%y%m%d-%H:%M:%S')}"
    + + + def _launch_in_main(self) -> None: + """Launch agent server in main-process""" + logger.info( + f"Launching agent server at [{self.host}:{self.port}]...", + ) + asyncio.run( + _setup_agent_server_async( + host=self.host, + port=self.port, + server_id=self.server_id, + max_pool_size=self.max_pool_size, + max_timeout_seconds=self.max_timeout_seconds, + local_mode=self.local_mode, + custom_agents=self.custom_agents, + ), + ) + + def _launch_in_sub(self) -> None: + """Launch an agent server in sub-process.""" + from agentscope._init import _INIT_SETTINGS + + self.stop_event = Event() + self.parent_con, child_con = Pipe() + start_event = Event() + server_process = Process( + target=_setup_agent_server, + kwargs={ + "host": self.host, + "port": self.port, + "server_id": self.server_id, + "init_settings": _INIT_SETTINGS, + "start_event": start_event, + "stop_event": self.stop_event, + "pipe": child_con, + "max_pool_size": self.max_pool_size, + "max_timeout_seconds": self.max_timeout_seconds, + "local_mode": self.local_mode, + "custom_agents": self.custom_agents, + }, + ) + server_process.start() + self.port = self.parent_con.recv() + start_event.wait() + self.server = server_process + logger.info( + f"Launch agent server at [{self.host}:{self.port}] success", + ) + +
    +[docs] + def launch(self, in_subprocess: bool = True) -> None: + """launch an agent server. + + Args: + in_subprocess (bool, optional): launch the server in subprocess. + Defaults to True. For agents that need to obtain command line + input, such as UserAgent, please set this value to False. + """ + if in_subprocess: + self._launch_in_sub() + else: + self._launch_in_main()
    + + +
    +[docs] + def wait_until_terminate(self) -> None: + """Wait for server process""" + if self.server is not None: + self.server.join()
    + + +
    +[docs] + def shutdown(self) -> None: + """Shutdown the agent server.""" + if self.server is not None: + if self.stop_event is not None: + self.stop_event.set() + self.stop_event = None + self.server.join() + if self.server.is_alive(): + self.server.kill() + logger.info( + f"Agent server at port [{self.port}] is killed.", + ) + self.server = None
    +
    + + + +
    +[docs] +def as_server() -> None: + """Launch an agent server with terminal command. + + Note: + + The arguments of `as_server` are listed as follows: + + * `--host`: the hostname of the server. + * `--port`: the socket port of the server. + * `--max-pool-size`: max number of agent reply messages that the server + can accommodate. Note that the oldest message will be deleted + after exceeding the pool size. + * `--max-timeout-seconds`: max time for reply messages to be cached + in the server. Note that expired messages will be deleted. + * `--local-mode`: whether the started agent server only listens to + local requests. + * `--model-config-path`: the path to the model config json file + + In most cases, you only need to specify the `--host`, `--port` and + `--model-config-path`. + + .. code-block:: shell + + as_server --host localhost --port 12345 --model-config-path config.json + + """ # noqa + parser = argparse.ArgumentParser() + parser.add_argument( + "--host", + type=str, + default="localhost", + help="hostname of the server", + ) + parser.add_argument( + "--port", + type=int, + default=12310, + help="socket port of the server", + ) + parser.add_argument( + "--max-pool-size", + type=int, + default=8192, + help=( + "max number of agent reply messages that the server " + "can accommodate. Note that the oldest message will be deleted " + "after exceeding the pool size." + ), + ) + parser.add_argument( + "--max-timeout-seconds", + type=int, + default=1800, + help=( + "max time for agent reply messages to be cached" + "in the server. Note that expired messages will be deleted." + ), + ) + parser.add_argument( + "--local-mode", + type=bool, + default=False, + help=( + "If `True`, only listen to requests from 'localhost', otherwise, " + "listen to requests from all hosts." + ), + ) + parser.add_argument( + "--model-config-path", + type=str, + help="path to the model config json file", + ) + args = parser.parse_args() + agentscope.init( + project="agent_server", + name=f"server_{args.host}:{args.port}", + runtime_id=_get_timestamp( + "server_{}_{}_%y%m%d-%H%M%S", + ).format(args.host, args.port), + model_configs=args.model_config_path, + ) + launcher = RpcAgentServerLauncher( + host=args.host, + port=args.port, + max_pool_size=args.max_pool_size, + max_timeout_seconds=args.max_timeout_seconds, + local_mode=args.local_mode, + ) + launcher.launch(in_subprocess=False) + launcher.wait_until_terminate()
    + +
    + +
    +
    +
    + +
    + +
    +

    © Copyright 2024, Alibaba Tongyi Lab.

    +
    + + Built with Sphinx using a + theme + provided by Read the Docs. + + +
    +
    +
    +
    +
    + + + + \ No newline at end of file diff --git a/en/_modules/agentscope/server/servicer.html b/en/_modules/agentscope/server/servicer.html new file mode 100644 index 000000000..07da63e14 --- /dev/null +++ b/en/_modules/agentscope/server/servicer.html @@ -0,0 +1,467 @@ + + + + + + + agentscope.server.servicer — AgentScope documentation + + + + + + + + + + + + + + + + + + + + +
    + + +
    + +
    +
    +
    + +
    +
    +
    +
    + +

    Source code for agentscope.server.servicer

    +# -*- coding: utf-8 -*-
    +""" Server of distributed agent"""
    +import threading
    +import base64
    +import json
    +import traceback
    +from concurrent import futures
    +from loguru import logger
    +
    +try:
    +    import dill
    +    import grpc
    +    from grpc import ServicerContext
    +    from expiringdict import ExpiringDict
    +    from ..rpc.rpc_agent_pb2 import RpcMsg  # pylint: disable=E0611
    +    from ..rpc.rpc_agent_pb2_grpc import RpcAgentServicer
    +except ImportError as import_error:
    +    from agentscope.utils.tools import ImportErrorReporter
    +
    +    dill = ImportErrorReporter(import_error, "distribute")
    +    grpc = ImportErrorReporter(import_error, "distribute")
    +    ServicerContext = ImportErrorReporter(import_error, "distribute")
    +    ExpiringDict = ImportErrorReporter(import_error, "distribute")
    +    RpcMsg = ImportErrorReporter(  # type: ignore[misc]
    +        import_error,
    +        "distribute",
    +    )
    +    RpcAgentServicer = ImportErrorReporter(import_error, "distribute")
    +
    +from ..agents.agent import AgentBase
    +from ..message import (
    +    Msg,
    +    PlaceholderMessage,
    +    deserialize,
    +)
    +
    +
    +
    +[docs] +class AgentServerServicer(RpcAgentServicer): + """A Servicer for RPC Agent Server (formerly RpcServerSideWrapper)""" + +
    +[docs] + def __init__( + self, + host: str = "localhost", + port: int = None, + max_pool_size: int = 8192, + max_timeout_seconds: int = 1800, + ): + """Init the AgentServerServicer. + + Args: + host (`str`, defaults to "localhost"): + Hostname of the rpc agent server. + port (`int`, defaults to `None`): + Port of the rpc agent server. + max_pool_size (`int`, defaults to `8192`): + The max number of agent reply messages that the server can + accommodate. Note that the oldest message will be deleted + after exceeding the pool size. + max_timeout_seconds (`int`, defaults to `1800`): + Maximum time for reply messages to be cached in the server. + Note that expired messages will be deleted. + """ + self.host = host + self.port = port + self.result_pool = ExpiringDict( + max_len=max_pool_size, + max_age_seconds=max_timeout_seconds, + ) + self.executor = futures.ThreadPoolExecutor(max_workers=None) + self.task_id_lock = threading.Lock() + self.agent_id_lock = threading.Lock() + self.task_id_counter = 0 + self.agent_pool: dict[str, AgentBase] = {}
    + + +
    +[docs] + def get_task_id(self) -> int: + """Get the auto-increment task id. + Each reply call will get a unique task id.""" + with self.task_id_lock: + self.task_id_counter += 1 + return self.task_id_counter
    + + +
    +[docs] + def agent_exists(self, agent_id: str) -> bool: + """Check whether the agent exists. + + Args: + agent_id (`str`): the agent id. + + Returns: + bool: whether the agent exists. + """ + return agent_id in self.agent_pool
    + + +
    +[docs] + def check_and_generate_agent( + self, + agent_id: str, + agent_configs: dict, + ) -> None: + """ + Check whether the agent exists, and create new agent instance + for new agent. + + Args: + agent_id (`str`): the agent id. + agent_configs (`dict`): configuration used to initialize the agent, + with three fields (generated in `_AgentMeta`): + + .. code-block:: python + + { + "class_name": {name of the agent} + "args": {args in tuple type to init the agent} + "kwargs": {args in dict type to init the agent} + } + + """ + with self.agent_id_lock: + if agent_id not in self.agent_pool: + agent_class_name = agent_configs["class_name"] + agent_instance = AgentBase.get_agent_class(agent_class_name)( + *agent_configs["args"], + **agent_configs["kwargs"], + ) + agent_instance._agent_id = agent_id # pylint: disable=W0212 + self.agent_pool[agent_id] = agent_instance + logger.info(f"create agent instance [{agent_id}]")
    + + +
    +[docs] + def check_and_delete_agent(self, agent_id: str) -> None: + """ + Check whether the agent exists, and delete the agent instance + for the agent_id. + + Args: + agent_id (`str`): the agent id. + """ + with self.agent_id_lock: + if agent_id in self.agent_pool: + self.agent_pool.pop(agent_id) + logger.info(f"delete agent instance [{agent_id}]")
    + + +
    +[docs] + def call_func( # pylint: disable=W0236 + self, + request: RpcMsg, + context: ServicerContext, + ) -> RpcMsg: + """Call the specific servicer function.""" + if hasattr(self, request.target_func): + if request.target_func not in ["_create_agent", "_get"]: + if not self.agent_exists(request.agent_id): + return context.abort( + grpc.StatusCode.INVALID_ARGUMENT, + f"Agent [{request.agent_id}] not exists.", + ) + return getattr(self, request.target_func)(request) + else: + # TODO: support other user defined method + logger.error(f"Unsupported method {request.target_func}") + return context.abort( + grpc.StatusCode.INVALID_ARGUMENT, + f"Unsupported method {request.target_func}", + )
    + + + def _reply(self, request: RpcMsg) -> RpcMsg: + """Call function of RpcAgentService + + Args: + request (`RpcMsg`): + Message containing input parameters or input parameter + placeholders. + + Returns: + `RpcMsg`: A serialized Msg instance with attributes name, host, + port and task_id + """ + if request.value: + msg = deserialize(request.value) + else: + msg = None + task_id = self.get_task_id() + self.result_pool[task_id] = threading.Condition() + self.executor.submit( + self.process_messages, + task_id, + request.agent_id, + msg, # type: ignore[arg-type] + ) + return RpcMsg( + value=Msg( # type: ignore[arg-type] + name=self.agent_pool[request.agent_id].name, + content=None, + task_id=task_id, + ).serialize(), + ) + + def _get(self, request: RpcMsg) -> RpcMsg: + """Get a reply message with specific task_id. + + Args: + request (`RpcMsg`): + The task id that generated this message, with json format:: + + { + 'task_id': int + } + + Returns: + `RpcMsg`: Concrete values of the specific message (or part of it). + """ + msg = json.loads(request.value) + while True: + result = self.result_pool.get(msg["task_id"]) + if isinstance(result, threading.Condition): + with result: + result.wait(timeout=1) + else: + break + return RpcMsg(value=result.serialize()) + + def _observe(self, request: RpcMsg) -> RpcMsg: + """Observe function of the original agent. + + Args: + request (`RpcMsg`): + The serialized input to be observed. + + Returns: + `RpcMsg`: Empty RpcMsg. + """ + msgs = deserialize(request.value) + for msg in msgs: + if isinstance(msg, PlaceholderMessage): + msg.update_value() + self.agent_pool[request.agent_id].observe(msgs) + return RpcMsg() + + def _create_agent(self, request: RpcMsg) -> RpcMsg: + """Create a new agent instance with the given agent_id. + + Args: + request (RpcMsg): request message with a `agent_id` field. + """ + self.check_and_generate_agent( + request.agent_id, + agent_configs=( + dill.loads(base64.b64decode(request.value)) + if request.value + else None + ), + ) + return RpcMsg() + + def _clone_agent(self, request: RpcMsg) -> RpcMsg: + """Clone a new agent instance from the origin instance. + + Args: + request (RpcMsg): The `agent_id` field is the agent_id of the + agent to be cloned. + + Returns: + `RpcMsg`: The `value` field contains the agent_id of generated + agent. + """ + agent_id = request.agent_id + with self.agent_id_lock: + if agent_id not in self.agent_pool: + raise ValueError(f"Agent [{agent_id}] not exists") + ori_agent = self.agent_pool[agent_id] + new_agent = ori_agent.__class__( + *ori_agent._init_settings["args"], # pylint: disable=W0212 + **ori_agent._init_settings["kwargs"], # pylint: disable=W0212 + ) + with self.agent_id_lock: + self.agent_pool[new_agent.agent_id] = new_agent + return RpcMsg(value=new_agent.agent_id) # type: ignore[arg-type] + + def _delete_agent(self, request: RpcMsg) -> RpcMsg: + """Delete the agent instance of the specific agent_id. + + Args: + request (RpcMsg): request message with a `agent_id` field. + """ + self.check_and_delete_agent(request.agent_id) + return RpcMsg() + +
    +[docs] + def process_messages( + self, + task_id: int, + agent_id: str, + task_msg: dict = None, + ) -> None: + """Processing an input message and generate its reply message. + + Args: + task_id (`int`): task id of the input message, . + agent_id (`str`): the id of the agent that accepted the message. + task_msg (`dict`): the input message. + """ + if isinstance(task_msg, PlaceholderMessage): + task_msg.update_value() + cond = self.result_pool[task_id] + try: + result = self.agent_pool[agent_id].reply(task_msg) + self.result_pool[task_id] = result + except Exception: + error_msg = traceback.format_exc() + logger.error(f"Error in agent [{agent_id}]:\n{error_msg}") + self.result_pool[task_id] = Msg( + name="ERROR", + role="assistant", + __status="ERROR", + content=f"Error in agent [{agent_id}]:\n{error_msg}", + ) + with cond: + cond.notify_all()
    +
    + +
    + +
    +
    +
    + +
    + +
    +

    © Copyright 2024, Alibaba Tongyi Lab.

    +
    + + Built with Sphinx using a + theme + provided by Read the Docs. + + +
    +
    +
    +
    +
    + + + + \ No newline at end of file diff --git a/en/_modules/agentscope/service.html b/en/_modules/agentscope/service.html index f393291c6..0ac212283 100644 --- a/en/_modules/agentscope/service.html +++ b/en/_modules/agentscope/service.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/service/execute_code/exec_python.html b/en/_modules/agentscope/service/execute_code/exec_python.html index 040e2c885..7f78e52f8 100644 --- a/en/_modules/agentscope/service/execute_code/exec_python.html +++ b/en/_modules/agentscope/service/execute_code/exec_python.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/service/execute_code/exec_shell.html b/en/_modules/agentscope/service/execute_code/exec_shell.html index af520df45..30614b13f 100644 --- a/en/_modules/agentscope/service/execute_code/exec_shell.html +++ b/en/_modules/agentscope/service/execute_code/exec_shell.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/service/file/common.html b/en/_modules/agentscope/service/file/common.html index 30419e981..bcc4f5cfe 100644 --- a/en/_modules/agentscope/service/file/common.html +++ b/en/_modules/agentscope/service/file/common.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/service/file/json.html b/en/_modules/agentscope/service/file/json.html index e196bdfab..b84ee99c2 100644 --- a/en/_modules/agentscope/service/file/json.html +++ b/en/_modules/agentscope/service/file/json.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/service/file/text.html b/en/_modules/agentscope/service/file/text.html index e489dadc2..477a17448 100644 --- a/en/_modules/agentscope/service/file/text.html +++ b/en/_modules/agentscope/service/file/text.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/service/retrieval/retrieval_from_list.html b/en/_modules/agentscope/service/retrieval/retrieval_from_list.html index 58fbd825f..e4651f1e5 100644 --- a/en/_modules/agentscope/service/retrieval/retrieval_from_list.html +++ b/en/_modules/agentscope/service/retrieval/retrieval_from_list.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/service/retrieval/similarity.html b/en/_modules/agentscope/service/retrieval/similarity.html index be9a02832..f318df93a 100644 --- a/en/_modules/agentscope/service/retrieval/similarity.html +++ b/en/_modules/agentscope/service/retrieval/similarity.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/service/service_response.html b/en/_modules/agentscope/service/service_response.html index e7142da4e..5204f1652 100644 --- a/en/_modules/agentscope/service/service_response.html +++ b/en/_modules/agentscope/service/service_response.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/service/service_status.html b/en/_modules/agentscope/service/service_status.html index e9aac81ca..8195f2644 100644 --- a/en/_modules/agentscope/service/service_status.html +++ b/en/_modules/agentscope/service/service_status.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/service/service_toolkit.html b/en/_modules/agentscope/service/service_toolkit.html index bca6158a8..8f9a2ca5f 100644 --- a/en/_modules/agentscope/service/service_toolkit.html +++ b/en/_modules/agentscope/service/service_toolkit.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/service/sql_query/mongodb.html b/en/_modules/agentscope/service/sql_query/mongodb.html index 39b220c5a..6d5437280 100644 --- a/en/_modules/agentscope/service/sql_query/mongodb.html +++ b/en/_modules/agentscope/service/sql_query/mongodb.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/service/sql_query/mysql.html b/en/_modules/agentscope/service/sql_query/mysql.html index 5653a7eb7..b1684648c 100644 --- a/en/_modules/agentscope/service/sql_query/mysql.html +++ b/en/_modules/agentscope/service/sql_query/mysql.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/service/sql_query/sqlite.html b/en/_modules/agentscope/service/sql_query/sqlite.html index 4da53a538..f8dbb1e7f 100644 --- a/en/_modules/agentscope/service/sql_query/sqlite.html +++ b/en/_modules/agentscope/service/sql_query/sqlite.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/service/text_processing/summarization.html b/en/_modules/agentscope/service/text_processing/summarization.html index 6a4b9b8eb..31c184a2c 100644 --- a/en/_modules/agentscope/service/text_processing/summarization.html +++ b/en/_modules/agentscope/service/text_processing/summarization.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/service/web/arxiv.html b/en/_modules/agentscope/service/web/arxiv.html index 87fa6c797..9602f8a68 100644 --- a/en/_modules/agentscope/service/web/arxiv.html +++ b/en/_modules/agentscope/service/web/arxiv.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/service/web/dblp.html b/en/_modules/agentscope/service/web/dblp.html index 89f8a1ed9..204863293 100644 --- a/en/_modules/agentscope/service/web/dblp.html +++ b/en/_modules/agentscope/service/web/dblp.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/service/web/download.html b/en/_modules/agentscope/service/web/download.html index ebf3b8972..b773faaea 100644 --- a/en/_modules/agentscope/service/web/download.html +++ b/en/_modules/agentscope/service/web/download.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/service/web/search.html b/en/_modules/agentscope/service/web/search.html index 66e7d58a4..f9e4fa56a 100644 --- a/en/_modules/agentscope/service/web/search.html +++ b/en/_modules/agentscope/service/web/search.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/service/web/web_digest.html b/en/_modules/agentscope/service/web/web_digest.html index 9aae158ba..3cac3a88d 100644 --- a/en/_modules/agentscope/service/web/web_digest.html +++ b/en/_modules/agentscope/service/web/web_digest.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/utils/common.html b/en/_modules/agentscope/utils/common.html index 07fc5f1af..6a05080de 100644 --- a/en/_modules/agentscope/utils/common.html +++ b/en/_modules/agentscope/utils/common.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/utils/logging_utils.html b/en/_modules/agentscope/utils/logging_utils.html index 4a0d99f61..2add2b48f 100644 --- a/en/_modules/agentscope/utils/logging_utils.html +++ b/en/_modules/agentscope/utils/logging_utils.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/utils/monitor.html b/en/_modules/agentscope/utils/monitor.html index 23b6d7b57..7e4663f38 100644 --- a/en/_modules/agentscope/utils/monitor.html +++ b/en/_modules/agentscope/utils/monitor.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/utils/token_utils.html b/en/_modules/agentscope/utils/token_utils.html index ff982e777..ba35e846b 100644 --- a/en/_modules/agentscope/utils/token_utils.html +++ b/en/_modules/agentscope/utils/token_utils.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/utils/tools.html b/en/_modules/agentscope/utils/tools.html index b4a8f2f06..5596dd960 100644 --- a/en/_modules/agentscope/utils/tools.html +++ b/en/_modules/agentscope/utils/tools.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • @@ -105,7 +106,8 @@

    Source code for agentscope.utils.tools

     import os.path
     import secrets
     import string
    -from typing import Any, Literal, List
    +import socket
    +from typing import Any, Literal, List, Optional
     
     from urllib.parse import urlparse
     
    @@ -166,6 +168,48 @@ 

    Source code for agentscope.utils.tools

     
     
     
    +
    +[docs] +def find_available_port() -> int: + """Get an unoccupied socket port number.""" + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind(("", 0)) + return s.getsockname()[1]
    + + + +
    +[docs] +def check_port(port: Optional[int] = None) -> int: + """Check if the port is available. + + Args: + port (`int`): + the port number being checked. + + Returns: + `int`: the port number that passed the check. If the port is found + to be occupied, an available port number will be automatically + returned. + """ + if port is None: + new_port = find_available_port() + logger.warning( + "agent server port is not provided, automatically select " + f"[{new_port}] as the port number.", + ) + return new_port + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + if s.connect_ex(("localhost", port)) == 0: + new_port = find_available_port() + logger.warning( + f"Port [{port}] is occupied, use [{new_port}] instead", + ) + return new_port + return port
    + + + def _guess_type_by_extension( url: str, ) -> Literal["image", "audio", "video", "file"]: diff --git a/en/_modules/agentscope/web/_app.html b/en/_modules/agentscope/web/_app.html index 0fee740b3..ad1a4df30 100644 --- a/en/_modules/agentscope/web/_app.html +++ b/en/_modules/agentscope/web/_app.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/web/studio/studio.html b/en/_modules/agentscope/web/studio/studio.html index 192e8d72c..1debdfde5 100644 --- a/en/_modules/agentscope/web/studio/studio.html +++ b/en/_modules/agentscope/web/studio/studio.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/web/studio/utils.html b/en/_modules/agentscope/web/studio/utils.html index 04037987b..a129cef9c 100644 --- a/en/_modules/agentscope/web/studio/utils.html +++ b/en/_modules/agentscope/web/studio/utils.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/web/workstation/workflow.html b/en/_modules/agentscope/web/workstation/workflow.html index 5f25948e8..eadc25a60 100644 --- a/en/_modules/agentscope/web/workstation/workflow.html +++ b/en/_modules/agentscope/web/workstation/workflow.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/web/workstation/workflow_dag.html b/en/_modules/agentscope/web/workstation/workflow_dag.html index d41c60b9f..08e85c945 100644 --- a/en/_modules/agentscope/web/workstation/workflow_dag.html +++ b/en/_modules/agentscope/web/workstation/workflow_dag.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/web/workstation/workflow_node.html b/en/_modules/agentscope/web/workstation/workflow_node.html index 4dbda0e34..69fc005ca 100644 --- a/en/_modules/agentscope/web/workstation/workflow_node.html +++ b/en/_modules/agentscope/web/workstation/workflow_node.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/agentscope/web/workstation/workflow_utils.html b/en/_modules/agentscope/web/workstation/workflow_utils.html index a901a719d..56bfb3228 100644 --- a/en/_modules/agentscope/web/workstation/workflow_utils.html +++ b/en/_modules/agentscope/web/workstation/workflow_utils.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/_modules/index.html b/en/_modules/index.html index f813a8b5c..6d5551b0f 100644 --- a/en/_modules/index.html +++ b/en/_modules/index.html @@ -67,6 +67,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • @@ -130,6 +131,8 @@

    All modules for which code is available

  • agentscope.prompt
  • agentscope.rpc.rpc_agent_client
  • agentscope.rpc.rpc_agent_pb2_grpc
  • +
  • agentscope.server.launcher
  • +
  • agentscope.server.servicer
  • agentscope.service
    • agentscope.service.execute_code.exec_python
    • agentscope.service.execute_code.exec_shell
    • diff --git a/en/_sources/agentscope.server.launcher.rst.txt b/en/_sources/agentscope.server.launcher.rst.txt new file mode 100644 index 000000000..64ada7a27 --- /dev/null +++ b/en/_sources/agentscope.server.launcher.rst.txt @@ -0,0 +1,6 @@ +agentscope.server.launcher +========================== +.. automodule:: agentscope.server.launcher + :members: + :undoc-members: + :show-inheritance: \ No newline at end of file diff --git a/en/_sources/agentscope.server.rst.txt b/en/_sources/agentscope.server.rst.txt new file mode 100644 index 000000000..409169aa9 --- /dev/null +++ b/en/_sources/agentscope.server.rst.txt @@ -0,0 +1,7 @@ +agentscope.server +================= + +.. automodule:: agentscope.server + :members: + :undoc-members: + :show-inheritance: \ No newline at end of file diff --git a/en/_sources/agentscope.server.servicer.rst.txt b/en/_sources/agentscope.server.servicer.rst.txt new file mode 100644 index 000000000..683ac7f80 --- /dev/null +++ b/en/_sources/agentscope.server.servicer.rst.txt @@ -0,0 +1,6 @@ +agentscope.server.servicer +========================== +.. automodule:: agentscope.server.servicer + :members: + :undoc-members: + :show-inheritance: \ No newline at end of file diff --git a/en/_sources/index.rst.txt b/en/_sources/index.rst.txt index fb81e2e64..1aad67356 100644 --- a/en/_sources/index.rst.txt +++ b/en/_sources/index.rst.txt @@ -38,6 +38,7 @@ AgentScope Documentation agentscope.pipelines agentscope.service agentscope.rpc + agentscope.server agentscope.web agentscope.prompt agentscope.utils diff --git a/en/_sources/tutorial/208-distribute.md.txt b/en/_sources/tutorial/208-distribute.md.txt index 714f2e05f..0381a13f1 100644 --- a/en/_sources/tutorial/208-distribute.md.txt +++ b/en/_sources/tutorial/208-distribute.md.txt @@ -59,14 +59,16 @@ b = AgentB( #### Independent Process Mode In the Independent Process Mode, we need to start the agent server process on the target machine first. +When starting the agent server process, you need to specify a model config file, which contains the models which can be used in the agent server, the IP address and port of the agent server process For example, start two agent server processes on the two different machines with IP `ip_a` and `ip_b`(called `Machine1` and `Machine2` accrodingly). -You can run the following code on `Machine1`: +You can run the following code on `Machine1`.Before running, make sure that the machine has access to all models that used in your application, specifically, you need to put your model config file in `model_config_path_a` and set environment variables such as your model API key correctly in `Machine1`. The example model config file instances are located under `examples/model_configs_template`. ```python # import some packages +# register models which can be used in the server agentscope.init( - ... + model_configs=model_config_path_a, ) # Create an agent service process server = RpcAgentServerLauncher( @@ -79,13 +81,20 @@ server.launch() server.wait_until_terminate() ``` -And run the following code on `Machine2`: +> For similarity, you can run the following command in your terminal rather than the above code: +> +> ```shell +> as_server --host ip_a --port 12001 --model-config-path model_config_path_a +> ``` + +Then put your model config file accordingly in `model_config_path_b`, set environment variables, and run the following code on `Machine2`. ```python # import some packages +# register models which can be used in the server agentscope.init( - ... + model_configs=model_config_path_b, ) # Create an agent service process server = RpcAgentServerLauncher( @@ -98,6 +107,12 @@ server.launch() server.wait_until_terminate() ``` +> Similarly, you can run the following command in your terminal to setup the agent server: +> +> ```shell +> as_server --host ip_b --port 12002 --model-config-path model_config_path_b +> ``` + Then, you can connect to the agent servers from the main process with the following code. ```python @@ -254,6 +269,9 @@ About more detailed technical implementation solutions, please refer to our [pap In agentscope, the agent server provides a running platform for various types of agents. Multiple agents can run in the same agent server and hold independent memory and other local states but they will share the same computation resources. + +After installing the distributed version of AgentScope, you can use the `as_server` command to start the agent server, and the detailed startup arguments can be found in the documentation of the {func}`as_server` function. + As long as the code is not modified, an agent server can provide services for multiple main processes. This means that when running mutliple applications, you only need to start the agent server for the first time, and it can be reused subsequently. diff --git a/en/agentscope.agents.agent.html b/en/agentscope.agents.agent.html index fe75594ef..75c8d49a3 100644 --- a/en/agentscope.agents.agent.html +++ b/en/agentscope.agents.agent.html @@ -66,6 +66,7 @@
    • agentscope.pipelines
    • agentscope.service
    • agentscope.rpc
    • +
    • agentscope.server
    • agentscope.web
    • agentscope.prompt
    • agentscope.utils
    • @@ -322,11 +323,19 @@
      • host (str, defaults to “localhost”) – Hostname of the rpc agent server.

      • port (int, defaults to None) – Port of the rpc agent server.

      • -
      • max_pool_size (int, defaults to 8192) – Max number of task results that the server can accommodate.

      • -
      • max_timeout_seconds (int, defaults to 1800) – Timeout for task results.

      • -
      • local_mode (bool, defaults to True) – Whether the started rpc server only listens to local +

      • max_pool_size (int, defaults to 8192) – Only takes effect when host and port are not filled in. +The max number of agent reply messages that the started agent +server can accommodate. Note that the oldest message will be +deleted after exceeding the pool size.

      • +
      • max_timeout_seconds (int, defaults to 1800) – Only takes effect when host and port are not filled in. +Maximum time for reply messages to be cached in the launched +agent server. Note that expired messages will be deleted.

      • +
      • local_mode (bool, defaults to True) – Only takes effect when host and port are not filled in. +Whether the started agent server only listens to local requests.

      • -
      • lazy_launch (bool, defaults to True) – Only launch the server when the agent is called.

      • +
      • lazy_launch (bool, defaults to True) – Only takes effect when host and port are not filled in. +If True, launch the agent server when the agent is called, +otherwise, launch the agent server immediately.

      • launch_server (bool, defaults to None) – This field has been deprecated and will be removed in future releases.

      diff --git a/en/agentscope.agents.dialog_agent.html b/en/agentscope.agents.dialog_agent.html index 3aedcd62e..6c5adeacc 100644 --- a/en/agentscope.agents.dialog_agent.html +++ b/en/agentscope.agents.dialog_agent.html @@ -66,6 +66,7 @@
    • agentscope.pipelines
    • agentscope.service
    • agentscope.rpc
    • +
    • agentscope.server
    • agentscope.web
    • agentscope.prompt
    • agentscope.utils
    • diff --git a/en/agentscope.agents.dict_dialog_agent.html b/en/agentscope.agents.dict_dialog_agent.html index 0d67c69cb..7500466cc 100644 --- a/en/agentscope.agents.dict_dialog_agent.html +++ b/en/agentscope.agents.dict_dialog_agent.html @@ -66,6 +66,7 @@
    • agentscope.pipelines
    • agentscope.service
    • agentscope.rpc
    • +
    • agentscope.server
    • agentscope.web
    • agentscope.prompt
    • agentscope.utils
    • diff --git a/en/agentscope.agents.html b/en/agentscope.agents.html index 2dd2fe7a7..63e3a7f92 100644 --- a/en/agentscope.agents.html +++ b/en/agentscope.agents.html @@ -71,7 +71,6 @@
    • ReActAgent
    • DistConf
    • RpcAgent
    • -
    • RpcAgentServerLauncher
  • agentscope.memory
  • @@ -80,6 +79,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • @@ -310,11 +310,19 @@
    • host (str, defaults to “localhost”) – Hostname of the rpc agent server.

    • port (int, defaults to None) – Port of the rpc agent server.

    • -
    • max_pool_size (int, defaults to 8192) – Max number of task results that the server can accommodate.

    • -
    • max_timeout_seconds (int, defaults to 1800) – Timeout for task results.

    • -
    • local_mode (bool, defaults to True) – Whether the started rpc server only listens to local +

    • max_pool_size (int, defaults to 8192) – Only takes effect when host and port are not filled in. +The max number of agent reply messages that the started agent +server can accommodate. Note that the oldest message will be +deleted after exceeding the pool size.

    • +
    • max_timeout_seconds (int, defaults to 1800) – Only takes effect when host and port are not filled in. +Maximum time for reply messages to be cached in the launched +agent server. Note that expired messages will be deleted.

    • +
    • local_mode (bool, defaults to True) – Only takes effect when host and port are not filled in. +Whether the started agent server only listens to local requests.

    • -
    • lazy_launch (bool, defaults to True) – Only launch the server when the agent is called.

    • +
    • lazy_launch (bool, defaults to True) – Only takes effect when host and port are not filled in. +If True, launch the agent server when the agent is called, +otherwise, launch the agent server immediately.

    • launch_server (bool, defaults to None) – This field has been deprecated and will be removed in future releases.

    @@ -725,63 +733,6 @@
    -
    -
    -class agentscope.agents.RpcAgentServerLauncher(host: str = 'localhost', port: int | None = None, max_pool_size: int = 8192, max_timeout_seconds: int = 1800, local_mode: bool = False, custom_agents: list | None = None, agent_class: Type[AgentBase] | None = None, agent_args: tuple = (), agent_kwargs: dict | None = None)[source]
    -

    Bases: object

    -

    The launcher of AgentPlatform (formerly RpcAgentServer).

    -
    -
    -__init__(host: str = 'localhost', port: int | None = None, max_pool_size: int = 8192, max_timeout_seconds: int = 1800, local_mode: bool = False, custom_agents: list | None = None, agent_class: Type[AgentBase] | None = None, agent_args: tuple = (), agent_kwargs: dict | None = None) None[source]
    -

    Init a rpc agent server launcher.

    -
    -
    Parameters:
    -
      -
    • host (str, defaults to “localhost”) – Hostname of the rpc agent server.

    • -
    • port (int, defaults to None) – Port of the rpc agent server.

    • -
    • max_pool_size (int, defaults to 8192) – Max number of task results that the server can accommodate.

    • -
    • max_timeout_seconds (int, defaults to 1800) – Timeout for task results.

    • -
    • local_mode (bool, defaults to False) – Whether the started rpc server only listens to local -requests.

    • -
    • custom_agents (list, defaults to None) – A list of custom agent classes that are not in -agentscope.agents.

    • -
    • agent_class (Type[AgentBase], deprecated) – The AgentBase subclass encapsulated by this wrapper.

    • -
    • agent_args (tuple, deprecated) – The args tuple used to -initialize the agent_class.

    • -
    • agent_kwargs (dict, deprecated) – The args dict used to -initialize the agent_class.

    • -
    -
    -
    -
    - -
    -
    -launch(in_subprocess: bool = True) None[source]
    -

    launch a rpc agent server.

    -
    -
    Parameters:
    -

    in_subprocess (bool, optional) – launch the server in subprocess. -Defaults to True. For agents that need to obtain command line -input, such as UserAgent, please set this value to False.

    -
    -
    -
    - -
    -
    -wait_until_terminate() None[source]
    -

    Wait for server process

    -
    - -
    -
    -shutdown() None[source]
    -

    Shutdown the rpc agent server.

    -
    - -
    - diff --git a/en/agentscope.agents.operator.html b/en/agentscope.agents.operator.html index 9562ac2e8..d8267768e 100644 --- a/en/agentscope.agents.operator.html +++ b/en/agentscope.agents.operator.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.agents.react_agent.html b/en/agentscope.agents.react_agent.html index 650e891b8..c54aca32b 100644 --- a/en/agentscope.agents.react_agent.html +++ b/en/agentscope.agents.react_agent.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.agents.rpc_agent.html b/en/agentscope.agents.rpc_agent.html index 5c0aa41a5..06369075a 100644 --- a/en/agentscope.agents.rpc_agent.html +++ b/en/agentscope.agents.rpc_agent.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • @@ -98,13 +99,6 @@

    agentscope.agents.rpc_agent

    Base class for Rpc Agent

    -
    -
    -agentscope.agents.rpc_agent.rpc_servicer_method(func)[source]
    -

    A decorator used to identify that the specific method is an rpc agent -servicer method, which can only be run in the rpc server process.

    -
    -
    class agentscope.agents.rpc_agent.RpcAgent(*args: tuple, **kwargs: dict)[source]
    @@ -198,233 +192,6 @@
    -
    -
    -agentscope.agents.rpc_agent.setup_rpc_agent_server(host: str, port: int, init_settings: dict | None = None, start_event: Event | None = None, stop_event: Event | None = None, pipe: int | None = None, local_mode: bool = True, max_pool_size: int = 8192, max_timeout_seconds: int = 1800, custom_agents: list | None = None) None[source]
    -

    Setup gRPC server rpc agent.

    -
    -
    Parameters:
    -
      -
    • host (str, defaults to “localhost”) – Hostname of the rpc agent server.

    • -
    • port (int) – The socket port monitored by grpc server.

    • -
    • init_settings (dict, defaults to None) – Init settings for agentscope.init.

    • -
    • start_event (EventClass, defaults to None) – An Event instance used to determine whether the child process -has been started.

    • -
    • stop_event (EventClass, defaults to None) – The stop Event instance used to determine whether the child -process has been stopped.

    • -
    • pipe (int, defaults to None) – A pipe instance used to pass the actual port of the server.

    • -
    • local_mode (bool, defaults to None) – Only listen to local requests.

    • -
    • max_pool_size (int, defaults to 8192) – Max number of task results that the server can accommodate.

    • -
    • max_timeout_seconds (int, defaults to 1800) – Timeout for task results.

    • -
    • custom_agents (list, defaults to None) – A list of custom agent classes that are not in agentscope.agents.

    • -
    -
    -
    -
    - -
    -
    -async agentscope.agents.rpc_agent.setup_rpc_agent_server_async(host: str, port: int, init_settings: dict | None = None, start_event: Event | None = None, stop_event: Event | None = None, pipe: int | None = None, local_mode: bool = True, max_pool_size: int = 8192, max_timeout_seconds: int = 1800, custom_agents: list | None = None) None[source]
    -

    Setup gRPC server rpc agent in an async way.

    -
    -
    Parameters:
    -
      -
    • host (str, defaults to “localhost”) – Hostname of the rpc agent server.

    • -
    • port (int) – The socket port monitored by grpc server.

    • -
    • init_settings (dict, defaults to None) – Init settings for agentscope.init.

    • -
    • start_event (EventClass, defaults to None) – An Event instance used to determine whether the child process -has been started.

    • -
    • stop_event (EventClass, defaults to None) – The stop Event instance used to determine whether the child -process has been stopped.

    • -
    • pipe (int, defaults to None) – A pipe instance used to pass the actual port of the server.

    • -
    • local_mode (bool, defaults to None) – Only listen to local requests.

    • -
    • max_pool_size (int, defaults to 8192) – Max number of task results that the server can accommodate.

    • -
    • max_timeout_seconds (int, defaults to 1800) – Timeout for task results.

    • -
    • custom_agents (list, defaults to None) – A list of custom agent classes that are not in agentscope.agents.

    • -
    -
    -
    -
    - -
    -
    -agentscope.agents.rpc_agent.find_available_port() int[source]
    -

    Get an unoccupied socket port number.

    -
    - -
    -
    -agentscope.agents.rpc_agent.check_port(port: int | None = None) int[source]
    -

    Check if the port is available.

    -
    -
    Parameters:
    -

    port (int) – the port number being checked.

    -
    -
    Returns:
    -

    the port number that passed the check. If the port is found -to be occupied, an available port number will be automatically -returned.

    -
    -
    Return type:
    -

    int

    -
    -
    -
    - -
    -
    -class agentscope.agents.rpc_agent.RpcAgentServerLauncher(host: str = 'localhost', port: int | None = None, max_pool_size: int = 8192, max_timeout_seconds: int = 1800, local_mode: bool = False, custom_agents: list | None = None, agent_class: Type[AgentBase] | None = None, agent_args: tuple = (), agent_kwargs: dict | None = None)[source]
    -

    Bases: object

    -

    The launcher of AgentPlatform (formerly RpcAgentServer).

    -
    -
    -__init__(host: str = 'localhost', port: int | None = None, max_pool_size: int = 8192, max_timeout_seconds: int = 1800, local_mode: bool = False, custom_agents: list | None = None, agent_class: Type[AgentBase] | None = None, agent_args: tuple = (), agent_kwargs: dict | None = None) None[source]
    -

    Init a rpc agent server launcher.

    -
    -
    Parameters:
    -
      -
    • host (str, defaults to “localhost”) – Hostname of the rpc agent server.

    • -
    • port (int, defaults to None) – Port of the rpc agent server.

    • -
    • max_pool_size (int, defaults to 8192) – Max number of task results that the server can accommodate.

    • -
    • max_timeout_seconds (int, defaults to 1800) – Timeout for task results.

    • -
    • local_mode (bool, defaults to False) – Whether the started rpc server only listens to local -requests.

    • -
    • custom_agents (list, defaults to None) – A list of custom agent classes that are not in -agentscope.agents.

    • -
    • agent_class (Type[AgentBase], deprecated) – The AgentBase subclass encapsulated by this wrapper.

    • -
    • agent_args (tuple, deprecated) – The args tuple used to -initialize the agent_class.

    • -
    • agent_kwargs (dict, deprecated) – The args dict used to -initialize the agent_class.

    • -
    -
    -
    -
    - -
    -
    -launch(in_subprocess: bool = True) None[source]
    -

    launch a rpc agent server.

    -
    -
    Parameters:
    -

    in_subprocess (bool, optional) – launch the server in subprocess. -Defaults to True. For agents that need to obtain command line -input, such as UserAgent, please set this value to False.

    -
    -
    -
    - -
    -
    -wait_until_terminate() None[source]
    -

    Wait for server process

    -
    - -
    -
    -shutdown() None[source]
    -

    Shutdown the rpc agent server.

    -
    - -
    - -
    -
    -class agentscope.agents.rpc_agent.AgentPlatform(host: str = 'localhost', port: int | None = None, max_pool_size: int = 8192, max_timeout_seconds: int = 1800)[source]
    -

    Bases: RpcAgentServicer

    -

    A platform for agent to run on (formerly RpcServerSideWrapper)

    -
    -
    -__init__(host: str = 'localhost', port: int | None = None, max_pool_size: int = 8192, max_timeout_seconds: int = 1800)[source]
    -

    Init the AgentPlatform.

    -
    -
    Parameters:
    -
      -
    • host (str, defaults to “localhost”) – Hostname of the rpc agent server.

    • -
    • port (int, defaults to None) – Port of the rpc agent server.

    • -
    • max_pool_size (int, defaults to 8192) – The max number of task results that the server can -accommodate. Note that the oldest result will be deleted -after exceeding the pool size.

    • -
    • max_timeout_seconds (int, defaults to 1800) – Timeout for task results. Note that expired results will be -deleted.

    • -
    -
    -
    -
    - -
    -
    -get_task_id() int[source]
    -

    Get the auto-increment task id.

    -
    - -
    -
    -agent_exists(agent_id: str) bool[source]
    -

    Check whether the agent exists.

    -
    -
    Parameters:
    -

    agent_id (str) – the agent id.

    -
    -
    Returns:
    -

    whether the agent exists.

    -
    -
    Return type:
    -

    bool

    -
    -
    -
    - -
    -
    -check_and_generate_agent(agent_id: str, agent_configs: dict) None[source]
    -

    Check whether the agent exists, and create new agent instance -for new agent.

    -
    -
    Parameters:
    -
      -
    • agent_id (str) – the agent id.

    • -
    • agent_configs (dict) –

      configuration used to initialize the agent, -with three fields (generated in _AgentMeta):

      -
      {
      -    "class_name": {name of the agent}
      -    "args": {args in tuple type to init the agent}
      -    "kwargs": {args in dict type to init the agent}
      -}
      -
      -
      -

    • -
    -
    -
    -
    - -
    -
    -check_and_delete_agent(agent_id: str) None[source]
    -

    Check whether the agent exists, and delete the agent instance -for the agent_id.

    -
    -
    Parameters:
    -

    agent_id (str) – the agent id.

    -
    -
    -
    - -
    -
    -call_func(request: RpcMsg, context: ServicerContext) RpcMsg[source]
    -

    Call the specific servicer function.

    -
    - -
    -
    -process_messages(task_id: int, agent_id: str, task_msg: dict | None = None) None[source]
    -

    Task processing.

    -
    - -
    -
    diff --git a/en/agentscope.agents.text_to_image_agent.html b/en/agentscope.agents.text_to_image_agent.html index ae79839c3..17009c52f 100644 --- a/en/agentscope.agents.text_to_image_agent.html +++ b/en/agentscope.agents.text_to_image_agent.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.agents.user_agent.html b/en/agentscope.agents.user_agent.html index 2032c52fd..e0fd29953 100644 --- a/en/agentscope.agents.user_agent.html +++ b/en/agentscope.agents.user_agent.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.constants.html b/en/agentscope.constants.html index 3a7905fbb..0f86700fe 100644 --- a/en/agentscope.constants.html +++ b/en/agentscope.constants.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.exception.html b/en/agentscope.exception.html index 2fe4bb72a..2d203bca3 100644 --- a/en/agentscope.exception.html +++ b/en/agentscope.exception.html @@ -80,6 +80,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.file_manager.html b/en/agentscope.file_manager.html index 3e3cf40a2..b193df22e 100644 --- a/en/agentscope.file_manager.html +++ b/en/agentscope.file_manager.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.html b/en/agentscope.html index 35a2659e8..477049f3c 100644 --- a/en/agentscope.html +++ b/en/agentscope.html @@ -72,6 +72,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.memory.html b/en/agentscope.memory.html index 3514823ba..609ef4cd6 100644 --- a/en/agentscope.memory.html +++ b/en/agentscope.memory.html @@ -72,6 +72,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.memory.memory.html b/en/agentscope.memory.memory.html index fb7bb8761..30e7f5791 100644 --- a/en/agentscope.memory.memory.html +++ b/en/agentscope.memory.memory.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.memory.temporary_memory.html b/en/agentscope.memory.temporary_memory.html index 92664f2e6..2d243fea6 100644 --- a/en/agentscope.memory.temporary_memory.html +++ b/en/agentscope.memory.temporary_memory.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.message.html b/en/agentscope.message.html index d8a944d51..e9b6be9e4 100644 --- a/en/agentscope.message.html +++ b/en/agentscope.message.html @@ -76,6 +76,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • @@ -377,7 +378,7 @@
    -agentscope.message.deserialize(s: str) MessageBase | Sequence[source]
    +agentscope.message.deserialize(s: str | bytes) MessageBase | Sequence[source]

    Deserialize json string into MessageBase

    diff --git a/en/agentscope.models.config.html b/en/agentscope.models.config.html index 5c10d9200..2037436ef 100644 --- a/en/agentscope.models.config.html +++ b/en/agentscope.models.config.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.models.dashscope_model.html b/en/agentscope.models.dashscope_model.html index 6b3c6f68d..5b517be6b 100644 --- a/en/agentscope.models.dashscope_model.html +++ b/en/agentscope.models.dashscope_model.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.models.gemini_model.html b/en/agentscope.models.gemini_model.html index e536950c0..187ceca44 100644 --- a/en/agentscope.models.gemini_model.html +++ b/en/agentscope.models.gemini_model.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.models.html b/en/agentscope.models.html index 25aa84a50..cb9750bc1 100644 --- a/en/agentscope.models.html +++ b/en/agentscope.models.html @@ -93,6 +93,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.models.litellm_model.html b/en/agentscope.models.litellm_model.html index ca8fe6bc5..df934ab07 100644 --- a/en/agentscope.models.litellm_model.html +++ b/en/agentscope.models.litellm_model.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.models.model.html b/en/agentscope.models.model.html index 753599ca0..5aa0a5ca3 100644 --- a/en/agentscope.models.model.html +++ b/en/agentscope.models.model.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.models.ollama_model.html b/en/agentscope.models.ollama_model.html index c26865161..a843d5487 100644 --- a/en/agentscope.models.ollama_model.html +++ b/en/agentscope.models.ollama_model.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.models.openai_model.html b/en/agentscope.models.openai_model.html index b74f5044c..e880e92f3 100644 --- a/en/agentscope.models.openai_model.html +++ b/en/agentscope.models.openai_model.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.models.post_model.html b/en/agentscope.models.post_model.html index c3aa3f480..7e1bbeba5 100644 --- a/en/agentscope.models.post_model.html +++ b/en/agentscope.models.post_model.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.models.response.html b/en/agentscope.models.response.html index f5b9e8a76..dbf1a1a2e 100644 --- a/en/agentscope.models.response.html +++ b/en/agentscope.models.response.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.models.zhipu_model.html b/en/agentscope.models.zhipu_model.html index c8acf91fa..e32a200db 100644 --- a/en/agentscope.models.zhipu_model.html +++ b/en/agentscope.models.zhipu_model.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.msghub.html b/en/agentscope.msghub.html index f165733cb..75aee6300 100644 --- a/en/agentscope.msghub.html +++ b/en/agentscope.msghub.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.parsers.code_block_parser.html b/en/agentscope.parsers.code_block_parser.html index f27125be1..745e7dbe8 100644 --- a/en/agentscope.parsers.code_block_parser.html +++ b/en/agentscope.parsers.code_block_parser.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.parsers.html b/en/agentscope.parsers.html index c880d5b9b..baeef6013 100644 --- a/en/agentscope.parsers.html +++ b/en/agentscope.parsers.html @@ -76,6 +76,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.parsers.json_object_parser.html b/en/agentscope.parsers.json_object_parser.html index 437e6bfa9..d0b921a16 100644 --- a/en/agentscope.parsers.json_object_parser.html +++ b/en/agentscope.parsers.json_object_parser.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.parsers.parser_base.html b/en/agentscope.parsers.parser_base.html index 0636fc046..e6dd5cc1c 100644 --- a/en/agentscope.parsers.parser_base.html +++ b/en/agentscope.parsers.parser_base.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.parsers.tagged_content_parser.html b/en/agentscope.parsers.tagged_content_parser.html index 8ee2da16e..002711a2a 100644 --- a/en/agentscope.parsers.tagged_content_parser.html +++ b/en/agentscope.parsers.tagged_content_parser.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.pipelines.functional.html b/en/agentscope.pipelines.functional.html index 87aa28783..0606cda8b 100644 --- a/en/agentscope.pipelines.functional.html +++ b/en/agentscope.pipelines.functional.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.pipelines.html b/en/agentscope.pipelines.html index 2d0463d76..6dbb85d02 100644 --- a/en/agentscope.pipelines.html +++ b/en/agentscope.pipelines.html @@ -81,6 +81,7 @@
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.pipelines.pipeline.html b/en/agentscope.pipelines.pipeline.html index 4f04b9772..d85bdeab6 100644 --- a/en/agentscope.pipelines.pipeline.html +++ b/en/agentscope.pipelines.pipeline.html @@ -66,6 +66,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • diff --git a/en/agentscope.prompt.html b/en/agentscope.prompt.html index 5435aee6f..b96a8c851 100644 --- a/en/agentscope.prompt.html +++ b/en/agentscope.prompt.html @@ -68,6 +68,7 @@
  • agentscope.pipelines
  • agentscope.service
  • agentscope.rpc
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
    • PromptType
    • diff --git a/en/agentscope.rpc.html b/en/agentscope.rpc.html index 71603382f..1beeb3dfe 100644 --- a/en/agentscope.rpc.html +++ b/en/agentscope.rpc.html @@ -23,7 +23,7 @@ - + @@ -77,6 +77,7 @@
    • add_RpcAgentServicer_to_server()
  • +
  • agentscope.server
  • agentscope.web
  • agentscope.prompt
  • agentscope.utils
  • @@ -264,7 +265,7 @@