Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: clamp the top_k a model passes to search_knowledge_base (#14222)
Roydon Sequeira772ac8b1f82b
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.pyindex 564ae55efd6..0ecd2a13f07 100644--- a/studio/backend/core/inference/tools.py+++ b/studio/backend/core/inference/tools.py@@ -15054,6 +15054,16 @@ def _scope_retrieval_kwargs(scope: dict) -> dict:return {"mode": mode if mode in ("hybrid", "dense", "lexical") else "hybrid"}+# The sidebar's Top K maximum. Uncapped, lexical binds top_k as an SQL LIMIT and dense fails past vec0's KNN limit.+_MAX_KNOWLEDGE_SEARCH_TOP_K = 50+++def _kb_top_k(value) -> int | None:+ """``value`` capped at the sidebar maximum, or None (the search default) when it is not a positive count."""+ k = _opt_int(value)+ return min(k, _MAX_KNOWLEDGE_SEARCH_TOP_K) if k is not None and k > 0 else None++def _search_knowledge_base(arguments: dict, rag_scope: dict | None) -> str:"""Run the RAG search bound to the hidden per-request ``rag_scope`` (the model supplies only``query``/``top_k``). Lazy import; missing sqlite-vec degrades to a friendly message."""@@ -15070,7 +15080,7 @@ def _search_knowledge_base(arguments: dict, rag_scope: dict | None) -> str:logger.warning("RAG tool unavailable: %s", exc)return "Knowledge base search is unavailable on this server."- top_k = _opt_int((arguments or {}).get("top_k") or scope.get("default_top_k"))+ top_k = _kb_top_k((arguments or {}).get("top_k")) or _kb_top_k(scope.get("default_top_k"))text, sources = search_knowledge_base_with_sources(query = str(query),scope_kb_id = scope.get("kb_id"),diff --git a/studio/backend/tests/test_rag_retrieval.py b/studio/backend/tests/test_rag_retrieval.pyindex 94c10739a33..0c1986d9f22 100644--- a/studio/backend/tests/test_rag_retrieval.py+++ b/studio/backend/tests/test_rag_retrieval.py@@ -469,6 +469,26 @@ def test_scope_overrides_reach_retrieval(monkeypatch):assert seen["mode"] == "hybrid"+def test_model_top_k_is_clamped_in_every_mode(rag_conn, bow_embeddings, monkeypatch):+ tools = _shared_setup_1(monkeypatch)+ for i in range(60):+ _add_doc(rag_conn, "kb_a", f"d{i}", f"f{i}.txt", f"h{i}", f"alpha note {i}")++ def returned(top_k, mode):+ out = tools._search_knowledge_base(+ {"query": "alpha", "top_k": top_k}, {"kb_id": "a", "mode": mode, "default_top_k": 6}+ )+ return out.partition(tools.RAG_SOURCES_SENTINEL)[0].count("<chunk id=")++ for mode in ("hybrid", "lexical", "dense"):+ assert returned(3, mode) == 3, mode+ # Not a count, so the sidebar's setting applies.+ assert returned(-1, mode) == 6, mode+ assert returned(-5, mode) == 6, mode+ assert returned(2000, "lexical") == tools._MAX_KNOWLEDGE_SEARCH_TOP_K+ assert returned(2000, "dense") == tools._MAX_KNOWLEDGE_SEARCH_TOP_K++def test_build_rag_autoinject_scope_overrides_env(monkeypatch):tools = _shared_setup_1(monkeypatch)seen: dict = {}diff --git a/tests/utils/test_attention_native_routing.py b/tests/utils/test_attention_native_routing.pyindex 711850290da..77e66e13678 100644--- a/tests/utils/test_attention_native_routing.py+++ b/tests/utils/test_attention_native_routing.py@@ -102,12 +102,20 @@ def _dense(monkeypatch, dtype, mask):def _gqa_falls_to_math(Q, K, masked):# Expand K/V only where GQA leaves math (or nothing) while the expanded call has a faster kernel:# before torch 2.14 the memory-efficient kernel rejects GQA, so masked fp32 GQA had only math.- mask = torch.ones(Q.shape[0], 1, Q.shape[2], K.shape[2], dtype = torch.bool, device = Q.device) if masked else None+ mask = (+ torch.ones(Q.shape[0], 1, Q.shape[2], K.shape[2], dtype = torch.bool, device = Q.device)+ if masked+ else None+ )math = int(SDPBackend.MATH)def choice(k, gqa):try:- return int(torch._fused_sdp_choice(Q, k, k, attn_mask = mask, is_causal = not masked, enable_gqa = gqa))+ return int(+ torch._fused_sdp_choice(+ Q, k, k, attn_mask = mask, is_causal = not masked, enable_gqa = gqa+ )+ )except RuntimeError: # no enabled backend takes this callreturn None