Skill

Query Databases with AI

Database Tool connects to a database using SQLAlchemy and allows an Agent to query the database and get information about the tables.

Works with sqlalchemypostgresql

57
Spark score
out of 100
Updated 4 days ago
Version 0.14.22
Models

Add to Favorites

Why it matters

Empower AI agents to directly interact with and query relational databases. Extract structured data and gain insights from your database tables.

Outcomes

What it gets done

01

List tables within a database schema.

02

Describe the schema of specific tables.

03

Execute SQL queries and retrieve results.

04

Integrate database access into AI agent workflows.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/li-tool-tools-database | bash

Capabilities

What this skill does

Query a database

Writes and executes SQL or NoSQL queries on databases.

Extract

Pulls structured data fields from unstructured text.

RAG index

Chunks, embeds, and indexes documents for semantic retrieval.

Classify

Labels or categorizes text, files, or data points.

Overview

Database Tool

What it does

a SQLAlchemy-based database connectivity tool that exposes three functions (list_tables, describe_tables, load_data) for agents

How it connects

you want an agent to inspect database schemas and retrieve data by accepting SQL queries

Source README

Database Tool

This tool connects to a database (using SQLAlchemy under the hood) and allows an Agent to query the database and get information about the tables.

Usage

This tool has more extensive example usage documented in a Jupyter notebook here.

Here's an example usage of the DatabaseToolSpec.

from llama_index.tools.database import DatabaseToolSpec
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI

db_tools = DatabaseToolSpec(
    scheme="postgresql",  # Database Scheme
    host="localhost",  # Database Host
    port="5432",  # Database Port
    user="postgres",  # Database User
    password="FakeExamplePassword",  # Database Password
    dbname="postgres",  # Database Name
)
agent = FunctionAgent(
    tools=db_tools.to_tool_list(),
    llm=OpenAI(model="gpt-4.1"),
)

print(await agent.run("What tables does this database contain"))
print(await agent.run("Describe the first table"))
print(await agent.run("Retrieve the first row of that table"))

The tools available are:

list_tables: A tool to list the tables in the database schema
describe_tables: A tool to describe the schema of a table
load_data: A tool that accepts an SQL query and returns the result

This loader is designed to be used as a way to load data as a Tool in a Agent.

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.