Tutorial
Build an AI-Powered Search Engine with LLMs: Complete Tutorial
April 17, 2026 · 9 min read
Traditional keyword search fails when users ask natural language questions. "How do I fix the login bug on mobile?" won't match a document titled "Authentication Flow Troubleshooting — iOS and Android." AI-powered search understands intent, not just keywords. Here's how to build one.
Architecture: AI Search Pipeline
- Query understanding: LLM expands and reformulates the user's query
- Candidate retrieval: Traditional search (BM25 / Elasticsearch) finds initial candidates
- AI reranking: LLM scores candidates by relevance to the original question
- Answer generation: LLM synthesizes a direct answer from top results
Why AI Search Beats Keyword Search
| Capability | Keyword Search | AI-Powered Search |
|---|---|---|
| Synonym handling | Manual synonyms list | Automatic (understands "car" = "vehicle") |
| Natural language queries | Poor | Excellent |
| Multilingual | Requires separate indexes | Built-in cross-lingual |
| Typo tolerance | Fuzzy matching (limited) | Understands intent despite typos |
| Question answering | Returns documents | Returns direct answers |
| Context understanding | None | Understands user context and history |
Step 1: Query Expansion
from openai import OpenAI
client = OpenAI(base_url="https://api.aipower.me/v1", api_key="YOUR_KEY")
def expand_query(user_query):
"""Use LLM to generate better search queries."""
response = client.chat.completions.create(
model="deepseek/deepseek-chat",
messages=[{
"role": "user",
"content": f"Generate 3 alternative search queries for: '{user_query}'\n"
"Return only the queries, one per line."
}],
max_tokens=100,
temperature=0.3,
)
queries = response.choices[0].message.content.strip().split("\n")
return [user_query] + queries # Original + expansionsStep 2: AI Reranking
def rerank_results(query, candidates, top_k=5):
"""Use LLM to rerank search results by relevance."""
candidates_text = "\n".join(
f"[{i}] {doc['title']}: {doc['snippet'][:200]}"
for i, doc in enumerate(candidates)
)
response = client.chat.completions.create(
model="deepseek/deepseek-chat",
messages=[{
"role": "user",
"content": f"Query: {query}\n\nDocuments:\n{candidates_text}\n\n"
f"Rank the top {top_k} most relevant document numbers. "
"Return only numbers separated by commas."
}],
max_tokens=50,
temperature=0,
)
ranked_ids = [int(x.strip()) for x in response.choices[0].message.content.split(",")]
return [candidates[i] for i in ranked_ids[:top_k]]Step 3: Answer Generation
def generate_answer(query, top_results):
"""Generate a direct answer from search results."""
context = "\n\n".join(
f"Source: {doc['title']}\n{doc['content'][:500]}"
for doc in top_results[:3]
)
response = client.chat.completions.create(
model="deepseek/deepseek-chat",
messages=[
{"role": "system", "content": "Answer the question using the provided sources. "
"Cite sources by title. Be concise."},
{"role": "user", "content": f"Sources:\n{context}\n\nQuestion: {query}"},
],
)
return response.choices[0].message.contentCost Analysis: AI Search
| Component | Model | Cost per Query | Latency |
|---|---|---|---|
| Query expansion | DeepSeek V3 | $0.00005 | 200ms |
| Reranking (20 docs) | DeepSeek V3 | $0.0003 | 400ms |
| Answer generation | DeepSeek V3 | $0.0002 | 500ms |
| Total per query | $0.00055 | 1.1s |
At $0.00055 per query, you can serve 1.8 million searches per $1,000 using DeepSeek through AIPower.
Build your AI search engine today at aipower.me — DeepSeek V3 at $0.34/M input tokens makes AI search economically viable at any scale. 10 free API calls to start.
GET STARTED WITH AIPOWER
16 AI models. One API. OpenAI SDK compatible.
Who should use AIPower?
- • Developers needing both Chinese and Western AI models
- • Chinese teams that can't access OpenAI / Anthropic directly
- • Startups wanting multi-model redundancy through one API
- • Anyone tired of paying grey-market intermediary premiums
3 steps to first API call
- Sign up — email only, 10 free trial calls, no card
- Copy your API key from the dashboard
- Change
base_urlin your OpenAI SDK → done
from openai import OpenAI
client = OpenAI(
base_url="https://api.aipower.me/v1", # ← only change
api_key="sk-your-aipower-key",
)
response = client.chat.completions.create(
model="auto-cheap", # or anthropic/claude-opus, deepseek/deepseek-chat, openai/gpt-5, etc.
messages=[{"role": "user", "content": "Hello"}],
)
print(response.choices[0].message.content)+100 bonus calls on first $5 top-up · WeChat Pay + Alipay + card accepted · docs · security