character-ai / prompt-poet

Streamlines and simplifies prompt design for both developers and non-technical users with a low code approach.
https://pypi.org/project/prompt-poet/
MIT License
922 stars 76 forks source link

Add expected_template_data_keys validation to PromptPart in prompt.py #17

Closed zzon0 closed 3 months ago

zzon0 commented 3 months ago

Overview

This pull request introduces a new attribute, expected_template_data_keys, to the PromptPart class. This attribute ensures that specific keys are present in the template data when rendering a prompt. The changes enhance the integrity of template data and prevent runtime errors due to missing keys.

Changes

  1. New Attribute in PromptPart:

    • expected_template_data_keys: A list of keys expected to be present in the template data.
  2. Validation Method:

    • Added _validate_template_replacements method in the Prompt class to check for the presence of required keys in template_data.
    • This method is invoked in the _render_parts method for each PromptPart instance.
  3. Refactoring:

    • Updated _render_parts method to include the validation step.
    • Minor refactoring for code readability and maintenance.

Rationale

The addition of expected_template_data_keys helps ensure that all necessary data is available in the template context, reducing the likelihood of runtime errors caused by missing template data. This makes the code more robust and easier to debug.

Testing

Related Issues

This change addresses issues related to template data integrity and aims to prevent errors that occur due to missing keys in the template data.

Thank you!