World helpers: write a world in a few lines¶
Start from a working world
agentplaykit new world auction writes auction/auction_world.py (built with these
helpers), a scenario.yaml that runs it with no key, and a test. Then:
cd auction, agentplaykit play scenario.yaml, pytest.
Most worlds share the same bookkeeping: whose turn it is, how many rounds are left, and checking
that a move is valid. The helpers in agentplaykit.worlds do that for you, so you write only what
makes your world different (SPEC-017). A world built with
them is an ordinary world: the runner, logs, viewer and reports treat it like any other.
| Helper | Gives you |
|---|---|
TurnBased |
Turn order, rounds, self.history of accepted moves, self.rng, finish(), a default outcome |
TextMoves |
Free-text moves: length limit, too_long: cut, a hint when a model sends a placeholder word |
ChoiceMoves |
One move from a list, with forgiving matching ("4" for 4, "Rock " for "rock") |
Combine TurnBased with one move helper.
A text world: a debate in 29 lines¶
from agentplaykit.worlds import TextMoves, TurnBased
class Debate(TurnBased, TextMoves):
name, version = "debate", "2"
move_name = "argument" # used in hints: 'your action was just the word "argument"...'
rounds, max_chars, too_long = 4, 400, "cut"
def observe(self, agent):
return {
"topic": self.config["topic"],
"debate_so_far": [{"by": h["by"], "argument": h["move"]} for h in self.history],
"your_arguments_left": self.moves_left(agent),
}
The full version is
examples/debate/debate_world.py.
A choice world: negotiation¶
from agentplaykit.worlds import ChoiceMoves, TurnBased
class Negotiation(TurnBased, ChoiceMoves):
name, version = "negotiation", "1"
players, rounds = ["a", "b"], 5
def setup(self, seed): # per-game state; self.rng is already seeded
self.values = {a: self.rng.choice(VALUES) for a in self.agents()}
def legal(self, agent): # the moves this agent may make now
return [*OFFERS, "walk away"]
def play(self, agent, move): # what a move does
if move == "walk away":
self.finish([]) # end the game: no winners
def observe(self, agent):
return {"your_values": self.values[agent], "moves_so_far": self.history}
The full version is
examples/negotiation/negotiation_world.py.
What to write¶
| Method or attribute | When |
|---|---|
observe(agent) |
Always: what this agent sees |
legal(agent) |
With ChoiceMoves: the moves allowed now |
play(agent, move) |
When a move changes state; return a dict to add to the step's info |
setup(seed) |
When a game needs state, such as private values or a hidden target |
outcome() |
When the result is not "the agents in self.winners win" |
players, rounds |
To set the agents and the number of moves each (config players and rounds override them; rounds: null means no limit) |
move_name, max_chars, too_long |
With TextMoves (config overrides max_chars and too_long) |
Call self.finish(winners) to end a game early.
Every error says what a valid move looks like¶
Small models recover when a rejection tells them exactly what to send:
your action was just the word "argument"; write the argument itself as the action,
for example {"action": "<your argument as plain text>"}, not only in "say"
"scissors" is not a legal move; legal moves: 1, 2, 3, "rock", "paper". Reply like {"action": 1}
Test it¶
agentplaykit test my_world.py
The testkit plays text-move worlds too: it samples texts for moves, and checks that a value the world must reject (a number, for a text move) is rejected.
Test it against sloppy models¶
Small models make the same mistakes again and again: a placeholder word such as "argument" as the move, text that is far too long, prose with no JSON, the move under the wrong key, a number sent as text. The sloppy model makes those mistakes on purpose, so you can see how your world copes before real models play it:
agentplaykit test my_world.py --sloppy
sloppy models vs Auction from auction_world.py (5 seeds per mistake)
word_only 5 finished, 0 forfeits
too_long 3 finished, 2 forfeits
error: "too long" <- does not say what a valid move looks like
prose 5 finished, 0 forfeits
...
A flagged error is one a model cannot learn from; add an example of a valid move to it. In
pytest, use run_with_sloppy_models from agentplaykit.testkit:
from agentplaykit.testkit import run_with_sloppy_models
def test_small_model_mistakes_do_not_break_it():
for result in run_with_sloppy_models(Auction, seeds=3):
assert result.failed == 0 and result.unhelpful == [], result
The mistakes are word_only, too_long, prose, wrong_key, fenced, wrong_type,
empty and repeat. SloppyModel({"mistakes": [...], "rate": 0.5, "recover_after": 2}) is also
an ordinary agent you can put in a scenario.
Rejected replies and max_steps¶
Every rejected reply normally uses up a step. For worlds where small models often need a second try, count only accepted moves:
limits:
max_steps: 16
count_rejected_steps: false # rejected replies don't count toward max_steps
max_illegal: 5 # but 5 rejected replies in a row still forfeit