File size: 1,471 Bytes
e61e934
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
vector_search.py

Thin wrapper helpers used to orchestrate searches and resets from the app or admin UI.
"""

from typing import List, Dict, Any
from core import vector_store, vector_sync

def semantic_search(query: str, top_k: int = 6) -> List[Dict[str, Any]]:
    """
    Safe semantic search that falls back gracefully to empty list.
    """
    try:
        return vector_store.search_index(query, top_k=top_k)
    except Exception as e:
        print(f"⚠️ semantic_search error: {e}")
        return []

def reset_faiss_and_rebuild(glossary_builder_fn=None, rebuild_index_fn=None) -> str:
    """
    Clear local caches and run rebuilds. The app's reset_faiss_cache() can call this.
    - glossary_builder_fn: optional function to run to rebuild glossary (if provided)
    - rebuild_index_fn: optional function that triggers full rebuild (if provided)
    """
    try:
        vector_store.clear_local_faiss()
    except Exception as e:
        print(f"⚠️ clear_local_faiss failed: {e}")

    out = "🧹 Cleared local FAISS files.\n"

    # If a glossary builder was provided, run it (safe)
    if glossary_builder_fn:
        try:
            out += glossary_builder_fn() + "\n"
        except Exception as e:
            out += f"⚠️ Glossary builder failed: {e}\n"

    if rebuild_index_fn:
        try:
            out += rebuild_index_fn()
        except Exception as e:
            out += f"⚠️ Rebuild index failed: {e}\n"

    return out