-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_client.py
More file actions
160 lines (136 loc) · 5.16 KB
/
example_client.py
File metadata and controls
160 lines (136 loc) · 5.16 KB
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
#!/usr/bin/env python3
"""Example client for the Query Automation Agent API."""
import sys
import requests
def query_agent(question: str, base_url: str = "http://127.0.0.1:8000"):
"""Send a natural language query to the agent."""
try:
response = requests.post(
f"{base_url}/query",
headers={"Content-Type": "application/json"},
json={"query": question},
timeout=60, # 60 second timeout
)
if response.status_code == 200:
result = response.json()
if result["success"]:
print(f"✓ Query: {result['query']}")
print(f"Generated SQL: {result['sql']}")
print(f"Rows returned: {result['row_count']}")
if result["data"]:
print("Data preview (first 5 rows):")
for i, row in enumerate(result["data"][:5], 1):
print(f" {i}. {row}")
print(
f"Execution: {result['metadata']['successful_steps']}/{result['metadata']['total_steps']} steps successful"
)
return True
else:
print(f"✗ Query failed: {result['error']}")
return False
else:
print(f"API Error: {response.status_code} - {response.text}")
return False
except requests.exceptions.RequestException as e:
print(f"Connection Error: {e}")
return False
except Exception as e:
print(f"Unexpected Error: {e}")
return False
def check_health(base_url: str = "http://127.0.0.1:8000"):
"""Check if the API server is healthy."""
try:
response = requests.get(f"{base_url}/health", timeout=10)
if response.status_code == 200:
health = response.json()
if health["status"] == "healthy":
print("✓ API server is healthy and ready")
return True
else:
print(f"! API server is unhealthy: {health['message']}")
return False
else:
print(f"Health check failed: {response.status_code}")
return False
except Exception as e:
print(f"Cannot connect to API server: {e}")
return False
def get_schema(base_url: str = "http://127.0.0.1:8000"):
"""Get database schema information."""
try:
response = requests.get(f"{base_url}/schema", timeout=30)
if response.status_code == 200:
schema = response.json()
if schema["success"]:
db_info = schema["database_info"]
print(f"Database has {db_info['table_count']} tables:")
for table in db_info["tables"]:
print(f" - {table}")
return True
else:
print(f"✗ Schema request failed: {schema['error']}")
return False
else:
print(f"Schema request error: {response.status_code}")
return False
except Exception as e:
print(f"Schema request error: {e}")
return False
def main():
"""Main function - interactive mode or command line query."""
base_url = "http://127.0.0.1:8000"
print("Query Automation Agent - API Client")
print("=" * 50)
# Check if server is running
if not check_health(base_url):
print("\nMake sure the API server is running with: python run_api.py")
sys.exit(1)
# If query provided as command line argument
if len(sys.argv) > 1:
query = " ".join(sys.argv[1:])
print(f"\nProcessing query: {query}")
print("-" * 50)
query_agent(query, base_url)
return
# Interactive mode
print("\nAvailable commands:")
print(" - Type a natural language query")
print(" - 'schema' - Show database schema")
print(" - 'examples' - Show example queries")
print(" - 'quit' or 'exit' - Exit")
example_queries = [
"Show all employees",
"Find the highest paid employee",
"Count employees by department",
"Show average salary by department",
"List employees hired in the last year",
"Find employees with salary > 50000",
]
while True:
try:
query = input("\nEnter your query: ").strip()
if query.lower() in ["quit", "exit", "q"]:
print("Goodbye!")
break
elif query.lower() == "schema":
print("\nDatabase Schema:")
print("-" * 30)
get_schema(base_url)
elif query.lower() == "examples":
print("\nExample queries:")
print("-" * 20)
for i, example in enumerate(example_queries, 1):
print(f" {i}. {example}")
elif query:
print(f"\nProcessing: {query}")
print("-" * 50)
query_agent(query, base_url)
else:
print("Please enter a query or command")
except KeyboardInterrupt:
print("\n\nGoodbye!")
break
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()