Skip to content

Commit

Permalink
fix typing issues in pipeline packages
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-x-c committed Jan 16, 2024
1 parent c455988 commit b4e5c3b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 47 deletions.
42 changes: 23 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
35 changes: 20 additions & 15 deletions src/agentscope/pipelines/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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]`):
Expand All @@ -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.
Expand All @@ -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`):
Expand All @@ -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)
Expand All @@ -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
Expand Down
27 changes: 14 additions & 13 deletions src/agentscope/pipelines/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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.
"""


Expand Down Expand Up @@ -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,
)


Expand Down Expand Up @@ -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,
)


Expand Down Expand Up @@ -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,
)


Expand Down Expand Up @@ -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,
)


Expand All @@ -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)

0 comments on commit b4e5c3b

Please sign in to comment.