DuckDbTools enable an Agent to run SQL and analyze data using DuckDb.

Prerequisites

The following example requires DuckDB library. To install DuckDB, run the following command:

pip install duckdb

For more installation options, please refer to DuckDB documentation.

Example

The following agent will analyze the movies file using SQL and return the result.

cookbook/tools/duckdb_tools.py
from phi.agent import Agent
from phi.tools.duckdb import DuckDbTools

agent = Agent(
    tools=[DuckDbTools()],
    show_tool_calls=True,
    system_prompt="Use this file for Movies data: https://phidata-public.s3.amazonaws.com/demo_data/IMDB-Movie-Data.csv",
)
agent.print_response("What is the average rating of movies?", markdown=True, stream=False)

Toolkit Params

ParameterTypeDefaultDescription
db_pathstr-Specifies the path to the database file.
connectionDuckDBPyConnection-Provides an existing DuckDB connection object.
init_commandsList-A list of initial SQL commands to run on database connection.
read_onlyboolFalseConfigures the database connection to be read-only.
configdict-Configuration options for the database connection.
run_queriesboolTrueDetermines whether to run SQL queries during the operation.
inspect_queriesboolFalseEnables inspection of SQL queries without executing them.
create_tablesboolTrueAllows creation of tables in the database during the operation.
summarize_tablesboolTrueEnables summarization of table data during the operation.
export_tablesboolFalseAllows exporting tables to external formats during the operation.

Toolkit Functions

FunctionDescription
show_tablesFunction to show tables in the database
describe_tableFunction to describe a table
inspect_queryFunction to inspect a query and return the query plan. Always inspect your query before running them.
run_queryFunction that runs a query and returns the result.
summarize_tableFunction to compute a number of aggregates over a table. The function launches a query that computes a number of aggregates over all columns, including min, max, avg, std and approx_unique.
get_table_name_from_pathGet the table name from a path
create_table_from_pathCreates a table from a path
export_table_to_pathSave a table in a desired format (default: parquet). If the path is provided, the table will be saved under that path. Eg: If path is /tmp, the table will be saved as /tmp/table.parquet. Otherwise it will be saved in the current directory
load_local_path_to_tableLoad a local file into duckdb
load_local_csv_to_tableLoad a local CSV file into duckdb
load_s3_path_to_tableLoad a file from S3 into duckdb
load_s3_csv_to_tableLoad a CSV file from S3 into duckdb
create_fts_indexCreate a full text search index on a table
full_text_searchFull text Search in a table column for a specific text/keyword

Information