Skip to content

Choose Your Own Adventure

Andrew edited this page Aug 6, 2024 · 2 revisions

Below we use microagents to implement a Choose Your Own Adventure-style story game. Each function serves as an microagent that handles a small part of the overall architecture. The program itself is a mix of fuzzy state (interpreted by the LLM) and concrete state (interpreted by Python).

from dataclasses import dataclass

from manifest import ai


@dataclass
class World:
    datetime: str  # when are we? iso8601
    location: str  # where are we?
    general_environment: str  # a description of the player's overall surroundings
    specific_environment: str  # a description of the player's immediate surroundings


@dataclass
class Player:
    identity: str  # who am i?
    objectives: list[str]  # what do i want?
    disposition: str  # how do i feel?
    current_task: str  # what am i doing?
    inventory: list[str]  # what do i have?


@ai
def initialize_world(description: str) -> World:
    """Initialize the world state based on a rough description"""


@ai
def initialize_player(description: str) -> Player:
    """Initialize the player state based on a rough description"""


@ai
def determine_consequences(
    *,
    action: str,
    player: Player,
    world: World,
) -> list[str]:
    """Determine the consequences of an action on the player and world state.
    The consequences should be plausible with the action in the context of the
    given world and player states. The consequences should represent the
    immediate impact of the action, not necessarily the long-term impact."""


@ai
def update_states(
    *,
    action: str,
    consequences: list[str],
    player: Player,
    world: World,
) -> tuple[Player, World]:
    """Update the current world state and player state based on the previous
    world state, player state, an action, and consequences. Both the world and
    the player state should update in a plausible way based on the action and
    consequences. The returned world and player states should reflect the impact
    of the action and consequences."""


@ai
def produce_narration(
    *,
    last_narration: str | None = None,
    action: str | None = None,
    consequences: list[str] | None = None,
    player: Player,
    world: World,
) -> str:
    """Produce a short narration based on the current world state and player
    state. It should be written from the player's first-person perspective. If
    an action and consequences are provided, the narration should focus
    primarily on the action and consequences, within the context of the
    environment.

    If a last_narration is provided, the new narration should be a continuation
    of the last_narration, using the last_narration as context for the new
    narration.

    The narration should be plausible while also being engaging from a
    storytelling perspective.
    """


world = initialize_world("Boarding the Titanic for its maiden voyage")
player = initialize_player("A wealthy passenger on the Titanic")
narration = produce_narration(player=player, world=world)
print(narration)

while True:
    action = input("\nWhat do you want to do?\n")
    consequences = determine_consequences(
        action=action,
        player=player,
        world=world,
    )
    player, world = update_states(
        action=action,
        consequences=consequences,
        player=player,
        world=world,
    )
    narration = produce_narration(
        last_narration=narration,
        action=action,
        consequences=consequences,
        player=player,
        world=world,
    )
    print()
    print(narration)
Clone this wiki locally