Vanshcc commited on
Commit
879a310
·
verified ·
1 Parent(s): a313919

Update retrieval.py

Browse files
Files changed (1) hide show
  1. retrieval.py +18 -11
retrieval.py CHANGED
@@ -1,20 +1,27 @@
1
  import json
2
  import numpy as np
3
- from db import cursor
4
  from embeddings import embedding_model, cosine_similarity
5
 
6
  def retrieve_top_chunks(question, k=3):
7
  q_embedding = embedding_model.encode(question)
8
 
9
- cursor.execute(
10
- "SELECT document, chunk_id, text, embedding FROM chunks"
11
- )
 
 
 
 
12
 
13
- results = []
14
- for doc, cid, text, emb_json in cursor.fetchall():
15
- emb = np.array(json.loads(emb_json))
16
- score = cosine_similarity(q_embedding, emb)
17
- results.append((score, doc, cid, text))
18
 
19
- results.sort(key=lambda x: x[0], reverse=True)
20
- return results[:k]
 
 
 
 
1
  import json
2
  import numpy as np
3
+ from db import get_db_connection
4
  from embeddings import embedding_model, cosine_similarity
5
 
6
  def retrieve_top_chunks(question, k=3):
7
  q_embedding = embedding_model.encode(question)
8
 
9
+ conn = get_db_connection()
10
+ cursor = conn.cursor()
11
+
12
+ try:
13
+ cursor.execute(
14
+ "SELECT document, chunk_id, text, embedding FROM chunks"
15
+ )
16
 
17
+ results = []
18
+ for doc, cid, text, emb_json in cursor.fetchall():
19
+ emb = np.array(json.loads(emb_json))
20
+ score = cosine_similarity(q_embedding, emb)
21
+ results.append((score, doc, cid, text))
22
 
23
+ results.sort(key=lambda x: x[0], reverse=True)
24
+ return results[:k]
25
+ finally:
26
+ cursor.close()
27
+ conn.close()