Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -287,7 +287,7 @@ def find_similar_conversation(
|
|
| 287 |
return {"conversation": conv, "score": score}
|
| 288 |
|
| 289 |
|
| 290 |
-
def find_top_k_similar(question: str, k: int = 3) -> list[dict[str, Any]]:
|
| 291 |
idx = load_embed_index()
|
| 292 |
if not idx or SentenceTransformer is None:
|
| 293 |
return []
|
|
@@ -298,6 +298,8 @@ def find_top_k_similar(question: str, k: int = 3) -> list[dict[str, Any]]:
|
|
| 298 |
|
| 299 |
results: list[tuple[str, float]] = []
|
| 300 |
for cid, data in idx.items():
|
|
|
|
|
|
|
| 301 |
try:
|
| 302 |
vec = np.array(data["vector"], dtype=float)
|
| 303 |
except Exception:
|
|
@@ -592,7 +594,7 @@ async def api(request: Request):
|
|
| 592 |
if match and match.get("conversation"):
|
| 593 |
conversation = match["conversation"]
|
| 594 |
best = best_answer_payload(conversation)
|
| 595 |
-
related = find_top_k_similar(question, k=3)
|
| 596 |
return JSONResponse({
|
| 597 |
"ok": True,
|
| 598 |
"matched": True,
|
|
|
|
| 287 |
return {"conversation": conv, "score": score}
|
| 288 |
|
| 289 |
|
| 290 |
+
def find_top_k_similar(question: str, k: int = 3, exclude_id: str | None = None) -> list[dict[str, Any]]:
|
| 291 |
idx = load_embed_index()
|
| 292 |
if not idx or SentenceTransformer is None:
|
| 293 |
return []
|
|
|
|
| 298 |
|
| 299 |
results: list[tuple[str, float]] = []
|
| 300 |
for cid, data in idx.items():
|
| 301 |
+
if exclude_id and cid == exclude_id:
|
| 302 |
+
continue
|
| 303 |
try:
|
| 304 |
vec = np.array(data["vector"], dtype=float)
|
| 305 |
except Exception:
|
|
|
|
| 594 |
if match and match.get("conversation"):
|
| 595 |
conversation = match["conversation"]
|
| 596 |
best = best_answer_payload(conversation)
|
| 597 |
+
related = find_top_k_similar(question, k=3, exclude_id=conversation.get("id"))
|
| 598 |
return JSONResponse({
|
| 599 |
"ok": True,
|
| 600 |
"matched": True,
|