File size: 4,697 Bytes
2379054 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | """
KNOWLEDGE BASE VERIFICATION
Check if chunks are correctly created and retrievable
"""
import os
import sys
import json
from pathlib import Path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
def verify_knowledge_base():
print("\n" + "=" * 60)
print(" 🔍 KNOWLEDGE BASE VERIFICATION")
print("=" * 60 + "\n")
# 1. Check ChromaDB
print("1️⃣ Checking ChromaDB...")
try:
import chromadb
client = chromadb.PersistentClient(path="knowledge/chroma_db")
collection = client.get_collection("track_design_knowledge")
count = collection.count()
print(f" ✅ Collection exists with {count} documents")
except Exception as e:
print(f" ❌ Error: {e}")
return False
# 2. Sample query - Network Rail standards
print("\n2️⃣ Testing search - 'Network Rail ballast depth'...")
try:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('all-MiniLM-L6-v2')
query = "Network Rail ballast depth requirement"
embedding = model.encode([query])
results = collection.query(
query_embeddings=embedding.tolist(),
n_results=3
)
if results and results.get('documents'):
print(f" ✅ Found {len(results['documents'][0])} results")
for i, doc in enumerate(results['documents'][0][:3], 1):
print(f"\n Result {i}:")
print(f" Source: {results['metadatas'][0][i-1].get('source', 'Unknown')}")
print(f" Preview: {doc[:200]}...")
else:
print(" ⚠️ No results found")
except Exception as e:
print(f" ❌ Error: {e}")
# 3. Test S&C query
print("\n3️⃣ Testing search - 'S&C switch crossing NR56'...")
try:
query = "NR56 switch and crossing details"
embedding = model.encode([query])
results = collection.query(
query_embeddings=embedding.tolist(),
n_results=3
)
if results and results.get('documents'):
print(f" ✅ Found {len(results['documents'][0])} results")
for i, doc in enumerate(results['documents'][0][:3], 1):
print(f"\n Result {i}:")
print(f" Source: {results['metadatas'][0][i-1].get('source', 'Unknown')}")
print(f" Preview: {doc[:200]}...")
else:
print(" ⚠️ No results found")
except Exception as e:
print(f" ❌ Error: {e}")
# 4. Test SOP query
print("\n4️⃣ Testing search - 'SOP track design procedure'...")
try:
query = "track design SOP procedure"
embedding = model.encode([query])
results = collection.query(
query_embeddings=embedding.tolist(),
n_results=3
)
if results and results.get('documents'):
print(f" ✅ Found {len(results['documents'][0])} results")
for i, doc in enumerate(results['documents'][0][:3], 1):
print(f"\n Result {i}:")
print(f" Source: {results['metadatas'][0][i-1].get('source', 'Unknown')}")
print(f" Preview: {doc[:200]}...")
else:
print(" ⚠️ No results found")
except Exception as e:
print(f" ❌ Error: {e}")
# 5. Check chunk distribution
print("\n5️⃣ Checking chunk distribution...")
try:
# Get all documents
all_results = collection.get()
if all_results and all_results.get('metadatas'):
sources = {}
for meta in all_results['metadatas']:
source = meta.get('source', 'Unknown')
# Clean source name
if 'REPW' in source:
source = source[:15] # Show first 15 chars for REPW files
sources[source] = sources.get(source, 0) + 1
print(f" Total chunks: {len(all_results['metadatas'])}")
print(f" Unique sources: {len(sources)}")
# Show top sources
print("\n Top sources:")
sorted_sources = sorted(sources.items(), key=lambda x: x[1], reverse=True)
for source, count in sorted_sources[:10]:
print(f" - {source}: {count} chunks")
except Exception as e:
print(f" ❌ Error: {e}")
print("\n" + "=" * 60)
print(" ✅ VERIFICATION COMPLETE")
print("=" * 60)
return True
if __name__ == "__main__":
verify_knowledge_base()
|