How character-ai/prompt-poet works
Main Components
• Prompt Creation Engine: This is the core component that ties together the templating process. It processes a “raw_template” with provided “template_data”.
• Rendering Stage: Represent the use of Jinja2 to perform variable substitutions, control flow (e.g., loops/conditionals), and even invoke Python functions. This is where the raw template transforms into an intermediate string.
• Loading/Parsing Stage: Indicate the subsequent YAML parsing stage that converts the rendered string into a structured format – a list of prompt “parts” with attributes such as name, role, content, and optional truncation priority.
• Tokenization Module: Include the component responsible for calling the tokenizer (by default using TikToken “o200k_base” or a custom encode_func) to convert the final prompt string into token sequences.
• Truncation Logic: Show how the tokenized prompt is processed to apply cache-aware or custom truncation logic, ensuring that the prompt meets token limits while preserving high cache hit rates.
• Exception Handling: Include prompt-related exceptions handled by the “pp_exceptions” module.
• External Dependencies/Integrations: Identify external libraries and services such as LangChain (for LLM communication) and TikToken for tokenization.
Displaying Relationships and Data Flow
• Finally, illustrate that the fully processed prompt (as string, tokens, or structured messages) is passed to an “LLM Integration” component (e.g., ChatOpenAI).
Architectural Patterns and Designs
• Two-Stage Pipeline:
• Stage 1 (Rendering): Use Jinja2 to embed control flow and direct Python function calls.
• Stage 2 (Parsing): Process the output using a YAML parser to ensure a uniform data structure for prompt elements.
• Data Transformation process where a raw string evolves into a structured prompt.
• That the system differentiates between various prompt “parts” (system instructions, user query, reply prompt, etc.) based on its YAML structure.
• Plug-in architecture that supports different encoding functions, custom truncation, and template registries to facilitate extendability and performance tuning.
• The multi-stage transformation of prompt templates.
• The extensibility offered by the template registry.
• External integration for model invocation.