File size: 1,624 Bytes
b038dbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests


def generate_sql(
    api_key: str,
    project_id: str,
    query: str,
    thread_id: str = "",
) -> tuple[dict, str]:
    """Generate SQL from natural language query."""
    base_url = "https://cloud.getwren.ai/api/v1"
    endpoint = f"{base_url}/generate_sql"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    payload = {
        "projectId": project_id,
        "question": query,
    }
    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 {}, e


def generate_chart(
    api_key: str,
    project_id: str,
    question: str,
    sql: str,
    sample_size: int = 1000,
    thread_id: str = ""
) -> tuple[dict, str]:
    """Generate a chart from query results."""
    base_url = "https://cloud.getwren.ai/api/v1"
    endpoint = f"{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 {}, e