Boost Your Search with LLM Query Rewriting
Search functionality is crucial for your app or website. However, users often struggle to find what they need due to vague or ambiguous queries. This can lead to frustration, low engagement, and missed opportunities. Fortunately, Large Language Models (LLMs) can provide a solution.
Imagine seamlessly converting imprecise user queries into focused keywords. This would significantly improve search results, even for complex queries, allowing users to find exactly what they need with ease. Enhanced engagement would follow, making your search feature a competitive advantage. This is the potential of LLM-driven query rewriting, and it is more achievable than you might think.
The Query Rewriting Challenge
Consider a user searching for "latest advancements LLMs healthcare?" They are interested in cutting-edge applications of language models in healthcare, but your search engine might struggle with the vague term "latest advancements," resulting in irrelevant results.
The challenge lies in the fact that users express themselves in natural language, while search engines rely on specific keywords. Manually bridging this gap for every possible query is impractical. However, LLMs can automatically translate imprecise user queries into concise keywords at scale.
The LLM-Powered Solution
Using Google's Gemini Flash model, we can create a query rewriting engine that takes a natural language query and provides:
- A rewritten query optimized for search.
- A list of extracted keywords.
- Clarifying questions that might enhance understanding.
- A summary of the original query.
With this structured output, we can significantly improve search relevance, guide users to better queries, and gain insights into user needs. Best of all, this can be implemented with just a few lines of Python!
Here’s how to build your own LLM-powered query rewriting engine using the instructor
library:
import instructor
import google.generativeai as genai
from pydantic import BaseModel, Field
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Configure the Gemini API key
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
# Initialize the Gemini client with Instructor
client = instructor.from_gemini(
client=genai.GenerativeModel(
model_name="models/gemini-1.5-flash-latest",
),
mode=instructor.Mode.GEMINI_JSON,
)
class RewrittenQuery(BaseModel):
rewritten_query: str = Field(description="The rewritten query for improved search.")
keywords: list[str] = Field(description="Keywords extracted from the original query.")
hypothetical_questions: list[str] = Field(
default_factory=list,
description="Hypothetical questions that the original query might be asking.",
)
summary: str = Field(description="A brief summary of the original query.")
def rewrite_query(query: str) -> RewrittenQuery:
"""Rewrites a user query using Gemini and returns a structured output."""
return client.chat.completions.create(
response_model=RewrittenQuery,
messages=[
{
"role": "user",
"content": f"""
Please analyze the following query and provide:
- A rewritten query for improved search relevance.
- A list of keywords extracted from the query.
- A list of hypothetical questions that the query might be asking.
- A brief summary of the query.
Query: {query}
""",
}
],
)
if __name__ == "__main__":
sample_query = "latest advancements LLMs healthcare?"
rewritten_query = rewrite_query(sample_query)
print(rewritten_query.model_dump_json(indent=2))
Output
{
"rewritten_query": "Recent advancements in large language models for healthcare applications",
"keywords": [
"LLMs",
"large language models",
"healthcare",
"advancements",
"applications",
"medical"
],
"hypothetical_questions": [
"What are the most recent breakthroughs in using LLMs for healthcare?",
"How are LLMs being applied to improve healthcare?",
"What are the potential benefits and risks of using LLMs in healthcare?",
"What are the latest research papers on LLMs in healthcare?"
],
"summary": "The query seeks information on the newest developments in the use of large language models within the healthcare industry."
}
Simply pass a user query to the rewrite_query
function, and you will receive a structured result ready to enhance your search system.
The Business-Changing Results
Integrating LLM query rewriting into your search can yield transformative results:
- Increased search relevance and user satisfaction.
- Higher engagement as users quickly find what they need.
- Improved conversion rates from search to purchase or signup.
- Valuable insights into user needs and language from query analysis.
- A scalable search architecture that adapts to your business growth.
This is the promise of LLM-powered search—a significant enhancement in user experience and business outcomes. By leveraging language models with minimal engineering effort, any app or website can benefit from semantic search that understands user intent deeply.
Get Started Today
Ready to enhance your search capabilities? Here’s how to begin:
- Sign up for access to Google's Gemini Flash.
- Use the Python code snippet provided above.
- Integrate the rewritten queries and keywords into your search backend.
- Monitor results and continue to iterate.
The beauty of this approach lies in its simplicity and the ability to measure impact. Start with a small subset of queries, assess the relevance improvements, and gradually expand to your entire search experience.
As you scale, consider exploring advanced architectures like query expansion, faceted search, and personalization. The possibilities are vast, all beginning with the powerful technique of LLM-based query rewriting.
Have you experimented with LLMs to enhance your search? Share your experiences, tips, and results. Together, let’s explore the potential of language model-enhanced search and deliver better experiences to our users. The future of search is here, powered by LLMs!