Prompt Matrix
A Python package to expand prompt matrix strings. For example, the string "The <dog|cat> in the hat" expands to the list ["The dog in the hat", "The cat in the hat"].
The motivating case for this package is to compare the effects of different prompts in text and image generation systems such as Stable Diffusion and GPT-3.
Features
A prompt string may contain multiple alternations. For example, "The <dog|cat> in the <cardigan|hat>" produces a list of the four strings "The dog in the cardigan", "The dog in the hat", "The cat in the cardigan", and "The cat in the hat".
A prompt string may contain nested alternations. For example, "The <<small|large> dog|cat>" produces the strings "The small dog", "The large do", and "The cat".
Brackets [] enclose optional elements. For example, "The [small] cat" is
equivalent to "The <small,> cat", and both produce the strings "The small cat" and "The cat".
The special characters <>{}| can be replaced by different strings, or disabled
by specifying
None or the empty string.
Note: The disjunction is bounded by the enclosing brackets, if any.
"The dog|cat in the cardigan|hat"generates the three strings"The dog","cat in the gardigan", and"hat". This is in contrast to some other systems, that scope a disjunction to the text delimited by surrounding whitespace.
Install
$ pip install prompt-matrix
Usage
Prompt Matrix provides two functions for expanding a prompt matrix:
expand and iterexpand. Both take a string that specifies
a prompt matrix.
expand returns an array of strings with all possible combinations of the
prompt matrix elements.
import prompt_matrix
prompt = "<hi|hello> <there|you>"
expansion = prompt_matrix.expand(prompt)
print(expansion) # ["hi there", "hi you", "hello there", "hello you"]
iterexpand returns a generator that yields the expansions one by
one.
import prompt_matrix
prompt = "<hi|hello> <there|you>"
for expansion in prompt_matrix.iterexpand(prompt):
print(expansion) # "hi there", "hi you", "hello there", "hello you"
Examples
Example 1: Basic usage
import prompt_matrix
prompt_matrix.expand("The <dog|cat> in the hat")
# ->
# ["The dog in the hat",
# "The cat in the hat"]
Example 2: Using multiple alternations
prompt_matrix.expand("The <dog|cat> in the <cardigan|hat>")
# ->
# ["The dog in the cardigan",
# "The dog in the hat",
# "The cat in the cardigan",
# "The cat in the hat"]
Example 3: Using nested brackets
prompt_matrix.expand("The <<small|large> <brown|black> dog|<red|blue> fish>")
# ->
# ["The small brown dog",
# "The small black dog",
# "The large brown dog",
# "The large black dog",
# "The red fish",
# "The blue fish"]
Example 4: Using custom brackets and separator
prompt_matrix.expand("The {dog,cat} in the {cardigan,hat}",
brackets=['{', '}'], alt=',')
# ->
# ["The dog in the cardigan",
# "The dog in the hat",
# "The cat in the cardigan",
# "The cat in the hat"]
License
MIT
Metadata
Release files for prompt-matrix 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| prompt_matrix-0.1.4.tar.gz | 4.7 kB | Without provenance | Details for prompt_matrix-0.1.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| prompt_matrix-0.1.4-py3-none-any.whl | Python 3 | none | any | Details for prompt_matrix-0.1.4-py3-none-any.whl |
Total release size: 10.6 kB
Release files / prompt_matrix-0.1.4.tar.gz
| Download URL | prompt_matrix-0.1.4.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
07350cb54aaca60e2fb8106e463053cd97c606064cb31e0a865a4a8b7e8d22e9
|
|
BLAKE2b-256 checksum How to use checksums |
40c3c2131ef06b660b61d053ffedf53ec71c87afbe34e3580a031fc1a07f8280
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.1 CPython/3.11.2 Linux/5.15.0-1034-azure
|
Release files / prompt_matrix-0.1.4-py3-none-any.whl
| Download URL | prompt_matrix-0.1.4-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
53fc5822897f820c9d6776f3f4994364d00daffd4b2cfc9dfab6df8306b2e16d
|
|
BLAKE2b-256 checksum How to use checksums |
6fc06d8b5b1ca423d4e92a3f9075902f719c41d72a7f188a52cb3d828fd8611b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.1 CPython/3.11.2 Linux/5.15.0-1034-azure
|
Security for prompt-matrix 0.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.