提示矩陣是一個小型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。
麻省理工學院