AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


LlamaIndex中的Prompt engineering(提示工程)
发布日期:2024-04-26 11:55:15 浏览次数: 2088


Prompt(提示)是赋予 LLM 表达能力的基本输入。LlamaIndex 使用提示来构建索引、执行插入、在查询期间检索、合成最终答案。

LlamaIndex 提供了一组开箱即用的默认提示模板:

https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/prompts/default_prompts.py

另外,这里还有一些专门针对 gpt-3.5-turbo 等聊天模型编写和使用的提示

https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/prompts/chat_prompts.py

自定义提示

用户还可以提供自己的提示模板来进一步定制框架的行为。自定义的最佳方法是从上面的链接复制默认提示,然后在其基础上进行修改:

from llama_index.core import PromptTemplate
template = ("We have provided context information below. \n""---------------------\n""{context_str}""\n---------------------\n""Given this information, please answer the question: {query_str}\n")qa_template = PromptTemplate(template)
# 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=...)

你甚至可以直接将英文提示翻译成中文,然后使用你喜欢的中文 LLM。

注意!不要更改 {context_str} 和 {query_str} 这两个字符串。

我们也可以自定义聊天消息的模板:

from llama_index.core import ChatPromptTemplatefrom llama_index.core.llms import ChatMessage, MessageRole
message_templates = [ChatMessage(content="You are an expert system.", role=MessageRole.SYSTEM),ChatMessage(content="Generate a short story about {topic}",role=MessageRole.USER,),]chat_template = ChatPromptTemplate(message_templates=message_templates)
# you can create message prompts (for chat API)messages = chat_template.format_messages(topic=...)
# or easily convert to text prompt (for completion API)prompt = chat_template.format(topic=...)


获取和设定自定义提示

最常用的提示模板如下:

  • text_qa_template:用于从检索到的节点中获取查询的初始答案。

  • refine_template:用户当检索到的文本不适合使用 response_mode =“compact”(默认)的单个LLM调用时,或者当使用 response_mode =“refine” 检索多个节点时。第一个查询的答案将作为现有答案插入,LLM 必须根据新上下文更新或重复现有答案。


我们可以在 LlamaIndex 中的许多模块上调用 get_prompts 来获取模块和嵌套子模块中使用的提示列表:

query_engine = index.as_query_engine(response_mode="compact")prompts_dict = query_engine.get_prompts()print(list(prompts_dict.keys()))

输出如下:

['response_synthesizer:text_qa_template', 'response_synthesizer:refine_template']

注意,每个提示都有自己所属的子模块作为命名空间。

我们可以使用 update_prompts 函数根据键更新提示

query_engine.update_prompts({"response_synthesizer:text_qa_template": qa_template})


更改查询引擎中的提示

对于查询引擎,我们还可以在查询时直接传入自定义提示

  1. 通过高级 API:

query_engine = index.as_query_engine(text_qa_template=custom_qa_prompt, refine_template=custom_refine_prompt)

2. 通过低级 API:

retriever = index.as_retriever()synth = get_response_synthesizer(text_qa_template=custom_qa_prompt, refine_template=custom_refine_prompt)query_engine = RetrieverQueryEngine(retriever, response_synthesizer)

上面的两种方法是等效的,其中 1 本质上是 2 的语法糖,并隐藏了潜在的复杂性。我们可以使用 1 快速修改一些常用参数,并使用 2 进行更精细的控制。

更改建立索引时使用的提示

最常用的 VectorStoreIndex 和 SummaryIndex 在构建过程中不使用任何 Prompt。

但是有的索引会在构建过程中使用不同的提示。例如,TreeIndex 使用摘要提示来分层汇总节点,KeywordTableIndex 使用关键字提取提示来提取关键字。

有两种等效的方法可以覆盖默认提示:

index = TreeIndex(nodes, summary_template=custom_prompt)


index = TreeIndex.from_documents(docs, summary_template=custom_prompt)


一些高级提示技巧

部分格式化,先填充一部分变量,其余变量以后再填充:

from llama_index.core import PromptTemplate
prompt_tmpl_str = "{foo} {bar}"prompt_tmpl = PromptTemplate(prompt_tmpl_str)partial_prompt_tmpl = prompt_tmpl.partial_format(foo="abc")
fmt_str = partial_prompt_tmpl.format(bar="def")

模板变量映射

LlamaIndex 提示抽象通常需要某些键(如果你熟悉 Python 字符串格式化的话)。例如,text_qa_prompt需要 context_str 作为上下文,query_str 作为用户查询。

如果我们在其他地方发现了很好的提示模板,但是模板中的键不一样,要么我们手工替换这些键,要么定义 template_var_mappings:

template_var_mappings = {"context_str": "my_context", "query_str": "my_query"}
prompt_tmpl = PromptTemplate(qa_prompt_tmpl_str, template_var_mappings=template_var_mappings)

函数映射

将函数作为模板变量而不是固定值传递。这是相当先进和强大的,允许我们进行动态提示。

下面是重新格式化 context_str 的示例:

def format_context_fn(**kwargs):# format context with bullet pointscontext_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})
prompt_tmpl.format(context_str="context", query_str="query")



53AI,企业落地应用大模型首选服务商

产品:大模型应用平台+智能体定制开发+落地咨询服务

承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

与创始人交个朋友

回到顶部

 
扫码咨询