Spaces:
Sleeping
Sleeping
Update retriever.py
Browse files- retriever.py +85 -5
retriever.py
CHANGED
|
@@ -1,9 +1,89 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
|
|
|
| 3 |
|
| 4 |
-
VECTOR_DB_DIR = "vector_db"
|
| 5 |
|
| 6 |
def get_retriever():
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Unified retriever with clean reference formatting.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
from pathlib import Path
|
| 8 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 9 |
+
from langchain_community.vectorstores import Chroma
|
| 10 |
|
|
|
|
| 11 |
|
| 12 |
def get_retriever():
|
| 13 |
+
"""Get retriever with consistent formatting"""
|
| 14 |
+
|
| 15 |
+
# Locate vector_db
|
| 16 |
+
vector_db_path = Path("vector_db")
|
| 17 |
+
if not vector_db_path.exists():
|
| 18 |
+
vector_db_path = Path("../vector_db")
|
| 19 |
+
|
| 20 |
+
if not vector_db_path.exists():
|
| 21 |
+
raise FileNotFoundError("Vector database not found")
|
| 22 |
+
|
| 23 |
+
print(f"Loading from: {vector_db_path.absolute()}")
|
| 24 |
+
|
| 25 |
+
# Init embedding model
|
| 26 |
+
embedding_model = HuggingFaceEmbeddings(model_name="BAAI/bge-small-en")
|
| 27 |
+
|
| 28 |
+
# Load the correct collection
|
| 29 |
+
vectordb = Chroma(
|
| 30 |
+
persist_directory=str(vector_db_path.absolute()),
|
| 31 |
+
embedding_function=embedding_model,
|
| 32 |
+
collection_name="legal_documents"
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
count = vectordb._collection.count()
|
| 36 |
+
print(f"Found {count} documents in collection 'legal_documents'")
|
| 37 |
+
|
| 38 |
+
if count == 0:
|
| 39 |
+
raise ValueError("Collection 'legal_documents' is empty")
|
| 40 |
+
|
| 41 |
+
return vectordb.as_retriever(search_kwargs={"k": 5})
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def format_reference(doc) -> str:
|
| 45 |
+
"""
|
| 46 |
+
Format metadata into: [doc_type] Section — Source
|
| 47 |
+
"""
|
| 48 |
+
doc_type = doc.metadata.get("document_type", "general")
|
| 49 |
+
section = doc.metadata.get("section", "Unknown Section")
|
| 50 |
+
source = doc.metadata.get("source", "Unknown Source")
|
| 51 |
+
return f"[{doc_type}] {section} — {source}"
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def test_retriever():
|
| 55 |
+
"""Test the retriever with sample queries"""
|
| 56 |
+
try:
|
| 57 |
+
retriever = get_retriever()
|
| 58 |
+
|
| 59 |
+
test_queries = [
|
| 60 |
+
"constitutional rights",
|
| 61 |
+
"FCCPA tribunal",
|
| 62 |
+
"labour law employment",
|
| 63 |
+
"data protection"
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
for query in test_queries:
|
| 67 |
+
print(f"\nTesting: '{query}'")
|
| 68 |
+
results = retriever.invoke(query)
|
| 69 |
+
print(f" Found {len(results)} results")
|
| 70 |
+
|
| 71 |
+
for doc in results:
|
| 72 |
+
ref = format_reference(doc)
|
| 73 |
+
preview = doc.page_content[:150].replace("\n", " ")
|
| 74 |
+
print(f" {ref}")
|
| 75 |
+
print(f" Preview: {preview}...")
|
| 76 |
+
|
| 77 |
+
return True
|
| 78 |
+
|
| 79 |
+
except Exception as e:
|
| 80 |
+
print(f"✗ Error: {e}")
|
| 81 |
+
return False
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
success = test_retriever()
|
| 86 |
+
if success:
|
| 87 |
+
print("\n✓ Retriever is working correctly with formatted references!")
|
| 88 |
+
else:
|
| 89 |
+
print("\n✗ Retriever test failed")
|