Skip to content

Commit 8e182cd

Browse files
authored
Merge pull request #36 from TransposeData/dev
✨ feat: AI Assistant & Schema endpoints & tests for Transpose::SQL
2 parents 8413280 + bf36484 commit 8e182cd

File tree

4 files changed

+194
-4
lines changed

4 files changed

+194
-4
lines changed

docs/sql.md

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ The **SQL API** provides direct SQL access to our entire ecosystem of indexed bl
44

55
## Usage
66

7+
### Calling a SQL Query
8+
79
You may use the SQL API within your python project as follows:
810

911
```python
@@ -19,16 +21,99 @@ parameters = {
1921
'limit': 10,
2022
}
2123

22-
response = api.endpoint.query("SELECT * FROM ethereum.logs LIMIT {{limit}}", parameters)
24+
response = api.sql.query("SELECT * FROM ethereum.logs LIMIT {{limit}}", parameters)
2325
```
2426

25-
## Parameters
27+
#### Parameters
2628

2729
| Parameter | Type | Description |
2830
| - | - | - |
2931
| `sql_query` | `string` | The SQL query to call. |
3032
| `parameters` | `dict` | The optional parameters for this call. |
3133

34+
### Getting the SQL Schema
35+
36+
You may fetch our internal SQL schema as a dictionary with the following method:
37+
38+
```python
39+
api.sql.schema() -> dict
40+
```
41+
42+
For example:
43+
44+
```python
45+
api = Transpose(api_key)
46+
47+
response = api.sql.schema()
48+
49+
print(response)
50+
51+
# {
52+
# "schema": {
53+
# "ethereum": {
54+
# "settlement_layer": [
55+
# {
56+
# "accounts": {
57+
# "meta": {
58+
# "table": "accounts",
59+
# "description": "The `ethereum.accounts` table provides indexed views of all accounts, including both externally-owned accounts (colloquially referred to as wallets) and contracts.",
60+
# "indexes": [
61+
# [
62+
# "created_timestamp",
63+
# "address"
64+
# ],
65+
# ...
66+
# ]
67+
# },
68+
# "schema": [
69+
# {
70+
# "column": "address",
71+
# "type": "text",
72+
# "description": "The address of the account."
73+
# },
74+
# ...
75+
# ]
76+
# }
77+
# },
78+
# ...
79+
# ]
80+
# },
81+
# ...
82+
# }
83+
# }
84+
```
85+
86+
### Generating queries with the AI Query Assistant
87+
88+
You may generate SQL queries with the AI Query Assistant as follows:
89+
90+
```python
91+
api.sql.generate_query(self, text: str, chain: str='ethereum') -> dict:
92+
```
93+
94+
For example:
95+
96+
```python
97+
api = Transpose(api_key)
98+
99+
response = api.sql.generate_query('Give me the most recently updated ENS name that is 3 characters long. Since all ens names end in ".eth", you\'ll need to add 4 to the search length. Optimize this query as much as possible.')
100+
101+
print(response)
102+
103+
# {
104+
# "options": [
105+
# "SELECT\n ens_name, last_refreshed\nFROM\n ethereum.ens_names\nWHERE\n LENGTH(ens_name) = 7 AND ens_name LIKE '%.eth'\nORDER BY\n last_refreshed DESC\nLIMIT 1;"
106+
# ]
107+
# }
108+
```
109+
110+
#### Parameters
111+
112+
| Parameter | Type | Description |
113+
| - | - | - |
114+
| `text` | `string` | The text to generate a query from. Explain to the AI what you want to do, and it will generate a query for you. |
115+
| `chain` | `string` | The optional chain to generate a query for. Defaults to `ethereum`. Valid chains are `ethereum`, `goerli`, `arbitrum`, `canto`, `scroll`, or `polygon`. |
116+
32117
## More Information
33118

34-
You can find more information about the Custom Endpoint API in our [documentation](https://docs.transpose.io/sql/overview/).
119+
You can find more information about the SQL API in our [documentation](https://docs.transpose.io/sql/overview/).
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import json
2+
from transpose import Transpose, api_key
3+
from transpose.src.base import TransposeBadRequest
4+
5+
def test_basic():
6+
try:
7+
api = Transpose(api_key)
8+
9+
response = api.sql.generate_query("Give me the most recently updated ENS name that is 3 characters long. Since all ens names end in \".eth\", you'll need to add 4 to the search length. Optimize this query as much as possible.", "ethereum")
10+
11+
# ensure resp is dict
12+
assert type(response) is dict
13+
assert 'options' in response
14+
assert type(response['options']) is list
15+
16+
for option in response['options']:
17+
assert 'SELECT' in option
18+
19+
except Exception:
20+
assert False

tests/test_get_schema.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import json
2+
from transpose import Transpose, api_key
3+
from transpose.src.base import TransposeBadRequest
4+
5+
def test_basic():
6+
try:
7+
api = Transpose(api_key)
8+
9+
response = api.sql.schema()
10+
11+
# ensure resp is dict
12+
assert type(response) is dict
13+
14+
# ensure resp exists
15+
assert 'schema' in response
16+
assert len(response['schema']) > 0
17+
18+
# ensure all chains are present
19+
assert 'ethereum' in response['schema']
20+
assert 'polygon' in response['schema']
21+
assert 'goerli' in response['schema']
22+
assert 'arbitrum' in response['schema']
23+
assert 'canto' in response['schema']
24+
assert 'scroll' in response['schema']
25+
26+
except Exception:
27+
assert False

transpose/src/api/sql/base.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def query(self,
2020
'Accept': 'application/json',
2121
}
2222

23-
# buold body
23+
# build body
2424
body = {
2525
'sql': sql_query,
2626
'parameters': parameters
@@ -34,6 +34,64 @@ def query(self,
3434
json=body
3535
)
3636

37+
# check for a successful response
38+
if request.status_code == 200:
39+
40+
response = request.json()
41+
return response
42+
else:
43+
raise_custom_error(request.status_code, request.json()['message'])
44+
45+
46+
# Gets the schema from the Transpose API
47+
def schema(self) -> dict:
48+
49+
# build headers
50+
request_headers = {
51+
'x-api-key': self.super.api_key,
52+
'Accept': 'application/json',
53+
}
54+
55+
# if in verbose mode, log the endpoint
56+
request = requests.get(
57+
"https://api.transpose.io/get-schema",
58+
headers=request_headers,
59+
)
60+
61+
# check for a successful response
62+
if request.status_code == 200:
63+
64+
response = request.json()
65+
return response
66+
else:
67+
raise_custom_error(request.status_code, request.json()['message'])
68+
69+
70+
# Calls the AI query assistant
71+
def generate_query(self, text: str, chain: str='ethereum') -> dict:
72+
73+
# build headers
74+
request_headers = {
75+
'x-api-key': self.super.api_key,
76+
'Accept': 'application/json',
77+
}
78+
79+
# build body
80+
body = {
81+
'text': text,
82+
'chain': chain
83+
}
84+
85+
# if in verbose mode, log the endpoint
86+
print("\n{}\n {}\n".format("https://api.transpose.io/text-to-sql", json.dumps(body, indent=4))) if self.super.verbose else None
87+
88+
# make request
89+
request = requests.post(
90+
"https://api.transpose.io/text-to-sql",
91+
headers=request_headers,
92+
json=body,
93+
)
94+
3795
# check for a successful response
3896
if request.status_code == 200:
3997

0 commit comments

Comments
 (0)