Daita Logo

from_db Quick Start

Create and run a database-backed Daita agent from a connection string or database plugin.

#Install Database Dependencies

Install the database extra for the database you want to use:

bash
pip install 'daita-agents[postgresql]'
pip install 'daita-agents[mysql]'
pip install 'daita-agents[sqlite]'
pip install 'daita-agents[snowflake]'
pip install 'daita-agents[mongodb]'

Set your LLM provider credentials, for example:

bash
export OPENAI_API_KEY=sk-...

#Create an Agent

python
from daita import Agent
 
agent = await Agent.from_db("postgresql://user:pass@localhost:5432/analytics")
 
await agent.start()
answer = await agent.run("How many orders were created last week?")
await agent.stop()

Agent.from_db() returns a normal Agent, so you can use run(), stream(), tracing, and the rest of the agent API.

#Connection Strings

python
# PostgreSQL
agent = await Agent.from_db("postgresql://user:pass@host:5432/mydb")
 
# MySQL
agent = await Agent.from_db("mysql://user:pass@host:3306/mydb")
 
# SQLite file
agent = await Agent.from_db("sqlite:///./data.db")
 
# SQLite in-memory
agent = await Agent.from_db("sqlite:///:memory:")
 
# MongoDB
agent = await Agent.from_db("mongodb://user:pass@host:27017/mydb")
 
# Snowflake uses a plugin instance
from daita.plugins import snowflake
 
sf = snowflake(
    account="account",
    user="user",
    password="pass",
    database="analytics",
    schema="public",
)
agent = await Agent.from_db(sf)

#Plugin Instances

Use a plugin instance when you want explicit plugin configuration:

python
from daita import Agent
from daita.plugins import postgresql
 
db = postgresql(
    host="localhost",
    database="analytics",
    username="user",
    password="pass",
)
 
agent = await Agent.from_db(db, mode="analyst")

Snowflake also uses this plugin-instance pattern.

#Add Domain Context

Use prompt for business definitions, preferred terminology, and known caveats:

python
agent = await Agent.from_db(
    "postgresql://user:pass@host/db",
    prompt="""
    Revenue is order_total_cents / 100, excludes refunded orders,
    and should be reported in USD. All timestamps are UTC.
    """,
)

#Inspect the Agent

Every from_db agent gets a public agent.db context:

python
print(agent.db.mode)
print(agent.db.summary)
print(agent.db.suggested_questions)

You can also call describe() for compact metadata:

python
metadata = agent.describe()
print(metadata["db"]["table_count"])
print(metadata["db"]["query_policy"])

#Choose a Mode

python
agent = await Agent.from_db(
    "postgresql://user:pass@host/db",
    mode="governed",
)

The default mode is analyst. Use Modes to choose between simple, analyst, governed, and data_team.