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

Merged
merged 197 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 76 commits
Commits
Show all changes
197 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
d29a4ac
quick fix on chatroom
chenyushuo Oct 22, 2024
3a805e9
fix comments
pan-x-c Oct 23, 2024
e2fda5d
fix comments
pan-x-c Oct 23, 2024
c9a07ce
fix comments
pan-x-c Oct 23, 2024
f7b84a3
Merge branch 'main' into feature/pxc/env
pan-x-c Oct 23, 2024
fc98f32
add notes
pan-x-c Oct 23, 2024
a50e58c
fix in chatroom
chenyushuo Oct 23, 2024
4eb9940
fix in chatroom
chenyushuo Oct 23, 2024
7d59469
fix in chatroom
chenyushuo Oct 23, 2024
c6684b1
bug fix in leave chatroom
chenyushuo Oct 23, 2024
dfa5606
bug fix in intro
chenyushuo Oct 23, 2024
84c44de
precommit fix
chenyushuo Oct 23, 2024
b11fa41
Merge pull request #10 from chenyushuo/new_env
pan-x-c Oct 23, 2024
872ffb6
update docs
pan-x-c Oct 23, 2024
d1ae655
update paper info
pan-x-c Oct 28, 2024
72aaa72
simplify env structure
pan-x-c Oct 28, 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 examples/distributed_parallel_optimization/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def parse_args() -> argparse.Namespace:
model_config_name="my_model",
)
if args.use_dist:
answerer = answerer.to_dist(lazy_launch=False)
answerer = answerer.to_dist()
answerers.append(answerer)

user_agent = UserAgent()
Expand Down
7 changes: 7 additions & 0 deletions examples/environments/envs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
"""Unit tests for environment"""
from .mutable import MutableEnv
from .immutable import ImmutableEnv
from .point2d import Point2D, EnvWithPoint2D
from .map2d import Map2D
from .chatroom import ChatRoom
164 changes: 164 additions & 0 deletions examples/environments/envs/chatroom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
# -*- coding: utf-8 -*-
"""An env used as a chatroom."""
from typing import List
from copy import deepcopy

from agentscope.agents import AgentBase
from agentscope.message import Msg
from agentscope.exception import (
EnvListenerError,
)
from agentscope.environment import (
Env,
BasicEnv,
EventListener,
Event,
event_func,
)


class ChatRoomMember(BasicEnv):
"""A member of chatroom."""

def __init__(
self,
name: str,
agent: AgentBase,
history_idx: int = 0,
) -> None:
super().__init__(name)
self._agent = agent
self._history_idx = history_idx

@property
def agent_name(self) -> str:
"""Get the name of the agent."""
return self._agent.name

@property
def history_idx(self) -> int:
"""Get the history index of the agent."""
return self._history_idx

@property
def agent(self) -> AgentBase:
"""Get the agent of the member."""
return self._agent


class ChatRoom(BasicEnv):
"""A chatroom env."""

def __init__(
self,
name: str = None,
announcement: Msg = None,
participants: List[AgentBase] = None,
all_history: bool = False,
) -> None:
"""Init a ChatRoom instance.

Args:
name (`str`): The name of the chatroom.
announcement (`Msg`): The announcement message.
participants (`List[AgentBase]`): A list of agents
all_history (`bool`): If `True`, new participant can see all
history messages, else only messages generated after joining
can be seen. Default to `False`.
"""
super().__init__(
name=name,
)
self.children = {
p.name: p for p in (participants if participants else [])
}
self.event_listeners = {}
self.all_history = all_history
self.history = []
self.announcement = announcement

@event_func
def join(self, agent: AgentBase) -> bool:
"""Add a participant to the chatroom."""
if agent.agent_id in self.children:
return False
self.children[agent.agent_id] = ChatRoomMember(
name=agent.agent_id,
agent=agent,
history_idx=len(self.history),
)
return True

@event_func
def leave(self, agent: AgentBase) -> bool:
"""Remove the participant agent from the chatroom."""
if agent.agent_id not in self.children:
return False
del self.children[agent.agent_id]
return True

@event_func
def speak(self, message: Msg) -> None:
"""Speak a message in the chatroom."""
self.history.append(message)

@event_func
def get_history(self, agent_id: str) -> List[Msg]:
"""Get all history messages, since the participant join in the
chatroom"""
if agent_id not in self.children:
# only participants can get history message
return []
if self.all_history:
history_idx = 0
else:
history_idx = self.children[agent_id].history_idx
return deepcopy(self.history[history_idx:])

@event_func
def set_announcement(self, announcement: Msg) -> None:
"""Set the announcement of the chatroom."""
self.announcement = announcement

@event_func
def get_announcement(self) -> Msg:
"""Get the announcement of the chatroom."""
return deepcopy(self.announcement)

# Syntaic sugar, not an event function
def listen_to(
self,
target_names: List[str],
listener: EventListener,
) -> None:
"""The listener will be called when a message whose name is in
`target_names` is send to the chatroom."""
if target_names is None or len(target_names) == 0:
return

class ListenTo(EventListener):
"""A middleware that activates `target_listener`"""

def __init__(
self,
name: str,
target_names: List[str],
target_listener: EventListener,
) -> None:
super().__init__(name=name)
self.target_names = target_names
self.target_listener = target_listener

def __call__(self, env: Env, event: Event) -> None:
if event.args["message"].name in self.target_names:
self.target_listener(env, event)

if not self.add_listener(
"speak",
listener=ListenTo(
name=f"listen_to_{listener.name}",
target_names=target_names,
target_listener=listener,
),
):
raise EnvListenerError("Fail to add listener.")
42 changes: 42 additions & 0 deletions examples/environments/envs/immutable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
"""An env that is immutable and only support get."""

from typing import List, Any
from copy import deepcopy

from agentscope.environment import (
Env,
BasicEnv,
EventListener,
event_func,
)
from agentscope.environment.event import Getable


class ImmutableEnv(BasicEnv, Getable):
"""An immutable env that can be get and set."""

def __init__(
self,
name: str,
value: Any,
listeners: dict[str, List[EventListener]] = None,
children: List[Env] = None,
parent: Env = None,
) -> None:
super().__init__(
name=name,
listeners=listeners,
children=children,
parent=parent,
)
self._value = value

@property
def value(self) -> Any:
"""Get the value of the env."""
return self.get()

@event_func
def get(self) -> Any:
return deepcopy(self._value)
Loading