Skip to main content
doc0blade47/mem0documentation home
Sign in

Reranking Models

Last updated Oct 11, 2026100% grounded
Relevant source files

The following files were used as context for generating this wiki page:

Overview

Reranking models refine initial memory retrieval candidates by scoring query-document relevance through specialized neural architectures, cloud APIs, and language model evaluators. Positioned downstream of vector and keyword searches across both Python and TypeScript runtimes, the reranking subsystem elevates high-precision matches before results are returned to agent pipelines.

Sources: mem0/reranker/cohere_reranker.py:16-36, mem0/reranker/sentence_transformer_reranker.py:21-51, mem0-ts/src/oss/src/utils/factory.ts:217-242

Reranker Interface and Factory Integration

TypeScript Reranker Implementation and Result Data Structures

The Typescript Core defines the contract for search rerankers via the Reranker interface and the RerankResult data structure. Every concrete provider implementation — including CrossEncoderReranker, CohereReranker, LLMReranker, and ZeroEntropyReranker — implements an asynchronous rerank method accepting a query string, an array of candidate document strings, and an optional topK constraint.

export interface RerankResult {
  index: number;
  rerankScore: number;
}
 
export interface Reranker {
  rerank(
    query: string,
    documents: string[],
    topK?: number,
  ): Promise<RerankResult[]>;
}

Sources: mem0-ts/src/oss/src/rerankers/cross_encoder.ts:2-3, mem0-ts/src/oss/src/rerankers/cohere.ts:2-3

Each reranking driver handles empty document collections by immediately returning an empty array without making external API calls or invoking local models. When operational failures occur during reranking, drivers execute safe fallback behavior: they catch the error, emit a warning via console.warn, assign a fallback relevance score (0.0 for API and neural rerankers, 0.5 for prompt-based LLM scoring), preserve the original candidate indices, and slice the results to topK if specified.

Warning

Rerankers fall back to the original candidate order with neutral scores when runtime execution fails. Consumers should verify whether a returned score of 0.0 or 0.5 represents a genuine relevance metric or a fallback exception recovery path.

Sources: mem0-ts/src/oss/src/rerankers/cross_encoder.ts:57-96, mem0-ts/src/oss/src/rerankers/cohere.ts:58-86, mem0-ts/src/oss/src/rerankers/llm.ts:37-56, mem0-ts/src/oss/src/rerankers/zeroentropy.ts:54-83

Python Reranker Package Exports

The Python search architecture organizes its reranking subsystem around the BaseReranker abstract base class and exposes concrete implementations through its package module initialization. Consumers import reranker drivers directly from the module namespace.

Reranker ClassModule FilePrimary Function
BaseRerankermem0/reranker/base.pyAbstract base class defining the standard rerank interface
CohereRerankermem0/reranker/cohere_reranker.pyCloud-based reranking via the Cohere API client
HuggingFaceRerankermem0/reranker/huggingface_reranker.pyLocal sequence classification via Hugging Face inference
LLMRerankermem0/reranker/llm_reranker.pyPrompt-based relevance scoring via language models
SentenceTransformerRerankermem0/reranker/sentence_transformer_reranker.pyCross-encoder scoring using Sentence Transformers
ZeroEntropyRerankermem0/reranker/zero_entropy_reranker.pyZeroEntropy API-driven relevance reranking

Sources: mem0/reranker/init.py:5-18

Cohere API Reranking Driver

Configuration Settings and Parameters

The Cohere reranking driver is configured using dedicated parameter classes across both runtimes. In Python, CohereRerankerConfig inherits from BaseRerankerConfig and defines specific attributes for model selection, document text return behavior, and chunking limits. In TypeScript, corresponding options are supplied via the general RerankerConfig interface.

Parameter NamePython DefaultTypeScript DefaultDescription
model"rerank-v3.5""rerank-v3.5"The Cohere rerank model identifier
return_documentsFalsefalseWhether to return the document texts in the response
max_chunks_per_docNoneundefinedMaximum number of chunks per document
top_k / topKNoneundefinedNumber of top documents to return

Sources: mem0/configs/rerankers/cohere.py:8-16, mem0-ts/src/oss/src/rerankers/cohere.ts:5-28

Client Lifecycle and Peer Loading

Initialization differs between runtimes regarding dependency management and connection handling. The Python driver checks COHERE_AVAILABLE during instantiation and immediately raises an ImportError if the cohere package is missing. It resolves the API key from config.api_key or falls back to the COHERE_API_KEY environment variable before instantiating cohere.Client.

        if not COHERE_AVAILABLE:
            raise ImportError("cohere package is required for CohereReranker. Install with: pip install cohere")
        
        self.config = config
        self.api_key = config.api_key or os.getenv("COHERE_API_KEY")
        if not self.api_key:
            raise ValueError("Cohere API key is required. Set COHERE_API_KEY environment variable or pass api_key in config.")
            
        self.model = config.model
        self.client = cohere.Client(self.api_key)

Sources: mem0/reranker/cohere_reranker.py:26-35

The TypeScript driver employs a lazy loading pattern via loadPeer to import the optional cohere-ai peer only when reranking is executed, avoiding mandatory dependency installation for consumers that do not use Cohere.

  private async createClient(): Promise<any> {
    const sdk = await loadPeer(
      "cohere-ai",
      "Cohere reranker",
      () => import("cohere-ai"),
    );
    return new sdk.CohereClient({ token: this.apiKey });
  }

Sources: mem0-ts/src/oss/src/rerankers/cohere.ts:44-51

Warning

Both client implementations require a valid Cohere API key. If COHERE_API_KEY is omitted from both the runtime configuration and the environment variables, initialization fails with a runtime error.

Sources: mem0/reranker/cohere_reranker.py:30-32, mem0-ts/src/oss/src/rerankers/cohere.ts:17-22

Scoring Behavior and Fallback Execution

When executing rerank, the Python driver extracts text content from incoming dictionary objects by inspecting keys in priority order: 'memory', 'text', 'content', or falling back to string conversion of the entire document. It calls self.client.rerank with model, query, documents, top_n, return_documents, and max_chunks_per_doc, then maps result.relevance_score to each document's 'rerank_score' field.

            response = self.client.rerank(
                model=self.model,
                query=query,
                documents=doc_texts,
                top_n=top_k or self.config.top_k or len(documents),
                return_documents=self.config.return_documents,
                max_chunks_per_doc=self.config.max_chunks_per_doc,
            )

Sources: mem0/reranker/cohere_reranker.py:65-73

If the remote Cohere API call raises an exception, both runtimes catch the error, log a warning, and execute fallback logic. They assign a relevance score of 0.0 to all documents while preserving their original relative ordering and applying any requested top_k slice.

Sources: mem0/reranker/cohere_reranker.py:84-93, mem0-ts/src/oss/src/rerankers/cohere.ts:75-86

Hugging Face Cross-Encoder Scoring

Configuration and Model Initialization

The Hugging Face reranker configuration is defined by HuggingFaceRerankerConfig, which extends BaseRerankerConfig with model defaults, device placement, batch processing limits, and score normalization parameters.

OptionTypeDefaultDescription
modelOptional[str]"BAAI/bge-reranker-base"The Hugging Face model identifier to use for reranking
deviceOptional[str]NoneDevice to run the model on ('cpu', 'cuda', etc.)
batch_sizeint32Batch size for processing documents
max_lengthint512Maximum length for tokenization
normalizeboolTrueWhether to normalize scores

Sources: mem0/configs/rerankers/huggingface.py:7-17

In the Python runtime, HuggingFaceReranker checks if the transformers package is available at import time, raising an ImportError if it is missing. During initialization, it automatically selects "cuda" if PyTorch detects an available GPU, or falls back to "cpu". It loads both the tokenizer and sequence classification model from the configured model ID, moves the model to the target device, and puts it into evaluation mode.

        if self.config.device is None:
            self.device = "cuda" if torch.cuda.is_available() else "cpu"
        else:
            self.device = self.config.device
 
        self.tokenizer = AutoTokenizer.from_pretrained(self.config.model)
        self.model = AutoModelForSequenceClassification.from_pretrained(self.config.model)
        self.model.to(self.device)
        self.model.eval()

Sources: mem0/reranker/huggingface_reranker.py:30-62

The TypeScript implementation (CrossEncoderReranker) utilizes a lazy-loading pattern via load() to import @huggingface/transformers and its native ONNX runtime bindings only when a rerank operation executes. This prevents the ONNX native runtime from being pulled into every new Memory() instantiation, avoiding version collisions with FastEmbed.

  private load() {
    if (!this.loaded) {
      this.loaded = (async () => {
        const { AutoModelForSequenceClassification, AutoTokenizer } =
          await import("@huggingface/transformers");
        const options: any = {};
        if (this.device) options.device = this.device;
        const model = await AutoModelForSequenceClassification.from_pretrained(
          this.modelId,
          options,
        );
        const tokenizer = await AutoTokenizer.from_pretrained(this.modelId);
        return { model, tokenizer };
      })();
    }
    return this.loaded;
  }

Sources: mem0-ts/src/oss/src/rerankers/cross_encoder.ts:29-50

Note

In CrossEncoderReranker, configuration parameters batchSize and showProgressBar are accepted strictly for config parity with the Python SDK but operate as no-ops because memory searches rerank candidate sets in a single forward pass.

Sources: mem0-ts/src/oss/src/rerankers/cross_encoder.ts:12-15

Logit Extraction and Score Normalization

Cross-encoder models emit unbounded raw logits during inference. To convert these into interpretable relevance scores in the [0, 1] range without set-relative distortion, both implementations apply a sigmoid function (1 / (1 + e^-x)) when normalize is enabled.

    @staticmethod
    def _normalize_scores(scores: List[float]) -> List[float]:
        if not scores:
            return []
        arr = np.asarray(scores, dtype=float)
        return (1.0 / (1.0 + np.exp(-arr))).tolist()

Sources: mem0/reranker/huggingface_reranker.py:63-81

In TypeScript, logit rows are extracted from model execution outputs, checked for array structure, and mapped through a local sigmoid helper.

const sigmoid = (x: number) => 1 / (1 + Math.exp(-x));

Sources: mem0-ts/src/oss/src/rerankers/cross_encoder.ts:4-4

      const { logits } = await model(inputs);
      const rows: unknown[] = logits.tolist();
 
      const scored = rows.map((row, index) => {
        const logit = Array.isArray(row) ? (row[0] as number) : (row as number);
        return {
          index,
          rerankScore: this.normalize ? sigmoid(logit) : logit,
        };
      });

Sources: mem0-ts/src/oss/src/rerankers/cross_encoder.ts:72-81

Warning

Sigmoid normalization replaces legacy min-max scaling. Min-max scaling forced the lowest-ranked document to 0.0 and collapsed single-document or entirely tied score sets to zero, whereas per-document sigmoid scoring evaluates each item on its own individual merit.

Sources: mem0/reranker/huggingface_reranker.py:66-76

Sentence Transformer Reranker Pipeline

Configuration Options and Defaults

The SentenceTransformerReranker relies on SentenceTransformerRerankerConfig for parameter initialization. If initialized with a dictionary or a generic BaseRerankerConfig, the pipeline maps fields to default values.

FieldTypeDefault ValueDescription
providerstr"sentence_transformer"Reranker provider identifier
modelOptional[str]"cross-encoder/ms-marco-MiniLM-L-6-v2"Cross-encoder model name
deviceOptional[str]NoneDevice specification ('cpu', 'cuda', etc., auto-detected if None)
batch_sizeint32Batch size for processing documents
show_progress_barboolFalseWhether to display a progress bar during inference
api_keyOptional[str]NoneAPI key for authentication when applicable
top_kOptional[int]NoneMaximum number of top documents to return
class SentenceTransformerRerankerConfig(BaseRerankerConfig):
    """
    Configuration class for Sentence Transformer reranker-specific parameters.
    Inherits from BaseRerankerConfig and adds Sentence Transformer-specific settings.
    """
 
    model: Optional[str] = Field(default="cross-encoder/ms-marco-MiniLM-L-6-v2", description="The cross-encoder model name to use")
    device: Optional[str] = Field(default=None, description="Device to run the model on ('cpu', 'cuda', etc.)")
    batch_size: int = Field(default=32, description="Batch size for processing documents")
    show_progress_bar: bool = Field(default=False, description="Whether to show progress bar during processing")

Sources: mem0/configs/rerankers/sentence_transformer.py:7-16

# Convert to SentenceTransformerRerankerConfig if needed
if isinstance(config, dict):
    config = SentenceTransformerRerankerConfig(**config)
elif isinstance(config, BaseRerankerConfig) and not isinstance(config, SentenceTransformerRerankerConfig):
    # Convert BaseRerankerConfig to SentenceTransformerRerankerConfig with defaults
    config = SentenceTransformerRerankerConfig(
        provider=getattr(config, 'provider', 'sentence_transformer'),
        model=getattr(config, 'model', 'cross-encoder/ms-marco-MiniLM-L-6-v2'),
        api_key=getattr(config, 'api_key', None),
        top_k=getattr(config, 'top_k', None),
        device=None,  # Will auto-detect
        batch_size=32,  # Default
        show_progress_bar=False,  # Default
    )

Sources: mem0/reranker/sentence_transformer_reranker.py:34-47

Caution

If the sentence-transformers package is not installed in the Python environment, SentenceTransformerReranker.__init__ raises an immediate ImportError instructing the user to run pip install sentence-transformers.

Sources: mem0/reranker/sentence_transformer_reranker.py:31-33

Document Extraction and Scoring Workflow

When rerank is invoked, the pipeline parses incoming documents by checking for specific text keys (memory, text, or content), falling back to string conversion if none are present. It pairs each extracted document text with the search query, passes the pairs into the CrossEncoder model predict method, and sorts the results in descending order by score.

# Extract text content for reranking
doc_texts = []
for doc in documents:
    if 'memory' in doc:
        doc_texts.append(doc['memory'])
    elif 'text' in doc:
        doc_texts.append(doc['text'])  
    elif 'content' in doc:
        doc_texts.append(doc['content'])
    else:
        doc_texts.append(str(doc))
 
try:
    # Create query-document pairs
    pairs = [[query, doc_text] for doc_text in doc_texts]
 
    scores = self.model.predict(
        pairs,
        batch_size=self.config.batch_size,
        show_progress_bar=self.config.show_progress_bar,
    )
    if isinstance(scores, np.ndarray):
        scores = scores.tolist()
 
    # Combine documents with scores
    doc_score_pairs = list(zip(documents, scores))
 
    # Sort by score (descending)
    doc_score_pairs.sort(key=lambda x: x[1], reverse=True)
 

Sources: mem0/reranker/sentence_transformer_reranker.py:67-96

Note

If model prediction or score processing raises an exception, the reranker catches the error, logs a warning, and falls back to the original document order with a default rerank_score of 0.0, applying any configured top_k slice to the fallback collection.

Sources: mem0/reranker/sentence_transformer_reranker.py:111-120

LLM Prompt-Based Evaluator

Configuration and System Prompt Architecture

The LLM-based reranker evaluates document relevance by leveraging language model generations guided by a strict system prompt and deterministic generation parameters. In Python, the LLMRerankerConfig class controls model initialization through parameters including provider, model name, API keys, temperature, maximum tokens, and optional nested LLM configurations.

ParameterTypeDefault ValueDescription
modelstr"gpt-5-mini"LLM model identifier for reranking evaluations
api_keyOptional[str]NoneAuthentication API key for the LLM provider
providerstr"openai"LLM provider backend identifier
top_kOptional[int]NoneMaximum number of top documents returned after reranking
temperaturefloat0.0Sampling temperature for deterministic scoring
max_tokensint100Maximum token limit for the LLM response
scoring_promptOptional[str]NoneDeprecated custom prompt template (used as system message)
llmOptional[Dict[str, Any]]NoneNested configuration dictionary with provider and config keys
model: str = Field(
    default="gpt-5-mini",
    description="LLM model to use for reranking"
)
api_key: Optional[str] = Field(
    default=None,
    description="API key for the LLM provider"
)
provider: str = Field(
    default="openai",
    description="LLM provider (openai, anthropic, etc.)"
)
top_k: Optional[int] = Field(
    default=None,
    description="Number of top documents to return after reranking"
)
temperature: float = Field(
    default=0.0,
    description="Temperature for LLM generation"
)
max_tokens: int = Field(
    default=100,
    description="Maximum tokens for LLM response"
)
scoring_prompt: Optional[str] = Field(
    default=None,
    description="Custom prompt template for scoring documents"
)
llm: Optional[Dict[str, Any]] = Field(
    default=None,
    description="Nested LLM configuration with 'provider' and 'config' keys. "
    "Overrides top-level provider/model/api_key when provided.",
)

Sources: mem0/configs/rerankers/llm.py:22-54

Warning

Setting a custom scoring_prompt triggers a DeprecationWarning advising that the configuration attribute is deprecated and will be removed in a future version, with the provided string automatically mapped to the system message.

Sources: mem0/reranker/llm_reranker.py:64-74

Both Python and TypeScript implementations utilize a system prompt defining an explicit relevance scoring rubric ranging from 0.0 to 1.0.

          LLM Prompt-Based Relevance Scoring Rubric
         ┌───────────────────────────────────────────┐
         │ 1.0 = Perfectly relevant & answers query  │
         │ 0.8-0.9 = Highly relevant with good info │
         │ 0.6-0.7 = Moderately relevant            │
         │ 0.4-0.5 = Slightly relevant              │
         │ 0.0-0.3 = Not relevant                   │
         └───────────────────────────────────────────┘

Sources: mem0/reranker/llm_reranker.py:78-89, mem0-ts/src/oss/src/rerankers/llm.ts:5-14

Response Parsing and Score Extraction

To prevent prompt injection and flooding attacks, both runtimes truncate queries and document texts to a maximum length of 4000 characters using _MAX_INPUT_LEN. The user message combines the safe query and safe document into a structured prompt format before invoking the underlying LLM client.

safe_query = query[: self._MAX_INPUT_LEN]
safe_doc = doc_text[: self._MAX_INPUT_LEN]
user_message = f"Query: {safe_query}\n\nDocument: {safe_doc}"
 
response = self.llm.generate_response(
    messages=[
        {"role": "system", "content": self._system_prompt},
        {"role": "user", "content": user_message},
    ]
)

Sources: mem0/reranker/llm_reranker.py:138-147

private async score(query: string, document: string): Promise<number> {
  const safeQuery = query.slice(0, MAX_INPUT_LEN);
  const safeDoc = document.slice(0, MAX_INPUT_LEN);
  const userMessage = `Query: ${safeQuery}\n\nDocument: ${safeDoc}`;
 
  const response = await this.llm.generateResponse([
    { role: "system", content: SYSTEM_PROMPT },
    { role: "user", content: userMessage },
  ]);

Sources: mem0-ts/src/oss/src/rerankers/llm.ts:58-66

The score extraction logic parses raw LLM output text by searching for decimal numbers first, falling back to integers, and clamping the resulting floating-point value between 0.0 and 1.0. If parsing fails or no valid number matches, the system returns a fallback score of 0.5.

def _extract_score(self, response_text: str) -> float:
    """Extract numerical score from LLM response."""
    # Prefer a decimal, fall back to an integer, then clamp: out-of-range outputs
    # like "2.0"/"5" become 1.0 instead of being mis-parsed into a stray 0/1 digit.
    matches = re.findall(r'-?\d+\.\d+', response_text) or re.findall(r'-?\d+', response_text)
 
    if matches:
        score = float(matches[0])
        return min(max(score, 0.0), 1.0)  # Clamp between 0.0 and 1.0
 
    # Fallback: return 0.5 if no valid score found
    return 0.5

Sources: mem0/reranker/llm_reranker.py:94-105

private extractScore(responseText: string): number {
  const matches =
    responseText.match(/-?\d+\.\d+/g) || responseText.match(/-?\d+/g);
 
  if (matches && matches.length > 0) {
    const score = parseFloat(matches[0]);
    return Math.min(Math.max(score, 0.0), 1.0);
  }
 
  return 0.5;
}

Sources: mem0-ts/src/oss/src/rerankers/llm.ts:76-86

Caution

Out-of-range numerical outputs such as "2.0" or "5" are clamped to 1.0 by the clipping function min(max(score, 0.0), 1.0), preventing mis-parsed numbers from corrupting relative document rankings.

Sources: mem0/reranker/llm_reranker.py:96-102, mem0-ts/src/oss/src/rerankers/llm.ts:81-82

ZeroEntropy API Reranking Client

ZeroEntropy Reranker Configuration and Initialization

The ZeroEntropy reranker integration relies on the zeroentropy package across both Python and TypeScript runtimes, utilizing the default model identifier "zerank-1" when no explicit model is provided. In Python, ZeroEntropyReranker validates package availability via an ImportError guard and inspects configuration or environment variables for ZERO_ENTROPY_API_KEY.

def __init__(self, config):
    """
    Initialize Zero Entropy reranker.
 
    Args:
        config: ZeroEntropyRerankerConfig object with configuration parameters
    """
    if not ZERO_ENTROPY_AVAILABLE:
        raise ImportError("zeroentropy package is required for ZeroEntropyReranker. Install with: pip install zeroentropy")
 
    self.config = config
    self.api_key = config.api_key or os.getenv("ZERO_ENTROPY_API_KEY")
    if not self.api_key:
        raise ValueError("Zero Entropy API key is required. Set ZERO_ENTROPY_API_KEY environment variable or pass api_key in config.")
 
    self.model = config.model or "zerank-1"
 
    # Initialize Zero Entropy client
    if self.api_key:
        self.client = ZeroEntropy(api_key=self.api_key)
    else:
        self.client = ZeroEntropy()  # Will use ZERO_ENTROPY_API_KEY from environment
 

Sources: mem0/reranker/zero_entropy_reranker.py:19-41

In the TypeScript runtime, ZeroEntropyReranker employs a lazy peer loading utility to dynamically import zeroentropy only upon the first execution, preventing unnecessary dependency requirements for consumers that do not use ZeroEntropy.

private async getClient(): Promise<any> {
  if (this.clientInstance) return this.clientInstance;
  if (!this.clientPromise) {
    this.clientPromise = this.createClient();
  }
  this.clientInstance = await this.clientPromise;
  return this.clientInstance;
}
 
private async createClient(): Promise<any> {
  const sdk = await loadPeer(
    "zeroentropy",
    "ZeroEntropy reranker",
    () => import("zeroentropy"),
  );
  return new sdk.ZeroEntropy({ apiKey: this.apiKey });
}

Sources: mem0-ts/src/oss/src/rerankers/zeroentropy.ts:31-47

ParameterTypeDefaultDescription
modelstr"zerank-1"Model identifier for ZeroEntropy reranking (zerank-1, zerank-1-small)
api_keyOptional[str]NoneAuthentication API key checked against ZERO_ENTROPY_API_KEY
top_kOptional[int]NoneMaximum number of top documents returned after reranking

Sources: mem0/configs/rerankers/zero_entropy.py:17-28

Warning

Both Python and TypeScript implementations require a valid API key, raising a ValueError if neither config.api_key nor the ZERO_ENTROPY_API_KEY environment variable is defined.

Sources: mem0/reranker/zero_entropy_reranker.py:30-32, mem0-ts/src/oss/src/rerankers/zeroentropy.ts:15-20

Batch Relevance Scoring and Error Fallback

During execution, rerank extracts document texts from keys including 'memory', 'text', or 'content', falling back to string conversion if none match. It then submits the query and text array to client.models.rerank, maps the resulting relevance scores to each document index, sorts candidates in descending order by score, and applies the top_k limit.

def rerank(self, query: str, documents: List[Dict[str, Any]], top_k: int = None) -> List[Dict[str, Any]]:
    """
    Rerank documents using Zero Entropy's rerank API.
 
    Args:
        query: The search query
        documents: List of documents to rerank
        top_k: Number of top documents to return
 
    Returns:
        List of reranked documents with rerank_score
    """
    if not documents:
        return documents
 
    # Extract text content for reranking
    doc_texts = []
    for doc in documents:
        if 'memory' in doc:
            doc_texts.append(doc['memory'])
        elif 'text' in doc:
            doc_texts.append(doc['text'])  
        elif 'content' in doc:
            doc_texts.append(doc['content'])
        else:
            doc_texts.append(str(doc))
 
    try:
        # Call Zero Entropy rerank API
        response = self.client.models.rerank(
            model=self.model,
            query=query,
            documents=doc_texts,
        )
 
        # Create reranked results
        reranked_docs = []
        for result in response.results:
            original_doc = documents[result.index].copy()
            original_doc['rerank_score'] = result.relevance_score
            reranked_docs.append(original_doc)
 
        # Sort by relevance score in descending order
        reranked_docs.sort(key=lambda x: x['rerank_score'], reverse=True)
 
        # Apply top_k limit
        if top_k:
            reranked_docs = reranked_docs[:top_k]
        elif self.config.top_k:
            reranked_docs = reranked_docs[:self.config.top_k]
 
        return reranked_docs

Sources: mem0/reranker/zero_entropy_reranker.py:42-93

async rerank(
  query: string,
  documents: string[],
  topK?: number,
): Promise<RerankResult[]> {
  if (documents.length === 0) return [];
 
  try {
    const client = await this.getClient();
    const response = await client.models.rerank({
      model: this.model,
      query,
      documents,
    });
 
    const scored: RerankResult[] = response.results.map((result: any) => ({
      index: result.index,
      rerankScore: result.relevance_score,
    }));
    scored.sort((a, b) => b.rerankScore - a.rerankScore);
 
    const finalTopK = topK || this.topK;
    return finalTopK ? scored.slice(0, finalTopK) : scored;

Sources: mem0-ts/src/oss/src/rerankers/zeroentropy.ts:49-71

Tip

If the ZeroEntropy API call throws an exception, both clients fail open by catching the error, logging a warning, and returning documents in their original order with a fallback rerank_score of 0.0 up to the requested top_k limit.

Sources: mem0/reranker/zero_entropy_reranker.py:95-104, mem0-ts/src/oss/src/rerankers/zeroentropy.ts:72-82

Was this page helpful?