pluto90 commited on
Commit
bc03939
Β·
verified Β·
1 Parent(s): 8a919a4

Update app/core/rag_service.py

Browse files
Files changed (1) hide show
  1. app/core/rag_service.py +7 -63
app/core/rag_service.py CHANGED
@@ -1,60 +1,6 @@
1
- # # app/core/rag_service.py
2
-
3
- # from app.core.embedding_engine import embedder, COLLECTION_NAME
4
- # from qdrant_client.http.models import Filter, FieldCondition, MatchValue
5
- # from qdrant_client import QdrantClient
6
- # from app.core.config import QDRANT_URL, QDRANT_API_KEY
7
-
8
- # qdrant_client = QdrantClient(
9
- # url=QDRANT_URL,
10
- # api_key=QDRANT_API_KEY,
11
- # check_compatibility=False
12
- # )
13
-
14
- # def get_rag_context(query, doc_id, top_k=3):
15
-
16
- # # βœ… Embed query
17
- # query_vector = embedder.encode(query).tolist()
18
-
19
- # # βœ… Query SINGLE collection + filter by doc_id
20
- # results = qdrant_client.query_points(
21
- # collection_name="smartnotes", # πŸ”₯ FIXED
22
- # query=query_vector,
23
- # limit=top_k,
24
- # query_filter=Filter(
25
- # must=[
26
- # FieldCondition(
27
- # key="doc_id",
28
- # match=MatchValue(value=doc_id)
29
- # )
30
- # ]
31
- # )
32
- # )
33
-
34
- # points = results.points
35
-
36
- # if not points:
37
- # return "", [], []
38
-
39
- # context = "\n".join([p.payload["text"] for p in points])
40
- # sources = [p.payload.get("source") for p in points]
41
- # scores = [p.score for p in points]
42
-
43
- # return context, sources, scores
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
  # app/core/rag_service.py
57
- from app.core.embedding_engine import embed_query, COLLECTION_NAME # βœ… use the correct function
 
58
  from qdrant_client.http.models import Filter, FieldCondition, MatchValue
59
  from qdrant_client import QdrantClient
60
  from app.core.config import QDRANT_URL, QDRANT_API_KEY
@@ -66,14 +12,15 @@ qdrant_client = QdrantClient(
66
  )
67
 
68
 
69
- def get_rag_context(query, doc_id, top_k=5): # βœ… top_k=5 for better recall
70
- query_vector = embed_query(query) # βœ… uses "query: " prefix + returns list
 
71
 
72
  results = qdrant_client.query_points(
73
  collection_name=COLLECTION_NAME,
74
  query=query_vector,
75
  limit=top_k,
76
- score_threshold=0.35, # βœ… filter truly irrelevant results early
77
  query_filter=Filter(
78
  must=[FieldCondition(key="doc_id", match=MatchValue(value=doc_id))]
79
  )
@@ -84,12 +31,9 @@ def get_rag_context(query, doc_id, top_k=5): # βœ… top_k=5 for better recall
84
  if not points:
85
  return "", [], []
86
 
87
- context = "\n\n---\n\n".join([p.payload["text"] for p in points]) # βœ… clearer separator
88
  sources = [p.payload.get("chunk_id", i) for i, p in enumerate(points)]
89
  scores = [p.score for p in points]
90
 
91
  return context, sources, scores
92
 
93
-
94
-
95
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # app/core/rag_service.py
2
+
3
+ from app.core.embedding_engine import embed_query, COLLECTION_NAME
4
  from qdrant_client.http.models import Filter, FieldCondition, MatchValue
5
  from qdrant_client import QdrantClient
6
  from app.core.config import QDRANT_URL, QDRANT_API_KEY
 
12
  )
13
 
14
 
15
+ def get_rag_context(query, doc_id, top_k=5):
16
+ """Retrieve relevant chunks with BGE embeddings."""
17
+ query_vector = embed_query(query)
18
 
19
  results = qdrant_client.query_points(
20
  collection_name=COLLECTION_NAME,
21
  query=query_vector,
22
  limit=top_k,
23
+ score_threshold=0.25, # βœ… LOWERED from 0.35 β€” better recall
24
  query_filter=Filter(
25
  must=[FieldCondition(key="doc_id", match=MatchValue(value=doc_id))]
26
  )
 
31
  if not points:
32
  return "", [], []
33
 
34
+ context = "\n\n---\n\n".join([p.payload["text"] for p in points])
35
  sources = [p.payload.get("chunk_id", i) for i, p in enumerate(points)]
36
  scores = [p.score for p in points]
37
 
38
  return context, sources, scores
39