Skip to content

Dynamic Prompting

altoiddealer edited this page Apr 20, 2024 · 4 revisions

Dynamic Prompting

Note: I did not copy/paste any code at all from sd-dynamic-prompts.

However, I did replicate most of the behavior as described in their Wiki.

This wiki page IS mostly copy/pasted from there.


Recommended Usage:

  • Install sd-dynamic-prompts extension in your A1111 / Forge.
  • For Wildcards, copy/paste or create symbolic links from the collections folder to the wildcards folder.
  • Copy/paste the wildcards directory into /ad_discordbot/ or create a symbolic link.

Main differences from sd-dynamic-prompts

  • The syntax for Variants in ad_discordbot is {{double|brackets}} (opposed to {single|brackets})

WHY?? Because I already use single brackets {} for variables like {llm_0}, etc.

  • The syntax for Wildcards in ad_discordbot is ##wildcard-value (opposed to __wildcard-value__)
  • The syntax within wildcard .txt files is identical to sd-dynamic-prompts: __wildcard-value__

WHY?? Because # is on the main keyboard view on mobile devices, so it is super easy to use double hashtags instead of wrapping with double underscores


This guide will walk you through the template language used to generate dynamic prompts using variants and wildcards.

Table of contents

Variants

Variants allow you to randomly generate one or more options from a list of possibilities. They can be weighted, and you can control the number of options to be chosen.

Basic Syntax

To create a variant, wrap your options in double curly brackets {{}} and separate them with a vertical bar |. For example:

{{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

Weighting Options

You can add weights to options to control their relative frequency. To do this, add a double colon :: followed by the weight before the option:

`{{0.5::summer|0.1::autumn|0.3::winter|0.1::spring}}`

The weights are relative and do not have to add up to 1. If you omit a weight, it is assumed to be 1. Weights are also possible in YAML wildcard files, see below.

Choosing Multiple Values

To choose multiple values, add a number followed by double dollar signs $$ before your options:

My favourite ice-cream flavours are {{2$$chocolate|vanilla|strawberry}}

This will generate one of the following:

  • My favourite ice-cream flavours are chocolate, vanilla
  • My favourite ice-cream flavours are chocolate, strawberry
  • My favourite ice-cream flavours are vanilla, chocolate
  • ... etc

Values are chosen without replacement, so you won't get repeats.

Custom Separator

You can change the default separator by adding a custom separator between the double dollar signs:

My favourite ice-cream flavours are {{2$$ and $$chocolate|vanilla|strawberry}}

This will generate one of the following:

  • My favourite ice-cream flavours are chocolate and vanilla
  • My favourite ice-cream flavours are chocolate and strawberry
  • My favourite ice-cream flavours are vanilla and chocolate
  • ...

Range of Options

To provide a range for the number of options to be chosen, use a dash - between the lower and upper bounds:

My favourite ice-cream flavours are {{1-2$$ and $$chocolate|vanilla|strawberry}}

This will generate:

  • My favourite ice-cream flavours are chocolate
  • My favourite ice-cream flavours are strawberry
  • My favourite ice-cream flavours are vanilla
  • My favourite ice-cream flavours are chocolate and vanilla
  • My favourite ice-cream flavours are chocolate and strawberry
  • My favourite ice-cream flavours are vanilla and chocolate
  • ...

Omitting Bounds

You can omit the lower or upper bound, and it will be treated as the minimum or maximum possible value:

{{-2$$chocolate|vanilla|strawberry}} == {{1-2$$chocolate|vanilla|strawberry}}
{{1-$$chocolate|vanilla|strawberry}} == {{1-2$$chocolate|vanilla|strawberry}}

Limitations

If you request more options than values in the variant, you will only get as many items as are available:

p{{4$$chocolate|vanilla|strawberry}} == chocolate, vanilla, strawberry

Wildcards

Basic Syntax

Wildcards are placeholders that inject values from a file into your prompt. Create a file with the desired values and use double hashtags ## to indicate the wildcard:

##season## is coming

Assuming you have a seasons.txt file with the following content:

# seasons.txt
summer
autumn
winter
spring

This prompt will randomly generate one of the following:

  • summer is coming
  • autumn is coming
  • winter is coming
  • spring is coming

Wildcards in Variants

You can choose multiple values from a wildcard as follows:

My favourite ice-cream flavours are {{2$$##flavours}}

This syntax is also possible:

My favourite ice-cream flavours are {{2$$##flavours|##flavours}}

but the first version will guarantee no duplicates.

Variants in Wildcards

Wildcard values can also contain variants. For example, if your seasons.txt file contains:

# seasons.txt
summer
{{autumn|fall}}
winter
spring

The possible outputs are:

  • summer is coming
  • autumn is coming
  • fall is coming
  • winter is coming
  • spring is coming

Nested Wildcards

You can use wildcards inside other wildcards. If you have a file called people_of_the_world.txt containing:

# people_of_the_world.txt
__asia__
__africa__
__europe__
__north_america__
__south_america__
__australisia__
...

And another file called africa.txt containing:

# africa.txt
Zimbabwean
Namibian
Basotho
...

Then

##people_of_the_world

will first select a value in people_of_the_world.txt. If that value is a wildcard, say __africa__, it will then choose a value from within `africa.txt. Using nesting, you can create an sophisticated wildcard hierarchies.

Resolving Wildcards with Globbing

Globbing allows you to match multiple wildcard files at once. This can be useful if you have multiple files that contain similar data and want to use values from all of them in your prompts.

For example, if you have two files, colours-cold.txt and colours-warm.txt, you can use globbing to resolve values from both of these files by using an asterisk * as a wildcard.

Basic Syntax

To use globbing, simply include an asterisk * in your wildcard pattern:

##colours*

In this example, any file that starts with colours will be matched. So both colours-cold.txt and colours-warm.txt will be used to resolve values.

Example

Suppose you have the following files:

colours-cold.txt:

# colours-cold.txt
blue
green

colours-warm.txt:

# colours-warm.txt:
red
yellow

Using the ##colours* wildcard will randomly select a value from both files:

The colour of my shirt is ##colours*

Possible outputs are:

  • The colour of my shirt is blue
  • The colour of my shirt is green
  • The colour of my shirt is red
  • The colour of my shirt is yellow

Recursive globbing

You can use two wildcards, e.g. artists/** to match any wildcard in the artists/ hierarchy, no matter how deeply nested.

File formats

Text files

The basic wildcard file is a simple text file with a .txt extension. It has one value per line. You can comment out a line with a #, e.g.

# this is a comment
summer
autumn
winter
spring