1
0
Fork 0

ensure entity names are unique

This commit is contained in:
Sean Sube 2024-05-04 17:17:56 -05:00
parent bb7cdac04f
commit 3574542a94
Signed by: ssube
GPG Key ID: 3EED7B957D362AF1
2 changed files with 48 additions and 15 deletions

View File

@ -3,6 +3,7 @@ from random import choice, randint
from typing import Callable, List from typing import Callable, List
from packit.agent import Agent from packit.agent import Agent
from packit.loops import loop_retry
from adventure.models import Actor, Item, Room, World from adventure.models import Actor, Item, Room, World
@ -12,12 +13,22 @@ logger = getLogger(__name__)
def generate_room( def generate_room(
agent: Agent, world_theme: str, existing_rooms: List[str], callback agent: Agent, world_theme: str, existing_rooms: List[str], callback
) -> Room: ) -> Room:
name = agent( 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,
"Generate one room, area, or location that would make sense in the world of {world_theme}. " "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. " "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}', 'Do not prefix the name with "the", do not wrap it in quotes. The existing rooms are: {existing_rooms}',
world_theme=world_theme, context={
existing_rooms=existing_rooms, "world_theme": world_theme,
"existing_rooms": existing_rooms,
},
result_parser=unique_name,
) )
callback(f"Generating room: {name}") callback(f"Generating room: {name}")
desc = agent( desc = agent(
@ -50,14 +61,24 @@ def generate_item(
else: else:
dest_note = "The item will be placed in the world" dest_note = "The item will be placed in the world"
name = agent( 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,
"Generate one item or object that would make sense in the world of {world_theme}. {dest_note}. " "Generate one item or object that would make sense in the world of {world_theme}. {dest_note}. "
"Only respond with the item name, do not include a description or any other text. Do not prefix the " "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. ' 'name with "the", do not wrap it in quotes. Do not include the name of the room. '
"Do not create any duplicate items in the same room. Do not give characters any duplicate items. The existing items are: {existing_items}", "Do not create any duplicate items in the same room. Do not give characters any duplicate items. The existing items are: {existing_items}",
dest_note=dest_note, context={
existing_items=existing_items, "dest_note": dest_note,
world_theme=world_theme, "existing_items": existing_items,
"world_theme": world_theme,
},
result_parser=unique_name,
) )
callback(f"Generating item: {name}") callback(f"Generating item: {name}")
desc = agent( desc = agent(
@ -73,14 +94,24 @@ def generate_item(
def generate_actor( def generate_actor(
agent: Agent, world_theme: str, dest_room: str, existing_actors: List[str], callback agent: Agent, world_theme: str, dest_room: str, existing_actors: List[str], callback
) -> Actor: ) -> Actor:
name = agent( 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,
"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. " "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. ' '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. '
"Do not include the name of the room. Do not give characters any duplicate names." "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}", "Do not create any duplicate characters. The existing characters are: {existing_actors}",
dest_room=dest_room, context={
existing_actors=existing_actors, "dest_room": dest_room,
world_theme=world_theme, "existing_actors": existing_actors,
"world_theme": world_theme,
},
result_parser=unique_name,
) )
callback(f"Generating actor: {name}") callback(f"Generating actor: {name}")
description = agent( description = agent(
@ -125,7 +156,7 @@ def generate_world(
rooms.append(room) rooms.append(room)
item_count = randint(0, 3) item_count = randint(0, 3)
callback(f"Generating {item_count} items for room {room.name}") callback(f"Generating {item_count} items for room: {room.name}")
for j in range(item_count): for j in range(item_count):
item = generate_item( item = generate_item(
@ -139,7 +170,7 @@ def generate_world(
existing_items.append(item.name) existing_items.append(item.name)
actor_count = randint(0, 3) actor_count = randint(0, 3)
callback(f"Generating {actor_count} actors for room {room.name}") callback(f"Generating {actor_count} actors for room: {room.name}")
for j in range(actor_count): for j in range(actor_count):
actor = generate_actor( actor = generate_actor(

View File

@ -313,10 +313,12 @@ def main():
save_world(world, world_file) save_world(world, world_file)
create_agents(world, memory=memory, players=players) create_agents(world, memory=memory, players=players)
if args.server:
server_system(world, 0)
# load extra actions # load extra actions
extra_actions = [] extra_actions = []
for action_name in args.actions: for action_name in args.actions or []:
logger.info(f"Loading extra actions from {action_name}") logger.info(f"Loading extra actions from {action_name}")
module_actions = load_plugin(action_name) module_actions = load_plugin(action_name)
logger.info( logger.info(
@ -330,7 +332,7 @@ def main():
save_world_state(world, step, world_state_file) save_world_state(world, step, world_state_file)
extra_systems = [(snapshot_system, None)] extra_systems = [(snapshot_system, None)]
for system_name in args.systems: for system_name in args.systems or []:
logger.info(f"Loading extra systems from {system_name}") logger.info(f"Loading extra systems from {system_name}")
module_systems = load_plugin(system_name) module_systems = load_plugin(system_name)
logger.info( logger.info(