docs/examples/prompts/prompt_mixin.ipynb
<a href="https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/prompts/prompt_mixin.ipynb" target="_parent"></a>
LlamaIndex contains a variety of higher-level modules (query engines, response synthesizers, retrievers, etc.), many of which make LLM calls + use prompt templates.
This guide shows how you can 1) access the set of prompts for any module (including nested) with get_prompts, and 2) update these prompts easily with update_prompts.
If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙.
%pip install llama-index
import os
os.environ["OPENAI_API_KEY"] = "sk-..."
from llama_index.core import Settings
from llama_index.llms.openai import OpenAI
from llama_index.embeddings.openai import OpenAIEmbedding
# Set the default embedding model and LLM
Settings.embed_model = OpenAIEmbedding(model_name="text-embedding-3-small")
Settings.llm = OpenAI(model="gpt-4o-mini")
Here we build a vector index over a toy dataset (PG's essay), and access the query engine.
The query engine is a simple RAG pipeline consisting of top-k retrieval + LLM synthesis.
Download Data
!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
from llama_index.core import SimpleDirectoryReader
# load documents
documents = SimpleDirectoryReader("./data/paul_graham/").load_data()
from llama_index.core import VectorStoreIndex
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine(response_mode="tree_summarize")
from IPython.display import Markdown, display
# define prompt viewing function
def display_prompt_dict(prompts_dict):
for k, p in prompts_dict.items():
text_md = f"**Prompt Key**: {k}
" f"**Text:**
"
display(Markdown(text_md))
print(p.get_template())
display(Markdown("
"))
Here we get the prompts from the query engine. Note that all prompts are returned, including ones used in sub-modules in the query engine. This allows you to centralize a view of these prompts!
prompts_dict = query_engine.get_prompts()
display_prompt_dict(prompts_dict)
get_prompts on Response SynthesizerYou can also call get_prompts on the underlying response synthesizer, where you'll see the same list.
prompts_dict = query_engine.response_synthesizer.get_prompts()
display_prompt_dict(prompts_dict)
get_prompts with a different response synthesis strategyHere we try the default compact method.
We'll see that the set of templates used are different; a QA template and a refine template.
# set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine(response_mode="compact")
prompts_dict = query_engine.get_prompts()
display_prompt_dict(prompts_dict)
response = query_engine.query("What did the author do growing up?")
print(str(response))
You can also update/customize the prompts with the update_prompts function. Pass in arg values with the keys equal to the keys you see in the prompt dictionary.
Here we'll change the summary prompt to use Shakespeare.
from llama_index.core import PromptTemplate
# reset
query_engine = index.as_query_engine(response_mode="tree_summarize")
# shakespeare!
new_summary_tmpl_str = (
"Context information is below.\n"
"---------------------\n"
"{context_str}\n"
"---------------------\n"
"Given the context information and not prior knowledge, "
"answer the query in the style of a Shakespeare play.\n"
"Query: {query_str}\n"
"Answer: "
)
new_summary_tmpl = PromptTemplate(new_summary_tmpl_str)
query_engine.update_prompts(
{"response_synthesizer:summary_template": new_summary_tmpl}
)
prompts_dict = query_engine.get_prompts()
display_prompt_dict(prompts_dict)
response = query_engine.query("What did the author do growing up?")
print(str(response))
Here we take a look at some other modules: query engines, routers/selectors, evaluators, and others.
from llama_index.core.agent.workflow import ReActAgent
from llama_index.core.selectors import LLMMultiSelector
from llama_index.core.evaluation import FaithfulnessEvaluator, DatasetGenerator
from llama_index.core.postprocessor import LLMRerank
agent = ReActAgent(tools=[])
prompts_dict = agent.get_prompts()
display_prompt_dict(prompts_dict)
flare_query_engine = FLAREInstructQueryEngine(query_engine)
prompts_dict = flare_query_engine.get_prompts()
display_prompt_dict(prompts_dict)
from llama_index.core.selectors import LLMSingleSelector
selector = LLMSingleSelector.from_defaults()
prompts_dict = selector.get_prompts()
display_prompt_dict(prompts_dict)
evaluator = FaithfulnessEvaluator()
prompts_dict = evaluator.get_prompts()
display_prompt_dict(prompts_dict)
dataset_generator = DatasetGenerator.from_documents(documents)
prompts_dict = dataset_generator.get_prompts()
display_prompt_dict(prompts_dict)
llm_rerank = LLMRerank()
prompts_dict = dataset_generator.get_prompts()
display_prompt_dict(prompts_dict)