TableGPT Agent
repository·main·Indexed 20 days ago
https://github.com/tablegpt/tablegpt-agentA Langgraph-based agentic interface for TableGPT2, a multimodal model optimized for tabular data reasoning and question answering. It features an iterative code generation and execution workflow using an IPython sandbox, support for visual analysis via VLMs, and a safety mechanism to filter hazard categories. The package includes a Code Sandbox for isolated execution in local, remote, and cluster environments, as well as evaluation frameworks for benchmarks like RealTabBench, bird, and spider.
What's inside tablegpt-agent
- TableGPT Agent is a pre-built agent designed for interacting with TableGPT2, a series of Large Multimodal Models (LMMs) specialized for table-based question answering. The agent is built using the Langgraph library, providing a structured interface for complex tabular reasoning tasks.
Introduction to TableGPT Agent
mainWhat is Code Sandbox and how does it work?
mainCode Sandbox is a security layer for
tablegpt-agentthat isolates the execution of generated Python code. Since the agent generates code for data analysis, running it directly in production poses security and stability risks. Code Sandbox uses thepyboxlibrary to confine execution to a controlled, isolated environment.It supports three execution modes:
- Local Environment: Uses
pyboxto run code in a local sandbox. Best for development and debugging. - Remote Environment: Uses
RemotePyBoxManagerandJupyter Enterprise Gatewayto create shared computing environments. - Cluster Environment: Uses
KubePyBoxManagerto communicate directly with kernel pods in a Kubernetes cluster, bypassing proxy services like Enterprise Gateway.
- Local Environment: Uses
How the TableGPT Agent workflow works
mainThe
tablegpt-agentoperates as a single-agent system that processes user queries through an iterative loop of code generation and execution. The core components are an agent powered by TableGPT2 and an IPython tool that executes code in a sandbox.Core Workflow Steps
- User Input: Receives the query.
- Security Assessment (optional): Evaluates the query for sensitive topics.
- Data Retrieval (optional): Uses a retriever plugin to fetch dataset metadata (columns, values, etc.).
- Code Generation: The agent writes Python code to solve the task.
- Code Execution: The code runs in an IPython sandbox.
- Result Generation: The agent processes execution results into a final response.
- Visual Analysis (optional): Uses a Visual Language Model (VLM) to summarize generated charts/images.
Iterative Debugging
If code execution fails, the system automatically enters an iterative debugging cycle. It repeats steps 4 through 7 (Code Generation $\rightarrow$ Result Generation) to resolve errors. The default maximum iteration limit is 25.
How Incluster Code Execution works in tablegpt-agent
mainWhen usingtablegpt-agent, the agent instructstablegptto generate Python code specifically for data analysis tasks. To maintain system security, this generated code is not executed in the main process. Instead, it is run within a sandboxed environment managed by thepyboxlibrary. This ensures that any data analysis code generated by the model is isolated from the core application process.Understand the TableGPT Evaluation dataset structure
mainIn the context of TableGPT evaluation, a 'dataset' consists of samples used for testing. Each sample requires an
inputfield representing the user query. For data analysis tasks, samples should also include anexpected outputfield to serve as the ground truth for comparison.Note that the dataset being analyzed is referred to as 'reference data' to distinguish it from the evaluation samples. When generating
expected output, ensure high accuracy as it is used as the ground truth for evaluation results.Download required database files
mainInstall the TableGPT Evaluation environment
mainTo set up the evaluation environment, create a virtual environment and install the required dependencies using
pip.# Create and activate a virtual environment python -m venv venv source ./venv/bin/activate # On Windows, use `.\venv\Scripts\activate` # Install dependencies pip install -r requirements.txtpython -m venv venv source ./venv/bin/activate pip install -r requirements.txtRun the evaluation script
mainTo execute an evaluation, you must provide a configuration file and ensure all necessary environment variables are set (either via exported shell variables or a
.envfile in the root directory). Run the evaluation using theagent_evalmodule.python -m agent_eval --config path/to/your/config.yamlpython -m agent_eval --config path/to/your/config.yamlRun code in a Remote Sandbox environment
mainRemote mode uses
RemotePyBoxManagerto connect to aJupyter Enterprise Gatewayservice. This allows multiple services to share access to the same remote environment.Prerequisites:
- An active
enterprise_gatewayservice. - The service address must be accessible (e.g.,
http://example.com).
Advanced Configuration:
RemotePyBoxManagersupports two configuration options for environment variables:env_file: Path to a file containing environment variables.kernel_env: A dictionary of key-value pairs for environment variables.
from uuid import uuid4 from pybox import RemotePyBoxManager, PyBoxOut # Initialize the remote sandbox manager, replacing with the actual Enterprise Gateway service address pybox_manager = RemotePyBoxManager(host="http://example.com") # Assign a unique Kernel ID kernel_id = str(uuid4()) # Start the remote sandbox environment box = pybox_manager.start(kernel_id) # Define the test code test_code = """ import math result = math.sqrt(16) result """ # Run the code in the sandbox out: PyBoxOut = box.run(code=test_code) # Print the execution result print(out)- An active
Run code in a Kubernetes Cluster environment
mainIn a Kubernetes cluster,
KubePyBoxManagercommunicates directly with Kernel Pods created by thejupyter-kernel-controller, removing the need for an intermediary gateway.Prerequisites:
- The
jupyter-kernel-controllerservice must be deployed in your cluster.
Configuration:
KubePyBoxManageruses the same environment configuration parameters asRemotePyBoxManager:env_file: Path to an environment variable file.kernel_env: A dictionary of environment variables.
from uuid import uuid4 from pybox import KubePyBoxManager, PyBoxOut # Initialize the cluster sandbox manager pybox_manager = KubePyBoxManager( env_file="YOUR_ENV_FILE_PATH", # Path to the environment variable file kernel_env="YOUR_KERNEL_ENV_DICT", # Kernel environment variable configuration ) # Assign a unique Kernel ID kernel_id = str(uuid4()) # Start the cluster sandbox environment box = pybox_manager.start(kernel_id) # Define the test code test_code = """ import math result = math.sqrt(16) result """ # Run the code in the sandbox out: PyBoxOut = box.run(code=test_code) # Print the execution result print(out)- The
Run table-related benchmark evaluations
mainThe repository includes evaluation scripts and datasets for various academic and table-related benchmarks. These resources are located in therealtabbenchdirectory. To perform evaluations, refer to the specific instructions in therealtabbench/README.mdfile.