CSV
The CsvTools toolkit enables an Agent to read and write CSV files.
CsvTools enable an Agent to read and write CSV files.
Example
The following agent will download the IMDB csv file and allow the user to query it using a CLI app.
1import httpx2from pathlib import Path3from kern.agent import Agent4from kern.tools.csv_toolkit import CsvTools56url = "https://kern-public.s3.amazonaws.com/demo_data/IMDB-Movie-Data.csv"7response = httpx.get(url)89imdb_csv = Path(__file__).parent.joinpath("wip").joinpath("imdb.csv")10imdb_csv.parent.mkdir(parents=True, exist_ok=True)11imdb_csv.write_bytes(response.content)1213agent = Agent(14 tools=[CsvTools(csvs=[imdb_csv])],15 markdown=True,16 instructions=[17 "First always get the list of files",18 "Then check the columns in the file",19 "Then run the query to answer the question",20 "Always wrap column names with double quotes if they contain spaces or special characters",21 "Remember to escape the quotes in the JSON string (use \")",22 "Use single quotes for string values"23 ],24)2526agent.cli_app(stream=False)Toolkit Params
| Parameter | Type | Default | Description |
|---|---|---|---|
csvs | List[Union[str, Path]] | None | A list of CSV files or paths to be processed or read. |
row_limit | int | None | The maximum number of rows to process from each CSV file. |
duckdb_connection | Any | None | Specifies a connection instance for DuckDB database operations. |
duckdb_kwargs | Dict[str, Any] | None | A dictionary of keyword arguments for configuring DuckDB operations. |
enable_read_csv_file | bool | True | Enables the functionality to read data from specified CSV files. |
enable_list_csv_files | bool | True | Enables the functionality to list all available CSV files. |
enable_get_columns | bool | True | Enables the functionality to read the column names from CSV files. |
enable_query_csv_file | bool | True | Enables the functionality to execute queries on data within CSV files. |
all | bool | False | Enables all functionality when set to True. |
Toolkit Functions
| Function | Description |
|---|---|
list_csv_files | Lists all available CSV files. |
read_csv_file | This function reads the contents of a csv file |
get_columns | This function returns the columns of a csv file |
query_csv_file | This function queries the contents of a csv file |
Developer Resources
- View Tools