Last active
June 5, 2024 20:06
-
-
Save 0xsamgreen/269a38dcfb2ed73c84e55d3ab1e6d042 to your computer and use it in GitHub Desktop.
Python example for using Agentc API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
# Get your API key from https://agentc.xyz (upper right corner) | |
API_KEY = "YOUR_API_KEY" | |
# Set the base URL for the API | |
BASE_URL = "https://api.agentc.xyz/api/v1" | |
# Function to translate natural language question to SQL | |
def translate_question(question): | |
url = f"{BASE_URL}/translate" | |
headers = { | |
"accept": "application/json; charset=utf-8", | |
"X-API-Key": API_KEY, | |
"Content-Type": "application/json; charset=utf-8", | |
} | |
data = {"question": question} | |
response = requests.post(url, headers=headers, json=data) | |
response.raise_for_status() # Raise an error for bad status codes | |
return response.json()["sql"] | |
# Function to query the SQL and get the data payload | |
def query_sql(sql_query): | |
url = f"{BASE_URL}/query" | |
headers = { | |
"accept": "application/json; charset=utf-8", | |
"X-API-Key": API_KEY, | |
"Content-Type": "application/json; charset=utf-8", | |
} | |
data = {"sql": sql_query} | |
response = requests.post(url, headers=headers, json=data) | |
response.raise_for_status() # Raise an error for bad status codes | |
return response.json()["data"] | |
# Main function to get data payload for a given question | |
def get_data_for_question(question): | |
try: | |
sql_query = translate_question(question) | |
data_payload = query_sql(sql_query) | |
return data_payload | |
except requests.exceptions.HTTPError as err: | |
print(f"HTTP error occurred: {err}") | |
except Exception as err: | |
print(f"An error occurred: {err}") | |
if __name__ == "__main__": | |
question = "what is the price of ETH?" | |
data = get_data_for_question(question) | |
print(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment