How to connect Databases (SQL, DWH)
Purpose: Integration with external databases allows the AI agent to execute SQL queries and use the results during a conversation. PostgreSQL support is verified.
How to Set Up a Database Integration
- Go to Settings > Connectivity.
- Open Utility integrations > Database.
- Select Add +.
- Enter the following information:
| Field | Description |
|---|---|
Name, Description | Custom name and description of the integration. |
Database | Name of the database. |
Host | IP address of the database server. |
Port | Database connection port. |
User name | Database user login. |
Password | Password for database access. |
- Click Save.
How to use
Connect your database integration to the agent. After that the following variables will be accessible in agent's environment:
| Field | Corresponding variable |
|---|---|
Database | DATABASE_NAME |
Host | DATABASE_HOST |
Port | DATABASE_PORT |
User name | DATABASE_USERNAME |
Password | DATABASE_PASSWORD |
You can access these variables in your custom tools to connect to the database. An example tool:
import os
import asyncpg
import asyncio
async def connect_to_database(**kwargs):
db_name = os.getenv("DATABASE_NAME")
db_host = os.getenv("DATABASE_HOST")
db_port = os.getenv("DATABASE_PORT")
db_user = os.getenv("DATABASE_USERNAME")
db_pass = os.getenv("DATABASE_PASSWORD")
if not all([db_name, db_host, db_port, db_user, db_pass]):
raise OSError("Missing one or more required database environment variables.")
conn = await asyncpg.connect(
user=db_user,
password=db_pass,
database=db_name,
host=db_host,
port=int(db_port),
)
result = await conn.fetch("SELECT NOW() AS current_time;")
# Your code here
conn.close()
return result