97 高级提示技巧:变量映射与函数映射

高级提示技巧:变量映射与函数映射

在处理大型语言模型(LLM)时,优化提示(prompt)是提升输出质量的关键。本文将介绍一些高级提示技巧,包括部分格式化、提示模板变量映射和提示函数映射,帮助你定义更自定义和表达性的提示,重用现有提示,并以更少的代码行数表达某些操作。

前置知识

在深入学习这些高级提示技巧之前,你需要了解以下基础知识:

  • Python编程:熟悉Python语言及其常用库。
  • 自然语言处理(NLP):了解基本的NLP概念和技术。
  • 大型语言模型(LLM):了解LLM的基本工作原理和常见应用。

安装与配置

首先,我们需要安装必要的库,并配置OpenAI API密钥。

%pip install llama-index-llms-openai

from llama_index.core import PromptTemplate
from llama_index.llms.openai import OpenAI

1. 部分格式化(Partial Formatting)

部分格式化(partial_format)允许你部分格式化提示,填充一些变量,同时保留其他变量稍后填充。这是一个方便的功能,这样你就不必在整个格式化过程中维护所有必需的提示变量,可以在它们进来时部分格式化。

这将创建提示模板的副本。

qa_prompt_tmpl_str = """\
Context information is below.
---------------------
{context_str}
---------------------
Given the context information and not prior knowledge, answer the query.
Please write the answer in the style of {tone_name}
Query: {query_str}
Answer: \
"""

prompt_tmpl = PromptTemplate(qa_prompt_tmpl_str)
partial_prompt_tmpl = prompt_tmpl.partial_format(tone_name="Shakespeare")
partial_prompt_tmpl.kwargs

输出:

{'tone_name': 'Shakespeare'}

格式化后的提示:

fmt_prompt = partial_prompt_tmpl.format(
    context_str="In this work, we develop and release Llama 2, a collection of pretrained and fine-tuned large language models (LLMs) ranging in scale from 7 billion to 70 billion parameters",
    query_str="How many params does llama 2 have",
)
print(fmt_prompt)

输出:

Context information is below.
---------------------
In this work, we develop and release Llama 2, a collection of pretrained and fine-tuned large language models (LLMs) ranging in scale from 7 billion to 70 billion parameters
---------------------
Given the context information and not prior knowledge, answer the query.
Please write the answer in the style of Shakespeare
Query: How many params does llama 2 have
Answer: 

2. 提示模板变量映射(Prompt Template Variable Mappings)

模板变量映射允许你指定从“预期”提示键(例如,用于响应合成的context_str和query_str)到模板中实际键的映射。这允许你重用现有的字符串模板,而不必烦人地更改模板变量。

# 注意:这里我们使用 `my_context` 和 `my_query` 作为模板变量

qa_prompt_tmpl_str = """\
Context information is below.
---------------------
{my_context}
---------------------
Given the context information and not prior knowledge, answer the query.
Query: {my_query}
Answer: \
"""

template_var_mappings = {"context_str": "my_context", "query_str": "my_query"}

prompt_tmpl = PromptTemplate(
    qa_prompt_tmpl_str, template_var_mappings=template_var_mappings
)
fmt_prompt = prompt_tmpl.format(
    context_str="In this work, we develop and release Llama 2, a collection of pretrained and fine-tuned large language models (LLMs) ranging in scale from 7 billion to 70 billion parameters",
    query_str="How many params does llama 2 have",
)
print(fmt_prompt)

输出:

Context information is below.
---------------------
In this work, we develop and release Llama 2, a collection of pretrained and fine-tuned large language models (LLMs) ranging in scale from 7 billion to 70 billion parameters
---------------------
Given the context information and not prior knowledge, answer the query.
Query: How many params does llama 2 have
Answer: 

3. 提示函数映射(Prompt Function Mappings)

你还可以将函数作为模板变量传递,而不是固定值。这允许你根据其他值动态注入某些值,在查询时依赖。

以下是一些基本示例。我们在提示工程指南中展示了更多高级示例(例如,少量示例)。

qa_prompt_tmpl_str = """\
Context information is below.
---------------------
{context_str}
---------------------
Given the context information and not prior knowledge, answer the query.
Query: {query_str}
Answer: \
"""

def format_context_fn(**kwargs):
    # 用项目符号格式化上下文
    context_list = kwargs["context_str"].split("\n\n")
    fmtted_context = "\n\n".join([f"- {c}" for c in context_list])
    return fmtted_context

prompt_tmpl = PromptTemplate(
    qa_prompt_tmpl_str, function_mappings={"context_str": format_context_fn}
)
context_str = """\
In this work, we develop and release Llama 2, a collection of pretrained and fine-tuned large language models (LLMs) ranging in scale from 7 billion to 70 billion parameters.

Our fine-tuned LLMs, called Llama 2-Chat, are optimized for dialogue use cases.

Our models outperform open-source chat models on most benchmarks we tested, and based on our human evaluations for helpfulness and safety, may be a suitable substitute for closed-source models.
"""

fmt_prompt = prompt_tmpl.format(
    context_str=context_str, query_str="How many params does llama 2 have"
)
print(fmt_prompt)

输出:

Context information is below.
---------------------
- In this work, we develop and release Llama 2, a collection of pretrained and fine-tuned large language models (LLMs) ranging in scale from 7 billion to 70 billion parameters.

- Our fine-tuned LLMs, called Llama 2-Chat, are optimized for dialogue use cases.

- Our models outperform open-source chat models on most benchmarks we tested, and based on our human evaluations for helpfulness and safety, may be a suitable substitute for closed-source models.

---------------------
Given the context information and not prior knowledge, answer the query.
Query: How many params does llama 2 have
Answer: 

总结

通过使用部分格式化、提示模板变量映射和提示函数映射,你可以显著提升LLM的输出质量。这些高级提示技巧允许你定义更自定义和表达性的提示,重用现有提示,并以更少的代码行数表达某些操作。希望本文能为你提供有价值的参考,让你在实际应用中取得更好的效果。

参考文献:

扩展阅读:

希望这篇博客能为你带来启发和帮助,让我们在编程的世界里,更加高效地驾驭数据和信息!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

需要重新演唱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值