File size: 7,123 Bytes
28e46ad
 
 
 
 
 
795ed51
 
28e46ad
 
795ed51
28e46ad
 
 
795ed51
 
28e46ad
795ed51
 
 
 
 
 
 
28e46ad
 
 
 
 
 
795ed51
28e46ad
 
 
 
 
 
 
 
795ed51
 
28e46ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
795ed51
28e46ad
 
795ed51
28e46ad
795ed51
28e46ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
795ed51
28e46ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
795ed51
28e46ad
 
 
 
 
 
 
 
 
 
 
 
 
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
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python3
"""
Seed script to create demo data for the agentic system.
Creates a sample workflow with instructions for testing.
"""

import requests
import json
import time
import os

# Configuration
MCP_URL = os.getenv("MCP_URL", "http://localhost:8000/mcp")
API_KEY = os.getenv("MCP_API_KEY", "dev-key-123")

def call_mcp(tool, params=None):
    """Call the MCP API"""
    response = requests.post(
        MCP_URL,
        headers={"X-API-Key": API_KEY, "Content-Type": "application/json"},
        json={"tool": tool, "params": params or {}}
    )
    return response.json()

def create_demo_workflow():
    """Create a demo workflow for testing"""
    print("🌱 Seeding demo data...")
    
    # Create demo workflow
    workflow_result = call_mcp("write_graph", {
        "action": "create_node",
        "label": "Workflow",
        "properties": {
            "id": "demo-workflow-1",
            "name": "Entity Resolution Demo",
            "description": "Demo workflow showing SQL generation and execution",
            "status": "active",
            "created_at": time.strftime("%Y-%m-%dT%H:%M:%SZ")
        }
    })
    
    if 'created' not in workflow_result:
        print(f"❌ Failed to create workflow: {workflow_result}")
        return False
    
    print("βœ… Created demo workflow")
    
    # Create demo instructions
    instructions = [
        {
            "id": "demo-inst-1",
            "type": "discover_schema",
            "sequence": 1,
            "description": "Discover database schema",
            "parameters": "{}"
        },
        {
            "id": "demo-inst-2", 
            "type": "generate_sql",
            "sequence": 2,
            "description": "Generate SQL for customer orders",
            "parameters": json.dumps({
                "question": "Show me all customers who have placed orders, including their order details"
            })
        },
        {
            "id": "demo-inst-3",
            "type": "review_results", 
            "sequence": 3,
            "description": "Review and format results",
            "parameters": "{}"
        }
    ]
    
    for inst in instructions:
        inst_result = call_mcp("write_graph", {
            "action": "create_node",
            "label": "Instruction",
            "properties": {
                "id": inst["id"],
                "type": inst["type"],
                "sequence": inst["sequence"],
                "description": inst["description"],
                "status": "pending",
                "pause_duration": 300,  # 5 minutes
                "parameters": inst["parameters"],
                "created_at": time.strftime("%Y-%m-%dT%H:%M:%SZ")
            }
        })
        
        if 'created' not in inst_result:
            print(f"❌ Failed to create instruction {inst['id']}: {inst_result}")
            continue
            
        # Link instruction to workflow
        link_result = call_mcp("query_graph", {
            "query": "MATCH (w:Workflow), (i:Instruction) WHERE w.id = wid AND i.id = iid CREATE (w)-[:HAS_INSTRUCTION]->(i)",
            "parameters": {"wid": "demo-workflow-1", "iid": inst["id"]}
        })
        
        print(f"βœ… Created instruction: {inst['type']}")
    
    # Create instruction chain
    for i in range(len(instructions) - 1):
        current_id = instructions[i]["id"]
        next_id = instructions[i + 1]["id"]
        
        chain_result = call_mcp("query_graph", {
            "query": "MATCH (i1:Instruction), (i2:Instruction) WHERE i1.id = id1 AND i2.id = id2 CREATE (i1)-[:NEXT_INSTRUCTION]->(i2)",
            "parameters": {"id1": current_id, "id2": next_id}
        })
    
    print("βœ… Created instruction chain")
    
    # Create query templates for demo
    templates = [
        {
            "id": "template-1",
            "question": "How many customers do we have?",
            "query": "SELECT COUNT(*) as customer_count FROM customers"
        },
        {
            "id": "template-2", 
            "question": "Show me recent orders",
            "query": "SELECT o.id, o.order_date, c.name FROM orders o JOIN customers c ON o.customer_id = c.id ORDER BY o.order_date DESC LIMIT 10"
        },
        {
            "id": "template-3",
            "question": "What's the total revenue?", 
            "query": "SELECT SUM(total_amount) as total_revenue FROM orders"
        }
    ]
    
    for template in templates:
        template_result = call_mcp("write_graph", {
            "action": "create_node",
            "label": "QueryTemplate",
            "properties": {
                "id": template["id"],
                "question": template["question"],
                "query": template["query"],
                "created_at": time.strftime("%Y-%m-%dT%H:%M:%SZ")
            }
        })
    
    print("βœ… Created query templates")
    
    # Verify the setup
    verification = call_mcp("query_graph", {
        "query": """
            MATCH (w:Workflow {id: 'demo-workflow-1'})-[:HAS_INSTRUCTION]->(i:Instruction)
            RETURN w.name, count(i) as instruction_count
        """,
        "parameters": {}
    })
    
    if verification.get('data'):
        workflow_name = verification['data'][0]['w.name']
        instruction_count = verification['data'][0]['instruction_count']
        print(f"βœ… Verification: '{workflow_name}' with {instruction_count} instructions")
    
    print("πŸŽ‰ Demo data seeded successfully!")
    print("\nπŸ“‹ Next steps:")
    print("1. Open http://localhost:3000 in your browser")
    print("2. Ask a question like: 'Show me all customers who have placed orders'")
    print("3. Watch the agent process the workflow")
    print("4. During the 5-minute pause, you can edit instructions in Neo4j Browser")
    print("5. Neo4j Browser: http://localhost:7474 (username: neo4j, password: password)")
    
    return True

def check_services():
    """Check if required services are available"""
    try:
        # Check MCP health
        health_response = requests.get(f"{MCP_URL.replace('/mcp', '/health')}", timeout=5)
        if health_response.status_code != 200:
            print("❌ MCP service not available")
            return False
            
        # Check MCP API
        test_response = requests.post(
            MCP_URL,
            headers={"X-API-Key": API_KEY, "Content-Type": "application/json"},
            json={"tool": "get_schema"},
            timeout=5
        )
        if test_response.status_code != 200:
            print("❌ MCP API not responding")
            return False
            
        print("βœ… Services are available")
        return True
        
    except Exception as e:
        print(f"❌ Service check failed: {e}")
        return False

if __name__ == "__main__":
    print("πŸš€ Starting seed process...")
    
    if not check_services():
        print("❌ Cannot proceed - services not available")
        exit(1)
    
    if create_demo_workflow():
        print("βœ… Seed completed successfully")
        exit(0)
    else:
        print("❌ Seed failed")
        exit(1)