File size: 8,366 Bytes
4bdab37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
"""Wrapper to convert a ChatArena environment into a PettingZoo compatible environment."""
from __future__ import annotations

import functools
from typing import Any, Dict, Optional

import pettingzoo
from gymnasium import spaces
from pettingzoo.utils.env import AgentID, ObsType

import chatarena
from chatarena.arena import Arena

import string

CHAR_SET = string.printable


class PettingZooCompatibilityV0(pettingzoo.AECEnv):
    """This compatibility wrapper converts a ChatArena environment into a PettingZoo environment.

    ChatArena (or Chat Arena) is a Multi-Agent Language Game Environments for LLMs. The goal is to develop communication and collaboration capabilities of AIs.
    """

    metadata = {
        "render_modes": ["human"],
        "name": "PettingZooCompatibilityV0",
        "is_parallelizable": False,
    }

    def __init__(
            self,
            env: chatarena.arena.Arena | None = None,
            arena_name: str | None = None,
            string_observation: bool | None = True,
            max_turns: int | None = 25,
            render_mode: str | None = None,
    ):
        """Wrapper to convert a ChatArena environment into a PettingZoo environment.

        Args:
            env (chatarena.arena.Arena): chatarena arena to wrap
            arena_name (Optional[str]): chatarena environment to load from file (e.g., "examples/chameleon.json")
            max_turns (Optional[int]): maximum number of turns before environment truncates
            render_mode (Optional[str]): rendering mode
        """
        super().__init__()

        if env is not None:
            self._env = env
        elif arena_name is not None:
            self._env = Arena.from_config(arena_name)
        else:
            raise ValueError("Arena not specified, please us env or arena_name arguments.")

        self._env.reset()  # this resets the underlying arena as well as each player

        self.possible_agents = list(self._env.name_to_player.keys())
        self.name_to_player_mapping = self._env.name_to_player

        self.string_observation = string_observation
        self.max_turns = max_turns
        self.render_mode = render_mode

        self.terminations = {}
        self.truncations = {}
        self.rewards = {}
        self.infos = {a: {} for a in self.possible_agents}

    @functools.lru_cache(maxsize=None)
    def observation_space(self, agent: AgentID):
        """observation_space.

        We get the observation space from the underlying environment.
        Args:
            agent (AgentID): agent
        """
        # TODO: finalize obs space (dicts with agent name may not be necessary)
        observation_space = spaces.Dict(
            {
                agent: spaces.Text(max_length=256, min_length=0, charset=CHAR_SET)
                for agent in self.possible_agents
            }
        )
        return observation_space

    @functools.lru_cache(maxsize=None)
    def action_space(self, agent: AgentID):
        """action_space.

        Get the action space from the underlying environment.

        Args:
            agent (AgentID): agent

        Returns:
            space
        """
        # TODO: finalize action space (this enables agents to send messages to specific other players)
        action_space = spaces.Dict(
            {
                agent: spaces.Text(max_length=256, min_length=0, charset=CHAR_SET)
                for agent in self.possible_agents
            }
        )
        return action_space

    def render(self):
        """render.

        Print the current game state.
        """
        if not hasattr(self, "initial_timestep"):
            raise UserWarning(
                "You must reset the environment using reset() before calling render()."
            )

        self._env.environment.print()
        pass

    def observe(self, agent: AgentID) -> ObsType:
        """observe.

        Args:
            agent (AgentID): agent (e.g., "Player 1")

        Returns:
            observation
        """
        messages = self._env.environment.get_observation(agent)  # this will only return the messages this agent can see
        if len(messages) > 0:
            self.current_turn = messages[-1].turn
        else:
            self.current_turn = 0
        new_messages = [m for m in messages if
                        m.turn == self.current_turn]  # we only send the current timestep messages

        # string observation
        if self.string_observation == True:
            observation = ""
            for m in new_messages:
                observation += f"{m.agent_name}: {m.content}"

        # dict observation
        else:
            observation = {m.agent_name: m.content for m in new_messages}

        self.infos[agent]["obs_dict"] = {m.agent_name: m.content for m in new_messages}

        return observation

    def close(self):
        """close."""
        pass

    def _unravel_timestep(self, timestep: chatarena.arena.TimeStep):
        # get observation
        messages = timestep.observation
        if len(messages) > 0:
            self.current_turn = messages[-1].turn
        else:
            self.current_turn = 0
        new_messages = [m for m in messages if
                        m.turn == self.current_turn]  # we only send the current timestep messages

        # string observation
        if self.string_observation == True:
            observation = ""
            for m in new_messages:
                observation += f"{m.agent_name}: {m.content}"

        # dict observation
        else:
            observation = {m.agent_name: m.content for m in new_messages}

        # get rewards
        rewards = timestep.reward

        # get termination
        termination = timestep.terminal

        # get truncation
        truncation = self.current_turn > self.max_turns

        # get info
        player_idx = self.possible_agents.index(self.agent_selection)
        player_obj = self._env.players[player_idx]
        info = {"turn": self.current_turn, "global_prompt": player_obj.global_prompt,
                "agent_desc": player_obj.role_desc}

        return observation, rewards, termination, truncation, info

    def reset(
            self,
            return_info: bool | None = False,
            seed: int | None = None,
            options: dict | None = None,
    ):
        """reset.

        Args:
            seed (Optional[int]): seed
            options (Optional[Dict]): options
        """

        if seed is not None:
            print("WARNING: seeding is not supported for LLMs.")

        # reset the chat arena environment
        self.initial_timestep = self._env.reset()
        self.turn = 0

        # get the first player
        self.agent_selection = self._env.environment.get_next_player()

        observation, reward, termination, truncation, info = self._unravel_timestep(self.initial_timestep)

        agent = self.agent_selection
        self.rewards = reward
        self.terminations[agent] = termination
        self.truncations[agent] = truncation
        self.infos[agent] = info

        # all agents
        self.agents = self.possible_agents[:]

        # boilerplate stuff
        self._cumulative_rewards = {a: 0 for a in self.agents}
        self.rewards = self.initial_timestep.reward
        self.terminations = {a: False for a in self.agents}
        self.truncations = {a: False for a in self.agents}

    def step(self, action: str):
        """Steps.

        Steps the agent with an action.

        Args:
            action (str): action
        """
        if (
                self.terminations[self.agent_selection]
                or self.truncations[self.agent_selection]
        ):
            return self._was_dead_step(action)

        agent = self.agent_selection
        timestep = self._env.environment.step(player_name=agent, action=action)

        observation, reward, termination, truncation, info = self._unravel_timestep(timestep)

        self.rewards = reward
        self.terminations[agent] = termination
        self.truncations[agent] = truncation
        self.infos[agent] = info

        self._cumulative_rewards[agent] = 0

        self.agent_selection = self._env.environment.get_next_player()

        self._accumulate_rewards()

        if self.render_mode == "human":
            self.render()