La matriz rápida es un pequeño paquete de Python que expande una cadena que especifica una matriz rápida. Por ejemplo, la cadena "The <dog|cat> in the hat"
se expande a la lista ["The dog in the hat", "The cat in the hat"]
.
El caso motivador para este paquete es comparar los efectos de las diferentes indicaciones en los sistemas de generación de texto e imágenes, como la difusión estable y GPT-3.
Una cadena rápida puede contener múltiples alternancias. Por ejemplo, "The <dog|cat> in the <cardigan|hat>"
produce una lista de las cuatro cuerdas "The dog in the cardigan"
, "The dog in the hat"
, "The cat in the cardigan"
, y "The cat in the hat"
.
Una cadena rápida puede contener alternancias anidadas. Por ejemplo, "The <<small|large> dog|cat>"
produce las cuerdas "The small dog"
, "The large do"
y "The cat"
.
Los soportes []
encierran elementos opcionales. Por ejemplo, "The [small] cat"
es equivalente a "The <small,> cat"
, y ambos producen las cuerdas "The small cat"
y "The cat"
.
Los caracteres especiales <>{}|
puede ser reemplazado por diferentes cadenas, o deshabilitado especificando None
o la cadena vacía.
Nota : La disyunción está limitada por los soportes de encerrado, si los hay.
"The dog|cat in the cardigan|hat"
genera las tres cuerdas"The dog"
,"cat in the gardigan"
y"hat"
. Esto contrasta con otros sistemas, que alcanzan una disyunción al texto delimitado por el espacio en blanco circundante.
$ pip install prompt-matrix
La matriz rápida proporciona dos funciones para expandir una matriz rápida: expand
y iterexpand
. Ambos toman una cadena que especifica una matriz rápida.
expand
devuelve una matriz de cadenas con todas las combinaciones posibles de los elementos de matriz de inmediato.
import prompt_matrix
prompt = "<hi|hello> <there|you>"
expansion = prompt_matrix . expand ( prompt )
print ( expansion ) # ["hi there", "hi you", "hello there", "hello you"]
iterexpand
devuelve un generador que produce las expansiones una por una.
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"
Ejemplo 1: Uso básico
import prompt_matrix
prompt_matrix . expand ( "The <dog|cat> in the hat" )
# ->
# ["The dog in the hat",
# "The cat in the hat"]
Ejemplo 2: Uso de múltiples alternancias
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"]
Ejemplo 3: Uso de soportes anidados
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"]
Ejemplo 4: Uso de soportes y separadores personalizados
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 versión JavaScript de esta biblioteca está en https://github.com/osteele/prompt-matrix.py.
MIT