Query and Analyze SQL Databases with Waii
Query, analyze, compare, and translate SQL through a LlamaIndex agent connected to a Waii-managed database.
Why it matters
Connect to Waii-managed database connections to generate SQL queries, analyze performance, describe datasets, and transcode SQL dialects.
Outcomes
What it gets done
Generate SQL queries from natural language questions.
Analyze the performance of existing SQL queries.
Describe SQL queries and datasets.
Transcode SQL queries between different dialects.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/li-tool-tools-waii | bash Overview
Waii Tool
A LlamaIndex tool connecting to Waii-managed database connections, letting an agent answer natural-language questions with SQL, analyze and compare query performance, describe datasets, and translate queries between SQL dialects. Use it when an agent needs natural-language database querying plus SQL performance analysis or dialect translation. Requires a waii.ai account, a Waii API key, and a connected database.
What it does
llama-index-tools-waii connects a LlamaIndex agent to database connections managed by Waii, giving it natural-language-to-SQL capability plus a set of SQL-specific analysis tools. get_answer turns a natural-language question into a SQL query, runs it, and explains the result. Alongside it, the tool exposes describe_query (explain what a SQL query does), performance_analyze (analyze a query's performance by query_id), diff_query (compare two SQL queries), describe_dataset (describe a table or schema), transcode (translate a SQL query to another SQL dialect), get_semantic_contexts (get the semantic context behind a query), generate_query_only (generate SQL without running it), and run_query (execute a given SQL query directly). The data can also be loaded directly as documents via load_data, independent of the agent-tool interface.
When to use - and when NOT to
Use this tool when you want a LlamaIndex agent to answer questions about a database in natural language, analyze or compare SQL query performance, or migrate queries between SQL dialects. It requires a waii.ai account, a Waii API key (distinct from any OpenAI API key), and a database connection key from an existing Waii-connected database - so it isn't usable without first setting up Waii and connecting your database through it.
Inputs and outputs
Most tools take either a natural-language question or a SQL query string and return structured results - a query plus its explained answer, a performance report keyed by query_id, a diff between two queries, or a translated query in the target dialect:
waii_tool = WaiiToolSpec(
url="https://tweakit.waii.ai/api/",
api_key="...",
database_key="...",
)
Follow-up questions can share context across calls by passing the same Context object to agent.run(), for example asking for the top 3 countries with the most car factories and then which car factories those countries have, in the same session. The transcode tool goes the other direction too - the project's own example feeds it a PySpark DataFrame snippet (window functions over a Spark session) and asks the agent to migrate it to a different SQL dialect, not just translate between two SQL dialects.
Integrations
Instantiate WaiiToolSpec with your Waii API url, api_key, and database_key, then pass its to_tool_list() output into any LlamaIndex agent, such as a FunctionAgent backed by an OpenAI model. Data can also be loaded independently with waii_tool.load_data() and indexed as a VectorStoreIndex for direct querying, separate from the agent-tool workflow.
Who it's for
Data teams building LlamaIndex agents that need natural-language database querying alongside query performance analysis, comparison, and cross-dialect SQL translation.
Source README
Waii Tool
This tool connects to database connections managed by Waii, which allows generic SQL queries, do performance analyze, describe a SQL query, and more.
Usage
First you need to create a waii.ai account, you request an account from here.
Initialize the tool with your account credentials:
from llama_index.tools.waii import WaiiToolSpec
waii_tool = WaiiToolSpec(
url="https://tweakit.waii.ai/api/",
# API Key of Waii (not OpenAI API key)
api_key="...",
# Connection key of WAII connected database, see https://github.com/waii-ai/waii-sdk-py#get-connections
database_key="...",
)
Tools
The tools available are:
get_answer: Get answer to natural language question (which generate a SQL query, run it, explain the result)describe_query: Describe a SQL queryperformance_analyze: Analyze performance of a SQL query (by query_id)diff_query: Compare two SQL queriesdescribe_dataset: Describe dataset, such as table, schema, etc.transcode: Transcode SQL query to another SQL dialectget_semantic_contexts: Get semantic contexts of a SQL querygenerate_query_only: Generate SQL query only (not run it)run_query: Run a SQL query
You can also load the data directly call load_data
Examples
Load data
documents = waii_tool.load_data("Get all tables with their number of columns")
index = VectorStoreIndex.from_documents(documents).as_query_engine()
print(index.query("Which table contains most columns?"))
Use as a Tool
Initialize the agent:
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
agent = FunctionAgent(
tools=waii_tool.to_tool_list(), llm=OpenAI(model="gpt-4-1106-preview")
)
Ask simple question
from llama_index.core.workflow import Context
ctx = Context(agent)
print(
await agent.run(
"Give me top 3 countries with the most number of car factory", ctx=ctx
)
)
print(
await agent.run("What are the car factories of these countries", ctx=ctx)
)
Do performance analyze
from llama_index.core.workflow import Context
ctx = Context(agent)
print(
await agent.run(
"Give me top 3 longest running queries, and their duration.", ctx=ctx
)
)
print(await agent.run("analyze the 2nd-longest running query", ctx=ctx))
Diff two queries
previous_query = """
SELECT
employee_id,
department,
salary,
AVG(salary) OVER (PARTITION BY department) AS department_avg_salary,
salary - AVG(salary) OVER (PARTITION BY department) AS diff_from_avg
FROM
employees;
"""
current_query = """
SELECT
employee_id,
department,
salary,
MAX(salary) OVER (PARTITION BY department) AS department_max_salary,
salary - AVG(salary) OVER (PARTITION BY department) AS diff_from_avg
FROM
employees;
LIMIT 100;
"""
print(
await agent.run(
f"tell me difference between {previous_query} and {current_query}",
ctx=ctx,
)
)
Describe dataset
print(await agent.run("Summarize the dataset", ctx=ctx))
print(
await agent.run(
"Give me questions which I can ask about this dataset", ctx=ctx
)
)
Describe a query
q = """
SELECT
employee_id,
department,
salary,
AVG(salary) OVER (PARTITION BY department) AS department_avg_salary,
salary - AVG(salary) OVER (PARTITION BY department) AS diff_from_avg
FROM
employees;
"""
print(await agent.run(f"what this query can do? {q}", ctx=ctx))
Migrate query to another dialect
q = """
from pyspark.sql import SparkSession
from pyspark.sql.functions import avg, col
from pyspark.sql.window import Window
#### Initialize Spark session
spark = SparkSession.builder.appName("example").getOrCreate()
#### Assuming you have a DataFrame called 'employees'
#### If not, you need to read your data into a DataFrame first
#### Defi
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.