Dynamic Prompts Templating Library

repository·main·Indexed 41 days ago

https://github.com/adieyal/dynamicprompts

A Python library for generating diverse prompts for text-to-image models like Stable Diffusion, MidJourney, and Dall-e 2. It features a flexible templating language supporting variants, wildcards, combinatorial generation, and AI-enhanced expansion via MagicPromptGenerator. The library includes specialized generators such as RandomPromptGenerator, CombinatorialPromptGenerator, and Jinja2Generator for advanced logic, and is integrated into ecosystems like Automatic1111, Deforum, and ComfyUI.

Tokens
13.7K
Snippets
59
Records
70
Agent score
51%

What's inside Dynamic Prompts

  1. Real-world integrations of Dynamic Prompts

    main

    Dynamic Prompts is integrated into several popular generative AI ecosystems, including:

    • SD Dynamic Prompts: An extension for the Automatic1111 Stable Diffusion WebUI.
    • Deforum 0.7: Available via Google Colab.
    • ComfyUI Nodes: Custom nodes for the ComfyUI workflow engine.
  2. How variants work in the templating language

    main

    Variants allow you to randomly select one item from a list enclosed in curly braces {}. Each item in the list is separated by a pipe |.

    Example syntax:

    {summer|autumn|winter|spring} is coming

    This will randomly generate one of the following:

    • summer is coming
    • autumn is coming
    • winter is coming
    • spring is coming
  3. Configure Samplers for variants and wildcards

    main

    Dynamic Prompts uses different sampling strategies to select values from variants {...} or wildcards __...__.

    Available Samplers

    1. Random Sampler (~): Picks values randomly. This is the default if no sampler is specified.
    2. Cyclical Sampler (@): Cycles through values in a repeating pattern. Useful for keeping multiple values in sync (e.g., matching embeddings).
    3. Combinatorial Sampler: Produces all possible combinations. Requires using a CombinatorialPromptGenerator (library) or combinatorial mode (extension).

    Explicit Sampler Syntax

    You can specify a sampler for a specific variant or wildcard using a prefix:

    • {~option1|option2} or __~wildcard__ for Random.
    • {@option1|option2} or __@wildcard__ for Cyclical.

    If using the combinatorial sampler, you can mix it with others by explicitly setting a random sampler on specific parts.

    # Random (default)
    {red|green|blue}
    
    # Cyclical
    {@red|green|blue}
    
    # Mixed: Random first, Cyclical second
    {red|green|blue} {@square|circle}
    
    # Mixed: Combinatorial first, Random second
    {red|green|blue} {~square|circle}
    
    # Synchronized Cyclical (keeps pairs together)
    {@embedding1|embedding2} and {@embedding3|embedding4}
  4. How wildcards work

    main

    Wildcards allow you to use external files as placeholders in your templates. A wildcard is referenced using double underscores __ around the filename (without the extension).

    Example syntax:

    __season__ is coming

    This will randomly select a value from a file named season.txt located in your wildcard directory.

  5. Preserve existing values in templates with `?=`

    main

    When using parameterized templates, you may want to assign a variable only if it hasn't already been set. This ensures consistency across nested templates and prevents overwriting values passed from a higher level.

    Use the ?= operator during assignment. If the variable already has a value, the assignment is ignored.

    # Example logic:
    # If ${subject} is already 'boy', ${subject?=!{man|woman}} will not overwrite it.
    "${subject?=!{man|woman}} ${weather?=!{sun|rain}} ${drink?=!{__examples/drink__}} a ${subject} standing in the ${weather} drinking ${drink}"
  6. How to choose multiple variants

    main

    You can use a specific syntax to select multiple values from a list. The syntax {N$$ separator $$A|B|C} tells the engine to choose N values from the list, separated by the provided separator string.

    Example syntax:

    {2$$ and $$A|B|C}

    This will generate combinations like:

    • A and B
    • A and C
    • B and A
    • B and C
    • C and A
    • C and B
  7. Implement nested wildcards

    main

    Wildcard files can contain other wildcards. When the engine resolves a wildcard, if the selected value is itself a wildcard (e.g., __africa__), it will then resolve that nested wildcard (e.g., looking into africa.txt).

    # If people_of_the_world.txt contains '__africa__'
    # and africa.txt contains 'Zimbabwean'
    
    __people_of_the_world__
  8. Use nested variants for complex prompt structures

    main

    You can nest variants within other variants using curly braces {}. This allows for hierarchical selection of options.

    Example: A diamond ring set on a {{rose|yellow|white} gold|platinum} band.

    This can produce:

    • A diamond ring set on a rose gold band
    • A diamond ring set on a white gold band
    • A diamond ring set on a platinum band

    Dynamic Prompts ignores whitespace and allows comments (using #) inside variants, which helps keep complex nested templates readable.

    A diamond ring set on a {
        {rose|yellow|white} gold   # you can also add comments
        | platinum                 # which will be ignored by the DP parser
    } band
  9. Select multiple items from a wildcard with specific counts

    main

    You can use a special syntax to select a specific number of items from a wildcard file. The syntax is {N$$__wildcard__}, where N is the number of items to pick.

    • Fixed count: {2$$__artists__} picks exactly 2 items from the artists.txt wildcard.
    • Range: {2-4$$__artists__} picks a random number of items between 2 and 4.
    • Custom separator: By default, items are separated by a comma (,). To use a different separator like and, use the syntax {N$$ separator $$__wildcard__}.

    Example (using and as a separator): by {2$$ and $$__artists/illustrations/childrens_books__}

    surfer in space, intricate detail, airbrush painting, illustration, by {2$$ and $$__artists/illustrations/childrens_books__}
  10. Use whitespace and Python-style comments

    main

    To improve readability in complex prompts, you can use whitespace (newlines) and Python-style comments (#).

    Note: Standard .txt wildcard files treat newlines as separators for new entries, so whitespace is not permitted there. For complex, multi-line, or commented prompts, use the YAML wildcard format instead.

    # Example of a readable YAML wildcard
    public-prompts:
      cute-stuffed-animals:
        - |
          # Usage: __public-prompts/cuddly-toys(figure=elephant)__
          cute kawaii Squishy ${figure} plush toy,
          realistic texture,
          vibrant studio lighting
  11. Use whitespace and comments in templates

    main

    The templating language supports whitespace for readability and allows you to add comments using the # symbol. This is useful for organizing complex, multi-line prompts.

    Example:

    # Add comments like this
    Funko pop
        {2$$ and $$
      	    yoda
    		|darth vader
    		|jabba the hutt
    		|princess leia
    		|chewbacca
    		|luke skywalker
    	}
    	figurine, made of
    	{
    		wood
    		|plastic
    		|metal
    		|stone
    	},
    	product studio shot, on a white background, diffused lighting, centered