Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add environment modules #378

Open
wants to merge 181 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
181 commits
Select commit Hold shift + click to select a range
1d60d2b
update as_studio
pan-x-c Jul 30, 2024
7f4e920
add environment
pan-x-c Jul 30, 2024
d049bbb
add env
pan-x-c Jul 31, 2024
08bca3f
refactor env and add test
pan-x-c Aug 2, 2024
320ee62
add basic environment
pan-x-c Aug 2, 2024
be8a93e
update map environment
pan-x-c Aug 5, 2024
ee41cf3
fix pre-commit
pan-x-c Aug 5, 2024
13ec5b5
fix missing file
pan-x-c Aug 5, 2024
0995de4
fix missing file
pan-x-c Aug 5, 2024
484902f
add create env
pan-x-c Aug 5, 2024
088949c
merge main
pan-x-c Aug 5, 2024
c87ef56
rename attr
pan-x-c Aug 5, 2024
fe186ec
fix distance
pan-x-c Aug 6, 2024
3063356
add chatroom
pan-x-c Aug 6, 2024
bb23293
merge main
pan-x-c Aug 6, 2024
0aa9794
add chat room
pan-x-c Aug 6, 2024
9ce81bd
add chat room
pan-x-c Aug 6, 2024
79f5fb9
update chatroom
pan-x-c Aug 7, 2024
f77b273
remove environment module
pan-x-c Aug 7, 2024
cdffe6a
add rpc object
pan-x-c Aug 7, 2024
14c7e7b
rerwrite rpc agent with rpc object
pan-x-c Aug 7, 2024
c4e4e97
Merge branch 'main' into feature/pxc/env
pan-x-c Aug 7, 2024
0eb7140
support custom func
pan-x-c Aug 8, 2024
344f1b1
rename to env
pan-x-c Aug 8, 2024
5781fa0
merge rpc server launcher func into rpc object
pan-x-c Aug 8, 2024
449eb06
update serialize and deserialize
pan-x-c Aug 8, 2024
acbfbb0
pass all test
pan-x-c Aug 8, 2024
65895bd
add env test
pan-x-c Aug 8, 2024
4f4d14e
add missing file
pan-x-c Aug 8, 2024
2386bb8
move environments to examples
pan-x-c Aug 9, 2024
9ff9234
support custom async func
pan-x-c Aug 9, 2024
7dc2980
rename test
pan-x-c Aug 9, 2024
1b5bde9
add two third game
pan-x-c Aug 9, 2024
633c001
add configs
pan-x-c Aug 9, 2024
69dfd50
add two third game
pan-x-c Aug 9, 2024
0eb5983
fix guess two third example
Aug 13, 2024
7e89051
add large scale simulation example
Aug 13, 2024
21377d0
add large scale simulation example
Aug 13, 2024
0572991
add large scale simulation readme
Aug 13, 2024
4548a98
fix bug of group env
Aug 13, 2024
5709f8d
fix readme
Aug 13, 2024
3e7d87f
add a simple auction simulation example for environment
PatrickG1014 Aug 13, 2024
18481dd
Merge branch 'main' into feature/pxc/env
pan-x-c Aug 14, 2024
7b6e161
update @event_func
pan-x-c Aug 14, 2024
028a3a9
refactor: update auction simulation bid logic
PatrickG1014 Aug 14, 2024
c1777f5
Merge remote-tracking branch 'p.agentscope/feature/pxc/env' into add_…
PatrickG1014 Aug 14, 2024
ef40672
update event_func usage in auction simulation example
PatrickG1014 Aug 14, 2024
a94e92f
rpcenv support eventlistener
pan-x-c Aug 15, 2024
30a069a
fix dependency
pan-x-c Aug 15, 2024
f46f8c1
Merge remote-tracking branch 'p.agentscope/feature/pxc/env' into add_…
PatrickG1014 Aug 15, 2024
95f896a
rpcenv auction simulation example
PatrickG1014 Aug 15, 2024
77c07e4
remove base64 encode/decode
pan-x-c Aug 15, 2024
ff53531
Hot fix for MonitorManager
chenyushuo Aug 15, 2024
ddf008e
Add chatroom_example.py
chenyushuo Aug 19, 2024
9f01d59
move AgentWithChatRoom to chatroom.py
chenyushuo Aug 19, 2024
bcc1ed7
add argparse
chenyushuo Aug 19, 2024
357ffbf
bug fix in chatroom example
chenyushuo Aug 19, 2024
dd4bab1
fix cross reference
pan-x-c Aug 19, 2024
41ee7e0
Merge branch 'feature/pxc/env' of https://github.com/pan-x-c/AgentSco…
chenyushuo Aug 19, 2024
bdfd999
fix cross reference
pan-x-c Aug 19, 2024
12dbe7f
Merge branch 'feature/pxc/env' of https://github.com/pan-x-c/AgentSco…
chenyushuo Aug 19, 2024
824cacf
fix cross reference
pan-x-c Aug 20, 2024
660a5b8
bug fix in chatroom example
chenyushuo Aug 20, 2024
647edd6
Merge branch 'feature/pxc/env' of https://github.com/pan-x-c/AgentSco…
chenyushuo Aug 20, 2024
753af8e
add listener for Mentioned
chenyushuo Aug 20, 2024
57cb1b1
fix serialize
pan-x-c Aug 20, 2024
dc70ca9
add studio support
chenyushuo Aug 20, 2024
e71e403
refactor placeholder
pan-x-c Aug 20, 2024
ecfb142
refactor placeholder
pan-x-c Aug 20, 2024
0c75456
Merge branch 'feature/pxc/env' of https://github.com/pan-x-c/AgentSco…
chenyushuo Aug 20, 2024
afb1bb9
speedup create agent
pan-x-c Aug 20, 2024
f7aedc4
Merge remote-tracking branch 'p.agentscope/feature/pxc/env' into add_…
PatrickG1014 Aug 20, 2024
017987b
fix create agent test
pan-x-c Aug 20, 2024
01a1ff2
add distributed auction simulation example
PatrickG1014 Aug 20, 2024
42eded5
fix linux test
Aug 20, 2024
1b8fe84
add chatroom_with_assistant_example.py
chenyushuo Aug 21, 2024
d87b4fe
Merge branch 'feature/pxc/env' of https://github.com/pan-x-c/AgentSco…
chenyushuo Aug 21, 2024
c7b8a06
add timeout to ChatRoomAgentWithAssistant
chenyushuo Aug 22, 2024
2ca23f3
replace asyncio with threading
chenyushuo Aug 22, 2024
72ca22f
add README.md for chatroom
chenyushuo Aug 22, 2024
e4e95c4
update README.md for chatroom
chenyushuo Aug 22, 2024
455330d
add redis result pool
pan-x-c Aug 26, 2024
1ca8668
reuse grpc channel
pan-x-c Aug 26, 2024
c40d74b
fix dependency
pan-x-c Aug 26, 2024
182a0ca
update redis
pan-x-c Aug 27, 2024
8abc671
add todo
pan-x-c Aug 27, 2024
7590d34
Merge branch 'main' into feature/pxc/redis
pan-x-c Aug 28, 2024
e132805
merge main
pan-x-c Aug 28, 2024
84b9286
merge main
pan-x-c Aug 28, 2024
9f13ed6
merge main
pan-x-c Aug 29, 2024
7c94944
test redis instance exists
pan-x-c Aug 29, 2024
50c4576
test redis instance exists
pan-x-c Aug 29, 2024
05213ca
add todo
pan-x-c Aug 29, 2024
f450e86
remove rpc_agent and rpc_env
pan-x-c Aug 29, 2024
ae39d56
finish async
pan-x-c Aug 29, 2024
67dcd4b
add async pool test
pan-x-c Aug 29, 2024
0347e09
add async pool test
pan-x-c Aug 29, 2024
66bd2e8
Merge branch 'main' into feature/pxc/env
pan-x-c Aug 29, 2024
9942c35
fix test
pan-x-c Aug 29, 2024
0fd1a7f
support set agent id
pan-x-c Aug 29, 2024
92791dd
update doc
pan-x-c Aug 30, 2024
d105b04
add sync_func
pan-x-c Aug 30, 2024
a972b23
remove set start method
pan-x-c Aug 30, 2024
43ef94f
update tutorial
pan-x-c Aug 30, 2024
6592014
fix vllm script
pan-x-c Aug 30, 2024
67abf3c
finish zh tutorial
pan-x-c Sep 2, 2024
3194420
finish english tutorial
pan-x-c Sep 2, 2024
2e5db8d
fix linux test
pan-x-c Sep 2, 2024
c4195e7
fix large scale simulation example
pan-x-c Sep 2, 2024
cd2f70c
Merge branch 'feature/pxc/env' of https://github.com/pan-x-c/AgentSco…
chenyushuo Sep 2, 2024
e2e7999
fix grpc fork comp
pan-x-c Sep 2, 2024
6d520c2
Merge branch 'feature/pxc/env' of https://github.com/pan-x-c/AgentSco…
chenyushuo Sep 3, 2024
673f1b6
Merge branch 'main' into feature/pxc/env
pan-x-c Sep 3, 2024
2d0dbab
Merge branch 'feature/pxc/env' of github.com:pan-x-c/AgentScope into …
pan-x-c Sep 3, 2024
145c4ba
rename rpc agent client to rpc client
pan-x-c Sep 3, 2024
b796d90
Add RpcService
chenyushuo Sep 3, 2024
2ef1c89
Add RpcService
chenyushuo Sep 3, 2024
693b516
Add RpcService
chenyushuo Sep 3, 2024
0a2fe7e
Add example for parallel service
chenyushuo Sep 4, 2024
f1e027f
Add example for parallel service
chenyushuo Sep 4, 2024
3ae2aef
Merge branch 'feature/pxc/env' of https://github.com/pan-x-c/AgentSco…
chenyushuo Sep 5, 2024
9808caf
fix in parallel service
chenyushuo Sep 5, 2024
5d3bd26
fix bug alloc server
pan-x-c Sep 5, 2024
5255915
Merge branch 'feature/pxc/env' of https://github.com/pan-x-c/AgentSco…
chenyushuo Sep 5, 2024
6e4531f
fix bug alloc server
pan-x-c Sep 5, 2024
bf9c7bb
Merge branch 'feature/pxc/env' of https://github.com/pan-x-c/AgentSco…
chenyushuo Sep 5, 2024
4d09382
fix bug alloc server
pan-x-c Sep 5, 2024
2d096cb
Merge branch 'feature/pxc/env' of https://github.com/pan-x-c/AgentSco…
chenyushuo Sep 5, 2024
99bac7f
fix in parallel service
chenyushuo Sep 6, 2024
9cd5800
fix in parallel service
chenyushuo Sep 6, 2024
2c8675e
Update _client.py
chenyushuo Sep 6, 2024
7dc164c
code format
chenyushuo Sep 6, 2024
bee2d1f
bug fix
chenyushuo Sep 6, 2024
f0845bb
Merge pull request #8 from chenyushuo/environment
pan-x-c Sep 6, 2024
f4f67ee
merge main
pan-x-c Sep 9, 2024
b10f426
enhance error reporting
pan-x-c Sep 9, 2024
ae2ae9a
fix redis pool
pan-x-c Sep 9, 2024
acd4b77
fix set attr
pan-x-c Sep 9, 2024
8c1a668
fix redis pool
pan-x-c Sep 9, 2024
2ae6cdb
add fix
pan-x-c Sep 10, 2024
3ee5876
call in thread use pool
pan-x-c Sep 10, 2024
d9698dd
fix async test
pan-x-c Sep 12, 2024
c39e9c9
fix linux test
pan-x-c Sep 12, 2024
7a3f946
fix linux test
pan-x-c Sep 12, 2024
c54c9f1
fix pre-commit
pan-x-c Sep 12, 2024
0f98548
use timeout for update placeh
pan-x-c Sep 13, 2024
a8f4c83
server support set capacity
pan-x-c Sep 13, 2024
0b773ed
limit the capcity of sub-process mode
pan-x-c Sep 13, 2024
d52629d
update tutorial
pan-x-c Sep 13, 2024
9b9ace7
opt error report
pan-x-c Sep 13, 2024
f1838a5
fix update resp not exist
pan-x-c Sep 18, 2024
728b8bb
Merge remote-tracking branch 'p.agentscope/feature/pxc/env' into add_…
PatrickG1014 Sep 27, 2024
a1cf0ee
update env auction example
PatrickG1014 Sep 27, 2024
5b9e1a4
update experiment
pan-x-c Oct 8, 2024
bc9a0d1
add retry strategy
pan-x-c Oct 8, 2024
ea6ac25
add rpc agent test with retry strategy
pan-x-c Oct 8, 2024
7d90257
add rpc agent test with retry strategy
pan-x-c Oct 8, 2024
7e404f1
Merge branch 'main' into feature/pxc/env
pan-x-c Oct 8, 2024
987c19a
update doc
pan-x-c Oct 8, 2024
05d85af
fix test
pan-x-c Oct 8, 2024
60d0717
fix retry
pan-x-c Oct 8, 2024
089e695
fix retries
pan-x-c Oct 8, 2024
f5f3002
Merge pull request #7 from PatrickG1014/add_simple_env_example
pan-x-c Oct 8, 2024
e9c1856
simplify auction example
pan-x-c Oct 8, 2024
5d939fc
update tutorial
pan-x-c Oct 11, 2024
dc93bfc
update as_server command
pan-x-c Oct 11, 2024
d86d67c
update tutorial
pan-x-c Oct 11, 2024
2f0851f
update chatroom
pan-x-c Oct 11, 2024
fd41285
update participant
pan-x-c Oct 14, 2024
7620fbf
fix comments
pan-x-c Oct 14, 2024
39c5738
refactor chatroom
pan-x-c Oct 14, 2024
07d6c7f
refactor chatroom
pan-x-c Oct 14, 2024
4509ff4
Merge branch 'feature/pxc/chatroom_eh' into feature/pxc/env
pan-x-c Oct 14, 2024
df680e8
fix precommit
pan-x-c Oct 14, 2024
ca1085a
fix chatroom bug
pan-x-c Oct 14, 2024
5b36a3a
fix cls reference bug
pan-x-c Oct 14, 2024
e6f7dc3
fix cls info
pan-x-c Oct 15, 2024
6bf0fb5
fix chatroom example
pan-x-c Oct 16, 2024
d9f8143
fix
pan-x-c Oct 16, 2024
eb2bfc7
add persona config
pan-x-c Oct 16, 2024
a2f467c
opt retry info
pan-x-c Oct 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
python_requires=">=3.9",
entry_points={
"console_scripts": [
"as_studio=agentscope.studio:init",
"as_studio=agentscope.studio:as_studio",
"as_gradio=agentscope.web.gradio.studio:run_app",
"as_workflow=agentscope.web.workstation.workflow:main",
"as_server=agentscope.server.launcher:as_server",
Expand Down
25 changes: 25 additions & 0 deletions src/agentscope/environment/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
""" Import all environment related modules in the package. """
from .event import Event, event_func
from .attribute import Attribute, BasicAttribute, EventListener
from .environment import Environment
from .attributes.mutable import MutableAttribute
from .attributes.immutable import ImmutableAttribute
from .attributes.chatroom import ChatRoom
from .attributes.point2d import Point2D, AttributeWithPoint2D
from .attributes.map2d import Map2D

__all__ = [
"Event",
"event_func",
"Attribute",
"BasicAttribute",
"EventListener",
"Environment",
"MutableAttribute",
"ImmutableAttribute",
"ChatRoom",
"Point2D",
"AttributeWithPoint2D",
"Map2D",
]
333 changes: 333 additions & 0 deletions src/agentscope/environment/attribute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,333 @@
# -*- coding: utf-8 -*-
"""The attribute used in environment."""
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import Any, List

from ..exception import (
EnvAttributeNotFoundError,
EnvAttributeAlreadyExistError,
)
from .event import Event


class EventListener(ABC):
"""A class representing a listener for listening the event of an
attribute."""

def __init__(self, name: str) -> None:
"""Init a EventListener instance.

Args:
name (`str`): The name of the listener.
"""
self.name = name

@abstractmethod
def __call__(
self,
attr: Attribute,
event: Event,
) -> None:
"""Activate the listener.

Args:
attr (`Attribute`): The attribute bound to the listener.
event (`Event`): The event information.
"""


class Attribute(ABC):
"""The Attribute Interface.
Attribute is a key concept of AgentScope, which is used to
represent global data shared among agents.

Each attribute has its own name and value, and multiple attributes
can be organized into a tree structure, where each attribute can
have multiple children attributes and one parent attribute.

Different implementation of attributes may have different event functions,
which are marked by `@event_func`.
Users can bind `EventListener` to specific event functions, and
the listener will be activated when the event function is called.
"""

@property
@abstractmethod
def name(self) -> str:
"""Name of the attribute.

Returns:
`str`: The name of the attribute.
"""

@abstractmethod
def get_parent(self) -> Attribute:
"""Get the parent attribute of the current attribute.

Returns:
`Attribute`: The parent attribute.
"""

@abstractmethod
def set_parent(self, parent: Attribute) -> None:
"""Set the parent attribute of the current attribute.

Args:
parent (`Attribute`): The parent attribute.
"""

@abstractmethod
def get_children(self) -> dict[str, Attribute]:
"""Get the children attributes of the current attribute.

Returns:
`dict[str, Attribute]`: The children attributes.
"""

@abstractmethod
def add_child(self, child: Attribute) -> bool:
"""Add a child attribute to the current attribute.

Args:
child (`Attribute`): The children
attributes.

Returns:
`bool`: Whether the children were added successfully.
"""

@abstractmethod
def remove_child(self, children_name: str) -> bool:
"""Remove a child attribute from the current attribute.

Args:
children_name (`str`): The name of the children attribute.

Returns:
`bool`: Whether the children were removed successfully.
"""

@abstractmethod
def add_listener(self, target_event: str, listener: EventListener) -> bool:
"""Add a listener to the attribute.

Args:
target_event (`str`): The event function to listen.
listener (`EventListener`): The listener to add.

Returns:
`bool`: Whether the listener was added successfully.
"""

@abstractmethod
def remove_listener(self, target_event: str, listener_name: str) -> bool:
"""Remove a listener from the attribute.

Args:
target_event (`str`): The event function.
listener_name (`str`): The name of the listener to remove.

Returns:
`bool`: Whether the listener was removed successfully.
"""

@abstractmethod
def __getitem__(self, attr_name: str) -> Attribute:
"""Get a child attribute."""

@abstractmethod
def __setitem__(self, attr_name: str, attr: Attribute) -> None:
"""Set a child attribute."""


class BasicAttribute(Attribute):
"""A basic implementation of Attribute, which has no event function
and cannot get value.

Note:
`BasicAttribute` is used as the base class to implement other
attributes. Application developers should not use this class.
"""

def __init__(
self,
name: str,
value: Any,
listeners: dict[str, List[EventListener]] = None,
children: List[Attribute] = None,
parent: Attribute = None,
) -> None:
"""Init an BasicAttribute instance.

Args:
name (`str`): The name of the attribute.
value (`Any`): The default value of the attribute.
listeners (`dict[str, List[EventListener]]`, optional): The
listener dict. Defaults to None.
env (`Environment`): An instance of the Environment class.
Defaults to None.
children (`List[Attribute]`, optional): A list of children
attributes. Defaults to None.
parent (`Attribute`, optional): The parent attribute. Defaults
to None.
"""
self._name = name
self._value = value
self.children = {
child.name: child for child in (children if children else [])
}
self.parent = parent
self.event_listeners = {}
if listeners:
for target_func, listener in listeners.items():
if isinstance(listener, EventListener):
self.add_listener(target_func, listener)
else:
for ls in listener:
self.add_listener(target_func, ls)

@property
def name(self) -> str:
"""Name of the attribtue"""
return self._name

def get_parent(self) -> Attribute:
"""Get the parent attribute of the current attribute.

Returns:
`Attribute`: The parent attribute.
"""
return self.parent

def set_parent(self, parent: Attribute) -> None:
"""Set the parent attribute of the current attribute.

Args:
parent (`Attribute`): The parent attribute.
"""
self.parent = parent

def get_children(self) -> dict[str, Attribute]:
"""Get the children attributes of the current attribute.

Returns:
`dict[str, Attribute]`: The children attributes.
"""
return self.children

def add_child(self, child: Attribute) -> bool:
"""Add a child attribute to the current attribute.

Args:
child (`Attribute`): The children
attributes.

Returns:
`bool`: Whether the children were added successfully.
"""
if child.name in self.children:
return False
self.children[child.name] = child
return True

def remove_child(self, children_name: str) -> bool:
"""Remove a child attribute from the current attribute.

Args:
children_name (`str`): The name of the children attribute.

Returns:
`bool`: Whether the children were removed successfully.
"""
if children_name in self.children:
del self.children[children_name]
return True
return False

def add_listener(self, target_event: str, listener: EventListener) -> bool:
"""Add a listener to the attribute.

Args:
target_event (`str`): The event function to listen.
listener (`EventListener`): The listener to add.

Returns:
`bool`: Whether the listener was added successfully.
"""
if (
hasattr(self, target_event)
and hasattr(getattr(self, target_event), "_is_event")
and getattr(self, target_event)._is_event # pylint: disable=W0212
):
if target_event not in self.event_listeners:
self.event_listeners[target_event] = {}
if listener.name not in self.event_listeners[target_event]:
self.event_listeners[target_event][listener.name] = listener
return True
return False

def remove_listener(self, target_event: str, listener_name: str) -> bool:
"""Remove a listener from the attribute.

Args:
target_event (`str`): The event function.
listener_name (`str`): The name of the listener to remove.

Returns:
`bool`: Whether the listener was removed successfully.
"""
if target_event in self.event_listeners:
if listener_name in self.event_listeners[target_event]:
del self.event_listeners[target_event][listener_name]
return True
return False

def _trigger_listener(self, event: Event) -> None:
"""Trigger the listeners of the specific event.

Args:
event_name (`str`): The event function name.
args (`dict`): The arguments to pass to the event.
"""
if event.name in self.event_listeners:
for listener in self.event_listeners[event.name].values():
listener(self, event)

def dump(self) -> dict:
"""Dump the attribute tree to a dict."""
return {
"value": self._value,
"type": self.__class__.__name__,
"children": {
child.name: child.dump()
for child in (self.children.values() if self.children else [])
},
}

def __getitem__(self, attr_name: str) -> Attribute:
if attr_name in self.children:
return self.children[attr_name]
else:
raise EnvAttributeNotFoundError(attr_name)

def __setitem__(self, attr_name: str, attr: Attribute) -> None:
if not isinstance(attr, Attribute):
raise TypeError("Only Attribute can be set")
if attr_name not in self.children:
self.children[attr_name] = attr
else:
raise EnvAttributeAlreadyExistError(attr_name)


class RpcAttribute(Attribute):
"""A attribute that can be accessed through RPC."""

def __init__(
self,
name: str,
host: str = "localhost",
port: int = None,
attr_id: str = None,
) -> None:
pass
Empty file.
Loading