talemate/docs/dev/templates.md
veguAI 95a17197ba
0.26.0 (#133)
* implement manually disabling and enabling clients

* relock

* fix warning spam

* start moving stuff around

* move more stuff

* start separating world state manager into more managable submodules

* character title

* scroll home to top always

* finish separating character state editor into components

* fix defered nav to character sections

* separate components for pin and contextdb managing

* fix issue with context character filter search

* fix world state manage ux state reset issues

* wsm menu refactor
allow updating character image from wsm
cover image layout fixes

* remove debug spam

* fix client deletion / disabling rubber banding issue

* deactivate / activate / delete characters through wsm

* reload character instead

* fix koboldcpp client jiggle arguments

* save scene title

* fix deferred nav

* fix issue where blanking a character detail would bug out

* some layout changes

* character import copies cover image

* remove debug message

* character import via wsm

* deactivate imported characters

* images nav option placeholder

* start move towards new world state templating system

* prompt tweak

* add templates/world-state/*.yaml

* switch to new world state template system in manager

* template editor progress

* more wsm template changes

* template applicator component

* template applicate added to attributes and details

* selective template application

* fix issue with template editing

* attribute and detail templates dont require instructions

* adjust character attributes and details template applicator integration

* add gpt-4o

add gpt-4o-2024-05-13

* autocomplete prompt and postprocessing tweaks

* prompt tweaks

* fix issue where saving a new scene could cause recent config changes to revert

* only download punkt if its not downloaded yet

* working character attribute templates

* character detail generate working
move template generate logic to worldstate.templates

* character creator first steps

* support contextual generate when character doesn't exist

* move talemate wsm templates to their own dir, add supports_spice and supports_style flags

* wsm character creator progress

* character creator progress

* character creator progress and wire up image creation in character editor

* templating progress

* contextual generate generation options

* ux tweaks

* wirte up writing style and spice to generation

* wire spice / writing style to detail generation

* notify when spice is applied

* tweaks to generation spice notifications

* add some help / information to template editor

* fix some issues with detail and attribute generation

* some context gen tweaks

* character gen tweaks

* character color changer

* link to templates form gen option ux

* gen options for dialogue example genrate

* ctrl click to max spice level

* unify spice application notification into a component for reuse

* improvements to example dialogue generation

* some refinements to character editor

* remove some old cruft from scene schema

* wsm scene editor progress

* relock

* relock

* debug message cleanup

* fix issue with tab selection when loading a scene

* scene editor progress

* centralized generation options

* pass generation settings through to character creator

* save changes from wsm view

* scene settings
save copy

* refactor world entry / states editor

* fix issue with applying non-character world state templates

* layout fixes

* allow updating of scene cover image

* move history manager to world editor

* add phi-3 base template

* dialogue cleanup improvements

* refactor scoped game-engine api

* separate legacy creator functions to own file

* remove cruft

* some cleanup and fixes

* add photo style

* remove noisy log message

* better handling of active scene

* some fixes to pin editor

* don't enforce height

* active scene context fixes

* fix intro and scene description generration

* tweak preset for scene direction and summarization tasks

* ensure memory db is open

* update frontend dependencies

* update frontend dependencies

* fix issue with prompt query_memory function returning None

* typo

* default  world state templates

* new scene creation fixes
remove legacy creator ux

* scene export

* fix scene loading from upload

* add claude 3.5 sonnet

* fix automatic client selection when the current client is disabled

* remove cruft

* agent modal extended to support multiple config panels
visual agent prompt prefixes and suffixes addeed

* fix issue with world state template group saving

* resolve attribute name issue `copy`

* RequestInput: fix form validation and keystroke submit

* support chara load from json files also refactor character loading to load.py

* implement simple act-as feature using tab to cycle through active characters in the scene

* docs progress

* tts settings tweaks

* fix issue with loading older talemate scenes

* docs progress

* fix issue with config validation on new installs

* some tweaks for agent setting modals

* default template changed to alpaca

* docs dependencies

* gemma2 template

* nemotron4 template

* docs

* docs

* docs

* change prompt template section to autocomplete

* fix agent config not loading for some agents

* allow deletion of player character

* fix some oddities with scene outline commit

* automatically active player characters and create player characters with the correct actor class

* also set the first npc created as immediately acitve

* add has_active_npcs property and re-emit message history when scene outline is updated.

* indicate when visualizer is busy in the scene tools

* check for busy instead

* prompt tweaks for movie script type dialogue format

* gemma2 prompt fixed

* scene message colors updated

* act as narrator

* move to _old

* scene message appearance tweaks

* fix rubberbanding when editing text field in agent configs

* fix autocompletionm when acting as different character or narrator

* disable autocomplete during command execution

* remove autocomplete button from scene tools

* docs

* relock

* docs

* docs

* improve context pins in dialogue context

* better approximate token count

* fix pin condition editing

* fix issue where scene save as would lose long term memory entries

* immediately clean message history when loading a new scene

* docs

* ensure intro text has formatting markers

* narrator messages written by the player can now be deleted.

* scene editor

* move docs around

* start character editor docs

* more character editor docs:

* fix some ux bugs

* fix template group deletrion not removing the file

* docs

* typos

* docs

* relock

* docs

* notify image generation errors

* linting

* gh pages workflow

* use poetry

* dont use poetry

* link to docs site

* set site_url

* add trailing slash

* fix image paths

* re-add tabbyai link

* fix image generation error triggering incorrectly

* fix intro formatting incosistencies

* remove cruft

* add time passed label to history view

* date adjustments

* tests

* add gpt-4o-mini

* fix links

* remove hard ntlk requirement for voice generation chunking

ntlk error handling

fix typo

* docs

* fix issdue with dupe character card intro text

* disable character forms while templates are being applied.

* failure during context generate no longer locks ux

* refactor client and agent status display in system bar

* llama 3.1 8b claude

* fix format

* adjustments to automcomplete dialogue instructions

* add mistral nemo

* debug info

* fix system agent status getting stuck

* readme

* readme

* fix autocomplete responses when they are framed by quotes
2024-07-26 21:43:06 +03:00

6 KiB

Template Overrides

!!! warning "Old documentation" This is old documentation and needs to be updated, however may still contain useful information.

Introduction to Templates

In Talemate, templates are used to generate dynamic content for various agents involved in roleplaying scenarios. These templates leverage the Jinja2 templating engine, allowing for the inclusion of variables, conditional logic, and custom functions to create rich and interactive narratives.

Template Structure

A typical template in Talemate consists of several sections, each enclosed within special section tags (<|SECTION:NAME|> and <|CLOSE_SECTION|>). These sections can include character details, dialogue examples, scenario overviews, tasks, and additional context. Templates utilize loops and blocks to iterate over data and render content conditionally based on the task requirements.

Overriding Templates

Users can customize the behavior of Talemate by overriding the default templates. To override a template, create a new template file with the same name in the ./templates/prompts/{agent}/ directory. When a custom template is present, Jinja2 will prioritize it over the default template located in the ./src/talemate/prompts/templates/{agent}/ directory.

Creator Agent Templates

The creator agent templates allow for the creation of new characters within the character creator. Following the naming convention character-attributes-*.jinja2, character-details-*.jinja2, and character-example-dialogue-*.jinja2, users can add new templates that will be available for selection in the character creator.

Requirements for Creator Templates

  • All three types (attributes, details, example-dialogue) need to be available for a choice to be valid in the character creator.
  • Users can check the human templates for an understanding of how to structure these templates.

Example Templates

  • src/talemate/prompts/templates/creator/character-attributes-human.jinja2
  • src/talemate/prompts/templates/creator/character-details-human.jinja2
  • src/talemate/prompts/templates/creator/character-example-dialogue-human.jinja2

These example templates can serve as a guide for users to create their own custom templates for the character creator.

Extending Existing Templates

Jinja2's template inheritance feature allows users to extend existing templates and add extra information. By using the {% extends "template-name.jinja2" %} tag, a new template can inherit everything from an existing template and then add or override specific blocks of content.

Example

To add a description of a character's hairstyle to the human character details template, you could create a new template like this:

{% extends "character-details-human.jinja2" %}
{% block questions %}
{% if character_details.q("what does "+character.name+"'s hair look like?") -%}
Briefly describe {{ character.name }}'s hair-style using a narrative writing style that reminds of mid 90s point and click adventure games. (2 - 3 sentences).
{% endif %}
{% endblock %}

This example shows how to extend the character-details-human.jinja2 template and add a block for questions about the character's hair. The {% block questions %} tag is used to define a section where additional questions can be inserted or existing ones can be overridden.

Advanced Template Topics

Jinja2 Functions in Talemate

Talemate exposes several functions to the Jinja2 template environment, providing utilities for data manipulation, querying, and controlling content flow. Here's a list of available functions:

  1. set_prepared_response(response, prepend): Sets the prepared response with an optional prepend string. This function allows the template to specify the beginning of the LLM response when processing the rendered template. For example, set_prepared_response("Certainly!") will ensure that the LLM's response starts with "Certainly!".
  2. set_prepared_response_random(responses, prefix): Chooses a random response from a list and sets it as the prepared response with an optional prefix.
  3. set_eval_response(empty): Prepares the response for evaluation, optionally initializing a counter for an empty string.
  4. set_json_response(initial_object, instruction, cutoff): Prepares for a JSON response with an initial object and optional instruction and cutoff.
  5. set_question_eval(question, trigger, counter, weight): Sets up a question for evaluation with a trigger, counter, and weight.
  6. disable_dedupe(): Disables deduplication of the response text.
  7. random(min, max): Generates a random integer between the specified minimum and maximum.
  8. query_scene(query, at_the_end, as_narrative): Queries the scene with a question and returns the formatted response.
  9. query_text(query, text, as_question_answer): Queries a text with a question and returns the formatted response.
  10. query_memory(query, as_question_answer, **kwargs): Queries the memory with a question and returns the formatted response.
  11. instruct_text(instruction, text): Instructs the text with a command and returns the result.
  12. retrieve_memories(lines, goal): Retrieves memories based on the provided lines and an optional goal.
  13. uuidgen(): Generates a UUID string.
  14. to_int(x): Converts the given value to an integer.
  15. config: Accesses the configuration settings.
  16. len(x): Returns the length of the given object.
  17. count_tokens(x): Counts the number of tokens in the given text.
  18. print(x): Prints the given object (mainly for debugging purposes).

These functions enhance the capabilities of templates, allowing for dynamic and interactive content generation.

Error Handling

Errors encountered during template rendering are logged and propagated to the user interface. This ensures that users are informed of any issues that may arise, allowing them to troubleshoot and resolve problems effectively.

By following these guidelines, users can create custom templates that tailor the Talemate experience to their specific storytelling needs.# Template Overrides in Talemate