Multi-Agent Language Game Environments for LLMs
Project description
🏟 ChatArena
Multi-Agent Language Game Environments for LLMs
ChatArena is a library that provides multi-agent language game environments and facilitates research about autonomous LLM agents and their social interactions. It provides the following features:
- Abstraction: it provides a flexible framework to define multiple players, environments and the interactions between them, based on Markov Decision Process.
- Language Game Environments: it provides a set of environments that can help understanding, benchmarking or training agent LLMs.
- User-friendly Interfaces: it provides both Web UI and CLI to develop/prompt engineer your LLM agents to act in environments.
Getting Started
Installation
Requirements:
- Python >= 3. 7
- OpenAI API key (optional, for using GPT-3.5-turbo or GPT-4 as an LLM agent)
Install with pip:
pip install chatarena
or install from source:
pip install git+https://github.com/chatarena/chatarena
To use GPT-3 as an LLM agent, set your OpenAI API key:
export OPENAI_API_KEY="your_api_key_here"
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:
gradio app.py
This will launch a demo server for ChatArena, and you can access it from your browser (port 8080).
Check out this video to learn how to use Web UI:
Basic Usage
Key Concepts
- 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.
- Environment: The environment stores the game state and executes game logics to make transitions between game
states. It also renders observations for players, the observations are natural languages.
- The game state is not directly visible to the players. Players can only see the observations.
- Language Backend: Language backends are the source of language intelligence. It takes text (or collection of text) as input and returns text in response.
- Player: The player is an agent that plays the game. In RL terminology, it’s a policy, a stateless function mapping from observations to actions.
Step 1: Define Multiple Players with LLM Backend
from chatarena.agent import Player
from chatarena.backends import OpenAIChat
# Describe the environment (which is shared by all players)
environment_description = "It is in a university classroom ..."
# A "Professor" player
player1 = Player(name="Professor", backend=OpenAIChat(),
role_desc="You are a professor in ...",
global_prompt=environment_description)
# A "Student" player
player2 = Player(name="Student", backend=OpenAIChat(),
role_desc="You are a student who is interested in ...",
global_prompt=environment_description)
# A "Teaching Assistant" player
player3 = Player(name="Teaching assistant", backend=OpenAIChat(),
role_desc="You are a teaching assistant of the module ...",
global_prompt=environment_description)
Step 2: Create a Language Game Environment
You can also create a language model-driven environment and add it to the ChatArena:
from chatarena.environments.conversation import Conversation
env = Conversation(player_names=[p.name for p in [player1, player2, player3]])
Step 3: Run the Language Game using Arena
Arena
is a utility class to help you run language games:
from chatarena.arena import Arena
arena = Arena(players=[player1, player2, player3],
environment=env, global_prompt=environment_description)
# Run the game for 10 steps
arena.run(num_steps=10)
# Alternatively, you can run your own main loop
for _ in range(10):
arena.step()
# Your code goes here ...
You can easily save your gameplay history to file:
arena.save_history(path=...)
and save your game config to file:
arena.save_config(path=...)
Other Utilities
Load Arena
from a config file -- here we use examples/nlp-classroom-3players.json
in this repository as an example:
arena = Arena.from_config("examples/nlp-classroom-3players.json")
arena.run(num_steps=10)
Run the game in an interactive CLI interface:
arena.launch_cli()
Check out this video to learn how to use CLI:
Advanced Usage
ModeratedConversation
: a LLM-driven Environment
We support a more advanced environment called ModeratedConversation
that allows you to control the game dynamics
using an LLM.
The moderator is a special player that controls the game state transition and determines when the game ends.
For example, you can define a moderator that tracks the board status of a board game and ends the game when a player
wins.
You can try out our Tic-tac-toe and Rock-paper-scissors games to get a sense of how it works:
# Tic-tac-toe example
Arena.from_config("examples/tic-tac-toe.json").launch_cli()
# Rock-paper-scissors example
Arena.from_config("examples/rock-paper-scissors.json").launch_cli()
General Custimization Guide
- Arena: Overriding Arena basically means one is going to write their own main loop. This can allow different interaction interfaces or drive games in a more automated manner, for example, running an online RL training loop
- Environment: A new environment corresponds to a new game, one can define the game dynamics here with hard-coded rules or a mixture of rules and language backend.
- Backend: If one needs to change the way of formatting observations (in terms of messages) into queries for the language model, the backend should be overridden.
- Player: By default, when a new observation is fed, players will query the language backend and return the response as actions. But one can also customize the way that players are interacting with the language backend.
Creating your Custom Environment
You can define your own environment by extending the Environment
class. Here are the general steps:
- Define the class by inheriting from a base class and setting
type_name
, then add the class toALL_ENVIRONMENTS
- Initialize the class by defining
__init__
method (its arguments will define the corresponding config) and initializing class attributes - Implement game mechanics in methods
step
- Handle game states and rewards by implementing methods such as
reset
,get_observation
,is_terminal
, andget_rewards
- Develop role description prompts (and a global prompt if necessary) for players using CLI or Web UI and save them to a config file.
We provide a detailed tutorial to demonstrate how to define a custom
environment,
using the Chameleon
environment as example.
If you want to port an existing library's environment to ChatArena, check
out PettingzooChess
environment as an example.
List of Games and Environments
Conversation
A multi-player language game environment that simulates a conversation.
- NLP Classroom: a 3-player language game environment that simulates a classroom setting. The game is played in turns, and each turn a player can either ask a question or answer a question. The game ends when all players have asked and answered all questions.
Moderator Conversation
Based on converstion, but with a moderator that controls the game dynamics.
- Rock-paper-scissors: a 2-player language game environment that simulates a rock-paper-scissors game with moderator conversation. Both player will act in parallel, and the game ends when one player wins 2 rounds.
- Tic-tac-toe: a 2-player language game environment that simulates a tic-tac-toe game with moderator conversation. The game is played in turns, and each turn a player can either ask for a move or make a move. The game ends when one player wins or the board is full.
Chameleon
A multi-player social deduction game. There are two roles in the game, chameleon and non-chameleon. The topic of the secret word will be first revealed to all the players. Then the secret word will be revealed to non-chameleons. The chameleon does not know the secret word. The objective in the game depends on the role of the player:
- If you are not a chameleon, your goal is to reveal the chameleon without exposing the secret word.
- If you are a chameleon, your aim is to blend in with other players, avoid being caught, and figure out the secret word. There are three stages in the game:
- The giving clues stage: each player will describe the clues about the secret word.
- The accusation stage: In this stage, each player will vote for another player who is most likely the chameleon. The chameleon should vote for other players.
- The guess stage: If the accusation is correct, the chameleon should guess the secret word given the clues revealed by other players.
PettingZooChess
A two-player chess game environment that uses the PettingZoo Chess environment.
Contributing
We welcome contributions to improve and extend ChatArena. Please follow these steps to contribute:
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Commit your changes to the new branch.
- Create a pull request describing your changes.
- We will review your pull request and provide feedback or merge your changes.
Please ensure your code follows the existing style and structure.
Citation
If you find ChatArena useful for your research, please cite our repository (our arxiv paper is coming soon):
@software{ChatArena,
author = {Yuxiang Wu, Zhengyao Jiang, Akbir Khan, Yao Fu, Laura Ruis, Edward Grefenstette, and Tim Rocktäschel},
title = {ChatArena: Multi-Agent Language Game Environments for Large Language Models},
year = {2023},
publisher = {GitHub},
journal = {GitHub repository},
version = {0.1},
howpublished = {\url{https://github.com/chatarena/chatarena}},
}
Contact
If you have any questions or suggestions, feel free to open an issue or submit a pull request. You can also follow us on Twitter or join our Slack channel to get the latest updates.
Happy chatting!
Sponsors
We would like to thank our sponsors for supporting this project:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file chatarena-0.1.9.tar.gz
.
File metadata
- Download URL: chatarena-0.1.9.tar.gz
- Upload date:
- Size: 32.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.28.2 requests-toolbelt/0.10.1 urllib3/1.26.15 tqdm/4.65.0 importlib-metadata/6.0.0 keyring/23.13.1 rfc3986/1.5.0 colorama/0.4.6 CPython/3.8.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9fcac45fe3357134cc7a57d304ba4de5ab8ef2dc354df220a1793128af04e761 |
|
MD5 | 744f5c4dc31eea73b99168324901e3fd |
|
BLAKE2b-256 | debe85946199fb324ac4db32f7e1406677e8f96516b562e17f036e817b71d47f |
File details
Details for the file chatarena-0.1.9-py3-none-any.whl
.
File metadata
- Download URL: chatarena-0.1.9-py3-none-any.whl
- Upload date:
- Size: 36.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.28.2 requests-toolbelt/0.10.1 urllib3/1.26.15 tqdm/4.65.0 importlib-metadata/6.0.0 keyring/23.13.1 rfc3986/1.5.0 colorama/0.4.6 CPython/3.8.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bdba764b554e67f4ca2636f8abc73b8f5f0c67fc0643f23c36967081abc2872f |
|
MD5 | dad2dc94bbe256c2aa77c28313358872 |
|
BLAKE2b-256 | 89b422dd30d3c4e69ab35e2ba85dbdf20da1457575bd059ff0824b7d0d316236 |