docs-rag-mcp 0.8.0 加入 cross-encoder 重排,SDK 文件首位命中升至 18/22
I added a reranker to my local RAG server. The right page now comes first for 18 of 22 questions instead of 12.
docs-rag-mcp 0.8.0 為 MCP 伺服器加入可選的 cross-encoder 重排,SDK 文件測試中正確頁面排第一的題數由 12/22 增至 18/22。功能先由混合搜尋選出 top 20 候選,再調整排序;SDK 文件的 MRR 由 0.67 升至 0.88。員工手冊測試則有一題由首位降至第二位,作者亦指出兩組評估資料規模較小。
docs-rag-mcp is an MCP server in C# that lets Claude, GitHub Copilot and Cursor search your own documents. In my first post I wrote about hybrid search: vector similarity blended with SQLite FTS5 keyword ranking. Version 0.8.0 adds an optional last step, reranking with a cross-encoder. This post covers why, how I wired it in, and what it changed on my evaluation sets.
Why embeddings alone hit a ceiling
An embedding model turns the question and each passage into vectors separately, then compares them. It never sees the two together. That's what makes it fast enough to search a whole index, and also why it misses distinctions that matter.
My harder test set is 22 developer questions about the MCP C# SDK docs: 30 pages and 556 chunks, where many pages share the same words (server, client, tool, request). With hybrid search, the right page came first for 12 of the 22 questions.
A cross-encoder reads the question and one passage together and outputs a relevance score. It is much better at telling which passage answers the question, and much too slow to run over every chunk. So it only reorders the best candidates: hybrid search picks the top 20, and the reranker decides their order.
Not adding a model runtime
I didn't want to ship a model inside the server. The options I looked at:
- ONNX Runtime in process: fully local, but tens of megabytes of native libraries for each platform, a tokenizer, and model downloads, in a package that is pure managed code today.
- A chat model as a judge: no new dependency, but slow, and the scores depend on the prompt and parsing.
-
A
/rerankHTTP endpoint: the request Cohere introduced (queryanddocumentsin,indexandrelevance_scoreout) is also served by Jina, vLLM, Infinity, LocalAI and llama.cpp'sllama-server --reranking.
I went with the endpoint. One small client covers a local model and hosted services, with nothing but HttpClient and System.Text.Json:
llama-server -hf gpustack/bge-reranker-v2-m3-GGUF:Q4_K_M --reranking --port 8012 -c 8192 -b 8192 -ub 8192
"env": { "DOCSRAG_RERANK_ENDPOINT": "http://localhost:8012/v1/rerank" }
Reranking is off unless that variable is set.
Three details that mattered
Asking for more results must not change the first ones. To rerank, the search has to return more candidates than topK. I made sure the first topK of a longer list are exactly what a normal search returns, so turning reranking off gives the same results as before, and a test checks it.
A failing reranker must not fail the search. If the endpoint is down, returns an error, returns something that doesn't match the request, or takes more than 30 seconds, the server logs a warning and returns the hybrid order. I tested this by pointing it at a port where nothing listens: the results were identical to plain hybrid search.
The score shown stays the same kind of number. Reranker scores are not on the same scale as cosine similarity, and they differ between models. The reranker only decides the order; the score next to each passage is still the cosine similarity, so it reads the same with or without reranking.
Results
Measured with Ollama and nomic-embed-text for the embeddings and bge-reranker-v2-m3 (4-bit) on CPU:
| Search mode | Handbook: right section first | SDK docs: right page first | SDK docs: in the top 3 | SDK docs: MRR |
|---|---|---|---|---|
| vector | 14 of 14 | 11 of 22 | 15 of 22 | 0.61 |
| hybrid | 14 of 14 | 12 of 22 | 17 of 22 | 0.67 |
| hybrid + rerank | 13 of 14 | 18 of 22 | 20 of 22 | 0.88 |
On the SDK docs, reranking put the right page first for six more questions and raised the mean reciprocal rank from 0.67 to 0.88. That is a bigger jump than hybrid search gave over plain vectors.
It isn't free. On the small employee handbook, which was already perfect, it moved one right answer from first to second place. And every search now waits for a second model. Both sets are small, so I read these numbers as a direction, not a benchmark. The evaluation script is in the repo, so you can run it on your own documents:
DOCSRAG_RERANK_ENDPOINT=http://localhost:8012/v1/rerank dotnet run samples/eval/eval.cs -- samples/eval/mcp-csharp-sdk-docs.json /tmp/csharp-sdk
Try it
claude mcp add docs-rag -- dnx DocsRagMcp --yes
docs-rag-mcp is open source (MIT), on NuGet and in the official MCP Registry. If you try reranking with another model or a hosted service, I'd like to hear how it does on your documents: GitHub.
來源:DEV Community · dev.to