Invite Matrix est un petit package Python qui développe une chaîne qui spécifie une matrice rapide. Par exemple, la chaîne "The <dog|cat> in the hat"
s'étend à la liste ["The dog in the hat", "The cat in the hat"]
.
Le cas de motivation de ce package est de comparer les effets de différentes invites dans les systèmes de génération de texte et d'image tels que la diffusion stable et le GPT-3.
Une chaîne d'invite peut contenir plusieurs alternations. Par exemple, "The <dog|cat> in the <cardigan|hat>"
produit une liste des quatre cordes "The dog in the cardigan"
, "The dog in the hat"
, "The cat in the cardigan"
et "The cat in the hat"
.
Une chaîne invite peut contenir des alternances imbriquées. Par exemple, "The <<small|large> dog|cat>"
produit les cordes "The small dog"
, "The large do"
et "The cat"
.
Supports []
fermer les éléments facultatifs. Par exemple, "The [small] cat"
est équivalent à "The <small,> cat"
, et tous deux produisent les cordes "The small cat"
et "The cat"
.
Les caractères spéciaux <>{}|
Peut être remplacé par différentes chaînes ou désactivé en spécifiant None
ou la chaîne vide.
Remarque : La disjonction est délimitée par les supports enfermés, le cas échéant.
"The dog|cat in the cardigan|hat"
génère les trois cordes"The dog"
,"cat in the gardigan"
et"hat"
. Cela contraste avec certains autres systèmes, qui étendent une disjonction au texte délimité par les espaces blancs environnants.
$ pip install prompt-matrix
Invite Matrix fournit deux fonctions pour élargir une matrice rapide: expand
et iterexpand
. Les deux prennent une chaîne qui spécifie une matrice rapide.
expand
Renvoie un tableau de chaînes avec toutes les combinaisons possibles des éléments de la matrice rapide.
import prompt_matrix
prompt = "<hi|hello> <there|you>"
expansion = prompt_matrix . expand ( prompt )
print ( expansion ) # ["hi there", "hi you", "hello there", "hello you"]
iterexpand
renvoie un générateur qui donne les extensions un par un.
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"
Exemple 1: utilisation de base
import prompt_matrix
prompt_matrix . expand ( "The <dog|cat> in the hat" )
# ->
# ["The dog in the hat",
# "The cat in the hat"]
Exemple 2: Utilisation de plusieurs 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"]
Exemple 3: Utilisation de supports imbriqués
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"]
Exemple 4: Utilisation de supports et séparateurs personnalisés
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"]
La version JavaScript de cette bibliothèque est sur https://github.com/osteele/prompt-matrix.py.
Mit