From 60fb4c94cf784246abf97e1fc0ed055c770e7e62 Mon Sep 17 00:00:00 2001 From: Sean Sube Date: Sun, 16 Jun 2024 16:36:24 -0500 Subject: [PATCH] make system format return a list of labels --- prompts/llama-base.yml | 24 ++++++++++++++---------- taleweave/game_system.py | 10 +++++++--- taleweave/systems/digest.py | 8 ++++---- taleweave/systems/logic.py | 4 ++-- taleweave/systems/planning.py | 1 + taleweave/utils/world.py | 24 +++++++++++++++--------- 6 files changed, 43 insertions(+), 28 deletions(-) diff --git a/prompts/llama-base.yml b/prompts/llama-base.yml index d5d250f..4e7f885 100644 --- a/prompts/llama-base.yml +++ b/prompts/llama-base.yml @@ -212,7 +212,8 @@ prompts: You are an experienced dungeon master creating a visually detailed world for a new adventure set in {{theme | punctuate}} Be creative and original, creating a world that is visually detailed, consistent, and plausible within the context of the setting. Do not repeat yourself unless you are given the same prompt with the same characters, room, and - context. {{flavor | punctuate}} The theme of the world must be: {{theme | punctuate}} + context. {{flavor | punctuate}} + The theme of the world must be: {{theme | punctuate}} world_generate_world_broadcast_theme: | Generating a {{theme}} with {{room_count}} rooms @@ -344,13 +345,15 @@ prompts: # world simulation world_simulate_character_action: | - You are currently in the {{room | name}} room. {{room | describe}}. {{attributes}}. - The room contains the following characters: {{visible_characters}}. - The room contains the following items: {{visible_items}}. - Your inventory contains the following items: {{character_items}}. - You can take the following actions: {{actions}}. - You can move in the following directions: {{directions}}. - {{notes_prompt}} {{events_prompt}} + You are currently in the {{room | name}} room. {{room | describe | punctuate}} + {{'\n'.join(attributes) | punctuate}} + The room contains the following characters: {{visible_characters | and_list}}. + The room contains the following items: {{visible_items | and_list}}. + Your inventory contains the following items: {{character_items | and_list}}. + You can take the following actions: {{actions | and_list}}. + You can move in the following directions: {{directions | and_list}}. + {{notes_prompt}} + {{events_prompt}} What will you do next? Reply with a JSON function call, calling one of the actions. You can only perform one action per turn. What is your next action? world_simulate_character_action_error_json: | @@ -367,11 +370,12 @@ prompts: You can check your notes for important facts or check your calendar for upcoming events. You have {{note_count}} notes. If you have plans with other characters, schedule them on your calendar. You have {{event_count}} events on your calendar. {{room_summary}} - Think about your goals and any quests that you are working on, and plan your next action accordingly. Try to keep your notes accurate and up-to-date. Replace or erase old notes when they are no longer accurate or useful. Do not keeps notes about upcoming events, use your calendar for that. + {{notes_prompt}} + {{events_prompt}} + Think about your goals and any quests that you are working on, and plan your next action accordingly. You can perform up to 3 planning actions in a single turn. When you are done planning, reply with 'END'. - {{notes_prompt}} {{events_prompt}} world_simulate_character_planning_done: | You are done planning your turn. world_simulate_character_planning_notes_some: | diff --git a/taleweave/game_system.py b/taleweave/game_system.py index 634121e..77febbb 100644 --- a/taleweave/game_system.py +++ b/taleweave/game_system.py @@ -1,5 +1,5 @@ from enum import Enum -from typing import Any, Callable, Protocol +from typing import Any, Callable, List, Protocol from packit.agent import Agent @@ -17,8 +17,12 @@ class SystemFormat(Protocol): self, entity: WorldEntity, perspective: FormatPerspective = FormatPerspective.SECOND_PERSON, - ) -> str: - # TODO: should this return a list? + ) -> List[str]: + """ + Format the given world entity for the given perspective. + + Returns a list of phrases or lines that should be added to the entity's description. + """ ... diff --git a/taleweave/systems/digest.py b/taleweave/systems/digest.py index 65fe55d..e3cdaf5 100644 --- a/taleweave/systems/digest.py +++ b/taleweave/systems/digest.py @@ -122,12 +122,12 @@ def digest_listener(event: GameEvent): def format_digest( entity: WorldEntity, perspective: FormatPerspective = FormatPerspective.SECOND_PERSON, -) -> str: +) -> List[str]: if not isinstance(entity, Character): - return "" + return [] if perspective != FormatPerspective.SECOND_PERSON: - return "" + return [] buffer = character_buffers[entity.name] @@ -140,7 +140,7 @@ def format_digest( raise ValueError("Character not found in any room") digest = create_turn_digest(world, room, entity, buffer) - return "\n".join(digest) + return digest def generate_digest(agent: Any, world: World, entity: WorldEntity): diff --git a/taleweave/systems/logic.py b/taleweave/systems/logic.py index 80826e3..4e3ae0f 100644 --- a/taleweave/systems/logic.py +++ b/taleweave/systems/logic.py @@ -152,7 +152,7 @@ def format_logic( entity: WorldEntity, rules: LogicTable, perspective: FormatPerspective = FormatPerspective.SECOND_PERSON, -) -> str: +) -> List[str]: labels = [] for label in rules.labels: @@ -167,7 +167,7 @@ def format_logic( if len(labels) > 0: logger.debug("adding attribute labels: %s", labels) - return " ".join(labels) + return labels def load_logic(filename: str, name_prefix: str = "logic") -> GameSystem: diff --git a/taleweave/systems/planning.py b/taleweave/systems/planning.py index 24b342b..6178708 100644 --- a/taleweave/systems/planning.py +++ b/taleweave/systems/planning.py @@ -199,4 +199,5 @@ def simulate_planning(world: World, turn: int, data: Any | None = None): def init_planning(): + # TODO: add format method that renders the recent notes and upcoming events return [GameSystem("planning", simulate=simulate_planning)] diff --git a/taleweave/utils/world.py b/taleweave/utils/world.py index ae98116..944130a 100644 --- a/taleweave/utils/world.py +++ b/taleweave/utils/world.py @@ -1,4 +1,5 @@ from logging import getLogger +from typing import List from taleweave.context import get_game_systems from taleweave.game_system import FormatPerspective @@ -25,7 +26,8 @@ def describe_character( else: raise ValueError(f"Perspective {perspective} is not implemented") - return f"{character_description} {attribute_descriptions}" + # TODO: use template + return f"{character_description} {'. '.join(attribute_descriptions)}" def describe_static(entity: WorldEntity) -> str: @@ -35,7 +37,9 @@ def describe_static(entity: WorldEntity) -> str: entity.name, attribute_descriptions, ) - return f"{entity.description} {attribute_descriptions}" + + # TODO: use template + return f"{entity.description} {'. '.join(attribute_descriptions)}" def describe_entity( @@ -51,20 +55,22 @@ def describe_entity( def format_attributes( entity: WorldEntity, perspective: FormatPerspective = FormatPerspective.SECOND_PERSON, -) -> str: +) -> List[str]: systems = get_game_systems() - attribute_descriptions = [ - system.format(entity, perspective=perspective) - for system in systems - if system.format - ] + attribute_descriptions = [] + for system in systems: + if system.format: + attribute_descriptions.extend( + system.format(entity, perspective=perspective) + ) + attribute_descriptions = [ description for description in attribute_descriptions if len(description.strip()) > 0 ] - return f"{'. '.join(attribute_descriptions)}" + return attribute_descriptions def name_entity(