1
0
Fork 0
taleweave-ai/adventure/generate.py

241 lines
8.2 KiB
Python
Raw Normal View History

2024-05-02 11:56:57 +00:00
from logging import getLogger
from random import choice, randint
2024-05-04 20:35:42 +00:00
from typing import Callable, List
2024-05-02 11:56:57 +00:00
from packit.agent import Agent
2024-05-04 22:17:56 +00:00
from packit.loops import loop_retry
2024-05-02 11:56:57 +00:00
from adventure.models import Actor, Item, Room, World
logger = getLogger(__name__)
2024-05-04 20:35:42 +00:00
def generate_room(
agent: Agent, world_theme: str, existing_rooms: List[str], callback
) -> Room:
2024-05-04 22:17:56 +00:00
def unique_name(name: str, **kwargs):
if name in existing_rooms:
raise ValueError(f"A room named {name} already exists")
return name
name = loop_retry(
agent,
2024-05-02 11:56:57 +00:00
"Generate one room, area, or location that would make sense in the world of {world_theme}. "
"Only respond with the room name, do not include the description or any other text. "
'Do not prefix the name with "the", do not wrap it in quotes. The existing rooms are: {existing_rooms}',
2024-05-04 22:17:56 +00:00
context={
"world_theme": world_theme,
"existing_rooms": existing_rooms,
},
result_parser=unique_name,
2024-05-02 11:56:57 +00:00
)
2024-05-04 20:35:42 +00:00
callback(f"Generating room: {name}")
2024-05-02 11:56:57 +00:00
desc = agent(
"Generate a detailed description of the {name} area. What does it look like? "
"What does it smell like? What can be seen or heard?",
name=name,
)
items = []
actors = []
actions = {}
return Room(
name=name, description=desc, items=items, actors=actors, actions=actions
)
def generate_item(
agent: Agent,
world_theme: str,
2024-05-04 20:35:42 +00:00
existing_items: List[str],
callback,
2024-05-02 11:56:57 +00:00
dest_room: str | None = None,
dest_actor: str | None = None,
) -> Item:
if dest_actor:
dest_note = "The item will be held by the {dest_actor} character"
elif dest_room:
dest_note = "The item will be placed in the {dest_room} room"
else:
dest_note = "The item will be placed in the world"
2024-05-04 22:17:56 +00:00
def unique_name(name: str, **kwargs):
if name in existing_items:
raise ValueError(f"An item named {name} already exists")
return name
name = loop_retry(
agent,
2024-05-02 11:56:57 +00:00
"Generate one item or object that would make sense in the world of {world_theme}. {dest_note}. "
2024-05-04 04:18:21 +00:00
"Only respond with the item name, do not include a description or any other text. Do not prefix the "
'name with "the", do not wrap it in quotes. Do not include the name of the room. '
2024-05-02 11:56:57 +00:00
"Do not create any duplicate items in the same room. Do not give characters any duplicate items. The existing items are: {existing_items}",
2024-05-04 22:17:56 +00:00
context={
"dest_note": dest_note,
"existing_items": existing_items,
"world_theme": world_theme,
},
result_parser=unique_name,
2024-05-02 11:56:57 +00:00
)
2024-05-04 20:35:42 +00:00
callback(f"Generating item: {name}")
2024-05-02 11:56:57 +00:00
desc = agent(
"Generate a detailed description of the {name} item. What does it look like? What is it made of? What does it do?",
name=name,
)
actions = {}
return Item(name=name, description=desc, actions=actions)
def generate_actor(
2024-05-04 20:35:42 +00:00
agent: Agent, world_theme: str, dest_room: str, existing_actors: List[str], callback
2024-05-02 11:56:57 +00:00
) -> Actor:
2024-05-04 22:17:56 +00:00
def unique_name(name: str, **kwargs):
if name in existing_actors:
raise ValueError(f"An actor named {name} already exists")
return name
name = loop_retry(
agent,
2024-05-02 11:56:57 +00:00
"Generate one person or creature that would make sense in the world of {world_theme}. The character will be placed in the {dest_room} room. "
'Only respond with the character name, do not include a description or any other text. Do not prefix the name with "the", do not wrap it in quotes. '
2024-05-04 04:18:21 +00:00
"Do not include the name of the room. Do not give characters any duplicate names."
"Do not create any duplicate characters. The existing characters are: {existing_actors}",
2024-05-04 22:17:56 +00:00
context={
"dest_room": dest_room,
"existing_actors": existing_actors,
"world_theme": world_theme,
},
result_parser=unique_name,
2024-05-02 11:56:57 +00:00
)
2024-05-04 20:35:42 +00:00
callback(f"Generating actor: {name}")
2024-05-02 11:56:57 +00:00
description = agent(
"Generate a detailed description of the {name} character. What do they look like? What are they wearing? "
2024-05-03 01:57:11 +00:00
"What are they doing? Describe their appearance from the perspective of an outside observer."
"Do not include the room or any other characters in the description, because they will move around.",
2024-05-02 11:56:57 +00:00
name=name,
)
backstory = agent(
"Generate a backstory for the {name} actor. Where are they from? What are they doing here? What are their "
'goals? Make sure to phrase the backstory in the second person, starting with "you are" and speaking directly to {name}.',
name=name,
)
return Actor(
name=name,
backstory=backstory,
description=description,
2024-05-04 04:18:21 +00:00
actions={},
2024-05-02 11:56:57 +00:00
)
2024-05-03 01:57:11 +00:00
def generate_world(
2024-05-04 04:18:21 +00:00
agent: Agent,
name: str,
theme: str,
room_count: int | None = None,
max_rooms: int = 5,
2024-05-04 20:35:42 +00:00
callback: Callable[[str], None] = lambda x: None,
2024-05-03 01:57:11 +00:00
) -> World:
2024-05-04 04:18:21 +00:00
room_count = room_count or randint(3, max_rooms)
2024-05-04 20:35:42 +00:00
callback(f"Generating a {theme} with {room_count} rooms")
2024-05-02 11:56:57 +00:00
existing_actors: List[str] = []
existing_items: List[str] = []
# generate the rooms
rooms = []
for i in range(room_count):
existing_rooms = [room.name for room in rooms]
2024-05-04 20:35:42 +00:00
room = generate_room(agent, theme, existing_rooms, callback=callback)
2024-05-02 11:56:57 +00:00
rooms.append(room)
item_count = randint(0, 3)
2024-05-04 22:17:56 +00:00
callback(f"Generating {item_count} items for room: {room.name}")
2024-05-04 20:35:42 +00:00
2024-05-02 11:56:57 +00:00
for j in range(item_count):
item = generate_item(
2024-05-04 20:35:42 +00:00
agent,
theme,
dest_room=room.name,
existing_items=existing_items,
callback=callback,
2024-05-02 11:56:57 +00:00
)
room.items.append(item)
existing_items.append(item.name)
actor_count = randint(0, 3)
2024-05-04 22:17:56 +00:00
callback(f"Generating {actor_count} actors for room: {room.name}")
2024-05-04 20:35:42 +00:00
2024-05-02 11:56:57 +00:00
for j in range(actor_count):
actor = generate_actor(
2024-05-04 20:35:42 +00:00
agent,
theme,
dest_room=room.name,
existing_actors=existing_actors,
callback=callback,
2024-05-02 11:56:57 +00:00
)
room.actors.append(actor)
existing_actors.append(actor.name)
# generate the actor's inventory
item_count = randint(0, 3)
2024-05-04 20:35:42 +00:00
callback(f"Generating {item_count} items for actor {actor.name}")
2024-05-02 11:56:57 +00:00
for k in range(item_count):
item = generate_item(
2024-05-04 20:35:42 +00:00
agent,
theme,
dest_room=room.name,
existing_items=existing_items,
callback=callback,
2024-05-02 11:56:57 +00:00
)
actor.items.append(item)
existing_items.append(item.name)
opposite_directions = {
"north": "south",
"south": "north",
"east": "west",
"west": "east",
}
2024-05-04 04:18:21 +00:00
# generate portals to link the rooms together
2024-05-02 11:56:57 +00:00
for room in rooms:
directions = ["north", "south", "east", "west"]
for direction in directions:
if direction in room.portals:
logger.debug(f"Room {room.name} already has a {direction} portal")
continue
opposite_direction = opposite_directions[direction]
if randint(0, 1):
dest_room = choice([r for r in rooms if r.name != room.name])
# make sure not to create duplicate links
if room.name in dest_room.portals.values():
logger.debug(
f"Room {dest_room.name} already has a portal to {room.name}"
)
continue
if opposite_direction in dest_room.portals:
logger.debug(
f"Room {dest_room.name} already has a {opposite_direction} portal"
)
continue
# create bidirectional links
room.portals[direction] = dest_room.name
dest_room.portals[opposite_directions[direction]] = room.name
2024-05-04 04:18:21 +00:00
# ensure actors act in a stable order
order = [actor.name for room in rooms for actor in room.actors]
return World(name=name, rooms=rooms, theme=theme, order=order)