-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
87 lines (63 loc) · 2.02 KB
/
main.py
File metadata and controls
87 lines (63 loc) · 2.02 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
from openapi_schema_validator import validate
from ollama import chat
from ollama import ChatResponse
from dotenv import load_dotenv
import requests
import json
import os
load_dotenv()
xc_token = os.getenv("API_KEY")
table_name = os.getenv("TABLE_NAME")
view_id = os.getenv("DEFAULT_VIEW")
LLM = ""
QUERY = """
What are the main jurisdictional funding sources in Alaska?
"""
OUTPUT = "data/response.json"
URL = f"https://app.nocodb.com/api/v2/tables/{table_name}/records"
HEADERS = {
"accept": "application/json",
"user-agent": "aksbdc/nocodb-to-gpt-via-api/0.1.1",
"xc-token": xc_token,
}
PARAMETERS = {"offset": "0", "limit": "25", "where": "", "viewID": view_id}
def project_description():
"""
Example entrypoint for command-line interface (CLI).
"""
print(">> Hello from nocodb-to-gpt-via-api!")
print(">> This is a simple script that fetches data from an instance via the API.")
print(">> The data is then used to train a GPT model.")
def sample_usage(question):
"""
Benchmark query analysis from research notes.
"""
response: ChatResponse = chat(
model=LLM,
messages=[
{"role": "user", "content": question},
],
)
def fetch_data(location):
"""
Retrieve data from the instance.
"""
try:
response = requests.request("GET", URL, headers=HEADERS, params=PARAMETERS)
response.raise_for_status()
data = response.json()
os.makedirs(os.path.dirname(location), exist_ok=True)
with open(location, "w", encoding="utf-8") as file:
json.dump(data, file, ensure_ascii=False, indent=4)
print(f">> Captured output: {location}")
except requests.exceptions.RequestException as e:
print(f"[!] Error w/ API request: {e}")
except ValueError as e:
print(f"[?] Error w/ JSON response parsing: {e}")
except IOError as e:
print(f"[*] Error w/ file write: {e}")
def main():
project_description()
fetch_data(OUTPUT)
if __name__ == "__main__":
main()