Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:666ghj BettaFish Clustering and Sampling

From Leeroopedia
Knowledge Sources
Domains Machine_Learning, Information_Retrieval, NLP
Last Updated 2026-10-08 00:00 GMT

Overview

Concrete wrapper that clusters search results using multilingual sentence embeddings and KMeans to select diverse, representative items, reducing hundreds of results to a manageable subset.

Description

The Clustering and Sampling logic in `InsightEngine/agent.py` uses the SentenceTransformer model (`paraphrase-multilingual-MiniLM-L12-v2`) to encode search result texts into embeddings, then applies KMeans clustering to group similar results. From each cluster, the top results by hotness score are selected, ensuring the final set covers diverse topics rather than just the most popular ones. This is a Wrapper Doc because it wraps the external `sentence_transformers` and `sklearn` libraries with InsightEngine-specific logic.

Usage

This logic is invoked by `DeepSearchAgent.execute_search_tool` when search results exceed `MAX_CLUSTERED_RESULTS` (50). It is not directly called by users but is integral to the InsightEngine pipeline.

Code Reference

Source Location

  • Repository: BettaFish
  • File: `InsightEngine/agent.py:L95-188`

Signature

# Lazy-loaded clustering model
def _get_clustering_model(self) -> SentenceTransformer:
    """Lazy-load paraphrase-multilingual-MiniLM-L12-v2."""

def _cluster_and_sample_results(
    self,
    results: List[QueryResult],
    max_results: int = 50,
    results_per_cluster: int = 5
) -> List[QueryResult]:
    """
    Cluster results by semantic similarity and sample diverse representatives.

    Args:
        results: Search results list (potentially hundreds)
        max_results: Maximum results to return (default 50)
        results_per_cluster: Top results per cluster by hotness (default 5)

    Returns:
        Diverse, representative subset of search results
    """

Import

from sentence_transformers import SentenceTransformer
from sklearn.cluster import KMeans
# Used internally by InsightEngine.agent.DeepSearchAgent

I/O Contract

Inputs

Name Type Required Description
results List[QueryResult] Yes Raw search results (potentially hundreds)
max_results int No Max results to return (default: 50)
results_per_cluster int No Results per cluster by hotness (default: 5)

Outputs

Name Type Description
sampled_results List[QueryResult] Diverse subset, max 50 items, covering all clusters

Usage Examples

Internal Usage in Agent

# This is called internally by DeepSearchAgent.execute_search_tool
# when results exceed MAX_CLUSTERED_RESULTS (50)

# The clustering process:
# 1. Encode each result's title_or_content[:500] with SentenceTransformer
embeddings = model.encode(texts, show_progress_bar=False)

# 2. Determine cluster count
n_clusters = min(max(2, max_results // results_per_cluster), len(results))

# 3. KMeans clustering
kmeans = KMeans(n_clusters=n_clusters, random_state=42, n_init=10)
labels = kmeans.fit_predict(embeddings)

# 4. From each cluster, pick top results by hotness_score
# 5. Return up to max_results diverse items

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment