惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
A
About on SuperTechFans
Recent Announcements
Recent Announcements
L
LangChain Blog
Microsoft Azure Blog
Microsoft Azure Blog
小众软件
小众软件
J
Java Code Geeks
博客园_首页
Jina AI
Jina AI
美团技术团队
H
Help Net Security
MyScale Blog
MyScale Blog
Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
S
SegmentFault 最新的问题

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
TF-IDF + LLM Reranking: How I Improved Vector Search Accu...
Rohith Davul · 2026-04-25 · via DEV Community

TF-IDF + LLM Reranking: How I Improved Vector Search Accuracy from 60% to 86%
Vector search is powerful — but it’s not perfect. When I was building a database discovery pipeline at work, our initial semantic search was only matching the right schemas about 60% of the time. That wasn’t good enough for production. Here’s exactly how I fixed it using a hybrid TF-IDF and LLM reranking approach.
The Problem
Our pipeline needed to match user queries to the correct database schemas from a large pool of candidates. Pure vector search (embeddings + cosine similarity) was fast but kept returning semantically similar but contextually wrong results.
For example, searching for “customer account balance” would return results about “user wallet transactions” — close, but not what we needed in a strict banking compliance context.
The Solution: Hybrid Retrieval + LLM Reranking
Instead of relying on one method, I combined three layers:
1. TF-IDF for keyword precision
2. Vector embeddings for semantic similarity
3. LLM reranking for contextual judgment
Step 1 — TF-IDF First Pass
TF-IDF is great at catching exact keyword matches that embeddings sometimes miss:

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np

def tfidf_retrieve(query: str, corpus: list, top_k: int = 20) -> list:
vectorizer = TfidfVectorizer()
tfidf_matrix = vectorizer.fit_transform(corpus)
query_vec = vectorizer.transform([query])
scores = cosine_similarity(query_vec, tfidf_matrix).flatten()
top_indices = np.argsort(scores)[::-1][:top_k]
return [(corpus[i], scores[i]) for i in top_indices]

This gives us a broad candidate set of top 20 results.
Step 2 — Vector Embedding Re-Filter
Next we re-score those 20 candidates using semantic embeddings:

from sentence_transformers import SentenceTransformer
import numpy as np

model = SentenceTransformer("all-MiniLM-L6-v2")

def embedding_rerank(query: str, candidates: list, top_k: int = 5) -> list:
query_embedding = model.encode(query)
scored = []
for text, _ in candidates:
emb = model.encode(text)
score = np.dot(query_embedding, emb)
scored.append((text, score))
scored.sort(key=lambda x: x[1], reverse=True)
return scored[:top_k]

Now we’re down to top 5 highly relevant candidates.
Step 3 — LLM Reranking
This is where the magic happens. We ask Gemini to pick the best match:

import google.generativeai as genai

def llm_rerank(query: str, candidates: list) -> str:
candidate_text = "\n".join(
[f"{i+1}. {c[0]}" for i, c in enumerate(candidates)]
)
prompt = f"""
Query: {query}

Candidates:
{candidate_text}

Which candidate best matches the query in a banking compliance context?
Return only the number of the best match.
"""
model = genai.GenerativeModel("gemini-2.0-flash")
response = model.generate_content(prompt)
return candidates[int(response.text.strip()) - 1][0]

Enter fullscreen mode Exit fullscreen mode

The LLM understands context, domain specifics, and nuance that pure math simply can’t capture.
The Results

Method Accuracy
Vector search only ~60%
TF-IDF only ~65%
TF-IDF + Embeddings ~75%
Full hybrid + LLM rerank 86%

Each layer added meaningful improvement. The LLM reranking alone jumped accuracy by 11 points.
Why This Works
• TF-IDF catches exact terminology matches
• Embeddings capture semantic meaning
• LLM applies domain reasoning and context
No single method is perfect. Combined, they cover each other’s weaknesses.
When Should You Use This?
Use this approach when:
• Your search corpus is domain-specific (legal, medical, banking)
• Exact keyword matches matter alongside semantic meaning
• You can afford a small LLM call per query
• Accuracy matters more than raw speed
Key Takeaway
Don’t default to pure vector search just because it’s trendy. A hybrid approach with LLM reranking is more accurate for specialized domains — and the implementation is simpler than you’d think.
Follow me for more practical AI engineering content. 🚀