跳转到主要内容

将提示矩阵字符串展开成提示列表。

项目描述

Prompt Matrix

Python PyPI License: MIT

一个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

用法

提示矩阵提供了两个用于展开提示矩阵的函数:expanditerexpand。这两个函数都接受一个指定提示矩阵的字符串。

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 (4.7 kB 查看散列)

上传时间

构建分布

prompt_matrix-0.1.4-py3-none-any.whl (5.9 kB 查看散列)

上传时间 Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面