Implementation:AnswerDotAI RAGatouille RAGPretrainedModel Search Encoded Docs
| Knowledge Sources | |
|---|---|
| Domains | NLP, Information_Retrieval, Search |
| Last Updated | 2026-02-12 12:00 GMT |
Overview
Concrete tool for searching through documents previously encoded in memory using exact MaxSim scoring provided by the RAGatouille library.
Description
The RAGPretrainedModel.search_encoded_docs() method searches through documents that were previously encoded via encode(). It delegates to ColBERT.search_encoded_docs() which encodes the query into token embeddings, then calls ColBERT._index_free_search() to compute exact MaxSim scores against the in-memory document tensors. Results include content, score, rank, result_index, and optionally document_metadata.
The delegation chain:
- RAGPretrainedModel.search_encoded_docs() → delegates to model
- ColBERT.search_encoded_docs() → encodes queries, calls _index_free_search, attaches metadata
- ColBERT._index_free_search() → computes MaxSim via _colbert_score, sorts results
Usage
Use after calling encode() to search through in-memory documents. Supports single and batch queries.
Code Reference
Source Location
- Repository: RAGatouille
- File: ragatouille/RAGPretrainedModel.py
- Lines: L386-406
Signature
def search_encoded_docs(
self,
query: Union[str, list[str]],
k: int = 10,
bsize: int = 32,
) -> list[dict[str, Any]]:
"""Search through documents encoded in-memory.
Parameters:
query: Query string or list of queries.
k: Number of results per query (default 10).
bsize: Batch size for query encoding (default 32).
Returns:
list[dict]: Results with keys: content, score, rank, result_index,
and optionally document_metadata.
"""
Import
from ragatouille import RAGPretrainedModel
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| query | Union[str, list[str]] | Yes | Single query string or list of queries |
| k | int | No | Number of top results per query (default 10) |
| bsize | int | No | Batch size for query encoding (default 32) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | list[dict[str, Any]] | Results with keys: content (str), score (float), rank (int), result_index (int), and optionally document_metadata (dict) |
Usage Examples
Search Encoded Documents
from ragatouille import RAGPretrainedModel
RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
# Encode documents
RAG.encode([
"ColBERT uses late interaction.",
"BERT is a language model.",
"Transformers use attention.",
])
# Search
results = RAG.search_encoded_docs("What is ColBERT?", k=2)
for r in results:
print(f"[{r['rank']}] (score: {r['score']:.4f}) {r['content']}")
Batch Search with Metadata
RAG.encode(
documents=["Doc A", "Doc B", "Doc C"],
document_metadatas=[{"id": "a"}, {"id": "b"}, {"id": "c"}],
)
results = RAG.search_encoded_docs(
["query one", "query two"],
k=2,
)
# Returns list of lists for batch queries