File size: 12,022 Bytes
bfd6209
 
b038dbc
 
 
7df9c64
 
 
bfd6209
 
 
 
 
 
 
 
 
7df9c64
bfd6209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b038dbc
 
 
bfd6209
b038dbc
bfd6209
 
b038dbc
 
7df9c64
b038dbc
 
 
 
 
 
bfd6209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7df9c64
bfd6209
 
 
 
 
 
 
 
b038dbc
 
 
 
 
 
 
 
 
bfd6209
 
 
 
 
 
 
 
 
 
 
 
 
7df9c64
bfd6209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0208146
 
bfd6209
 
7df9c64
bfd6209
 
 
 
 
 
 
 
 
 
 
b038dbc
 
 
 
 
 
 
bfd6209
b038dbc
 
 
7df9c64
b038dbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfd6209
 
 
 
 
 
 
 
 
 
 
 
7df9c64
bfd6209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7df9c64
bfd6209
 
 
 
 
 
 
 
 
 
 
 
 
7df9c64
bfd6209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7df9c64
bfd6209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7df9c64
bfd6209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7df9c64
bfd6209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7df9c64
bfd6209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0208146
bfd6209
7df9c64
bfd6209
 
 
 
 
 
 
0208146
bfd6209
0208146
bfd6209
 
 
 
 
 
 
7df9c64
bfd6209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7df9c64
bfd6209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7df9c64
bfd6209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0208146
bfd6209
7df9c64
bfd6209
 
 
 
 
 
 
0208146
bfd6209
0208146
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
from typing import Optional

import requests


API_BASE_URL = "https://cloud.getwren.ai/api/v1"


def ask(
    api_key: str,
    project_id: str,
    question: str,
    thread_id: str = "",
    language: str = "English",
    sample_size: int = 1000,
) -> tuple[dict, str]:
    """Ask a question and get an answer with SQL and explanation."""
    endpoint = f"{API_BASE_URL}/ask"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    payload = {
        "projectId": project_id,
        "question": question,
        "sampleSize": sample_size,
        "language": language,
    }
    if thread_id:
        payload["threadId"] = thread_id
    
    try:
        response = requests.post(endpoint, json=payload, headers=headers)
        response.raise_for_status()
        return response.json(), ""
    except requests.exceptions.RequestException as e:
        return {}, str(e)


def generate_sql(
    api_key: str,
    project_id: str,
    question: str,
    thread_id: str = "",
    language: str = "English",
    return_sql_dialect: bool = False,
) -> tuple[dict, str]:
    """Generate SQL from natural language query."""
    endpoint = f"{API_BASE_URL}/generate_sql"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    payload = {
        "projectId": project_id,
        "question": question,
        "language": language,
        "returnSqlDialect": return_sql_dialect,
    }
    if thread_id:
        payload["threadId"] = thread_id
    
    try:
        response = requests.post(endpoint, json=payload, headers=headers)
        response.raise_for_status()
        return response.json(), ""
    except requests.exceptions.RequestException as e:
        return {}, str(e)


def run_sql(
    api_key: str,
    project_id: str,
    sql: str,
    thread_id: str = "",
    limit: int = 1000,
) -> tuple[dict, str]:
    """Execute SQL query and return results."""
    endpoint = f"{API_BASE_URL}/run_sql"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    payload = {
        "projectId": project_id,
        "sql": sql,
        "limit": limit,
    }
    if thread_id:
        payload["threadId"] = thread_id
    
    try:
        response = requests.post(endpoint, json=payload, headers=headers)
        response.raise_for_status()
        return response.json(), ""
    except requests.exceptions.RequestException as e:
        return {}, str(e)


def generate_summary(
    api_key: str,
    project_id: str,
    question: str,
    sql: str,
    thread_id: str = "",
    language: str = "English",
    sample_size: int = 1000,
) -> tuple[dict, str]:
    """Generate a summary of query results."""
    endpoint = f"{API_BASE_URL}/generate_summary"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    payload = {
        "projectId": project_id,
        "question": question,
        "sql": sql,
        "language": language,
        "sampleSize": sample_size,
    }
    if thread_id:
        payload["threadId"] = thread_id
    
    try:
        response = requests.post(endpoint, json=payload, headers=headers)
        response.raise_for_status()
        return response.json(), ""
    except requests.exceptions.RequestException as e:
        return {}, str(e)


def stream_explanation(
    api_key: str,
    query_id: str,
) -> tuple[dict, str]:
    """Stream explanation for a query."""
    endpoint = f"{API_BASE_URL}/stream_explanation/{query_id}"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Accept": "text/event-stream",
    }

    try:
        response = requests.get(endpoint, headers=headers, stream=True)
        response.raise_for_status()
        return response.json(), ""
    except requests.exceptions.RequestException as e:
        return {}, str(e)


def generate_chart(
    api_key: str,
    project_id: str,
    question: str,
    sql: str,
    thread_id: str = "",
    sample_size: int = 1000,
) -> tuple[dict, str]:
    """Generate a chart from query results."""
    endpoint = f"{API_BASE_URL}/generate_vega_chart"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    payload = {
        "projectId": project_id,
        "question": question,
        "sql": sql,
        "sampleSize": sample_size
    }
    if thread_id:
        payload["threadId"] = thread_id
    
    try:
        response = requests.post(endpoint, json=payload, headers=headers)
        response.raise_for_status()
        return response.json(), ""
    except requests.exceptions.RequestException as e:
        return {}, str(e)


def stream_ask(
    api_key: str,
    project_id: str,
    question: str,
    thread_id: str = "",
    language: str = "English",
    sample_size: int = 1000,
) -> tuple[dict, str]:
    """Stream ask endpoint for real-time responses."""
    endpoint = f"{API_BASE_URL}/stream/ask"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
        "Accept": "text/event-stream",
    }
    payload = {
        "projectId": project_id,
        "question": question,
        "language": language,
        "sampleSize": sample_size,
    }
    if thread_id:
        payload["threadId"] = thread_id
    
    try:
        response = requests.post(endpoint, json=payload, headers=headers, stream=True)
        response.raise_for_status()
        return response, ""
    except requests.exceptions.RequestException as e:
        return {}, str(e)


def stream_generate_sql(
    api_key: str,
    project_id: str,
    question: str,
    thread_id: str = "",
    language: str = "English",
    return_sql_dialect: bool = False,
) -> tuple[dict, str]:
    """Stream SQL generation endpoint for real-time responses."""
    endpoint = f"{API_BASE_URL}/stream/generate_sql"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
        "Accept": "text/event-stream",
    }
    payload = {
        "projectId": project_id,
        "question": question,
        "language": language,
        "returnSqlDialect": return_sql_dialect,
    }
    if thread_id:
        payload["threadId"] = thread_id
    
    try:
        response = requests.post(endpoint, json=payload, headers=headers, stream=True)
        response.raise_for_status()
        return response.json(), ""
    except requests.exceptions.RequestException as e:
        return {}, str(e)


def get_models(
    api_key: str,
    project_id: str,
) -> tuple[dict, str]:
    """Get latest deployed models for a project."""
    endpoint = f"{API_BASE_URL}/projects/{project_id}/models"
    headers = {
        "Authorization": f"Bearer {api_key}",
    }
    
    try:
        response = requests.get(endpoint, headers=headers)
        response.raise_for_status()
        return response.json(), ""
    except requests.exceptions.RequestException as e:
        return {}, str(e)


def get_instructions(
    api_key: str,
    project_id: str,
) -> tuple[dict, str]:
    """Get all instructions for a project."""
    endpoint = f"{API_BASE_URL}/projects/{project_id}/knowledge/instructions"
    headers = {
        "Authorization": f"Bearer {api_key}",
    }
    
    try:
        response = requests.get(endpoint, headers=headers)
        response.raise_for_status()
        return response.json(), ""
    except requests.exceptions.RequestException as e:
        return {}, str(e)


def create_instruction(
    api_key: str,
    project_id: str,
    instruction: str,
    is_global: bool = True,
    questions: Optional[list[str]] = None,
) -> tuple[dict, str]:
    """Create a new instruction for a project."""
    endpoint = f"{API_BASE_URL}/projects/{project_id}/knowledge/instructions"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    payload = {
        "instruction": instruction,
        "isGlobal": is_global,
        "questions": questions or [],
    }
    
    try:
        response = requests.post(endpoint, json=payload, headers=headers)
        response.raise_for_status()
        return response.json(), ""
    except requests.exceptions.RequestException as e:
        return {}, str(e)


def update_instruction(
    api_key: str,
    project_id: str,
    instruction_id: str,
    instruction: str,
    is_global: bool = True,
    questions: Optional[list[str]] = None,
) -> tuple[dict, str]:
    """Update an existing instruction."""
    endpoint = f"{API_BASE_URL}/projects/{project_id}/knowledge/instructions/{instruction_id}"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    payload = {
        "instruction": instruction,
        "isGlobal": is_global,
        "questions": questions or [],
    }
    
    try:
        response = requests.put(endpoint, json=payload, headers=headers)
        response.raise_for_status()
        return response.json(), ""
    except requests.exceptions.RequestException as e:
        return {}, str(e)


def delete_instruction(
    api_key: str,
    project_id: str,
    instruction_id: str,
) -> str:
    """Delete an instruction."""
    endpoint = f"{API_BASE_URL}/projects/{project_id}/knowledge/instructions/{instruction_id}"
    headers = {
        "Authorization": f"Bearer {api_key}",
    }
    
    try:
        response = requests.delete(endpoint, headers=headers)
        response.raise_for_status()
        return ""
    except requests.exceptions.RequestException as e:
        return str(e)


def get_sql_pairs(
    api_key: str,
    project_id: str,
) -> tuple[dict, str]:
    """Get all SQL pairs for a project."""
    endpoint = f"{API_BASE_URL}/projects/{project_id}/knowledge/sql_pairs"
    headers = {
        "Authorization": f"Bearer {api_key}",
    }
    
    try:
        response = requests.get(endpoint, headers=headers)
        response.raise_for_status()
        return response.json(), ""
    except requests.exceptions.RequestException as e:
        return {}, str(e)


def create_sql_pair(
    api_key: str,
    project_id: str,
    question: str,
    sql: str,
) -> tuple[dict, str]:
    """Create a new SQL pair for a project."""
    endpoint = f"{API_BASE_URL}/projects/{project_id}/knowledge/sql_pairs"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    payload = {
        "question": question,
        "sql": sql,
    }
    
    try:
        response = requests.post(endpoint, json=payload, headers=headers)
        response.raise_for_status()
        return response.json(), ""
    except requests.exceptions.RequestException as e:
        return {}, str(e)


def update_sql_pair(
    api_key: str,
    project_id: str,
    sql_pair_id: str,
    question: str,
    sql: str,
) -> tuple[dict, str]:
    """Update an existing SQL pair."""
    endpoint = f"{API_BASE_URL}/projects/{project_id}/knowledge/sql_pairs/{sql_pair_id}"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    payload = {
        "question": question,
        "sql": sql,
    }
    
    try:
        response = requests.put(endpoint, json=payload, headers=headers)
        response.raise_for_status()
        return response.json(), ""
    except requests.exceptions.RequestException as e:
        return {}, str(e)


def delete_sql_pair(
    api_key: str,
    project_id: str,
    sql_pair_id: str,
) -> str:
    """Delete an SQL pair."""
    endpoint = f"{API_BASE_URL}/projects/{project_id}/knowledge/sql_pairs/{sql_pair_id}"
    headers = {
        "Authorization": f"Bearer {api_key}",
    }
    
    try:
        response = requests.delete(endpoint, headers=headers)
        response.raise_for_status()
        return ""
    except requests.exceptions.RequestException as e:
        return str(e)