ツールキットはエージェントに追加できる関数のコレクションです。
ここでは、データベース対応ツール CSV, Pandas, Postgres, SQL の使い方を説明します。
Agno : ユーザガイド : コンセプト : ツール – ツールキット : データ (CSV, Pandas, Postgres, SQL)
作成 : クラスキャット・セールスインフォメーション
作成日時 : 08/04/2025
バージョン : Agno 1.7.5
* 本記事は docs.agno.com の以下のページを独自に翻訳した上で、補足説明を加えてまとめ直しています :
- User Guide : Concepts : Tools – Toolkits : Data : CSV
- User Guide : Concepts : Tools – Toolkits : Data : Pandas
- User Guide : Concepts : Tools – Toolkits : Data : Postgres
- User Guide : Concepts : Tools – Toolkits : Data : SQL
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
◆ お問合せ : 下記までお願いします。
- クラスキャット セールス・インフォメーション
- sales-info@classcat.com
- ClassCatJP
Agno ユーザガイド : コンセプト : ツール – ツールキット : データ : CSV
CsvTools はエージェントが CSV ファイルの読む書きできるようにします。
例
次のエージェントは IMDB csv ファイルをダウンロードして、ユーザが CLI アプリケーションを使用してクエリーできるようにします。
cookbook/tools/csv_tools.py
import httpx
from pathlib import Path
from agno.agent import Agent
from agno.tools.csv_toolkit import CsvTools
url = "https://agno-public.s3.amazonaws.com/demo_data/IMDB-Movie-Data.csv"
response = httpx.get(url)
imdb_csv = Path(__file__).parent.joinpath("wip").joinpath("imdb.csv")
imdb_csv.parent.mkdir(parents=True, exist_ok=True)
imdb_csv.write_bytes(response.content)
agent = Agent(
tools=[CsvTools(csvs=[imdb_csv])],
markdown=True,
show_tool_calls=True,
instructions=[
"First always get the list of files",
"Then check the columns in the file",
"Then run the query to answer the question",
"Always wrap column names with double quotes if they contain spaces or special characters",
"Remember to escape the quotes in the JSON string (use \")",
"Use single quotes for string values"
],
)
agent.cli_app(stream=False)
Agno ユーザガイド : コンセプト : ツール – ツールキット : データ : Pandas
PandasTools はエージェントが Pandas ライブラリを使用してデータ操作タスクを実行できるようにします。
cookbook/tools/pandas_tool.py
from agno.agent import Agent
from agno.tools.pandas import PandasTools
# Create an agent with PandasTools
agent = Agent(tools=[PandasTools()])
# Example: Create a dataframe with sample data and get the first 5 rows
agent.print_response("""
Please perform these tasks:
1. Create a pandas dataframe named 'sales_data' using DataFrame() with this sample data:
{'date': ['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04', '2023-01-05'],
'product': ['Widget A', 'Widget B', 'Widget A', 'Widget C', 'Widget B'],
'quantity': [10, 15, 8, 12, 20],
'price': [9.99, 15.99, 9.99, 12.99, 15.99]}
2. Show me the first 5 rows of the sales_data dataframe
""")
出力
Agno ユーザガイド : コンセプト : ツール – ツールキット : データ : Postgres
PostgresTools はエージェントが PostgreSQL データベースを操作することを可能にします。
前提条件
後述の例は psycopg2 ライブラリが必要です。
pip install -U psycopg2
データベースも必要です。後述の例は Docker コンテナで動作する Postgres データベースを使用します。
docker run -d \
-e POSTGRES_DB=ai \
-e POSTGRES_USER=ai \
-e POSTGRES_PASSWORD=ai \
-e PGDATA=/var/lib/postgresql/data/pgdata \
-v pgvolume:/var/lib/postgresql/data \
-p 5532:5432 \
--name pgvector \
agno/pgvector:16
例
次のエージェントはデータベースのすべてのテーブルを一覧表示します。
(訳註: 実際には user テーブルから、直近 30 日間にサインアップしたユーザの一覧表示)
cookbook/tools/postgres.py
from agno.agent import Agent
from agno.tools.postgres import PostgresTools
# Initialize PostgresTools with connection details
postgres_tools = PostgresTools(
host="localhost",
port=5532,
db_name="ai",
user="ai",
password="ai"
)
# Create an agent with the PostgresTools
agent = Agent(tools=[postgres_tools])
# Example: Ask the agent to run a SQL query
agent.print_response("""
Please run a SQL query to get all users from the users table
who signed up in the last 30 days
""")
Agno ユーザガイド : コンセプト : ツール – ツールキット : データ : SQL
SQLTools はエージェントが SQL クエリーを実行しデータベースを操作することを可能にします。
前提条件
後述の例は sqlalchemy とデータベース URL が必要です。
pip install -U sqlalchemy
また、使用したい特定のデータベース用の適切な Python アダプターのインストールも必要です。
PostgreSQL
PostgreSQL については、psycopg2-binary アダプターをインストールできます :
pip install -U psycopg2-binary
MySQL
MySQL については、mysqlclient アダプターをインストールできます :
pip install -U mysqlclient
mysqlclient アダプターは追加のシステムレベルの依存関係があるかもしれません。Please consult the official installation guide for more details.
データベースもまた必要です。後述の例は Docker コンテナで動作する Postgres データベースを使用します。
docker run -d \
-e POSTGRES_DB=ai \
-e POSTGRES_USER=ai \
-e POSTGRES_PASSWORD=ai \
-e PGDATA=/var/lib/postgresql/data/pgdata \
-v pgvolume:/var/lib/postgresql/data \
-p 5532:5432 \
--name pgvector \
agno/pgvector:16
例
次のエージェントは SQL クエリーを実行してデータベースのすべてのテーブルを一覧表示し、テーブルの一つのコンテンツを説明します。
cookbook/tools/sql_tools.py
from agno.agent import Agent
from agno.tools.sql import SQLTools
db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
agent = Agent(tools=[SQLTools(db_url=db_url)])
agent.print_response("List the tables in the database. Tell me about contents of one of the tables", markdown=True)
以上