Query Construction
Overview
Query Construction is a stage in the RAG pipeline where the original user query is transformed into a form suitable for searching a vector database. It includes embedding generation, metadata filtering, and query transformation into SQL or Cypher for graph databases.
Methods
- Query Embedding: converting text into a vector using an embedding model.
- Metadata Filtering: adding conditions based on date, category, author.
- Structured Query Generation: LLM forms SQL/Cypher for precise search.
- Multivector Query: creating multiple embeddings for different aspects of the query.
Example
from langchain.chains.query_constructor.base import load_query_constructor_runnable
chain = load_query_constructor_runnable(llm, "your_collection")
query = chain.invoke({"query": "AI articles from 2024"})
# query: {"query": "AI", "filter": {"year": {"$eq": 2024}}}