Skip to main content

llama-index tools waii integration

Project description

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.agent.openai import OpenAIAgent
from llama_index.llms.openai import OpenAI

agent = OpenAIAgent.from_tools(
    waii_tool.to_tool_list(), llm=OpenAI(model="gpt-4-1106-preview")
)

Ask simple question

agent.chat("Give me top 3 countries with the most number of car factory")
agent.chat("What are the car factories of these countries")

Do performance analyze

agent.chat("Give me top 3 longest running queries, and their duration.")
agent.chat("analyze the 2nd-longest running query")

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;
"""
agent.chat(f"tell me difference between {previous_query} and {current_query}")

Describe dataset

agent.chat("Summarize the dataset")
agent.chat("Give me questions which I can ask about this dataset")

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;
"""
agent.chat(f"what this query can do? {q}")

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

# Define window specification
windowSpec = Window.partitionBy("department")

# Perform the query
result = (employees
          .select(
              col("employee_id"),
              col("department"),
              col("salary"),
              avg("salary").over(windowSpec).alias("department_avg_salary"),
              (col("salary") - avg("salary").over(windowSpec)).alias("diff_from_avg")
          ))

# Show the result
result.show()
"""
agent.chat(f"translate this pyspark query {q}, to Snowflake")

Use Waii API directly

You can also use Waii API directly, see here

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llama_index_tools_waii-0.3.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

llama_index_tools_waii-0.3.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file llama_index_tools_waii-0.3.0.tar.gz.

File metadata

  • Download URL: llama_index_tools_waii-0.3.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.10 Darwin/22.3.0

File hashes

Hashes for llama_index_tools_waii-0.3.0.tar.gz
Algorithm Hash digest
SHA256 f9610f09a3f03b0531798ca462077a0d6858881796d22e42a0bd75bd752074bd
MD5 2696ea32acbdd2ce771094955d703be8
BLAKE2b-256 0c821037f43aa4ae0d30884f62e7121da5fdf3040b5717cff6b532c3fd1b39cb

See more details on using hashes here.

File details

Details for the file llama_index_tools_waii-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for llama_index_tools_waii-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fb922cc1cec91e236efda5b79d4f638c8170f7bfc997769457f92a9ed6c56f24
MD5 a8b4e20af77e7e18a1623de5c53400b6
BLAKE2b-256 850bd5cbe5f4d45b8456a90943090a1bebd4fbebd40e3916b2a332b62b2c0408

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page