Skip to main content

Build an agent to talk to your data

In this tutorial, you'll use aiXplain's SQL tool in a conversational agent to query a data source like csv or sqlite.

Create SQL Tool

Instantiate the SQL tool pointing to your data source. This can be either postgresql, csv or sqlite.

from aixplain.factories import AgentFactory

csv_tool = AgentFactory.create_sql_tool(
source="/content/DataSheet.csv", #replace with file path
source_type="csv",
enable_commit=False,
description="A tool for querying a generic tabular dataset",
)

Create the Agent

Combine your SQL tool and prompts into a conversational agent. In the instructions parameter, include:

  • Table schema & columns: List your table name and relevant fields so the agent knows what data is available.
  • Example SQL snippets: Provide sample queries for record lookups, aggregate statistics, and keyword searches.
  • Clarification flow: Instruct the agent to ask a follow-up question if the user’s request is ambiguous before executing any SQL.
from textwrap import dedent

TASK_PROMPT = dedent("""
**Task:** Answer user questions by querying the `dataset` table.

**Instructions:**
- Always translate user intent into SQL.
- If the question is unclear, ask follow-up before running any query.

**Use-cases:**
1. **Record Lookup:**
- User supplies a specific `id`.
- SQL example:
SELECT * FROM dataset WHERE id = '123' LIMIT 1;

2. **Aggregate Stats:**
- e.g. total count, averages.
- SQL example:
SELECT COUNT(*) AS total_records FROM dataset;

3. **Field Search:**
- Keyword matches in a column.
- SQL example:
SELECT * FROM dataset WHERE column_name LIKE '%keyword%';
""")

GOAL_PROMPT = dedent("""
**Goal:** Fetch and format the correct data from your table using SQL, then present it clearly.
1. Clarify if the user’s request is ambiguous.
2. Construct and execute the SQL query.
3. Return results in a concise, readable format.
""")

INSTRUCTION = f"TASK:\n{TASK_PROMPT}\nGOAL:\n{GOAL_PROMPT}"
from aixplain.factories import AgentFactory

agent = AgentFactory.create(
name="Data Agent",
description = "An agent that allows you to search your data",
instructions=INSTRUCTION,
tools=[
csv_tool,
]
)

Run your agent

Ask questions and inspect responses.

response = agent.run("How many records are in the dataset?")

response.data.output

Deploy your Agent

By default, agents are saved as drafts available for 24 hours. Once you are happy with your agent's responses, deploy your agent.

agent.deploy()