File size: 815 Bytes
d10de1b
 
76d6b8f
 
 
d10de1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Retrieve the most relevant chunks for a user query."""

from config import TOP_K
from embeddings import embed_query
from vector_store import load_index, search_index

# Cached index loaded once at first call
_index = None
_chunks = None


def get_index():
    global _index, _chunks
    if _index is None:
        _index, _chunks = load_index()
    return _index, _chunks


def retrieve(query: str, top_k: int = TOP_K) -> list[dict]:
    """Return top_k relevant chunks for the given query."""
    index, chunks = get_index()
    query_vec = embed_query(query)
    results = search_index(index, chunks, query_vec, top_k)
    return results


def reset_index_cache():
    """Force reload of the index on next retrieval (useful after rebuilding)."""
    global _index, _chunks
    _index = None
    _chunks = None