From b4e5c3bc50982ad20b2fceda3f040c68ca358cf7 Mon Sep 17 00:00:00 2001 From: "panxuchen.pxc" Date: Tue, 16 Jan 2024 12:07:34 +0800 Subject: [PATCH] fix typing issues in pipeline packages --- README.md | 42 ++++++++++++++------------ src/agentscope/pipelines/functional.py | 35 ++++++++++++--------- src/agentscope/pipelines/pipeline.py | 27 +++++++++-------- 3 files changed, 57 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index e261ef38a..ad9065ec9 100644 --- a/README.md +++ b/README.md @@ -16,24 +16,28 @@ Welcome to join our community on [Discord](https://discord.gg/Fwg5hZ2S) or DingD Table of Contents ================= -* [Installation](#installation) - * [From source](#from-source) - * [Using pip](#using-pip) -* [Quick Start](#quick-start) - * [Basic Usage](#basic-usage) - * [Step 1: Prepare Model Configs](#step-1-prepare-model-configs) - * [Step2: Create Agents](#step-2-create-agents) - * [Step3: Construct Conversation](#step-3-construct-conversation) - * [Advanced Usage](#advanced-usage) - * [Pipeline and MsgHub](#pipeline-and-msghub) - * [Customize Your Own Agent](#customize-your-own-agent) - * [Built-in Resources](#built-in-resources) - * [Agent Pool](#agent-pool) - * [Services](#services) - * [Examples Applications](#example-applications) -* [License](#license) -* [Contributing](#contributing) -* [References](#references) +- [AgentScope](#agentscope) +- [Table of Contents](#table-of-contents) + - [Installation](#installation) + - [From source](#from-source) + - [Using pip](#using-pip) + - [Quick Start](#quick-start) + - [Basic Usage](#basic-usage) + - [Step 1: Prepare Model Configs](#step-1-prepare-model-configs) + - [OpenAI API Config](#openai-api-config) + - [Post Request API Config](#post-request-api-config) + - [Step 2: Create Agents](#step-2-create-agents) + - [Step 3: Construct Conversation](#step-3-construct-conversation) + - [Advanced Usage](#advanced-usage) + - [**Pipeline** and **MsgHub**](#pipeline-and-msghub) + - [Customize Your Own Agent](#customize-your-own-agent) + - [Built-in Resources](#built-in-resources) + - [Agent Pool](#agent-pool) + - [Services](#services) + - [Example Applications](#example-applications) + - [License](#license) + - [Contributing](#contributing) + - [References](#references) ## Installation @@ -198,7 +202,7 @@ To simplify the construction of agents communication, AgentScope provides two he ```python from agentscope.pipelines.functional import sequentialpipeline - x3 = sequentialpipeline([agent1, agent2, agent3], x=input_msg) + x3 = sequentialpipeline(operators=[agent1, agent2, agent3], x=input_msg) ``` - **MsgHub**: To achieve a group conversation, AgentScope provides message hub. diff --git a/src/agentscope/pipelines/functional.py b/src/agentscope/pipelines/functional.py index 558d36fbb..39a632c71 100644 --- a/src/agentscope/pipelines/functional.py +++ b/src/agentscope/pipelines/functional.py @@ -41,16 +41,14 @@ def sequentialpipeline( def ifelsepipeline( - x: dict, condition_func: Callable, if_body_operator: Operator, else_body_operator: Operator = placeholder, + x: Optional[dict] = None, ) -> dict: """Functional version of IfElsePipeline. Args: - x (`dict`): - The input dictionary. condition_func (`Callable`): A function that determines whether to exeucte `if_body_operator` or `else_body_operator` based on x. @@ -59,6 +57,8 @@ def ifelsepipeline( else_body_operator (`Operator`, defaults to `placeholder`): An operator executed when condition_func returns False, does nothing and just return the input by default. + x (`Optional[dict]`, defaults to `None`): + The input dictionary. Returns: `dict`: the output dictionary. @@ -70,18 +70,16 @@ def ifelsepipeline( def switchpipeline( - x: dict, - condition_func: Callable[[dict], Any], + condition_func: Callable[[Any], Any], case_operators: Mapping[Any, Operator], default_operator: Operator = placeholder, + x: Optional[dict] = None, ) -> dict: """Functional version of SwitchPipeline. Args: - x (`dict`): - The input dictionary. - condition_func (`Callable[[dict], Any]`): + condition_func (`Callable[[Any], Any]`): A function that determines which case_operator to execute based on the input x. case_operators (`Mapping[Any, Operator]`): @@ -91,6 +89,8 @@ def switchpipeline( An operator that is executed when the actual condition do not meet any of the case_operators, does nothing and just return the input by default. + x (`Optional[dict]`, defaults to `None`): + The input dictionary. Returns: dict: the output dictionary. @@ -103,16 +103,14 @@ def switchpipeline( def forlooppipeline( - x: dict, loop_body_operator: Operator, max_loop: int, break_func: Callable[[dict], bool] = lambda _: False, + x: Optional[dict] = None, ) -> dict: """Functional version of ForLoopPipeline. Args: - x (`dict`): - The input dictionary. loop_body_operator (`Operator`): An operator executed as the body of the loop. max_loop (`int`): @@ -121,10 +119,14 @@ def forlooppipeline( A function used to determine whether to break out of the loop based on the output of the loop_body_operator, defaults to `lambda _: False` + x (`Optional[dict]`, defaults to `None`): + The input dictionary. Returns: `dict`: The output dictionary. """ + if x is None: + x = {} for _ in range(max_loop): # loop body x = loop_body_operator(x) @@ -135,24 +137,27 @@ def forlooppipeline( def whilelooppipeline( - x: dict, loop_body_operator: Operator, - condition_func: Callable[[int, dict], bool] = lambda _, __: False, + condition_func: Callable[[int, Any], bool] = lambda _, __: False, + x: Optional[dict] = None, ) -> dict: """Functional version of WhileLoopPipeline. Args: - x (`dict`): The input dictionary. loop_body_operator (`Operator`): An operator executed as the body of the loop. - condition_func (`Callable[[int, dict], bool]`, optional): A function + condition_func (`Callable[[int, Any], bool]`, optional): A function that determines whether to continue executing the loop body based on the current loop number and output of the loop_body_operator, defaults to `lambda _,__: False` + x (`Optional[dict]`, defaults to `None`): + The input dictionary. Returns: `dict`: the output dictionary. """ + if x is None: + x = {} i = 0 while condition_func(i, x): # loop body diff --git a/src/agentscope/pipelines/pipeline.py b/src/agentscope/pipelines/pipeline.py index 097a592a1..892645be9 100644 --- a/src/agentscope/pipelines/pipeline.py +++ b/src/agentscope/pipelines/pipeline.py @@ -4,6 +4,7 @@ from typing import Callable, Sequence from typing import Any from typing import Mapping +from typing import Optional from abc import abstractmethod from .functional import ( @@ -25,15 +26,15 @@ class PipelineBase(Operator): """ @abstractmethod - def __call__(self, x: dict = None) -> dict: - r"""Define the actions taken by this pipeline. + def __call__(self, x: Optional[dict] = None) -> dict: + """Define the actions taken by this pipeline. Args: - x (`dict`): + x (Optional[`dict`], optional): Dialog history and some environment information Returns: - The pipeline's response to the input. + `dict`: The pipeline's response to the input. """ @@ -71,12 +72,12 @@ def __init__( self.if_body_operator = if_body_operator self.else_body_operator = else_body_operator - def __call__(self, x: dict = None) -> dict: + def __call__(self, x: Optional[dict] = None) -> dict: return ifelsepipeline( - x, condition_func=self.condition_func, if_body_operator=self.if_body_operator, else_body_operator=self.else_body_operator, + x=x, ) @@ -117,12 +118,12 @@ def __init__( self.case_operators = case_operators self.default_operator = default_operator - def __call__(self, x: dict = None) -> dict: + def __call__(self, x: Optional[dict] = None) -> dict: return switchpipeline( - x, condition_func=self.condition_func, case_operators=self.case_operators, default_operator=self.default_operator, + x=x, ) @@ -166,12 +167,12 @@ def __init__( self.max_loop = max_loop self.break_func = break_func - def __call__(self, x: dict = None) -> dict: + def __call__(self, x: Optional[dict] = None) -> dict: return forlooppipeline( - x, loop_body_operator=self.loop_body_operator, max_loop=self.max_loop, break_func=self.break_func, + x=x, ) @@ -206,11 +207,11 @@ def __init__( self.condition_func = condition_func self.loop_body_operator = loop_body_operator - def __call__(self, x: dict = None) -> dict: + def __call__(self, x: Optional[dict] = None) -> dict: return whilelooppipeline( - x, loop_body_operator=self.loop_body_operator, condition_func=self.condition_func, + x=x, ) @@ -234,5 +235,5 @@ def __init__(self, operators: Sequence[Operator]) -> None: """ self.operators = operators - def __call__(self, x: dict = None) -> dict: + def __call__(self, x: Optional[dict] = None) -> dict: return sequentialpipeline(operators=self.operators, x=x)