Tool

Query and Analyze SQL Databases with Waii

Waii Tool connects to Waii-managed database connections for SQL queries, performance analysis, query description, and dataset exploration.

Works with waiiopenai

77
Spark score
out of 100
Updated 2 days ago
Version 0.14.23
Models
gpt 4ogpt 4

Add to Favorites

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

01

Generate SQL queries from natural language questions.

02

Analyze the performance of existing SQL queries.

03

Describe SQL queries and datasets.

04

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 spec that gives an agent nine Waii-backed SQL functions, from natural-language query generation to performance analysis and dialect translation. Use when an agent needs to work conversationally with a live SQL database already connected through Waii.

What it does

The Waii Tool connects a LlamaIndex agent to database connections managed by Waii, giving the agent a rich set of SQL-oriented capabilities beyond simple query execution: generating SQL from natural language, running queries, analyzing query performance, describing queries and datasets, comparing queries, and translating SQL between dialects.

The WaiiToolSpec is initialized with a Waii API URL, a Waii API key (distinct from any OpenAI key used elsewhere), and a database_key identifying the connected database. Nine tools are exposed: get_answer turns a natural-language question into a SQL query, runs it, and explains the result; describe_query explains what a given SQL query does; performance_analyze analyzes a query's performance by its query ID; diff_query compares two SQL queries and explains the difference; describe_dataset describes a dataset such as a table or schema; transcode translates a SQL query to another SQL dialect; get_semantic_contexts retrieves the semantic context behind a query; generate_query_only generates SQL without running it; and run_query executes a given SQL query directly. The tool spec also exposes load_data to load query results directly as LlamaIndex documents for indexing.

When to use - and when NOT to

Use it when a LlamaIndex agent needs to work with a live SQL database conversationally - answering natural-language questions by generating and running SQL, explaining what an existing query does, diagnosing slow queries, or porting a query to a different SQL dialect. load_data is the right choice when you want query results indexed for retrieval rather than answered directly through the agent. Do not use it without a Waii account and a database already connected through Waii - the tool is entirely dependent on Waii's managed connection layer, not a direct database driver.

Capabilities

Nine tools cover the SQL workflow: generate-and-answer (get_answer), query description (describe_query), performance analysis by query ID (performance_analyze), query comparison (diff_query), dataset description (describe_dataset), SQL dialect translation (transcode), semantic context lookup (get_semantic_contexts), query generation without execution (generate_query_only), and direct query execution (run_query). load_data loads query results as documents for indexing.

How to install

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="...",
)

Requires a Waii.ai account (requested via waii.ai), a Waii API key, and a database already connected through Waii with its connection key.

Who it's for

Developers building LlamaIndex agents that need to query, explain, tune, or translate SQL against a real database conversationally, without hand-writing separate SQL generation, performance-analysis, and dialect-translation logic.

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 query
  • performance_analyze: Analyze performance of a SQL query (by query_id)
  • diff_query: Compare two SQL queries
  • describe_dataset: Describe dataset, such as table, schema, etc.
  • transcode: Transcode SQL query to another SQL dialect
  • get_semantic_contexts: Get semantic contexts of a SQL query
  • generate_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.