高级提示技巧(变量映射、函数)¶
在这个笔记本中,我们将展示一些高级提示技巧。这些特性允许您定义更自定义/更具表达力的提示,重用现有的提示,并且可以用更少的代码行来表达某些操作。
我们将展示以下功能
- 部分格式化
- 提示模板变量映射
- 提示函数映射
- 动态少样本示例
In [ ]
已复制!
%pip install llama-index-llms-openai
%pip install llama-index-llms-openai
1. 部分格式化¶
部分格式化(partial_format
)允许您对提示进行部分格式化,填充一些变量,而将其余变量留待以后填充。
这是一个方便的功能,您无需将所有必需的提示变量一直维护到 format
,可以在变量传入时进行部分格式化。
这将创建提示模板的副本。
In [ ]
已复制!
from llama_index.core.prompts import RichPromptTemplate
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 = RichPromptTemplate(qa_prompt_tmpl_str)
from llama_index.core.prompts import RichPromptTemplate 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 = RichPromptTemplate(qa_prompt_tmpl_str)
In [ ]
已复制!
partial_prompt_tmpl = prompt_tmpl.partial_format(tone_name="Shakespeare")
partial_prompt_tmpl = prompt_tmpl.partial_format(tone_name="Shakespeare")
In [ ]
已复制!
partial_prompt_tmpl.kwargs
partial_prompt_tmpl.kwargs
Out[ ]
{'tone_name': 'Shakespeare'}
In [ ]
已复制!
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)
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:
我们也可以使用 format_messages
将提示格式化为 ChatMessage
对象。
In [ ]
已复制!
fmt_prompt = partial_prompt_tmpl.format_messages(
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)
fmt_prompt = partial_prompt_tmpl.format_messages( 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)
[ChatMessage(role=<MessageRole.USER: 'user'>, additional_kwargs={}, blocks=[TextBlock(block_type='text', text='Context information is below.'), TextBlock(block_type='text', text='---------------------'), TextBlock(block_type='text', text='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'), TextBlock(block_type='text', text='---------------------'), TextBlock(block_type='text', text='Given the context information and not prior knowledge, answer the query.'), TextBlock(block_type='text', text='Please write the answer in the style of Shakespeare'), TextBlock(block_type='text', text='Query: How many params does llama 2 have'), TextBlock(block_type='text', text='Answer:')])]
2. 提示模板变量映射¶
模板变量映射允许您指定从“预期”提示键(例如用于响应合成的 context_str
和 query_str
)到模板中实际键的映射。
这使您可以重用现有的字符串模板,而无需烦恼地更改模板变量。
In [ ]
已复制!
from llama_index.core.prompts import RichPromptTemplate
# NOTE: here notice we use `my_context` and `my_query` as template variables
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 = RichPromptTemplate(
qa_prompt_tmpl_str, template_var_mappings=template_var_mappings
)
from llama_index.core.prompts import RichPromptTemplate # NOTE: here notice we use `my_context` and `my_query` as template variables 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 = RichPromptTemplate( qa_prompt_tmpl_str, template_var_mappings=template_var_mappings )
In [ ]
已复制!
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)
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. 提示函数映射¶
您还可以传入函数作为模板变量,而不是固定值。
这允许您在查询时动态注入某些值,这些值依赖于其他值。
以下是一些基本示例。我们在 RAG 的提示工程指南中展示了更高级的示例(例如少样本示例)。
In [ ]
已复制!
from llama_index.core.prompts import RichPromptTemplate
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):
# format context with bullet points
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 = RichPromptTemplate(
qa_prompt_tmpl_str, function_mappings={"context_str": format_context_fn}
)
from llama_index.core.prompts import RichPromptTemplate 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): # format context with bullet points 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 = RichPromptTemplate( qa_prompt_tmpl_str, function_mappings={"context_str": format_context_fn} )
In [ ]
已复制!
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_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:
4. 动态少样本示例¶
利用函数映射,您还可以根据其他提示变量动态注入少样本示例。
下面是一个示例,它使用向量存储根据查询动态注入少样本的文本到 SQL 示例。
首先,让我们定义一个文本到 SQL 的提示模板。
In [ ]
已复制!
text_to_sql_prompt_tmpl_str = """\
You are a SQL expert. You are given a natural language query, and your job is to convert it into a SQL query.
Here are some examples of how you should convert natural language to SQL:
<examples>
{{ examples }}
</examples>
Now it's your turn.
Query: {{ query_str }}
SQL:
"""
text_to_sql_prompt_tmpl_str = """\ You are a SQL expert. You are given a natural language query, and your job is to convert it into a SQL query. Here are some examples of how you should convert natural language to SQL{{ examples }} Now it's your turn. Query: {{ query_str }} SQL: """
给定此提示模板,让我们定义并索引一些少样本的文本到 SQL 示例。
In [ ]
已复制!
import os
os.environ["OPENAI_API_KEY"] = "sk-..."
import os os.environ["OPENAI_API_KEY"] = "sk-..."
In [ ]
已复制!
from llama_index.core import Settings, VectorStoreIndex
from llama_index.core.schema import TextNode
from llama_index.llms.openai import OpenAI
from llama_index.embeddings.openai import OpenAIEmbedding
# Set global default LLM and embed model
Settings.llm = OpenAI(model="gpt-4o-mini")
Settings.embed_model = OpenAIEmbedding(model="text-embedding-3-small")
# Setup few-shot examples
example_nodes = [
TextNode(
text="Query: How many params does llama 2 have?\nSQL: SELECT COUNT(*) FROM llama_2_params;"
),
TextNode(
text="Query: How many layers does llama 2 have?\nSQL: SELECT COUNT(*) FROM llama_2_layers;"
),
]
# Create index
index = VectorStoreIndex(nodes=example_nodes)
# Create retriever
retriever = index.as_retriever(similarity_top_k=1)
from llama_index.core import Settings, VectorStoreIndex from llama_index.core.schema import TextNode from llama_index.llms.openai import OpenAI from llama_index.embeddings.openai import OpenAIEmbedding # Set global default LLM and embed model Settings.llm = OpenAI(model="gpt-4o-mini") Settings.embed_model = OpenAIEmbedding(model="text-embedding-3-small") # Setup few-shot examples example_nodes = [ TextNode( text="Query: How many params does llama 2 have?\nSQL: SELECT COUNT(*) FROM llama_2_params;" ), TextNode( text="Query: How many layers does llama 2 have?\nSQL: SELECT COUNT(*) FROM llama_2_layers;" ), ] # Create index index = VectorStoreIndex(nodes=example_nodes) # Create retriever retriever = index.as_retriever(similarity_top_k=1)
有了我们的检索器,我们可以创建带有函数映射的提示模板,以便根据查询动态注入少样本示例。
In [ ]
已复制!
from llama_index.core.prompts import RichPromptTemplate
def get_examples_fn(**kwargs):
query = kwargs["query_str"]
examples = retriever.retrieve(query)
return "\n\n".join(node.text for node in examples)
prompt_tmpl = RichPromptTemplate(
text_to_sql_prompt_tmpl_str,
function_mappings={"examples": get_examples_fn},
)
from llama_index.core.prompts import RichPromptTemplate def get_examples_fn(**kwargs): query = kwargs["query_str"] examples = retriever.retrieve(query) return "\n\n".join(node.text for node in examples) prompt_tmpl = RichPromptTemplate( text_to_sql_prompt_tmpl_str, function_mappings={"examples": get_examples_fn}, )
In [ ]
已复制!
prompt = prompt_tmpl.format(
query_str="What are the number of parameters in the llama 2 model?"
)
print(prompt)
prompt = prompt_tmpl.format( query_str="What are the number of parameters in the llama 2 model?" ) print(prompt)
You are a SQL expert. You are given a natural language query, and your job is to convert it into a SQL query. Here are some examples of how you should convert natural language to SQL: <examples> Query: How many params does llama 2 have? SQL: SELECT COUNT(*) FROM llama_2_params; </examples> Now it's your turn. Query: What are the number of parameters in the llama 2 model? SQL:
In [ ]
已复制!
response = Settings.llm.complete(prompt)
print(response.text)
response = Settings.llm.complete(prompt) print(response.text)
SELECT COUNT(*) FROM llama_2_params;