Databao docs

Configure data sources

SQL databases

Databao accesses SQL databases using SQLAlchemy. For example, to connect to a PostgreSQL database with credentials stored in environment variables, you can use the following code:

import os
from sqlalchemy import create_engine
import databao

user = os.environ.get("DATABASE_USER")
password = os.environ.get("DATABASE_PASSWORD")
host = os.environ.get("DATABASE_HOST")
database = os.environ.get("DATABASE_NAME")

engine = create_engine(
    f"postgresql://{user}:{password}@{host}/{database}"
)
pg_conn = engine.connect()

After you initialize an agent, you can add the database connection to it as follows:

...
agent.add_db(pg_conn)

To connect to other databases, you can adjust the code accordingly.

DuckDB

If you have data in a DuckDB database, you can add it as a data source as follows:

import duckdb
import databao

DB_PATH = "data.duckdb"
d_conn = duckdb.connect(DB_PATH, read_only=True)

After you initialize an agent, you can add the DuckDB connection to it as follows:

...
agent.add_db(d_conn)

Dataframe

Databao can access dataframes as data sources.

After you initialize an agent, add a dataframe to it as follows:

...
agent.add_df(df)

CSV files

To add a CSV file as a data source, create a dataframe from the file:

import pandas as pd
import databao

df_csv = pd.read_csv("data.csv")

After you initialize an agent, add the dataframe to it as follows:

...
agent.add_df(df_csv)

Context

After you initialize an agent, you can add context to it as a string or as a file path.

import databao
from pathlib import Path

...

agent.add_context("context string")
agent.add_context(Path("context.md"))

You can also add context together with a dataframe as follows:

import databao
from pathlib import Path

...

agent.add_df(df_csv, context="context string"))
agent.add_df(df_csv, context=Path("context.md"))

On this page