Skip to main content

Let an agent count and filter a spreadsheet

What you'll do: give the Trip planner agent the agency's bookings as a table, so it can answer questions such as "how many travellers booked the Lapland trip, and from how many countries?"

Search cannot answer that question. A search returns the few passages closest to the query, and a count needs every row. A tables knowledge base holds CSV files and Excel sheets as tables instead. The agent gets a SQL tool for it, writes one SELECT, and reads the exact result.

This guide builds on the agent from Run an agent with a knowledge base and a tool. The bookings file is in the repository:

GitHub

Each row is one booking: booking_id, trip, country, travellers, total_eur, booked_on and status, which is confirmed or cancelled.

1. Create a tables knowledge base from the agent​

Open Agents → Trip planner and click Attach knowledge base. The dialog lists the team's bases that this agent does not use yet. Click New knowledge base to create one without leaving the agent:

  • Kind: Tables. A tables base has no embedding model, and its files are not split into chunks.
  • Name: Trip bookings. The agent's SQL tool is named after it: query_trip_bookings.
  • Description: Every booking of the agency's trips: trip name, the traveller's country, number of travellers, total price in EUR, booking date and status (confirmed or cancelled). The model reads this text, with each table's columns and a few sample rows, to decide when to query and how to write the SQL.

The New knowledge base dialog with Tables selected, the name Trip bookings and its description

Click Create. The base is attached to the agent as a new card. Click Save on the agent.

2. Upload the CSV and try a query​

On the Trip bookings card, click Open base, then Choose files and pick bookings.csv. The file becomes a table named after it, bookings, with 60 rows and 7 columns. A base takes .csv and .xlsx files up to 10 MB each. Each sheet of an Excel workbook becomes its own table, named after the file and the sheet.

Try a query runs one SELECT the same way the agent does. Paste this and click Run query:

SELECT trip, count(*) AS bookings, sum(travellers) AS travellers,
count(DISTINCT country) AS countries
FROM bookings
WHERE status = 'confirmed'
GROUP BY trip
ORDER BY travellers DESC

Try a query with the SELECT above and three result rows: Lapland northern lights 26 bookings, 63 travellers, 11 countries; Lisbon 19, 39, 9; Kyoto 6, 14, 5

The query runs on a read-only copy of the base's tables, with no file or network access. Anything other than one SELECT is refused, and at most 200 rows come back. You can also click a table's name to read its rows, and add, change or delete rows.

Do this in code

knowledge_bases.create with kind="tables" creates the base. upload_files adds the CSV, and the table can be queried as soon as the call returns. agents.update attaches the base; its knowledge base list replaces the agent's whole list, so the snippet sends the bases already attached as well.

bookings = await hub.knowledge_bases.create(
"Trip bookings",
kind="tables",
description="Every booking of the agency's trips: trip name, the traveller's country, number of "
"travellers, total price in EUR, booking date and status (confirmed or cancelled).",
)
await hub.knowledge_bases.upload_files(bookings.id, ["data/bookings.csv"])

result = await hub.knowledge_bases.query(
bookings.id,
"SELECT trip, count(*) AS bookings, sum(travellers) AS travellers, "
"count(DISTINCT country) AS countries FROM bookings "
"WHERE status = 'confirmed' GROUP BY trip ORDER BY travellers DESC",
)
print([c["name"] for c in result.columns])
for row in result.rows:
print(row)

agent = next(a for a in await hub.agents.list() if a.name == "Trip planner")
attached = [
{"knowledge_base_id": k.id, "top_k": k.top_k, "rerank_model_id": k.rerank_model_id,
"decider_model_id": k.decider_model_id, "decider_threshold": k.decider_threshold}
for k in agent.knowledge_bases
]
agent = await hub.agents.update(agent.id, knowledge_bases=attached + [{"knowledge_base_id": bookings.id}])
print([k.tool_name for k in agent.knowledge_bases])
['trip', 'bookings', 'travellers', 'countries']
['Lapland northern lights', 26, 63, 11]
['Lisbon and the Atlantic coast', 19, 39, 9]
['Kyoto autumn colours', 6, 14, 5]
['search_trip_itineraries', 'query_trip_bookings']

3. Check the agent's tools​

Back on the agent's page, the Trip bookings card says the model calls it as query_trip_bookings. A tables card has no search settings, because SQL returns exact rows and has nothing to rerank.

The agent's Knowledge section: the Trip itineraries card with its search settings, and the Trip bookings card with 1 table, queried with SQL, called query_trip_bookings

4. Ask a counting question​

Click Chat and ask:

How many confirmed bookings does the Lapland trip have, how many travellers,
from how many countries, and what do they pay in total?

The agent answers in one step. Steps shows the query_trip_bookings call with its SQL and the start of the result. The answer gives 26 confirmed bookings, 63 travellers, 11 countries and 91,350 EUR, the same numbers as the query in step 2.

The chat page: the query_trip_bookings step and the answer with 26 confirmed bookings, 63 travellers from 11 countries and 91,350 EUR

5. Read the query in the trace​

Click View trace and open the numbered query Trip bookings step. Arguments holds the SQL the model wrote. Output holds the rows that came back, in the order of the columns in the SELECT. The step's attributes show the row count and whether the result was cut at 200 rows.

The expanded query Trip bookings step: the SQL with COUNT, SUM and COUNT DISTINCT over bookings, and the output row 26, 63, 11, 91350

When the SQL is wrong, the step fails and the model gets the error message, so it can fix the query and try again in the next step. The trace shows each attempt.

Next steps​