Spaces:
Running
Running
Commit ·
e2bfb4b
1
Parent(s): aa7cc4d
Simplify sample data loading with direct SQL to fix HF Spaces connection issues
Browse files- backend/database/__init__.py +122 -261
backend/database/__init__.py
CHANGED
|
@@ -80,278 +80,139 @@ def test_database_connection():
|
|
| 80 |
return False
|
| 81 |
|
| 82 |
def add_sample_data_for_hf():
|
| 83 |
-
"""Add sample data for HF Spaces using
|
| 84 |
if not os.getenv("SPACE_ID"):
|
| 85 |
return # Only run on HF Spaces
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
return
|
| 91 |
|
| 92 |
-
import json
|
| 93 |
-
import uuid
|
| 94 |
-
import hashlib
|
| 95 |
-
from backend.database.models import Trace, KnowledgeGraph, Entity, Relation
|
| 96 |
-
|
| 97 |
-
# Use SessionLocal instead of scoped Session for better control
|
| 98 |
-
session = SessionLocal()
|
| 99 |
try:
|
| 100 |
-
#
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
existing_traces = session.query(Trace).count()
|
| 105 |
-
existing_kgs = session.query(KnowledgeGraph).count()
|
| 106 |
-
|
| 107 |
-
print(f"🔍 HF Spaces environment check:")
|
| 108 |
-
print(f" • SPACE_ID: {os.getenv('SPACE_ID')}")
|
| 109 |
-
print(f" • Database URL: {DATABASE_URL}")
|
| 110 |
-
print(f" • Existing traces: {existing_traces}")
|
| 111 |
-
print(f" • Existing KGs: {existing_kgs}")
|
| 112 |
-
|
| 113 |
-
if existing_traces > 0 or existing_kgs > 0:
|
| 114 |
-
print("📊 Sample data already exists, skipping...")
|
| 115 |
-
return # Data already exists
|
| 116 |
-
|
| 117 |
-
print("🎯 HF Spaces: Adding sample data to in-memory database...")
|
| 118 |
-
|
| 119 |
-
# Sample trace content - Multi-agent customer service scenario
|
| 120 |
-
sample_trace_content = '''[
|
| 121 |
-
{
|
| 122 |
-
"role": "user",
|
| 123 |
-
"content": "I need help with my order #12345. It was supposed to arrive yesterday but I still haven't received it. This is very frustrating as I need it for an important meeting tomorrow.",
|
| 124 |
-
"timestamp": "2024-08-31T10:00:00Z",
|
| 125 |
-
"metadata": {"priority": "high", "emotion": "frustrated"}
|
| 126 |
-
},
|
| 127 |
-
{
|
| 128 |
-
"role": "assistant",
|
| 129 |
-
"name": "RouterAgent",
|
| 130 |
-
"content": "I understand your frustration about the delayed order. Let me route this to our order tracking specialist who can provide detailed information about your shipment.",
|
| 131 |
-
"timestamp": "2024-08-31T10:00:15Z",
|
| 132 |
-
"actions": [{"type": "route", "target": "OrderTrackingAgent", "priority": "high"}]
|
| 133 |
-
},
|
| 134 |
-
{
|
| 135 |
-
"role": "assistant",
|
| 136 |
-
"name": "OrderTrackingAgent",
|
| 137 |
-
"content": "I'll look up order #12345 immediately. Checking our tracking system... I found the issue - there was a delay at the distribution center due to weather conditions. Your package is currently in transit and should arrive by 2 PM today.",
|
| 138 |
-
"timestamp": "2024-08-31T10:01:00Z",
|
| 139 |
-
"actions": [
|
| 140 |
-
{"type": "database_query", "query": "SELECT * FROM orders WHERE order_id = '12345'"},
|
| 141 |
-
{"type": "api_call", "endpoint": "shipping_tracker", "response": "in_transit"}
|
| 142 |
-
],
|
| 143 |
-
"findings": {
|
| 144 |
-
"issue": "Weather delay at distribution center",
|
| 145 |
-
"current_status": "In transit",
|
| 146 |
-
"estimated_delivery": "2024-08-31T14:00:00Z"
|
| 147 |
-
}
|
| 148 |
-
},
|
| 149 |
-
{
|
| 150 |
-
"role": "assistant",
|
| 151 |
-
"name": "CompensationAgent",
|
| 152 |
-
"content": "Given this delay caused inconvenience, I'm authorizing a $10 credit to your account and providing free expedited shipping on your next order. I'm also escalating this to ensure better communication about delays in the future.",
|
| 153 |
-
"timestamp": "2024-08-31T10:02:00Z",
|
| 154 |
-
"actions": [
|
| 155 |
-
{"type": "credit_account", "amount": 10, "reason": "delivery_delay"},
|
| 156 |
-
{"type": "add_benefit", "type": "free_expedited_shipping"},
|
| 157 |
-
{"type": "escalate", "department": "operations", "issue": "delayed_delivery_communication"}
|
| 158 |
-
],
|
| 159 |
-
"error": "Failed to process account credit - payment system temporarily unavailable"
|
| 160 |
-
},
|
| 161 |
-
{
|
| 162 |
-
"role": "assistant",
|
| 163 |
-
"name": "SupervisorAgent",
|
| 164 |
-
"content": "I see there was an error processing the account credit. I'm manually flagging your account for the $10 credit and expedited shipping benefit. Our technical team will resolve the payment system issue. You should see the credit within 24 hours.",
|
| 165 |
-
"timestamp": "2024-08-31T10:03:00Z",
|
| 166 |
-
"actions": [
|
| 167 |
-
{"type": "manual_flag", "account_credit": 10},
|
| 168 |
-
{"type": "technical_escalation", "system": "payment_processor"},
|
| 169 |
-
{"type": "follow_up_schedule", "timeframe": "24_hours"}
|
| 170 |
-
],
|
| 171 |
-
"optimization_opportunity": "Implement payment system redundancy to prevent credit processing failures"
|
| 172 |
-
},
|
| 173 |
-
{
|
| 174 |
-
"role": "user",
|
| 175 |
-
"content": "Thank you for the quick response and compensation. I appreciate you taking care of this issue promptly.",
|
| 176 |
-
"timestamp": "2024-08-31T10:04:00Z",
|
| 177 |
-
"sentiment": "satisfied"
|
| 178 |
-
},
|
| 179 |
-
{
|
| 180 |
-
"role": "assistant",
|
| 181 |
-
"name": "QualityAgent",
|
| 182 |
-
"content": "Session completed successfully. Customer satisfaction restored. Identified system improvement: need better proactive communication about shipping delays and payment system reliability backup.",
|
| 183 |
-
"timestamp": "2024-08-31T10:05:00Z",
|
| 184 |
-
"session_analysis": {
|
| 185 |
-
"resolution_time": "5 minutes",
|
| 186 |
-
"customer_satisfaction": "high",
|
| 187 |
-
"agents_involved": 4,
|
| 188 |
-
"system_errors": 1,
|
| 189 |
-
"optimization_recommendations": [
|
| 190 |
-
"Implement proactive delay notifications",
|
| 191 |
-
"Add payment system redundancy",
|
| 192 |
-
"Improve agent handoff protocols"
|
| 193 |
-
]
|
| 194 |
-
}
|
| 195 |
-
}
|
| 196 |
-
]'''
|
| 197 |
-
|
| 198 |
-
# Generate trace metadata
|
| 199 |
-
trace_id = str(uuid.uuid4())
|
| 200 |
-
content_hash = hashlib.sha256(sample_trace_content.encode()).hexdigest()
|
| 201 |
-
|
| 202 |
-
# Create and save sample trace
|
| 203 |
-
sample_trace = Trace(
|
| 204 |
-
trace_id=trace_id,
|
| 205 |
-
filename="sample_customer_service.json",
|
| 206 |
-
title="Multi-Agent Customer Service Resolution",
|
| 207 |
-
description="Demonstration of multi-agent system handling customer complaint with error handling and optimization opportunities",
|
| 208 |
-
content=sample_trace_content,
|
| 209 |
-
content_hash=content_hash,
|
| 210 |
-
uploader="AgentGraph Demo",
|
| 211 |
-
trace_type="multi_agent",
|
| 212 |
-
trace_source="sample",
|
| 213 |
-
character_count=len(sample_trace_content),
|
| 214 |
-
turn_count=6,
|
| 215 |
-
status="processed",
|
| 216 |
-
tags=json.dumps(["sample", "customer_service", "multi_agent", "error_handling", "optimization"]),
|
| 217 |
-
trace_metadata=json.dumps({
|
| 218 |
-
"scenario": "customer_service",
|
| 219 |
-
"agents": ["RouterAgent", "OrderTrackingAgent", "CompensationAgent", "SupervisorAgent", "QualityAgent"],
|
| 220 |
-
"domain": "e_commerce",
|
| 221 |
-
"complexity": "high"
|
| 222 |
-
})
|
| 223 |
-
)
|
| 224 |
-
|
| 225 |
-
session.add(sample_trace)
|
| 226 |
-
session.flush() # Get the trace ID
|
| 227 |
-
|
| 228 |
-
# Create sample knowledge graph
|
| 229 |
-
sample_kg = KnowledgeGraph(
|
| 230 |
-
filename="sample_customer_service_kg.json",
|
| 231 |
-
creator="AgentGraph Demo",
|
| 232 |
-
entity_count=15,
|
| 233 |
-
relation_count=18,
|
| 234 |
-
namespace="customer_service_demo",
|
| 235 |
-
system_name="Multi-Agent Customer Service System",
|
| 236 |
-
system_summary="An intelligent customer service system featuring multiple specialized agents working together to resolve customer issues, handle errors, and identify optimization opportunities. The system demonstrates sophisticated agent coordination, error recovery mechanisms, and continuous improvement processes.",
|
| 237 |
-
status="completed",
|
| 238 |
-
trace_id=trace_id,
|
| 239 |
-
window_index=0,
|
| 240 |
-
window_total=1,
|
| 241 |
-
processing_run_id="sample_demo_run"
|
| 242 |
-
)
|
| 243 |
-
|
| 244 |
-
session.add(sample_kg)
|
| 245 |
-
session.flush() # Get the KG ID
|
| 246 |
-
|
| 247 |
-
# Sample entities with rich properties
|
| 248 |
-
entities_data = [
|
| 249 |
-
# Agents
|
| 250 |
-
("agent_1", "agent", "RouterAgent", {"role": "traffic_routing", "specialization": "request_classification", "priority_handling": True}),
|
| 251 |
-
("agent_2", "agent", "OrderTrackingAgent", {"role": "order_management", "specialization": "shipping_tracking", "data_sources": ["internal_db", "shipping_apis"]}),
|
| 252 |
-
("agent_3", "agent", "CompensationAgent", {"role": "customer_retention", "specialization": "compensation_authorization", "max_credit_limit": 50}),
|
| 253 |
-
("agent_4", "agent", "SupervisorAgent", {"role": "escalation_handling", "specialization": "system_error_recovery", "override_authority": True}),
|
| 254 |
-
("agent_5", "agent", "QualityAgent", {"role": "quality_assurance", "specialization": "session_analysis", "improvement_tracking": True}),
|
| 255 |
-
|
| 256 |
-
# Systems and Tools
|
| 257 |
-
("system_1", "system", "OrderDatabase", {"type": "database", "function": "order_storage", "performance": "high"}),
|
| 258 |
-
("system_2", "system", "ShippingTracker", {"type": "external_api", "function": "package_tracking", "reliability": "99.5%"}),
|
| 259 |
-
("system_3", "system", "PaymentProcessor", {"type": "financial_system", "function": "account_credits", "status": "temporarily_unavailable"}),
|
| 260 |
-
|
| 261 |
-
# Issues and Problems
|
| 262 |
-
("issue_1", "issue", "DeliveryDelay", {"severity": "medium", "cause": "weather_conditions", "impact": "customer_satisfaction"}),
|
| 263 |
-
("issue_2", "issue", "PaymentSystemFailure", {"severity": "high", "cause": "system_unavailability", "impact": "compensation_processing"}),
|
| 264 |
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
except Exception as e:
|
| 348 |
print(f"❌ Failed to add sample data: {e}")
|
| 349 |
import traceback
|
| 350 |
print("Full error traceback:")
|
| 351 |
traceback.print_exc()
|
| 352 |
-
session.rollback()
|
| 353 |
-
finally:
|
| 354 |
-
session.close()
|
| 355 |
|
| 356 |
__all__ = [
|
| 357 |
'get_db',
|
|
|
|
| 80 |
return False
|
| 81 |
|
| 82 |
def add_sample_data_for_hf():
|
| 83 |
+
"""Add simple sample data for HF Spaces using direct SQL."""
|
| 84 |
if not os.getenv("SPACE_ID"):
|
| 85 |
return # Only run on HF Spaces
|
| 86 |
|
| 87 |
+
print(f"🔍 HF Spaces environment check:")
|
| 88 |
+
print(f" • SPACE_ID: {os.getenv('SPACE_ID')}")
|
| 89 |
+
print(f" • Database URL: {DATABASE_URL}")
|
|
|
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
try:
|
| 92 |
+
# Use direct connection to avoid session issues
|
| 93 |
+
with engine.connect() as conn:
|
| 94 |
+
# Begin transaction for atomic operations
|
| 95 |
+
trans = conn.begin()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
try:
|
| 98 |
+
# Check if data already exists
|
| 99 |
+
result = conn.execute("SELECT COUNT(*) FROM traces").fetchone()
|
| 100 |
+
existing_traces = result[0] if result else 0
|
| 101 |
+
|
| 102 |
+
result = conn.execute("SELECT COUNT(*) FROM knowledge_graphs").fetchone()
|
| 103 |
+
existing_kgs = result[0] if result else 0
|
| 104 |
+
|
| 105 |
+
print(f" • Existing traces: {existing_traces}")
|
| 106 |
+
print(f" • Existing KGs: {existing_kgs}")
|
| 107 |
+
|
| 108 |
+
if existing_traces > 0 or existing_kgs > 0:
|
| 109 |
+
print("📊 Sample data already exists, skipping...")
|
| 110 |
+
return
|
| 111 |
+
|
| 112 |
+
print("🎯 Adding sample data using direct SQL...")
|
| 113 |
+
|
| 114 |
+
import json
|
| 115 |
+
import uuid
|
| 116 |
+
import hashlib
|
| 117 |
+
from datetime import datetime
|
| 118 |
+
|
| 119 |
+
# Simple trace content
|
| 120 |
+
sample_trace_content = '''[
|
| 121 |
+
{"role": "user", "content": "I need help with my delayed order #12345. This is frustrating!", "timestamp": "2024-08-31T10:00:00Z"},
|
| 122 |
+
{"role": "assistant", "name": "RouterAgent", "content": "Let me route you to our order specialist.", "timestamp": "2024-08-31T10:00:15Z"},
|
| 123 |
+
{"role": "assistant", "name": "OrderAgent", "content": "I found the issue - weather delay. Your package arrives today at 2 PM.", "timestamp": "2024-08-31T10:01:00Z"},
|
| 124 |
+
{"role": "assistant", "name": "CompensationAgent", "content": "I'll authorize a $10 credit for the inconvenience.", "timestamp": "2024-08-31T10:02:00Z", "error": "Payment system unavailable"},
|
| 125 |
+
{"role": "assistant", "name": "SupervisorAgent", "content": "I'll manually flag your account for the credit. Technical team notified.", "timestamp": "2024-08-31T10:03:00Z"},
|
| 126 |
+
{"role": "user", "content": "Thank you for the quick resolution!", "timestamp": "2024-08-31T10:04:00Z", "sentiment": "satisfied"}
|
| 127 |
+
]'''
|
| 128 |
+
|
| 129 |
+
# Generate IDs
|
| 130 |
+
trace_id = str(uuid.uuid4())
|
| 131 |
+
content_hash = hashlib.sha256(sample_trace_content.encode()).hexdigest()
|
| 132 |
+
now = datetime.utcnow()
|
| 133 |
+
|
| 134 |
+
# Insert trace
|
| 135 |
+
conn.execute(
|
| 136 |
+
"""INSERT INTO traces (trace_id, filename, title, description, content, content_hash,
|
| 137 |
+
upload_timestamp, update_timestamp, uploader, trace_type, trace_source,
|
| 138 |
+
character_count, turn_count, status, tags, trace_metadata)
|
| 139 |
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
|
| 140 |
+
(trace_id, "sample_demo.json", "Multi-Agent Customer Service Demo",
|
| 141 |
+
"Demo showing agent coordination and error handling", sample_trace_content, content_hash,
|
| 142 |
+
now, now, "AgentGraph Demo", "multi_agent", "sample", len(sample_trace_content), 6,
|
| 143 |
+
"processed", '["demo", "customer_service", "multi_agent"]',
|
| 144 |
+
'{"scenario": "customer_service", "agents": ["RouterAgent", "OrderAgent", "CompensationAgent", "SupervisorAgent"]}')
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
# Insert knowledge graph
|
| 148 |
+
conn.execute(
|
| 149 |
+
"""INSERT INTO knowledge_graphs (filename, creator, entity_count, relation_count, namespace,
|
| 150 |
+
system_name, system_summary, status, trace_id, window_index, window_total, processing_run_id)
|
| 151 |
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
|
| 152 |
+
("demo_kg.json", "AgentGraph Demo", 5, 4, "demo", "Customer Service System",
|
| 153 |
+
"Multi-agent customer service system with error handling and coordination",
|
| 154 |
+
"completed", trace_id, 0, 1, "demo_run")
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
# Get KG ID
|
| 158 |
+
kg_result = conn.execute("SELECT id FROM knowledge_graphs WHERE trace_id = ?", (trace_id,))
|
| 159 |
+
kg_id = kg_result.fetchone()[0]
|
| 160 |
+
|
| 161 |
+
# Insert sample entities
|
| 162 |
+
entities = [
|
| 163 |
+
("agent_1", "agent", "RouterAgent", '{"role": "routing", "priority_handling": true}'),
|
| 164 |
+
("agent_2", "agent", "OrderAgent", '{"role": "order_tracking", "data_sources": ["db", "api"]}'),
|
| 165 |
+
("agent_3", "agent", "CompensationAgent", '{"role": "compensation", "max_credit": 50}'),
|
| 166 |
+
("agent_4", "agent", "SupervisorAgent", '{"role": "escalation", "override_authority": true}'),
|
| 167 |
+
("issue_1", "issue", "PaymentSystemFailure", '{"severity": "high", "impact": "service_disruption"}')
|
| 168 |
+
]
|
| 169 |
+
|
| 170 |
+
for entity_id, entity_type, name, properties in entities:
|
| 171 |
+
conn.execute(
|
| 172 |
+
"""INSERT INTO entities (graph_id, entity_id, type, name, properties, knowledge_graph_namespace)
|
| 173 |
+
VALUES (?, ?, ?, ?, ?, ?)""",
|
| 174 |
+
(kg_id, entity_id, entity_type, name, properties, "demo")
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
# Insert sample relations
|
| 178 |
+
relations = [
|
| 179 |
+
("rel_1", "agent_1", "routes_to", "agent_2", '{"priority": "high", "success": true}'),
|
| 180 |
+
("rel_2", "agent_2", "escalates_to", "agent_3", '{"reason": "compensation_needed"}'),
|
| 181 |
+
("rel_3", "agent_3", "escalates_to", "agent_4", '{"reason": "system_error"}'),
|
| 182 |
+
("rel_4", "agent_4", "resolves", "issue_1", '{"method": "manual_override"}')
|
| 183 |
+
]
|
| 184 |
+
|
| 185 |
+
for relation_id, from_entity, relation_type, to_entity, properties in relations:
|
| 186 |
+
conn.execute(
|
| 187 |
+
"""INSERT INTO relations (graph_id, relation_id, from_entity_id, relation_type, to_entity_id,
|
| 188 |
+
properties, knowledge_graph_namespace) VALUES (?, ?, ?, ?, ?, ?, ?)""",
|
| 189 |
+
(kg_id, relation_id, from_entity, relation_type, to_entity, properties, "demo")
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
# Commit transaction
|
| 193 |
+
trans.commit()
|
| 194 |
+
|
| 195 |
+
# Verify data
|
| 196 |
+
final_traces = conn.execute("SELECT COUNT(*) FROM traces").fetchone()[0]
|
| 197 |
+
final_kgs = conn.execute("SELECT COUNT(*) FROM knowledge_graphs").fetchone()[0]
|
| 198 |
+
final_entities = conn.execute("SELECT COUNT(*) FROM entities").fetchone()[0]
|
| 199 |
+
final_relations = conn.execute("SELECT COUNT(*) FROM relations").fetchone()[0]
|
| 200 |
+
|
| 201 |
+
print("✅ Sample data added successfully!")
|
| 202 |
+
print(f" • Traces: {final_traces}")
|
| 203 |
+
print(f" • Knowledge graphs: {final_kgs}")
|
| 204 |
+
print(f" • Entities: {final_entities}")
|
| 205 |
+
print(f" • Relations: {final_relations}")
|
| 206 |
+
|
| 207 |
+
except Exception as e:
|
| 208 |
+
trans.rollback()
|
| 209 |
+
raise e
|
| 210 |
+
|
| 211 |
except Exception as e:
|
| 212 |
print(f"❌ Failed to add sample data: {e}")
|
| 213 |
import traceback
|
| 214 |
print("Full error traceback:")
|
| 215 |
traceback.print_exc()
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
__all__ = [
|
| 218 |
'get_db',
|