Retriever

Embed and rerank with NeMo Retriever NIMs inside the RAG pipeline, and compare retrieval quality before and after reranking.

Updated CompTech Team

Most bad chatbot answers are bad retrieval. The generator never saw the right paragraph. NeMo Retriever is a pair of NIMs: an embedding model that finds a shortlist, and a reranking model that reads each candidate next to the question and reorders them. This lab wires both into the RAG path and measures whether the reranker changes recall.

The RAG overview explains why the two stages exist. Here you run them.

Prerequisites

  • The embedding NIM and the reranking NIM deployed on OpenShift, each with one GPU
  • A few dozen policy passages in a vector index. Milvus or pgvector both work
  • Ten to thirty questions where you already know which passage answers them
oc get pods -n nim -l app.kubernetes.io/name=embed
oc get pods -n nim -l app.kubernetes.io/name=rerank

The lab uses nvidia/llama-3.2-nv-embedqa-1b-v2 and nvidia/llama-3.2-nv-rerankqa-1b-v2. The larger retrieval NIMs speak the same API.

Embed passages and questions differently

These models expect an input type. Passages are indexed with passage. Questions are queried with query. Using the same type for both is a silent quality bug: the search runs, and the neighbours are worse.

# index time
curl -s http://embed-nim:8000/v1/embeddings -H 'Content-Type: application/json' \
  -d '{"model":"nvidia/llama-3.2-nv-embedqa-1b-v2","input":["Request password resets in the IT portal."],"input_type":"passage"}'

# query time
curl -s http://embed-nim:8000/v1/embeddings -H 'Content-Type: application/json' \
  -d '{"model":"nvidia/llama-3.2-nv-embedqa-1b-v2","input":["How do I reset my password?"],"input_type":"query"}'

Store the passage id, the text and the vector. At query time, embed the question and take the nearest 20 passages from the index. That list is the input to the reranker, not the input to the LLM.

Rerank the shortlist

The reranker scores the question together with each passage. It is too expensive to run across the whole index and cheap enough to run across 20 rows.

curl -s http://rerank-nim:8000/v1/ranking -H 'Content-Type: application/json' \
  -d '{
    "model": "nvidia/llama-3.2-nv-rerankqa-1b-v2",
    "query": {"text": "How do I reset my password?"},
    "passages": [
      {"text": "Request password resets in the IT portal. Have your employee id."},
      {"text": "The cafeteria closes at 16:00 on Fridays."}
    ]
  }'

Sort by the score in the response and keep the top 5. Those five are what the prompt in the RAG lab includes, with their source ids so the answer can cite them.

Compare recall, and write it down

Recall@k is the share of questions whose correct passage id appears in the top k. Compute it twice: once on the embedding order, once after reranking. Use the same questions.

def recall_at_k(questions, retrieve, k):
    hit = 0
    for q in questions:
        found = retrieve(q["question"], k)  # list of passage ids
        hit += int(q["passage_id"] in found)
    return f"{hit}/{len(questions)}"

# questions.jsonl: {"question": "...", "passage_id": "hr-14"}

Print embedding top-5 and reranked top-5 as counts, and copy them into the lab notes with the model names and the chunking you used. A reranker that does not move the count is still a result. Look at a question it got wrong. Typical causes:

Symptom First check
The right passage is not in the top 20 Chunks are too big, or the question and the passage were embedded with the same input_type
It is in the top 20 and reranking drops it The passage text is a fragment. Include the heading in the chunk
Both are right and the answer is wrong Retrieval worked. Read the guardrails and the prompt
Keyword questions fail (form numbers, names) Add hybrid search. The vector index misses exact tokens the RAG overview warns about

Change one thing at a time: chunk size, input type, or the reranker. A run that changes all three does not tell you which one mattered.

Re-index when you change the embedding model. Vectors from the previous model are not comparable with queries from the new one, and a mixed index looks like a reranker that got worse. The recall note should name the embedding model, so the next person does not "improve" the reranker when the vectors were simply stale.

What's next

  1. The index stays current when policies change by following Kafka for AI.
  2. The generator and the refusal behaviour are Guardrails.
  3. A model NIM does not ship is served with Triton, not forced into this NIM.