Spaces:
Running
Running
Commit ·
ebce353
1
Parent(s): e2bfb4b
Fix database schema mismatch causing 500 errors - correct INSERT field mappings
Browse files- backend/database/__init__.py +20 -15
backend/database/__init__.py
CHANGED
|
@@ -144,21 +144,19 @@ def add_sample_data_for_hf():
|
|
| 144 |
'{"scenario": "customer_service", "agents": ["RouterAgent", "OrderAgent", "CompensationAgent", "SupervisorAgent"]}')
|
| 145 |
)
|
| 146 |
|
| 147 |
-
# Insert knowledge graph
|
| 148 |
conn.execute(
|
| 149 |
-
"""INSERT INTO knowledge_graphs (filename,
|
| 150 |
-
|
| 151 |
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?
|
| 152 |
-
("demo_kg.json",
|
| 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"]}'),
|
|
@@ -167,14 +165,19 @@ def add_sample_data_for_hf():
|
|
| 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
|
| 173 |
-
VALUES (?, ?, ?, ?, ?
|
| 174 |
-
(kg_id, entity_id, entity_type, name, properties
|
| 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"}'),
|
|
@@ -183,10 +186,12 @@ def add_sample_data_for_hf():
|
|
| 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,
|
| 188 |
-
|
| 189 |
-
(kg_id, relation_id,
|
| 190 |
)
|
| 191 |
|
| 192 |
# Commit transaction
|
|
|
|
| 144 |
'{"scenario": "customer_service", "agents": ["RouterAgent", "OrderAgent", "CompensationAgent", "SupervisorAgent"]}')
|
| 145 |
)
|
| 146 |
|
| 147 |
+
# Insert knowledge graph with correct field names
|
| 148 |
conn.execute(
|
| 149 |
+
"""INSERT INTO knowledge_graphs (filename, entity_count, relation_count,
|
| 150 |
+
status, trace_id, window_index, window_total, processing_run_id)
|
| 151 |
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
|
| 152 |
+
("demo_kg.json", 5, 4, "completed", trace_id, 0, 1, "demo_run")
|
|
|
|
|
|
|
| 153 |
)
|
| 154 |
|
| 155 |
# Get KG ID
|
| 156 |
kg_result = conn.execute("SELECT id FROM knowledge_graphs WHERE trace_id = ?", (trace_id,))
|
| 157 |
kg_id = kg_result.fetchone()[0]
|
| 158 |
|
| 159 |
+
# Insert sample entities with correct field names
|
| 160 |
entities = [
|
| 161 |
("agent_1", "agent", "RouterAgent", '{"role": "routing", "priority_handling": true}'),
|
| 162 |
("agent_2", "agent", "OrderAgent", '{"role": "order_tracking", "data_sources": ["db", "api"]}'),
|
|
|
|
| 165 |
("issue_1", "issue", "PaymentSystemFailure", '{"severity": "high", "impact": "service_disruption"}')
|
| 166 |
]
|
| 167 |
|
| 168 |
+
# Insert entities and store their database IDs
|
| 169 |
+
entity_db_ids = {}
|
| 170 |
for entity_id, entity_type, name, properties in entities:
|
| 171 |
conn.execute(
|
| 172 |
+
"""INSERT INTO entities (graph_id, entity_id, type, name, properties)
|
| 173 |
+
VALUES (?, ?, ?, ?, ?)""",
|
| 174 |
+
(kg_id, entity_id, entity_type, name, properties)
|
| 175 |
)
|
| 176 |
+
# Get the database ID for this entity
|
| 177 |
+
result = conn.execute("SELECT id FROM entities WHERE graph_id = ? AND entity_id = ?", (kg_id, entity_id))
|
| 178 |
+
entity_db_ids[entity_id] = result.fetchone()[0]
|
| 179 |
|
| 180 |
+
# Insert sample relations using database IDs as foreign keys
|
| 181 |
relations = [
|
| 182 |
("rel_1", "agent_1", "routes_to", "agent_2", '{"priority": "high", "success": true}'),
|
| 183 |
("rel_2", "agent_2", "escalates_to", "agent_3", '{"reason": "compensation_needed"}'),
|
|
|
|
| 186 |
]
|
| 187 |
|
| 188 |
for relation_id, from_entity, relation_type, to_entity, properties in relations:
|
| 189 |
+
source_db_id = entity_db_ids[from_entity]
|
| 190 |
+
target_db_id = entity_db_ids[to_entity]
|
| 191 |
conn.execute(
|
| 192 |
+
"""INSERT INTO relations (graph_id, relation_id, type, source_id, target_id, properties)
|
| 193 |
+
VALUES (?, ?, ?, ?, ?, ?)""",
|
| 194 |
+
(kg_id, relation_id, relation_type, source_db_id, target_db_id, properties)
|
| 195 |
)
|
| 196 |
|
| 197 |
# Commit transaction
|