-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
136 lines (125 loc) · 3.74 KB
/
test.py
File metadata and controls
136 lines (125 loc) · 3.74 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
import os
import json
import requests
from dotenv import load_dotenv
import generate_jwt
from generate_jwt import JWTGenerator
import jwt as pyjwt
# Load environment variables from .env
load_dotenv()
if not os.path.isfile(os.getenv("RSA_PRIVATE_KEY_PATH")):
raise FileNotFoundError("Private key file not found")
# Instantiate JWT generator and get token
jwt = JWTGenerator(
os.getenv("ACCOUNT"),
os.getenv("USER"),
os.getenv("RSA_PRIVATE_KEY_PATH")
)
jwt_token = jwt.get_token()
# Debug JWT
# print("JWT Token:", jwt_token)
# print("JWT token (first 100 chars):", jwt_token[:100], "...")
# Build the payload
payloadForCortexSearch = {
"model": os.getenv("MODEL"),
"response_instruction": "You will always maintain a friendly tone and provide concise response.",
"tools": [
{
"tool_spec": {
"type": "cortex_search",
"name": "vehicles_info_search"
}
},
{
"tool_spec": {
"type": "cortex_analyst_text_to_sql",
"name": "supply_chain"
}
}
],
"tool_resources": {
"vehicles_info_search": {
"name": os.getenv("SEARCH_SERVICE"),
"max_results": 1,
"title_column": "title",
"id_column": "relative_path"
},
"supply_chain": {
"semantic_model_file": os.getenv("SEMANTIC_MODEL_SEARCH_SERVICE")
}
},
"tool_choice": {
"type": "auto"
},
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Can you show me a breakdown of customer support tickets by service type cellular vs business internet?"
}
]
}
]
}
payloadForCortexAgentOnSematicView = {
"model": os.getenv("MODEL"),
"response_instruction": "You will always maintain a friendly tone and provide concise response.",
"tools": [
{
"tool_spec": {
"type": "cortex_analyst_text_to_sql",
"name": "supply_chain"
}
}
],
"tool_resources": {
"supply_chain": {
"semantic_model_file": os.getenv("SEMANTIC_MODEL_SMV")
}
},
"tool_choice": {
"type": "auto"
},
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Show me the top selling brands in by total sales quantity in the state ‘TX' in the ‘Books' category in the year 2003"
}
]
}
]
}
payload_map = {
"SEMANTIC_VIEW": payloadForCortexAgentOnSematicView,
"SEARCH_SERVICE": payloadForCortexSearch
}
payload = payload_map.get(os.getenv("SERVICE_TYPE"), payloadForCortexSearch)
print(payload)
# Send the POST request
headers = {
"X-Snowflake-Authorization-Token-Type": "KEYPAIR_JWT",
"Authorization": f"Bearer {jwt_token}",
"Content-Type": "application/json",
"Accept": "application/json"
}
# Optional: Decode header/payload to confirm structure
# decoded_header = pyjwt.get_unverified_header(jwt_token)
# decoded_payload = pyjwt.decode(jwt_token, options={"verify_signature": False})
# print("JWT header:", decoded_header)
# print("JWT payload:", decoded_payload)
try:
response = requests.post(
os.getenv("AGENT_ENDPOINT"),
headers=headers,
data=json.dumps(payload)
)
response.raise_for_status()
print("✅ Cortex Agents Response:\n\n", response.text)
except requests.exceptions.RequestException as e:
error_info = f"{type(e).__name__} at line {e.__traceback__.tb_lineno} of {__file__}: {e}"
print("❌ Curl Error:", error_info)