Skip to content

Commit

Permalink
add more prompt filters, pass more data to initial prompt, test makin…
Browse files Browse the repository at this point in the history
…g characters aware of the game
  • Loading branch information
ssube committed Jun 6, 2024
1 parent af69d5b commit 9dd3a73
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 18 deletions.
16 changes: 12 additions & 4 deletions prompts/llama-base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ prompts:
action_check_calendar_each: |
{{name}} will happen in {{turns}} turn
# agent stuff
world_agent_backstory: |
{{ character.backstory }}
world_agent_backstory_other: |
You are {{character | name}}, a character in a text-based role-playing game. Your character's backstory is:
{{ character.backstory }}
Explore the world, interact with other characters, and complete quests to advance the story.
# default dungeon master
world_default_dungeon_master: |
You are the dungeon master in charge of creating an engaging fantasy world full of interesting characters who
Expand All @@ -201,10 +209,10 @@ prompts:
# world generation
world_generate_dungeon_master: |
You are an experienced dungeon master creating a visually detailed world for a new adventure. Be creative and
original, creating a world that is visually detailed and full of curious details. Do not repeat yourself unless you
are given the same prompt with the same characters, room, and context. {{flavor}}. The theme is:
{{theme}}.
You are an experienced dungeon master creating a visually detailed world for a new adventure set in {{theme}}. Be
creative and original, creating a world that is visually detailed and full of curious details. Do not repeat
yourself unless you are given the same prompt with the same characters, room, and context. {{flavor}}. The theme of
the world must be: {{theme}}.
world_generate_world_broadcast_theme: |
Generating a {{theme}} with {{room_count}} rooms
Expand Down
2 changes: 1 addition & 1 deletion taleweave/actions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
)
from taleweave.errors import ActionError
from taleweave.utils.conversation import loop_conversation
from taleweave.utils.prompt import format_prompt
from taleweave.utils.search import (
find_character_in_room,
find_item_in_character,
Expand All @@ -20,6 +19,7 @@
find_room,
)
from taleweave.utils.string import normalize_name
from taleweave.utils.template import format_prompt

logger = getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion taleweave/actions/optional.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
link_rooms,
)
from taleweave.utils.effect import apply_effects, is_effect_ready
from taleweave.utils.prompt import format_prompt
from taleweave.utils.search import find_character_in_room
from taleweave.utils.string import normalize_name
from taleweave.utils.template import format_prompt
from taleweave.utils.world import describe_entity

logger = getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion taleweave/actions/planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from taleweave.errors import ActionError
from taleweave.models.planning import CalendarEvent
from taleweave.utils.planning import get_recent_notes
from taleweave.utils.prompt import format_prompt
from taleweave.utils.template import format_prompt


def take_note(fact: str):
Expand Down
2 changes: 1 addition & 1 deletion taleweave/actions/quest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
get_quests_for_character,
set_active_quest,
)
from taleweave.utils.prompt import format_prompt
from taleweave.utils.search import find_character_in_room
from taleweave.utils.template import format_prompt


def accept_quest(character: str, quest: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion taleweave/bot/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
set_player,
)
from taleweave.render.comfy import render_event
from taleweave.utils.prompt import format_prompt
from taleweave.utils.search import list_characters
from taleweave.utils.template import format_prompt

logger = getLogger(__name__)
client = None
Expand Down
10 changes: 8 additions & 2 deletions taleweave/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from taleweave.models.event import GenerateEvent
from taleweave.utils import try_parse_float, try_parse_int
from taleweave.utils.effect import resolve_int_range
from taleweave.utils.prompt import format_prompt
from taleweave.utils.search import (
list_characters,
list_characters_in_room,
Expand All @@ -35,6 +34,7 @@
list_rooms,
)
from taleweave.utils.string import normalize_name
from taleweave.utils.template import format_prompt

logger = getLogger(__name__)

Expand Down Expand Up @@ -571,7 +571,13 @@ def generate_world(
world_config = get_world_config()
room_count = room_count or resolve_int_range(world_config.size.rooms) or 0

broadcast_generated(message=format_prompt("world_generate_world_broadcast_theme"))
broadcast_generated(
message=format_prompt(
"world_generate_world_broadcast_theme",
theme=theme,
room_count=room_count,
)
)
world = World(name=name, rooms=[], theme=theme, order=[])
set_current_world(world)

Expand Down
2 changes: 1 addition & 1 deletion taleweave/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
from taleweave.plugins import load_plugin
from taleweave.simulate import simulate_world
from taleweave.state import create_agents, save_world, save_world_state
from taleweave.utils.prompt import format_prompt
from taleweave.utils.template import format_prompt

# start the debugger, if needed
if environ.get("DEBUG", "false").lower() == "true":
Expand Down
2 changes: 1 addition & 1 deletion taleweave/simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
from taleweave.utils.conversation import make_keyword_condition, summarize_room
from taleweave.utils.effect import expire_effects
from taleweave.utils.planning import expire_events, get_upcoming_events
from taleweave.utils.prompt import format_prompt
from taleweave.utils.search import find_containing_room
from taleweave.utils.template import format_prompt
from taleweave.utils.world import format_attributes

logger = getLogger(__name__)
Expand Down
6 changes: 5 additions & 1 deletion taleweave/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)
from taleweave.models.entity import World
from taleweave.player import LocalPlayer
from taleweave.utils.template import format_prompt


def create_agents(
Expand All @@ -31,7 +32,10 @@ def create_agents(
agent_memory = restore_memory(memory.get(character.name, []))
agent.load_history(agent_memory)
else:
agent = Agent(character.name, character.backstory, {}, llm)
backstory = format_prompt(
"world_agent_backstory", character=character, world=world
)
agent = Agent(character.name, backstory, {}, llm)
agent.memory = restore_memory(memory.get(character.name, []))
set_character_agent(character.name, character, agent)

Expand Down
2 changes: 1 addition & 1 deletion taleweave/systems/digest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from taleweave.game_system import FormatPerspective, GameSystem
from taleweave.models.entity import Character, Room, World, WorldEntity
from taleweave.models.event import ActionEvent, GameEvent
from taleweave.utils.prompt import format_str
from taleweave.utils.search import find_containing_room, find_portal, find_room
from taleweave.utils.template import format_str

logger = getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion taleweave/utils/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from taleweave.context import broadcast, get_game_config
from taleweave.models.entity import Character, Room
from taleweave.models.event import ReplyEvent
from taleweave.utils.prompt import format_str
from taleweave.utils.template import format_str

from .string import and_list, normalize_name

Expand Down
30 changes: 30 additions & 0 deletions taleweave/utils/prompt.py → taleweave/utils/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,41 @@

logger = getLogger(__name__)


def a_prefix(name: str) -> str:
first_word = name.split(" ")[0]
if first_word.lower() in ["a", "an", "the"]:
return name

if name[0].lower() in "aeiou":
return f"an {name}"

return f"a {name}"


def the_prefix(name: str) -> str:
first_word = name.split(" ")[0]
if first_word.lower() in ["a", "an", "the"]:
return name

return f"the {name}"


def punctuate(name: str, suffix: str) -> str:
if name[-1] in [".", "!", "?", suffix]:
return name

return f"{name}{suffix}"


jinja_env = Environment()
jinja_env.filters["describe"] = describe_entity
jinja_env.filters["name"] = name_entity
jinja_env.filters["and_list"] = and_list
jinja_env.filters["or_list"] = or_list
jinja_env.filters["a_prefix"] = a_prefix
jinja_env.filters["the_prefix"] = the_prefix
jinja_env.filters["punctuate"] = punctuate


def format_prompt(prompt_key: str, **kwargs) -> str:
Expand Down
4 changes: 2 additions & 2 deletions worlds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ templates:
include colorful characters and make sure they will fully utilize all of the actions available to them
in this world, exploring and interacting with each other
- name: jurassic-park
theme: opening scenes from Jurassic Park
theme: opening scenes from the 1993 film Jurassic Park
flavor: |
follow the script of the film Jurassic Park exactly. do not deviate from the script in any way.
follow the script of the 1993 film Jurassic Park exactly. do not deviate from the script in any way.
include accurate characters and instruct them to utilize all of the actions available to them in this world
- name: star-wars
theme: opening scenes from the 1977 film Star Wars
Expand Down

0 comments on commit 9dd3a73

Please sign in to comment.