将提示矩阵字符串展开成提示列表。
项目描述
Prompt Matrix
一个Python包,用于展开提示矩阵字符串。例如,字符串 "The <dog|cat> in the hat"
展开成列表 ["The dog in the hat", "The cat in the hat"]
。
该包的动机案例是对比文本和图像生成系统(如Stable Diffusion和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
或空字符串来禁用。
注意:如果存在,析取操作被括号所限定。
"The dog|cat in the cardigan|hat"
生成三个字符串:"The dog"
、"cat in the gardigan"
和"hat"
。这与某些其他系统不同,这些系统将析取操作的范围限定在由周围空白字符分隔的文本中。
安装
$ 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"]
许可证
麻省理工学院
项目详情
下载文件
下载您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分布
构建分布
prompt_matrix-0.1.4.tar.gz 的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 07350cb54aaca60e2fb8106e463053cd97c606064cb31e0a865a4a8b7e8d22e9 |
|
MD5 | ebe7a18205f675007e48e0282001bd23 |
|
BLAKE2b-256 | 40c3c2131ef06b660b61d053ffedf53ec71c87afbe34e3580a031fc1a07f8280 |
prompt_matrix-0.1.4-py3-none-any.whl 的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 53fc5822897f820c9d6776f3f4994364d00daffd4b2cfc9dfab6df8306b2e16d |
|
MD5 | 308db3e3ad967ec4357a1ca684908c5d |
|
BLAKE2b-256 | 6fc06d8b5b1ca423d4e92a3f9075902f719c41d72a7f188a52cb3d828fd8611b |