-
Notifications
You must be signed in to change notification settings - Fork 2
/
interactive_query.py
102 lines (89 loc) · 2.6 KB
/
interactive_query.py
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
import requests
import argparse
import sys
from pathlib import Path
import json
# Add the project root to sys.path
sys.path.append(str(Path(__file__).parent.parent))
from src.utils import jwtutils, queryutils
def main():
parser = argparse.ArgumentParser(
description="Interactive Cortex Search Service API Query CLI"
)
parser.add_argument(
"-u",
"--url",
help="Snowflake account URL",
required=True,
)
parser.add_argument(
"-a",
"--account",
help="Snowflake account name, eg my_org-my_account",
required=True,
)
parser.add_argument(
"-n",
"--user-name",
help="Snowflake user name",
required=True,
)
parser.add_argument(
"-s",
"--qualified-service-name",
help="Qualified name of the Cortex Search Service, eg `MY_DB.MY_SCHEMA.MY_SERVICE`. Case insensitive.",
required=True,
)
parser.add_argument(
"-k",
"--private-key-path",
help="Absolute local path to RSA private key",
required=True,
)
parser.add_argument(
"-c",
"--columns",
help="Comma-separated list of columns to return",
required=True,
)
parser.add_argument(
"-q",
"--query",
help="Query string",
required=True,
)
parser.add_argument(
"-r",
"--role",
help="User role to use for queries. If provided, a session token scoped to this role will be generated for authenticating to the API.",
required=False,
)
args = parser.parse_args()
search_service = queryutils.CortexSearchService(
args.private_key_path,
args.url,
args.account,
args.user_name,
args.qualified_service_name,
args.role,
)
print("\n\nWelcome to the interactive Cortex Search Service query CLI!\n\n")
while True:
query_input = input("Enter your search query or type 'exit' to quit: ").strip()
if query_input.lower() == "exit":
print("Exiting the program.")
break
request_body = {
"columns": args.columns.split(","),
"query": args.query,
"limit": 5,
}
response = search_service.search(request_body=request_body)
if response is not None:
if response.status_code != 200:
print(f"Failed response with status code {response.status_code}")
print(json.dumps(response.json(), indent=4))
else:
print("Failed to fetch data.")
if __name__ == "__main__":
main()