aw.util

Utility tools and helper functions for agents.

Includes file sampling, LLM facades, and other common utilities.

Functions

claude_desktop_config([key_path, config_dir])

Get a FileStore interface to Claude Desktop's configuration.

compute_dataframe_info(df)

Compute comprehensive info about a DataFrame.

create_oa_chat()

Create a chat function through aw.llm.default_chat() (aix, else oa).

create_openai_chat([model])

Create a text-to-text chat function using OpenAI API.

default_llm_factory([model])

Factory to create LLM function from various specifications.

get_numeric_columns(df[, exclude_nulls])

Get numeric columns from DataFrame.

infer_loader_from_extension(extension)

Infer pandas loader function from file extension.

infer_loader_params(extension[, sample_text])

Infer parameters for pandas loader based on file characteristics.

Classes

FileSamplerTool([sample_size])

Tool to sample and analyze file metadata and content.

class aw.util.FileSamplerTool(sample_size=1024)[source]

Bases: object

Tool to sample and analyze file metadata and content.

Helps agents understand what kind of data they’re dealing with before attempting to load it.

Example

>>> sampler = FileSamplerTool()
>>> info = sampler('/path/to/data.csv')
>>> info['extension']
'.csv'
aw.util.claude_desktop_config(key_path='mcpServers', *, config_dir=None)[source]

Get a FileStore interface to Claude Desktop’s configuration.

By default, operates on the ‘mcpServers’ section.

Automatically creates the config file if it doesn’t exist, and creates the mcpServers section if it’s missing.

Parameters:
  • key_path – Path to nested section (default: ‘mcpServers’)

  • config_dir – Override default config directory location

Returns:

FileStore interface to Claude Desktop config

Example

>>> # Add/manage MCP servers
>>> mcp = claude_desktop_config()
>>>
>>> # Add a new server
>>> mcp['download'] = {
...     'command': 'python',
...     'args': ['/path/to/agent.py']
... }
>>>
>>> # List configured servers
>>> list(mcp)
>>>
>>> # Remove a server
>>> del mcp['old_server']
aw.util.compute_dataframe_info(df)[source]

Compute comprehensive info about a DataFrame.

Parameters:

df – pandas DataFrame

Return type:

dict

Returns:

Dict with shape, dtypes, null counts, sample rows, etc.

Example

>>> import pandas as pd
>>> df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
>>> info = compute_dataframe_info(df)
>>> info['shape']
(2, 2)
aw.util.create_oa_chat()[source]

Create a chat function through aw.llm.default_chat() (aix, else oa).

The name is kept for compatibility; generation is routed through aix.

Return type:

Callable[[str], str]

Returns:

A callable that takes a prompt and returns a response

Example

>>> chat = create_oa_chat()
>>> response = chat("Hello!")
aw.util.create_openai_chat(model='gpt-4', **default_kwargs)[source]

Create a text-to-text chat function using OpenAI API.

Parameters:
  • model (str) – OpenAI model name

  • **default_kwargs – Default parameters for API calls

Return type:

Callable[[str], str]

Returns:

A callable that takes a prompt and returns a response

Example

>>> chat = create_openai_chat("gpt-4o-mini")
>>> response = chat("What is 2+2?")

(Skipped: the call is billable and needs OPENAI_API_KEY.)

aw.util.default_llm_factory(model='gpt-4')[source]

Factory to create LLM function from various specifications.

Parameters:

model (Union[str, Callable]) – Either a model name string, or a callable

Return type:

Callable[[str], str]

Returns:

A callable text-to-text function

Example

>>> llm = default_llm_factory("gpt-4")
>>> llm = default_llm_factory(lambda p: f"Echo: {p}")
aw.util.get_numeric_columns(df, exclude_nulls=True)[source]

Get numeric columns from DataFrame.

Parameters:
  • df – pandas DataFrame

  • exclude_nulls (bool) – Whether to exclude columns with null values

Returns:

Generator of column names

Example

>>> for col in get_numeric_columns(df):
...     print(col)
aw.util.infer_loader_from_extension(extension)[source]

Infer pandas loader function from file extension.

Parameters:

extension (str) – File extension (e.g., ‘.csv’, ‘.json’)

Return type:

str

Returns:

Name of pandas loader function

Example

>>> infer_loader_from_extension('.csv')
'read_csv'
>>> infer_loader_from_extension('.xlsx')
'read_excel'
aw.util.infer_loader_params(extension, sample_text=None)[source]

Infer parameters for pandas loader based on file characteristics.

Parameters:
  • extension (str) – File extension

  • sample_text (str) – Optional sample of file content

Return type:

dict

Returns:

Dict of parameters to pass to loader

Example

>>> params = infer_loader_params('.csv', 'a,b,c\n1,2,3')
>>> params.get('sep')
','