生成提示变体
大型语言模型对其接收到的提示方式非常敏感。当提示不正确时,即使它们拥有响应提示所需的信息或能力,性能也可能大打折扣。提示挖掘旨在帮助我们发现语料库中更频繁出现的更好格式。
以下是论文中提供的一些挖掘到的补全示例。
手动提示 | 挖掘到的提示 |
---|---|
x 隶属于 y 宗教 | x 改信 y |
x 的总部位于 y | x 总部设在 y |
x 在 y 去世 | x 在其位于 y 的家中去世 |
x 由音乐厂牌 y 代表 | x 为 y 录制唱片 |
x 是 y 的子类 | x 是 y 的一种类型 |
原始论文利用大型维基百科语料库,通过查看提示的中间词并解析句子内的依赖关系来自动提取提示模板。我们在此提出一种更轻量级的方法,以便使用
instructor
实现类似的结果。
我们可以使用 instructor
实现提示挖掘,如下所示。
from pydantic import BaseModel, Field
import instructor
from openai import OpenAI
class PromptTemplate(BaseModel):
prompt_template: str = Field(
description=(
"""
A template that has the subject and object that we
want to extract from the prompt replaced with a
single placeholder of {subject} and {object}.
Rephrase the prompt if necessary to make it more
concise and easier to understand
"""
),
)
client = instructor.from_openai(OpenAI())
def generate_prompt_templates(prompt: str):
return client.chat.completions.create(
messages=[
{
"role": "system",
"content": (
"You are an expert prompt miner that excels at "
"generating prompt templates which are more "
"concise and easier to understand\n\nYou are "
"about to be passed a prompt to extract 3 new "
"prompt templates for"
),
},
{"role": "system", "content": prompt},
],
response_model=list[PromptTemplate],
temperature=0,
max_retries=3,
model="gpt-4o",
)
if __name__ == "__main__":
prompt = "France is the capital of Paris"
prompt_template = generate_prompt_templates(prompt)
for prompt in prompt_template:
print(prompt)
#> prompt_template='{subject} is the capital of {object}'
#> prompt_template='The capital of {object} is {subject}'
#> prompt_template="{object}'s capital is {subject}"