Spaces:
Running
Running
Dipti Dhawade commited on
Commit ·
861e6dd
1
Parent(s): 46acaee
fix: query open file using file and .txt twin paths
Browse files- backend/api.py +36 -18
backend/api.py
CHANGED
|
@@ -729,6 +729,18 @@ def query_notes(
|
|
| 729 |
embed = _get_query_embedder()
|
| 730 |
q_emb = embed.encode([full_q]).tolist()[0]
|
| 731 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 732 |
def _cos(a: list[float], b: list[float]) -> float:
|
| 733 |
dot = sum((x * y) for x, y in zip(a, b))
|
| 734 |
na = (sum((x * x) for x in a) or 1e-9) ** 0.5
|
|
@@ -748,25 +760,31 @@ def query_notes(
|
|
| 748 |
return out
|
| 749 |
|
| 750 |
def _query_path_docs(path: str, n_results: int = 5) -> list[dict]:
|
| 751 |
-
res = col.query(
|
| 752 |
-
query_embeddings=[q_emb],
|
| 753 |
-
n_results=n_results,
|
| 754 |
-
where={"path": path},
|
| 755 |
-
include=["documents", "metadatas", "distances"],
|
| 756 |
-
)
|
| 757 |
-
ids = (res.get("ids") or [[]])[0] if isinstance(res.get("ids"), list) else []
|
| 758 |
-
docs = (res.get("documents") or [[]])[0] if isinstance(res.get("documents"), list) else []
|
| 759 |
-
metas = (res.get("metadatas") or [[]])[0] if isinstance(res.get("metadatas"), list) else []
|
| 760 |
-
dists = (res.get("distances") or [[]])[0] if isinstance(res.get("distances"), list) else []
|
| 761 |
out = []
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 770 |
out.sort(key=lambda d: d.get("_score", -9), reverse=True)
|
| 771 |
return out
|
| 772 |
|
|
|
|
| 729 |
embed = _get_query_embedder()
|
| 730 |
q_emb = embed.encode([full_q]).tolist()[0]
|
| 731 |
|
| 732 |
+
def _query_path_candidates(path: str) -> list[str]:
|
| 733 |
+
"""For image/text-extracted notes, also try the .txt twin path."""
|
| 734 |
+
p = _strip_storage_path(path or "")
|
| 735 |
+
if not p:
|
| 736 |
+
return []
|
| 737 |
+
out = [p]
|
| 738 |
+
stem, ext = os.path.splitext(p)
|
| 739 |
+
if ext and ext.lower() != ".txt":
|
| 740 |
+
out.append(stem + ".txt")
|
| 741 |
+
# preserve order, dedupe
|
| 742 |
+
return list(dict.fromkeys(out))
|
| 743 |
+
|
| 744 |
def _cos(a: list[float], b: list[float]) -> float:
|
| 745 |
dot = sum((x * y) for x, y in zip(a, b))
|
| 746 |
na = (sum((x * x) for x in a) or 1e-9) ** 0.5
|
|
|
|
| 760 |
return out
|
| 761 |
|
| 762 |
def _query_path_docs(path: str, n_results: int = 5) -> list[dict]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 763 |
out = []
|
| 764 |
+
seen_local: set[str] = set()
|
| 765 |
+
for p in _query_path_candidates(path):
|
| 766 |
+
res = col.query(
|
| 767 |
+
query_embeddings=[q_emb],
|
| 768 |
+
n_results=n_results,
|
| 769 |
+
where={"path": p},
|
| 770 |
+
include=["documents", "metadatas", "distances"],
|
| 771 |
+
)
|
| 772 |
+
ids = (res.get("ids") or [[]])[0] if isinstance(res.get("ids"), list) else []
|
| 773 |
+
docs = (res.get("documents") or [[]])[0] if isinstance(res.get("documents"), list) else []
|
| 774 |
+
metas = (res.get("metadatas") or [[]])[0] if isinstance(res.get("metadatas"), list) else []
|
| 775 |
+
dists = (res.get("distances") or [[]])[0] if isinstance(res.get("distances"), list) else []
|
| 776 |
+
for i, cid in enumerate(ids):
|
| 777 |
+
key = f"{p}|{cid}"
|
| 778 |
+
if key in seen_local:
|
| 779 |
+
continue
|
| 780 |
+
seen_local.add(key)
|
| 781 |
+
content = (docs[i] if i < len(docs) else "") or ""
|
| 782 |
+
if not content.strip():
|
| 783 |
+
continue
|
| 784 |
+
md = metas[i] if i < len(metas) and isinstance(metas[i], dict) else {}
|
| 785 |
+
dist = float(dists[i]) if i < len(dists) and dists[i] is not None else 2.0
|
| 786 |
+
score = 1.0 - dist
|
| 787 |
+
out.append({"id": cid, "content": content, "metadata": md, "_score": score})
|
| 788 |
out.sort(key=lambda d: d.get("_score", -9), reverse=True)
|
| 789 |
return out
|
| 790 |
|