1+ #!/usr/bin/env python3
2+ """
3+ Basic usage demo for Querypls backend functionality.
4+ Demonstrates conversation, SQL generation, and CSV analysis.
5+ """
6+
7+ import sys
8+ import os
9+
10+ # Add the project root to Python path
11+ sys .path .insert (0 , os .path .dirname (os .path .dirname (os .path .abspath (__file__ ))))
12+
13+ from src .services .routing_service import IntelligentRoutingService
14+ from src .backend .orchestrator import BackendOrchestrator
15+ from src .schemas .requests import NewChatRequest
16+
17+
18+ def demo_conversation ():
19+ """Demo conversation functionality."""
20+ print ("🗣️ CONVERSATION DEMO" )
21+ print ("=" * 40 )
22+
23+ routing_service = IntelligentRoutingService ()
24+
25+ # Test different conversation types
26+ conversations = [
27+ "Hello" ,
28+ "How are you?" ,
29+ "What can you do?" ,
30+ "Thanks for your help" ,
31+ "Goodbye"
32+ ]
33+
34+ for query in conversations :
35+ print (f"\n User: { query } " )
36+ response = routing_service .handle_conversation_query (query )
37+ print (f"Assistant: { response } " )
38+
39+ print ("\n " + "=" * 40 )
40+
41+
42+ def demo_sql_generation ():
43+ """Demo SQL generation functionality."""
44+ print ("🗃️ SQL GENERATION DEMO" )
45+ print ("=" * 40 )
46+
47+ routing_service = IntelligentRoutingService ()
48+
49+ # Test different SQL queries
50+ sql_queries = [
51+ "Show me all users" ,
52+ "Find customers who made purchases in the last 30 days" ,
53+ "Get the total sales by month" ,
54+ "SELECT * FROM users WHERE status = 'active'"
55+ ]
56+
57+ for query in sql_queries :
58+ print (f"\n User: { query } " )
59+ response = routing_service .handle_sql_query (query , [])
60+ print (f"Assistant: { response [:200 ]} ..." )
61+
62+ print ("\n " + "=" * 40 )
63+
64+
65+ def demo_csv_analysis ():
66+ """Demo CSV analysis functionality."""
67+ print ("📊 CSV ANALYSIS DEMO" )
68+ print ("=" * 40 )
69+
70+ # Sample CSV data
71+ sample_csv = """name,age,salary,department
72+ Alice,25,50000,IT
73+ Bob,30,60000,HR
74+ Charlie,35,70000,IT
75+ Diana,28,55000,Finance
76+ Eve,32,65000,HR"""
77+
78+ print (f"Sample CSV Data:\n { sample_csv } " )
79+
80+ routing_service = IntelligentRoutingService ()
81+
82+ # Test different CSV analysis queries
83+ csv_queries = [
84+ "Show me the basic statistics of the data" ,
85+ "Create a bar chart of department distribution" ,
86+ "What is the average salary by department?" ,
87+ "Show me the top 3 highest paid employees"
88+ ]
89+
90+ for query in csv_queries :
91+ print (f"\n User: { query } " )
92+ response = routing_service .handle_csv_query (query , sample_csv )
93+ print (f"Assistant: { response [:300 ]} ..." )
94+
95+ print ("\n " + "=" * 40 )
96+
97+
98+ def demo_intelligent_routing ():
99+ """Demo intelligent routing functionality."""
100+ print ("🧠 INTELLIGENT ROUTING DEMO" )
101+ print ("=" * 40 )
102+
103+ routing_service = IntelligentRoutingService ()
104+
105+ # Test different types of queries
106+ test_queries = [
107+ ("Hello" , "CONVERSATION_AGENT" ),
108+ ("Show me all users" , "SQL_AGENT" ),
109+ ("Analyze this CSV data" , "CSV_AGENT" ),
110+ ("How are you?" , "CONVERSATION_AGENT" ),
111+ ("SELECT * FROM users" , "SQL_AGENT" ),
112+ ("Create a chart from the data" , "CSV_AGENT" )
113+ ]
114+
115+ for query , expected_agent in test_queries :
116+ print (f"\n Query: '{ query } '" )
117+ decision = routing_service .determine_agent (query , [], csv_loaded = True )
118+ print (f"Expected: { expected_agent } " )
119+ print (f"Actual: { decision .agent } " )
120+ print (f"Confidence: { decision .confidence } " )
121+ print (f"Reasoning: { decision .reasoning } " )
122+
123+ print ("\n " + "=" * 40 )
124+
125+
126+ def demo_orchestrator ():
127+ """Demo the main orchestrator functionality."""
128+ print ("🎼 ORCHESTRATOR DEMO" )
129+ print ("=" * 40 )
130+
131+ orchestrator = BackendOrchestrator ()
132+
133+ # Create a new session
134+ session_info = orchestrator .create_new_session (NewChatRequest (session_name = "Demo Session" ))
135+ session_id = session_info .session_id
136+ print (f"Created session: { session_id } " )
137+
138+ # Test different types of interactions
139+ interactions = [
140+ ("Hello" , "conversation" ),
141+ ("Show me all users" , "sql" ),
142+ ("What can you do?" , "conversation" )
143+ ]
144+
145+ for query , query_type in interactions :
146+ print (f"\n User ({ query_type } ): { query } " )
147+ response = orchestrator .generate_intelligent_response (session_id , query )
148+ print (f"Assistant: { response .content [:150 ]} ..." )
149+
150+ # Test CSV functionality
151+ sample_csv = "name,age,salary\n Alice,25,50000\n Bob,30,60000\n Charlie,35,70000"
152+ result = orchestrator .load_csv_data (session_id , sample_csv )
153+ print (f"\n CSV Load Result: { result ['status' ]} " )
154+
155+ response = orchestrator .generate_intelligent_response (session_id , "Analyze this data" )
156+ print (f"CSV Analysis: { response .content [:200 ]} ..." )
157+
158+ print ("\n " + "=" * 40 )
159+
160+
161+ def main ():
162+ """Run all demos."""
163+ print ("🚀 Querypls Backend Functionality Demo" )
164+ print ("=" * 50 )
165+
166+ demos = [
167+ ("Conversation" , demo_conversation ),
168+ ("SQL Generation" , demo_sql_generation ),
169+ ("CSV Analysis" , demo_csv_analysis ),
170+ ("Intelligent Routing" , demo_intelligent_routing ),
171+ ("Orchestrator" , demo_orchestrator ),
172+ ]
173+
174+ for demo_name , demo_func in demos :
175+ try :
176+ demo_func ()
177+ except Exception as e :
178+ print (f"❌ { demo_name } demo failed: { str (e )} " )
179+
180+ print ("\n 🎉 Demo completed! All backend functionality is working correctly." )
181+ print ("\n 📝 Summary:" )
182+ print ("- Conversation: Natural responses for greetings and help" )
183+ print ("- SQL Generation: Convert natural language to SQL queries" )
184+ print ("- CSV Analysis: Analyze CSV data with Python code" )
185+ print ("- Intelligent Routing: Automatically choose the right agent" )
186+ print ("- Orchestrator: Complete session management" )
187+
188+
189+ if __name__ == "__main__" :
190+ main ()
0 commit comments