提示#
概念#
提示是赋予 LLMs 表达能力的基础输入。LlamaIndex 使用提示来构建索引、执行插入、在查询期间执行遍历以及合成最终答案。
在构建 agentic 工作流时,构建和管理提示是开发过程中的关键部分。LlamaIndex 提供了一种灵活而强大的方式来管理提示,并以多种方式使用它们。
RichPromptTemplate
- 用于构建带有变量和逻辑的 jinja 风格提示的最新样式PromptTemplate
- 用于构建带有单个 f-string 提示的旧样式简单模板ChatPromptTemplate
- 用于构建带有消息和 f-strings 聊天提示的旧样式简单模板
LlamaIndex 使用一组默认提示模板,开箱即用效果良好。
此外,还有一些专门为聊天模型(如 gpt-3.5-turbo
)编写和使用的提示,请参见此处。
用户还可以提供自己的提示模板,以进一步自定义框架的行为。最佳自定义方法是复制上面链接中的默认提示,并以此作为任何修改的基础。
使用模式#
使用提示很简单。下面是如何使用 RichPromptTemplate
构建 jinja 风格提示模板的示例
from llama_index.core.prompts import RichPromptTemplate
template_str = """We have provided context information below.
---------------------
{{ context_str }}
---------------------
Given this information, please answer the question: {{ query_str }}
"""
qa_template = RichPromptTemplate(template_str)
# you can create text prompt (for completion API)
prompt = qa_template.format(context_str=..., query_str=...)
# or easily convert to message prompts (for chat API)
messages = qa_template.format_messages(context_str=..., query_str=...)
有关充分利用 RichPromptTemplate
和其他提示模板的更多详细信息,请参阅我们的使用模式指南。
示例指南#
提示工程指南
简单自定义示例
实验性