TextArena

0.7.4 · active · verified Thu Apr 16

TextArena is a Python library that provides a collection of competitive text-based games designed for evaluating language models and developing reinforcement learning agents. It offers a framework for defining games, creating agents (including human and GPT agents), and running competitive battles. As of version 0.7.4, it's under active development with a somewhat irregular release cadence, often introducing significant changes between minor versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up and run a simple game (Prisoner's Dilemma) using two base agents in TextArena. It initializes agents, configures an Arena instance with a specified game, and then executes the game, printing the final scores and full game history. For GPT-powered agents, ensure your OpenAI API key is set as an environment variable.

import os
from textarena.arena import Arena
from textarena.agent.agent import Agent, GPTAgent
from textarena.env.text_env import TextEnv
from textarena.game.prisoner_dilemma import PrisonerDilemmaGame

# 1. TextArena comes with pre-registered games, e.g., 'prisoner_dilemma'.
# If you define a custom game, you'd register it like this before use:
# TextEnv.register_game("my_custom_game", MyCustomGameClass)

# 2. Create agents
# For demonstration, we'll use base Agents. They generate random actions.
agent1 = Agent(name="Alice", temperature=0.7)
agent2 = Agent(name="Bob", temperature=0.7)

# To use GPTAgent, ensure OPENAI_API_KEY is set in your environment.
# The 'sk-dummy-key' is a placeholder and will not work with actual OpenAI API calls.
openai_api_key = os.environ.get('OPENAI_API_KEY', 'sk-dummy-key')
# agent1 = GPTAgent(name="Alice", api_key=openai_api_key, model="gpt-3.5-turbo")
# agent2 = GPTAgent(name="Bob", api_key=openai_api_key, model="gpt-3.5-turbo")

# 3. Initialize and run the arena with a pre-registered game
arena = Arena(
    game_name="prisoner_dilemma", # Using a pre-registered game
    agents=[agent1, agent2],
    max_turns=3 # Number of rounds for the dilemma
)

print(f"Starting TextArena with {arena.game_name} for {arena.max_turns} turns...")
results = arena.run()

print("\nArena run complete. Final Results:")
for agent_name, score in results['score'].items():
    print(f"- {agent_name}: {score} points")

print("\nFull game history:")
for turn_idx, turn_log in enumerate(arena.game_env.get_history()):
    print(f"--- Turn {turn_idx + 1} ---")
    print(turn_log)

view raw JSON →