O Promp Matrix é um pequeno pacote python que expande uma string que especifica uma matriz imediata. Por exemplo, a corda "The <dog|cat> in the hat"
se expande para a lista ["The dog in the hat", "The cat in the hat"]
.
O argumento motivador deste pacote é comparar os efeitos de diferentes instruções nos sistemas de geração de texto e imagem, como difusão estável e GPT-3.
Uma string prompt pode conter várias alternâncias. Por exemplo, "The <dog|cat> in the <cardigan|hat>"
produz uma lista das quatro cordas "The dog in the cardigan"
, "The dog in the hat"
, "The cat in the cardigan"
e "The cat in the hat"
.
Uma sequência rápida pode conter alternâncias aninhadas. Por exemplo, "The <<small|large> dog|cat>"
produz as cordas "The small dog"
, "The large do"
e "The cat"
.
Suportes []
incluem elementos opcionais. Por exemplo, "The [small] cat"
é equivalente a "The <small,> cat"
, e ambos produzem as cordas "The small cat"
e "The cat"
.
Os caracteres especiais <>{}|
pode ser substituído por strings diferentes, ou desativado, especificando None
ou a string vazia.
Nota : A disjunção é delimitada pelos colchetes anexos, se houver.
"The dog|cat in the cardigan|hat"
gera as três cordas"The dog"
,"cat in the gardigan"
e"hat"
. Isso contrasta com alguns outros sistemas, esse escopo uma disjunção do texto delimitada pelo espaço em branco ao redor.
$ pip install prompt-matrix
A Matrix Prompt fornece duas funções para expandir uma matriz imediata: expand
e iterexpand
. Ambos pegam uma string que especifica uma matriz rápida.
expand
retorna uma matriz de strings com todas as combinações possíveis dos elementos da matriz imediata.
import prompt_matrix
prompt = "<hi|hello> <there|you>"
expansion = prompt_matrix . expand ( prompt )
print ( expansion ) # ["hi there", "hi you", "hello there", "hello you"]
iterexpand
retorna um gerador que produz as expansões uma a uma.
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"
Exemplo 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"]
Exemplo 2: Usando várias alternâncias
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"]
Exemplo 3: Usando colchetes aninhados
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"]
Exemplo 4: Usando suportes e 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"]
A versão JavaScript desta biblioteca está em https://github.com/osteele/prompt-matrix.py.
Mit