Query Transformations#
LlamaIndex allows you to perform query transformations over your index structures. Query transformations are modules that will convert a query into another query. They can be single-step, as in the transformation is run once before the query is executed against an index.
They can also be multi-step, as in:
- The query is transformed, executed against an index,
- The response is retrieved.
- Subsequent queries are transformed/executed in a sequential fashion.
We list some of our query transformations in more detail below.
Use Cases#
Query transformations have multiple use cases:
- Transforming an initial query into a form that can be more easily embedded (e.g. HyDE)
- Transforming an initial query into a subquestion that can be more easily answered from the data (single-step query decomposition)
- Breaking an initial query into multiple subquestions that can be more easily answered on their own. (multi-step query decomposition)
HyDE (Hypothetical Document Embeddings)#
HyDE is a technique where given a natural language query, a hypothetical document/answer is generated first. This hypothetical document is then used for embedding lookup rather than the raw query.
To use HyDE, an example code snippet is shown below.
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.indices.query.query_transform.base import (
HyDEQueryTransform,
)
from llama_index.core.query_engine import TransformQueryEngine
# load documents, build index
documents = SimpleDirectoryReader("../paul_graham_essay/data").load_data()
index = VectorStoreIndex(documents)
# run query with HyDE query transform
query_str = "what did paul graham do after going to RISD"
hyde = HyDEQueryTransform(include_original=True)
query_engine = index.as_query_engine()
query_engine = TransformQueryEngine(query_engine, query_transform=hyde)
response = query_engine.query(query_str)
print(response)
Check out our example notebook for a full walkthrough.
Multi-Step Query Transformations#
Multi-step query transformations are a generalization on top of existing single-step query transformation approaches.
Given an initial, complex query, the query is transformed and executed against an index. The response is retrieved from the query. Given the response (along with prior responses) and the query, follow-up questions may be asked against the index as well. This technique allows a query to be run against a single knowledge source until that query has satisfied all questions.
An example image is shown below.
Here's a corresponding example code snippet.
from llama_index.core.indices.query.query_transform.base import (
StepDecomposeQueryTransform,
)
# gpt-4
step_decompose_transform = StepDecomposeQueryTransform(llm, verbose=True)
query_engine = index.as_query_engine()
query_engine = MultiStepQueryEngine(
query_engine, query_transform=step_decompose_transform
)
response = query_engine.query(
"Who was in the first batch of the accelerator program the author started?",
)
print(str(response))
Check out our example notebook for a full walkthrough.