Skip to main content

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

  1. Go to Settings > Connectivity.
  2. Open Utility integrations > Database.
  3. Select Add +.
  4. Enter the following information:
FieldDescription
Name, DescriptionCustom name and description of the integration.
DatabaseName of the database.
HostIP address of the database server.
PortDatabase connection port.
User nameDatabase user login.
PasswordPassword for database access.
  1. Click Save.

How to use

Connect your database integration to the agent. After that the following variables will be accessible in agent's environment:

FieldCorresponding variable
DatabaseDATABASE_NAME
HostDATABASE_HOST
PortDATABASE_PORT
User nameDATABASE_USERNAME
PasswordDATABASE_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