提示矩阵是一个小型Python软件包,它扩展了一个指定提示矩阵的字符串。例如,字符串"The <dog|cat> in the hat"
将其扩展到列表["The dog in the hat", "The cat in the hat"]
。
该软件包的激励案例是比较文本和图像生成系统中不同提示的影响,例如稳定扩散和GPT-3。
提示字符串可能包含多个交替。例如, "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"
。
提示字符串可能包含嵌套交替。例如, "The <<small|large> dog|cat>"
生成弦"The small dog"
, "The large do"
和"The cat"
。
括号[]
包含可选元素。例如, "The [small] cat"
等同于"The <small,> cat"
,并且两者都产生"The small cat"
和"The cat"
。
特殊字符<>{}|
可以用不同的字符串替换,也可以通过指定None
或空字符串来替换。
注意:如果有的话,脱节是由封闭括号(如果有)界定的。
"cat in the gardigan"
"The dog|cat in the cardigan|hat"
"hat"
"The dog"
这与其他某些系统相反,这些系统范围范围与周围空格界定的文本划分。
$ pip install prompt-matrix
提示矩阵提供了两个用于扩展提示矩阵的功能: expand
和iterexpand
。两者都采用指定提示矩阵的字符串。
expand
返回一系列字符串,并具有提示矩阵元素的所有可能组合。
import prompt_matrix
prompt = "<hi|hello> <there|you>"
expansion = prompt_matrix . expand ( prompt )
print ( expansion ) # ["hi there", "hi you", "hello there", "hello you"]
iterexpand
返回一个发电机,该发电机一一产生扩展。
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"
示例1:基本用法
import prompt_matrix
prompt_matrix . expand ( "The <dog|cat> in the hat" )
# ->
# ["The dog in the hat",
# "The cat in the hat"]
示例2:使用多个交替
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"]
示例3:使用嵌套支架
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"]
示例4:使用自定义支架和分离器
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"]
此库的JavaScript版本位于https://github.com/osteele/prompt-matrix.py。
麻省理工学院