数据STUDIO

Promptify: 用于 LLM 提示管理的 Python 库

Image

Image

Promptify 这个 Python 库拥有提示器、LLM 集成和管道,只需几行代码就能完成数小时的提示调整工作。

在本文中,云朵君将与大家分享 Promptify 为何能让处理 NLP 的人如沐春风,并通过代码示例进行简单解释。

Image
Image

Promptify 是什么?

你有一大段文字,比如一份医学报告或一个故事片段,你需要提取关键细节、对主题进行分类或提出其他一般类问题。通常情况下,你需要花费大量时间为 GPT 或 PaLM 等LLM精心设计近乎完美的提示。

Promptify 改变了这一切。

它是一个Python库,将prompter(用于制作提示)、LLM(如OpenAI的模型)和pipeline配对起来,以简化NLP任务。

今天列举的几个案例,Promptify 都能让人感觉毫不费力--不是因为你在作弊,而是因为这是一种智能编码。

我为什么喜欢它?

答案很简单: 预置模板(或自定义模板)意味着你不需要成为prompt向导。

灵活: 可处理命名实体识别(NER)、分类或问题生成等任务。

与你现有的系统一起工作: 支持 Python 3.7 以上版本、OpenAI 0.25 以上版本和多个 LLM。

接下来云朵君和大家一起看一些示例,以便去深入理解其中奥妙。

设置 Promptify

安装 Promptify 非常简单。如在 Python 3.8 设置上运行了它:

pip3 install promptify

想要最新版本?从 GitHub 获取:

pip3 install git+https://github.com/promptslab/Promptify.git

安装完成后,就可以开始大展拳脚了。你需要一个 LLM 的 API 密钥(我用的是 OpenAI 的),稍后再详述。

Promptify 实验

当我第一次试用 Promptify 时,我持怀疑态度--它真的能简化我在 NLP 方面的困难吗?我用三个任务对它进行了测试:从医疗记录中提取实体、对健康状况进行分类以及从故事中生成问题。结果如下。

示例 1:提取医疗实体(NER)

假设你在某家医疗保健单位工作,你需要解析一份杂乱无章的病历:

The patient is a 93-year-old female with a medical history of chronic right hip pain, osteoporosis, hypertension, depression, and chronic atrial fibrillation admitted for evaluation and management of severe nausea and vomiting and urinary tract infection

翻译成中文:患者是一位 93 岁的女性,有慢性右髋关节疼痛、骨质疏松症、高血压、抑郁症和慢性心房颤动病史,因严重恶心、呕吐和尿路感染入院接受评估和治疗。

此时需要提取年龄、病情和症状。以下是 Promptify 如何做到这一点的:

from promptify import Prompter, OpenAI, Pipeline

# 患者记录
sentence = """The patient is a 93-year-old female with a medical history of chronic right hip pain, osteoporosis, hypertension, depression, and chronic atrial fibrillation admitted for evaluation and management of severe nausea and vomiting and urinary tract infection""""
# 设置 OpenAI(交换 API 密钥)
model = OpenAI("
your_api_key_here")
# 选择 NER 模板
prompter = Prompter('ner.jinja')
# 创建管道
pipe = Pipeline(prompter, model)
# 运行
result = pipe.fit(sentence, domain="
medical", labels=None)
# 检查结果
print(result)

结果

[
    {"E": "93-year-old", "T": "Age"},
    {"E": "chronic right hip pain", "T": "Medical Condition"},
    {"E": "osteoporosis", "T": "Medical Condition"},
    {"E": "hypertension", "T": "Medical Condition"},
    {"E": "depression", "T": "Medical Condition"},
    {"E": "chronic atrial fibrillation", "T": "Medical Condition"},
    {"E": "severe nausea and vomiting", "T": "Symptom"},
    {"E": "urinary tract infection", "T": "Medical Condition"},
    {"Branch": "Internal Medicine", "Group": "Geriatrics"}
]

这个结果着实不错。没一会儿的功夫,我们就得到了一个清晰的实体列表,而且还能将病例归类到内科和老年病科。这必能为你节省了单位数据库手动标记的时间。

示例 2:医疗条件分类

接下来,我们对同一记录中的病症进行标记,并按医学领域进行分组。以下是推荐使用的代码:

from promptify import OpenAI, Prompter

# 同一患者记录
sentence = """The patient is a 93-year-old female with a medical history of chronic right hip pain, osteoporosis, hypertension, depression, and chronic atrial fibrillation admitted for evaluation and management of severe nausea and vomiting and urinary tract infection""""
# 初始化模型
model = OpenAI("
your_api_key_here")
# 设置提示器
nlp_prompter = Prompter(model)
# 运行多标签分类
result = nlp_prompter.fit('multilabel_classification.jinja', domain='medical', text_input=sentence)
# 查看结果
print(result)4

输出

[
    {
'1': 'Medicine',
'2': 'Osteoporosis',
'3': 'Hypertension',
'4': 'Depression',
'5': 'Atrial fibrillation',
'6': 'Nausea and vomiting',
'7': 'Urinary tract infection',
'branch': 'Health',
'group': 'Clinical medicine',
'main class': 'Health'
    }
]

这是整理数据的救星。每种情况都有一个清晰的标签,更广泛的 Health 和 Clinical medicine 标签帮助团队对记录进行分类以便分析。老实说,当我看到输出结果如此清晰时,我非常震惊。

示例3:生成故事问题

为了更进一步探索该库的潜在能力,我尝试用 Promptify 来完成一项创意任务。给它输入了《爱丽丝漫游奇境记》中的一句话:

The rabbit hole went straight on like a tunnel for some way, and then dipped suddenly down, so suddenly that Alice had not a moment to think about stopping herself before she found herself falling down a very deep well.

翻译成中文:兔子洞像隧道一样直走了一段路,然后突然向下倾斜,如此突然,以至于爱丽丝在发现自己掉进一口很深的井里之前,根本来不及考虑如何阻止自己。

假设我们需要为读书俱乐部的测验出题。下面是推荐出的题目:

from promptify import OpenAI, Prompter
# 故事片段
sentence = """The rabbit hole went straight on like a tunnel for some way, and then dipped suddenly down, so suddenly that Alice had not a moment to think about stopping herself before she found herself falling down a very deep well."""
# 设置模型
model = OpenAI("your_api_key_here")
# 挑选问答模板
nlp_prompter = Prompter(model)
# 生成问题
result = nlp_prompter.fit('qa_gen.jinja', domain='story_writing', text_input=sentence)
# 检查输出
print(result)

输出

[
    {'A': 'Alice found herself falling down a very deep well.', 'Q': 'What happened when Alice went down the rabbit-hole?'},
    {'A': 'Very deep.', 'Q': 'How deep was the well?'},
    {'A': 'No, she did not have a moment to think.', 'Q': 'Did Alice have time to think about stopping herself?'},
    {'A': 'It went straight on like a tunnel.', 'Q': 'What direction did the rabbit-hole go?'},
    {'A': 'No, she did not expect it.', 'Q': 'Did Alice expect to fall down a well?'}
]

如果你在一个读书俱乐部中使用了这些问题,我想极有可能会引发了一场精彩的讨论!我们使用Promptify 便能毫不费力地将一句话变成了一个小测验。

为什么 Promptify 能赢得我的青睐?

这么多年来,云朵君写过很多年关于科技工具的文章。而 Promptify 吸引我的原因是,它就像一个智能助手,能处理无聊的事情。以下是它值得你使用的原因:

节省脑力: 几行代码就能取代数小时的提示工程。

适应你: 使用内置模板,或针对特定任务调整自己的模板。

用途广泛: 医疗数据?创意写作?它都能帮你搞定。

与LLM完美契合: 在 OpenAI、Hugging Face或其他系统之间切换自由。

准备好试用 Promptify了吗?以下是我整理速成指南,看到这里,能否点个赞呢?🥰

  • 使用 pip3 install promptify 安装。
  • 获取一个 API 密钥(OpenAI 是一个很好的开始)。
  • 挑选一个模板,如 ner.jinja 或自己编写。
  • 使用 Pipeline 或 Prompter 处理文本。
  • 随心所欲地处理输出--它非常灵活。
🏴‍☠️宝藏级🏴‍☠️ 原创公众号『数据STUDIO』内容超级硬核。公众号以Python为核心语言,垂直于数据科学领域,包括可戳👉Python|MySQL|数据分析|数据可视化|机器学习与数据挖掘|爬虫等,从入门到进阶!

长按👇关注- 数据STUDIO -设为星标,干货速递ImageImage