Spaces:
Runtime error
Runtime error
Update retrieval.py
Browse files- retrieval.py +18 -11
retrieval.py
CHANGED
|
@@ -1,20 +1,27 @@
|
|
| 1 |
import json
|
| 2 |
import numpy as np
|
| 3 |
-
from db import
|
| 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 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|