A tool to interact with SQL databases for the datapizza-ai framework.
Project description
Datapizza AI - SQLDatabase Tool
A tool for Datapizza AI that allows agents to interact with SQL databases using SQLAlchemy.
This tool provides a robust and easy-to-use interface for connecting datapizza-ai agents to any SQL database supported by SQLAlchemy (including SQLite, PostgreSQL, MySQL, and more).
Agents equipped with this tool can inspect database schemas, list tables, and execute SQL queries to answer questions based on structured data.
⚠️ Warning: Risk of Data Modification
Using queries like
INSERT,UPDATE, andDELETEwill permanently modify the data in your database. Exercise extreme caution. Before performing write operations in a production environment, consider the following:
- Test queries in a development or staging environment.
- Use a "query-writing" agent to generate and validate the SQL before execution.
- Ensure you have recent backups of your database.
⚙️ How it Works
The SQLDatabase tool is a class that, once initialized with a database URI, exposes three distinct functionalities to an agent:
list_tables(): Lists all tables available in the database. Returns a newline-separated string of table names.get_table_schema(table_name: str): Retrieves the schema for a specified table. Returns a formatted string describing the columns and their data types.run_sql_query(query: str): Executes a given SQL query. ForSELECTstatements, it returns a JSON-formatted string of the results. For other operations (e.g.,INSERT,UPDATE), it returns a success message.
🚀 Quick Start
1. Installation
# Install the core framework
pip install datapizza-ai
# Install the SQLDatabase tool
pip install datapizza-ai-tools-sqldatabase
Note on Database Drivers:
This tool uses SQLAlchemy, which requires specific DB-API drivers to connect to different databases. For example, if you want to connect to PostgreSQL or MySQL, you'll need to install their respective drivers:
# For PostgreSQL pip install psycopg2-binary # For MySQL pip install mysql-connector-pythonPlease refer to the SQLAlchemy documentation for a full list of supported databases and their required drivers.
2. Example: Creating a Database Expert Agent
In this example, we'll create an agent that can answer questions about a simple SQLite database.
import sqlite3
from datapizza.agents import Agent
from datapizza.clients.openai import OpenAIClient
from datapizza.tools.SQLDatabase import SQLDatabase
# ---
# Setup: Create a dummy database for the example
# In a real scenario, you would connect to your existing database.
# ---
db_uri = "sqlite:///company.db"
# Clean up previous runs if the file exists
try:
import os
os.remove("company.db")
except OSError:
pass
# Create and populate the database
conn = sqlite3.connect('company.db')
cursor = conn.cursor()
cursor.execute("CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT, department TEXT, salary INTEGER)")
cursor.execute("INSERT INTO employees (name, department, salary) VALUES ('Alice', 'Engineering', 80000)")
cursor.execute("INSERT INTO employees (name, department, salary) VALUES ('Bob', 'HR', 65000)")
cursor.execute("INSERT INTO employees (name, department, salary) VALUES ('Charlie', 'Engineering', 95000)")
conn.commit()
conn.close()
# ---
# End of Setup
# ---
# 1. Initialize the SQLDatabase tool with the database URI
db_tools = SQLDatabase(db_uri=db_uri)
# 2. Initialize a client (e.g., OpenAI)
client = OpenAIClient(api_key="YOUR_API_KEY")
# 3. Create an agent and provide it with the database tools
agent = Agent(
name="database_expert",
client=client,
system_prompt="""You are an expert and careful SQL database assistant. Your primary goal is to answer questions about the database by executing queries.
Follow these steps:
1. Use `list_tables` to identify the relevant tables.
2. Use `get_table_schema` to understand the columns and data types of those tables before writing a query.
3. Construct an efficient SQL query to answer the user's question.
4. Execute the query using `run_sql_query`.
5. Analyze the results and provide a clear, human-readable answer to the user.
**Important:** Be extra cautious with `UPDATE` and `DELETE` operations. Ensure you understand the request correctly before modifying data.""",
tools=[
db_tools.list_tables,
db_tools.get_table_schema,
db_tools.run_sql_query
]
)
# 4. Run the agent to answer questions
print("--- Query 1: What are the available tables? ---")
response = agent.run("What tables are in the database?")
print(f"Agent Response: {response.text}\n")
print("--- Query 2: How many employees are in the Engineering department? ---")
response = agent.run("How many employees work in the Engineering department?")
print(f"Agent Response: {response.text}\n")
print("--- Query 3: Who is the highest-paid employee? ---")
response = agent.run("Who is the highest-paid employee and what is their salary?")
print(f"Agent Response: {response.text}\n")
print("--- Query 4: Update Bob's salary ---")
response = agent.run("Update Bob's salary to 70000.")
print(f"Agent Response: {response.text}\n")
print("--- Query 5: Add a new employee ---")
response = agent.run("Add a new employee named 'David' in the 'Sales' department with a salary of 75000.")
print(f"Agent Response: {response.text}\n")
print("--- Query 6: Delete an employee ---")
response = agent.run("Remove the employee named 'Bob' from the database.")
print(f"Agent Response: {response.text}\n")
Expected Output:
--- Query 1: What are the available tables? ---
Agent Response: The database contains the following table:
- **employees**
--- Query 2: How many employees are in the Engineering department? ---
Agent Response: There are 2 employees working in the Engineering department.
--- Query 3: Who is the highest-paid employee? ---
Agent Response: The highest-paid employee is Charlie, with a salary of $95,000.
--- Query 4: Update Bob's salary ---
Agent Response: Bob's salary has been successfully updated to 70000.
--- Query 5: Add a new employee ---
Agent Response: I have successfully added David to the employees table.
--- Query 6: Delete an employee ---
Agent Response: The employee named Bob has been removed from the database.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file datapizza_ai_tools_sqldatabase-0.0.3.tar.gz.
File metadata
- Download URL: datapizza_ai_tools_sqldatabase-0.0.3.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c38185a9049c4a05945ddfcf32660e1234a5ce1f5e55c0e38713471931f8ad57
|
|
| MD5 |
3a0a786ddb666d6dc32ee25a4775e35e
|
|
| BLAKE2b-256 |
04bac98070d51f319fb6bf5f96e75a241eda3aa0605b0fe828ffb234d1ac3096
|
File details
Details for the file datapizza_ai_tools_sqldatabase-0.0.3-py3-none-any.whl.
File metadata
- Download URL: datapizza_ai_tools_sqldatabase-0.0.3-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac9b441ed5976b5fde67cda54957eac95618d764c9b71a494b564508833e9df2
|
|
| MD5 |
f3ed4a9dc97db832e6ea1038359d56c6
|
|
| BLAKE2b-256 |
1c683f3f825061ba5d6667ca7ffa3fe9ae5649fa2b872c21d96e5c949fef783c
|