Skip to content

Commit

Permalink
Merge branch 'dev' into feat/add_bardapi
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxiang-wu authored Jun 5, 2023
2 parents 06c7d85 + a823b10 commit 04d7d07
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 69 deletions.
24 changes: 3 additions & 21 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ coverage.xml
*.py,cover
.hypothesis/
.pytest_cache/
<<<<<<< HEAD
cover/
=======
>>>>>>> c8585d9ea9ca5043d8ec786b798f6c85356bc96f

# Translations
*.mo
Expand All @@ -76,10 +72,7 @@ instance/
docs/_build/

# PyBuilder
<<<<<<< HEAD
.pybuilder/
=======
>>>>>>> c8585d9ea9ca5043d8ec786b798f6c85356bc96f
target/

# Jupyter Notebook
Expand All @@ -90,13 +83,9 @@ profile_default/
ipython_config.py

# pyenv
<<<<<<< HEAD
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
=======
.python-version
>>>>>>> c8585d9ea9ca5043d8ec786b798f6c85356bc96f

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
Expand All @@ -105,7 +94,6 @@ ipython_config.py
# install all needed dependencies.
#Pipfile.lock

<<<<<<< HEAD
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
Expand All @@ -122,9 +110,6 @@ ipython_config.py
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
=======
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
>>>>>>> c8585d9ea9ca5043d8ec786b798f6c85356bc96f
__pypackages__/

# Celery stuff
Expand Down Expand Up @@ -159,7 +144,6 @@ venv.bak/
dmypy.json

# Pyre type checker
<<<<<<< HEAD
.pyre/

# pytype static type analyzer
Expand All @@ -173,11 +157,9 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
=======
.idea/

.DS_Store
.pyre/
.idea
hf-spaces/
etc/
>>>>>>> c8585d9ea9ca5043d8ec786b798f6c85356bc96f
.conda
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,26 @@ To use GPT-3 as an LLM agent, set your OpenAI API key:
export OPENAI_API_KEY="your_api_key_here"
```

#### Optional Dependencies

By default `pip install chatarena` will only install dependencies necessary for ChatArena's core functionalities.
You can install optional dependencies with the following commands:
```bash
pip install chatarena[all_backends] # install dependencies for all supported backends: anthropic, cohere, huggingface, etc.
pip install chatarena[all_envs] # install dependencies for all environments, such as pettingzoo
pip install chatarena[all] # install all optional dependencies for full functionality
```

### Launch the Demo Locally

The quickest way to see ChatArena in action is via the demo Web UI.
To launch the demo on your local machine, you first need to git clone the repository and install it from source
(see above instruction). Then run the following command in the root directory of the repository:
To launch the demo on your local machine, you first pip install chatarena with extra gradio dependency, then git clone
this repository to your local folder, and finally call the `app.py` in the root directory of the repository:

```shell
pip install chatarena[gradio]
git clone https://github.com/chatarena/chatarena.git
cd chatarena
gradio app.py
```

Expand All @@ -82,10 +95,12 @@ This will launch a demo server for ChatArena, and you can access it from your br
Check out this video to learn how to use Web UI: [![Webui demo video](https://img.shields.io/badge/WebUI%20Demo%20Video-Vimeo-blue?logo=vimeo)](https://vimeo.com/816979419)

## For Developers

For a introduction to the ChatArena framework, please refer to [this document](docs/devdoc/design.md).
Here we provide a compact guide on minimal setup to run the game and some general advice on customization.
Here we provide a compact guide on minimal setup to run the game and some general advice on customization.

### Key Concepts

1. **Arena**: Arena encapsulates an environment and a collection of players. It drives the main loop of the game and
provides HCI utilities like webUI, CLI, configuration loading and data storage.
2. **Environment**: The environment stores the game state and executes game logics to make transitions between game
Expand All @@ -97,6 +112,7 @@ Here we provide a compact guide on minimal setup to run the game and some genera
mapping from observations to actions.

### Run the Game with Python API

Load `Arena` from a config file -- here we use `examples/nlp-classroom-3players.json` in this repository as an example:

```python
Expand All @@ -112,7 +128,8 @@ arena.launch_cli()

Check out this video to learn how to use
CLI: [![cli demo video](https://img.shields.io/badge/CLI%20Demo%20Video-Vimeo-blue?logo=vimeo)](https://vimeo.com/816989884)
A more detailed guide about how to run the main interaction loop with finer-grained control can be found [here](docs/devdoc/mainloop.md)
A more detailed guide about how to run the main interaction loop with finer-grained control can be
found [here](docs/devdoc/mainloop.md)

### General Custimization Advice

Expand Down Expand Up @@ -196,7 +213,8 @@ The objective in the game depends on the role of the player:
A two-player chess game environment that uses the PettingZoo Chess environment.

### [PettingZooTicTacTeo](chatarena/environments/pettingzoo_tictactoe.py)
A two-player tic-tac-toe game environment that uses the PettingZoo TicTacToe environment. Differing from the

A two-player tic-tac-toe game environment that uses the PettingZoo TicTacToe environment. Differing from the
`Moderator Conversation` environment, this environment is driven by hard-coded rules rather than a LLM moderator.

## Contributing
Expand Down
47 changes: 22 additions & 25 deletions chatarena/pettingzoo_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

CHAR_SET = string.printable


class PettingZooCompatibilityV0(pettingzoo.AECEnv):
"""This compatibility wrapper converts a ChatArena environment into a PettingZoo environment.
Expand All @@ -28,14 +29,14 @@ class PettingZooCompatibilityV0(pettingzoo.AECEnv):
}

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,
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 OpenSpiel environment into a PettingZoo environment.
"""Wrapper to convert a ChatArena environment into a PettingZoo environment.
Args:
env (chatarena.arena.Arena): chatarena arena to wrap
Expand All @@ -52,12 +53,11 @@ def __init__(
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._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
Expand All @@ -67,7 +67,6 @@ def __init__(
self.rewards = {}
self.infos = {a: {} for a in self.possible_agents}


@functools.lru_cache(maxsize=None)
def observation_space(self, agent: AgentID):
"""observation_space.
Expand Down Expand Up @@ -128,12 +127,13 @@ def observe(self, agent: AgentID) -> ObsType:
Returns:
observation
"""
messages = self._env.environment.get_observation(agent) # this will only return the messages this agent can see
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
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:
Expand All @@ -149,7 +149,6 @@ def observe(self, agent: AgentID) -> ObsType:

return observation


def close(self):
"""close."""
pass
Expand All @@ -161,7 +160,8 @@ def _unravel_timestep(self, timestep: chatarena.arena.TimeStep):
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
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:
Expand All @@ -173,7 +173,6 @@ def _unravel_timestep(self, timestep: chatarena.arena.TimeStep):
else:
observation = {m.agent_name: m.content for m in new_messages}


# get rewards
rewards = timestep.reward

Expand All @@ -186,15 +185,16 @@ def _unravel_timestep(self, timestep: chatarena.arena.TimeStep):
# 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}
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,
self,
return_info: bool | None = False,
seed: int | None = None,
options: dict | None = None,
):
"""reset.
Expand Down Expand Up @@ -239,8 +239,8 @@ def step(self, action: str):
action (str): action
"""
if (
self.terminations[self.agent_selection]
or self.truncations[self.agent_selection]
self.terminations[self.agent_selection]
or self.truncations[self.agent_selection]
):
return self._was_dead_step(action)

Expand All @@ -262,6 +262,3 @@ def step(self, action: str):

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



4 changes: 2 additions & 2 deletions docs/tutorials/pettingzoo_wrapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ print("ACT SPACE: ", env.action_space(env.agent_selection))
3. **Run the environment**
```python
agent_player_mapping = {agent: player_obj
for agent in env.possible_agents
for player_obj in env._env.players}
for agent in env.possible_agents
for player_obj in env._env.players}

for agent in env.agent_iter():
observation, reward, termination, truncation, info = env.last()
Expand Down
14 changes: 12 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "chatarena"
version = "0.1.10"
version = "0.1.10.2"
authors = [
{ name = "Yuxiang Wu", email = "yuxiang.cs@gmail.com" },
]
Expand All @@ -19,4 +19,14 @@ classifiers = [

[project.urls]
"Homepage" = "https://github.com/chatarena/chatarena"
"Bug Tracker" = "https://github.com/chatarena/chatarena/issues"
"Bug Tracker" = "https://github.com/chatarena/chatarena/issues"

[project.optional-dependencies]
anthropic = ["anthropic>=0.2.8"]
cohere = ["cohere>=4.3.1"]
huggingface = ["transformers>=4.27.4"]
gradio = ["gradio==3.20.0"]
pettingzoo = ["pettingzoo==1.23.0", "chess==1.9.4"]
all_backends = ["anthropic>=0.2.8", "cohere>=4.3.1", "transformers>=4.27.4"]
all_envs = ["pettingzoo==1.23.0", "chess==1.9.4"]
all = ["anthropic>=0.2.8", "cohere>=4.3.1", "transformers>=4.27.4", "gradio==3.20.0", "pettingzoo==1.23.0", "chess==1.9.4"]
42 changes: 28 additions & 14 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
from setuptools import setup, find_packages

_deps = [
"openai==0.27.2",
"anthropic==0.2.8",
"cohere==4.3.1",
"transformers>=4.27.4",
with open("README.md", "r") as f:
long_description = f.read()

base_requirements = [
"openai>=0.27.2",
"tenacity==8.2.2",
"gradio==3.20.0",
"rich==13.3.3",
"prompt_toolkit==3.0.38",
"pettingzoo==1.23.0",
"chess==1.9.4",
"bardapi==0.1.11",

]
anthropic_requirements = ["anthropic>=0.2.8"]
cohere_requirements = ["cohere>=4.3.1"]
hf_requirements = ["transformers>=4.27.4"]
bard_requirements = ["bardapi==0.1.11"]
gradio_requirements = ["gradio==3.20.0"]
pettingzoo_requirements = ["pettingzoo==1.23.0", "chess==1.9.4"]

with open("README.md", "r") as f:
long_description = f.read()

requirements = _deps
all_backends = anthropic_requirements + cohere_requirements + hf_requirements + bard_requirements
all_envs = pettingzoo_requirements
all_requirements = anthropic_requirements + cohere_requirements + hf_requirements + \
gradio_requirements + pettingzoo_requirements + bard_requirements

setup(
name="chatarena",
version="0.1.10",
version="0.1.10.2",
author="Yuxiang Wu",
author_email="yuxiang.cs@gmail.com",
description="",
Expand All @@ -35,5 +39,15 @@
"Topic :: Scientific/Engineering :: Artificial Intelligence",
],
python_requires=">=3.7",
install_requires=requirements,
install_requires=base_requirements,
extras_require={
"anthropic": anthropic_requirements,
"cohere": cohere_requirements,
"huggingface": hf_requirements,
"bard": bard_requirements,
"pettingzoo": pettingzoo_requirements,
"gradio": gradio_requirements,
"all_backends": all_backends,
"all": all_requirements,
},
)

0 comments on commit 04d7d07

Please sign in to comment.