Spaces:
Runtime error
Runtime error
File size: 7,854 Bytes
1813edc | 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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | """
Step 13: Load Sample Data into Qdrant
Creates sample documents for knowledge base and customer history
"""
import sys
from pathlib import Path
# Add project root to path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))
import asyncio
import json
import logging
from datetime import datetime
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
print("\n" + "="*80)
print("π LOADING SAMPLE DATA INTO QDRANT")
print("="*80)
# ============================================================================
# SAMPLE DATA
# ============================================================================
# Knowledge Base Documents (Company Policies, FAQs)
KB_DOCUMENTS = [
{
"id": "kb_001",
"title": "Return Policy",
"content": """
Return Policy: Customers can return unopened products within 30 days of purchase
for a full refund. Items must be in original condition with all packaging and accessories.
Refunds are processed within 5-7 business days. Shipping costs are non-refundable unless
the return is due to our error. For defective items, we offer replacements immediately.
"""
},
{
"id": "kb_002",
"title": "Shipping Information",
"content": """
Shipping Options: We offer standard shipping (5-7 days), express shipping (2-3 days),
and overnight shipping. Standard shipping is free for orders over $50. Tracking
information is provided via email. All orders are insured. We ship to most countries
worldwide. International orders may have customs delays.
"""
},
{
"id": "kb_003",
"title": "Product Warranty",
"content": """
Warranty Coverage: All electronics come with a 1-year manufacturer's warranty covering
defects in materials and workmanship. Warranty does not cover physical damage, water damage,
or normal wear. Warranty service is available through our support team or authorized
service centers. Extended warranty options are available for 2 or 3 years.
"""
},
{
"id": "kb_004",
"title": "Account Management",
"content": """
Account Features: Create an account to track orders, save preferences, and manage
payment methods. Password requirements: minimum 8 characters with upper/lowercase,
numbers, and symbols. Two-factor authentication available for security. Account
information can be updated anytime in settings. Contact support to delete account.
"""
}
]
# Customer History Records (Previous Interactions)
CUSTOMER_HISTORY = [
{
"customer_id": "CUST_001",
"interaction_type": "complaint",
"text": "Customer complained about slow shipping on previous order. Resolution: expedited reshipment provided."
},
{
"customer_id": "CUST_001",
"interaction_type": "purchase",
"text": "Purchased laptop model XPS-15 on 2025-11-20. Status: delivered. Customer satisfied."
},
{
"customer_id": "CUST_002",
"interaction_type": "inquiry",
"text": "Asked about warranty coverage for defective phone. Explained 1-year coverage policy. Customer satisfied."
},
{
"customer_id": "CUST_002",
"interaction_type": "refund_request",
"text": "Requested refund for unopened tablet within 30-day window. Refund approved and processed."
}
]
# ============================================================================
# LOAD DATA
# ============================================================================
async def load_sample_data():
"""Load sample data into Qdrant"""
try:
from rag.qdrant_manager import qdrant_manager
from rag.embedding_manager import embedding_manager
print("\n[1] Initializing managers...")
print(f" β
Qdrant Manager: {qdrant_manager}")
print(f" β
Embedding Manager: {embedding_manager}")
# ========== LOAD KNOWLEDGE BASE ==========
print("\n[2] Loading Knowledge Base documents...")
print(f" Documents to load: {len(KB_DOCUMENTS)}")
for doc in KB_DOCUMENTS:
try:
# Create document object for Qdrant
text = f"Title: {doc['title']}\n\n{doc['content']}"
logger.info(f"Adding KB doc: {doc['id']}")
# Add to knowledge base using qdrant_manager
qdrant_manager.add_to_kb(
documents=[{
"id": doc['id'],
"text": text,
"title": doc['title']
}]
)
print(f" β
{doc['title']} (ID: {doc['id']})")
except Exception as e:
print(f" β Error loading {doc['id']}: {str(e)}")
print(" β
Knowledge Base loaded")
# ========== LOAD CUSTOMER HISTORY ==========
print("\n[3] Loading Customer History...")
print(f" Records to load: {len(CUSTOMER_HISTORY)}")
for record in CUSTOMER_HISTORY:
try:
logger.info(f"Adding history for {record['customer_id']}")
# Add to customer history using qdrant_manager
qdrant_manager.add_to_history(
customer_id=record['customer_id'],
text=record['text'],
interaction_type=record['interaction_type']
)
print(f" β
{record['customer_id']}: {record['interaction_type']} ({len(record['text'])} chars)")
except Exception as e:
print(f" β Error loading history for {record['customer_id']}: {str(e)}")
print(" β
Customer History loaded")
# ========== VERIFY DATA ==========
print("\n[4] Verifying loaded data...")
try:
kb_info = qdrant_manager.get_collection_info("knowledge_base")
print(f" β
Knowledge Base:")
print(f" - Name: knowledge_base")
print(f" - Vector size: {kb_info.get('vector_size', 'N/A')}")
print(f" - Points count: {kb_info.get('points_count', 'N/A')}")
hist_info = qdrant_manager.get_collection_info("customer_history")
print(f" β
Customer History:")
print(f" - Name: customer_history")
print(f" - Vector size: {hist_info.get('vector_size', 'N/A')}")
print(f" - Points count: {hist_info.get('points_count', 'N/A')}")
except Exception as e:
print(f" β οΈ Could not verify: {str(e)}")
print("\n" + "="*80)
print("β
SAMPLE DATA LOADED SUCCESSFULLY")
print("="*80)
print("\nπ Summary:")
print(f" β’ Knowledge Base: {len(KB_DOCUMENTS)} documents loaded")
print(f" β’ Customer History: {len(CUSTOMER_HISTORY)} records loaded")
print("\nπ― Data is now available for:")
print(" β’ KB Search (retrieval_router node)")
print(" β’ Customer History Context (conditional retrieval)")
print(" β’ Personalized responses based on customer history")
print("="*80 + "\n")
return True
except Exception as e:
print(f"\nβ ERROR: {str(e)}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
print("\nπ Starting sample data loader...\n")
success = asyncio.run(load_sample_data())
sys.exit(0 if success else 1)
|